From 474bb0454b30057eac773e7eec71d2c4d43ae3d8 Mon Sep 17 00:00:00 2001 From: fajar khairil Date: Sat, 9 May 2015 01:01:17 +0700 Subject: [PATCH] allow wildcard path --- src/Task/Assets/Minify.php | 84 +++++++++++++++++++++++++++++--------- 1 file changed, 65 insertions(+), 19 deletions(-) diff --git a/src/Task/Assets/Minify.php b/src/Task/Assets/Minify.php index 6759d2317..50b4a856b 100644 --- a/src/Task/Assets/Minify.php +++ b/src/Task/Assets/Minify.php @@ -3,6 +3,7 @@ use Robo\Result; use Robo\Task\BaseTask; +use Symfony\Component\Finder\Finder; /** * Minifies asset file (CSS or JS). @@ -29,6 +30,8 @@ class Minify extends BaseTask /** @var string $text */ protected $text; + protected $files = array(); + /** @var string $dst */ protected $dst; @@ -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); @@ -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.'); @@ -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 %s', - $this->dst - ) - ); + + $minified_percent = number_format(100 - ($size_after / $size_before * 100), 1); $this->printTaskSuccess( sprintf( - 'Wrote %s (reduced by %s / %s%%)', - $this->formatBytes($size_after), - $this->formatBytes(($size_before - $size_after)), + 'Wrote %s (reduced by %s%%)', $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.'); + } } } \ No newline at end of file