PDA

View Full Version : Minifying multiple js-files with php and the yuicompressor



tobiu
9 Mar 2010, 1:49 AM
hi together,

i know there are already some ways to do it posted here (still have not tested rockstarapps), but i build a small tool to do it by myself. just put the yuicompressor in the project-folder, specify the path to the js-files (will parse it recursively) and make sure the rights are setted properly for the files ;)

don't use it in your dev-folder, since the files are replaced (you can change the target-path and insert "min" if needed).


kind regards,
tobiu

based upon an example from class dir of php.net



<?php
$dir = dirname(__FILE__)."/clientLib/";

$d = new RecDir($dir, false);

echo "Path: " . $d->getRootPath() . "\n<br>";
while (false !== ($entry = $d->read())) {

if(substr($entry, -3) == ".js"){
echo basename($entry)."\n<br>";
system('java -jar '.dirname(__FILE__).'/yuicompressor-2.4.2.jar '.$entry.' -o '.$entry.' --charset utf-8');
flush();
}
}
$d->close();

class RecDir
{
protected $currentPath;
protected $slash;
protected $rootPath;
protected $recursiveTree;

function __construct($rootPath,$win=false)
{
switch($win)
{
case true:
$this->slash = '\\';
break;
default:
$this->slash = '/';
}
$this->rootPath = $rootPath;
$this->currentPath = $rootPath;
$this->recursiveTree = array(dir($this->rootPath));
$this->rewind();
}

function __destruct()
{
$this->close();
}

public function close()
{
while(true === ($d = array_pop($this->recursiveTree)))
{
$d->close();
}
}

public function closeChildren()
{
while(count($this->recursiveTree)>1 && false !== ($d = array_pop($this->recursiveTree)))
{
$d->close();
return true;
}
return false;
}

public function getRootPath()
{
if(isset($this->rootPath))
{
return $this->rootPath;
}
return false;
}

public function getCurrentPath()
{
if(isset($this->currentPath))
{
return $this->currentPath;
}
return false;
}

public function read()
{
while(count($this->recursiveTree)>0)
{
$d = end($this->recursiveTree);
if((false !== ($entry = $d->read())))
{
if($entry!='.' && $entry!='..')
{
$path = $d->path.$entry;

if(is_file($path))
{
return $path;
}
elseif(is_dir($path.$this->slash))
{
$this->currentPath = $path.$this->slash;
if($child = @dir($path.$this->slash))
{
$this->recursiveTree[] = $child;
}
}
}
}
else
{
array_pop($this->recursiveTree)->close();
}
}
return false;
}

public function rewind()
{
$this->closeChildren();
$this->rewindCurrent();
}

public function rewindCurrent()
{
return end($this->recursiveTree)->rewind();
}
}
?>

steffenk
10 Mar 2010, 11:13 AM
As you already know, i do this within Eclipse. There is a cool plugin, you should install if you don't have it already:
RockstarApps

Update Url: http://update.rockstarapps.com
see: http://update.rockstarapps.com/index.html

tobiu
10 Mar 2010, 11:16 AM
hi steffen,

i still know ;) even mentioned it in the top posting and will take a look at it as soon as i have some free time left.

but who knows, maybe someone finds my fast-written attempt useful.


kind regards,
tobiu

steffenk
10 Mar 2010, 11:35 AM
Hey Tobi,

sure - was meant only as additional info.

wemerson.januario
10 Mar 2010, 5:51 PM
nice work

tobiu
11 Mar 2010, 7:40 AM
hi wemerson.januario,

thanks, but as mentioned above it's just based upon an example of php.net. i did not create the class RecDir, just the 10 lines above it =)


kind regards
tobiu