Skip to content

Commit

Permalink
more robust error handling on minify
Browse files Browse the repository at this point in the history
  • Loading branch information
fajar khairil committed May 8, 2015
1 parent 474bb04 commit 6fca2df
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/Task/Assets/Minify.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Minify extends BaseTask
protected $text;

protected $files = array();
protected $overwrite = False;

/** @var string $dst */
protected $dst;
Expand Down Expand Up @@ -61,21 +62,26 @@ public function __construct($input)
{
// by wildcard
$finder = new Finder();

$finder->exclude('dev');

$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) );
if( ! in_array( $assetType , $this->types ) ){
throw new \Robo\Exception\TaskException(get_class_methods($this),'Invalid file type, must be '.implode(' or ',$this->types) );
}

$this->type = $assetType;

if( false !== $wildcardIdx ){
$path = substr($input, 0,$wildcardIdx);
if( !is_dir($path) )
throw new \Robo\Exception\TaskException(get_class_methods($this),'directory '.$path.' not found.');

$finder->name( substr($input, $wildcardIdx) );
$iterator = $finder->in( substr($input, 0,$wildcardIdx) );
$iterator = $finder->in( $path );
}else{
$iterator = $finder->in($input);
throw new \Robo\Exception\TaskException(get_class_methods($this),'file or path not found.');
}

foreach ($iterator as $file) {
Expand Down Expand Up @@ -155,12 +161,22 @@ protected function fromFile($path)

if (empty($this->dst) && !empty($this->type)) {
$ext_length = strlen($this->type) + 1;
$this->dst = substr($path, 0, -$ext_length) . '.min.' . $this->type;

if( False === $this->overwrite )
$this->dst = substr($path, 0, -$ext_length) . '.min.' . $this->type;
else
$this->dst = $path;
}

return $this;
}

public function overwrite()
{
$this->overwrite = True;
return $this;
}

/**
* Gets file extension from path.
*
Expand Down

0 comments on commit 6fca2df

Please sign in to comment.