Skip to content

Commit

Permalink
allow wildcard path
Browse files Browse the repository at this point in the history
  • Loading branch information
fajar khairil committed May 8, 2015
1 parent e8b4fea commit 474bb04
Showing 1 changed file with 65 additions and 19 deletions.
84 changes: 65 additions & 19 deletions src/Task/Assets/Minify.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Robo\Result;
use Robo\Task\BaseTask;
use Symfony\Component\Finder\Finder;

/**
* Minifies asset file (CSS or JS).
Expand All @@ -29,6 +30,8 @@ class Minify extends BaseTask
/** @var string $text */
protected $text;

protected $files = array();

/** @var string $dst */
protected $dst;

Expand All @@ -49,8 +52,37 @@ class Minify extends BaseTask
*/
public function __construct($input)
{
if (file_exists($input)) {
return $this->fromFile($input);
if (file_exists($input))
{
$this->files[] = $input;
return $this;
}
else
{
// by wildcard
$finder = new Finder();

$wildcardIdx = strpos($input, '*');
$assetType = substr($input, $wildcardIdx + 2);

if( ! in_array( substr($input, $wildcardIdx + 2) , $this->types ) ){
throw new \Robo\Exception\TaskException('Invalid file type, must be '.implode(' or ',$this->types) );
}

$this->type = $assetType;

if( false !== $wildcardIdx ){
$finder->name( substr($input, $wildcardIdx) );
$iterator = $finder->in( substr($input, 0,$wildcardIdx) );
}else{
$iterator = $finder->in($input);
}

foreach ($iterator as $file) {
$this->files[] = $file->getRealpath();
}

return $this;
}

return $this->fromText($input);
Expand Down Expand Up @@ -214,11 +246,11 @@ public function __toString()
}

/**
* Writes minified result to destination.
* internal run
*
* @return Result
*/
public function run()
protected function _run()
{
if (empty($this->type)) {
return Result::error($this, 'Unknown asset type.');
Expand All @@ -243,25 +275,39 @@ public function run()
if (false === $write_result) {
return Result::error($this, 'File write failed.');
}
if ($size_before === 0) {
$minified_percent = 0;
} else {
$minified_percent = number_format(100 - ($size_after / $size_before * 100), 1);
}
$this->printTaskSuccess(
sprintf(
'Wrote <info>%s</info>',
$this->dst
)
);

$minified_percent = number_format(100 - ($size_after / $size_before * 100), 1);
$this->printTaskSuccess(
sprintf(
'Wrote <info>%s</info> (reduced by <info>%s</info> / <info>%s%%</info>)',
$this->formatBytes($size_after),
$this->formatBytes(($size_before - $size_after)),
'Wrote <info>%s</info> (reduced by <info>%s%%</info>)', $this->dst,
$minified_percent
)
);
return Result::success($this, 'Asset minified.');

return true;
}

/**
* Writes minified result to destination.
*
* @return Result
*/
public function run()
{
if( count($this->files) > 0 )
{
foreach ($this->files as $file)
{
$this->fromFile($file);
$result = $this->_run();
unset($this->dst);
}
return Result::success($this, 'Asset minified.');
}
else
{
if( $this->_run() )
return Result::success($this, 'Asset minified.');
}
}
}

0 comments on commit 474bb04

Please sign in to comment.