Skip to content

Commit

Permalink
Fixed artisan command.
Browse files Browse the repository at this point in the history
Fixes #82
  • Loading branch information
Stolz committed Jan 6, 2016
1 parent 064ed19 commit 69bc6d4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
5 changes: 5 additions & 0 deletions phpcodesniffer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@

<rule ref="Internal.NoCodeFound"/>

<rule ref="PHPCompatibility.PHP.ShortArray">
<!--Since Laravel requires PHP > 5.4 allow sort array syntax for Laravel stuff-->
<exclude-pattern>/src/Laravel/</exclude-pattern>
</rule>

<rule ref="PSR2">
<!--Allow one line control structures to have no curly braces-->
<exclude name="Generic.ControlStructures.InlineControlStructure.NotAllowed"/>
Expand Down
63 changes: 49 additions & 14 deletions src/Laravel/FlushPipelineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,64 @@ class FlushPipelineCommand extends Command
*/
public function fire()
{
// Determine the config namespace sepatator
$glue = (file_exists(app_path('config/packages/stolz/assets/config.php'))) ? '::' : '.';

// Get directory paths
$pipeDir = Config::get("assets{$glue}pipeline_dir", 'min');
$cssDir = public_path(Config::get("assets{$glue}css_dir", 'css') . DIRECTORY_SEPARATOR . $pipeDir);
$jsDir = public_path(Config::get("assets{$glue}js_dir", 'js') . DIRECTORY_SEPARATOR . $pipeDir);
// Get directories to purge
if( ! $directories = $this->getPipelineDirectories())
return $this->error('The provided group does not exist');

// Ask for confirmation
if( ! $this->option('force'))
{
$this->info(sprintf('All content of %s and %s will be deleted.', $cssDir, $jsDir));
$this->info('All content of the following directories will be deleted:');
foreach($directories as $dir)
$this->comment($dir);

if( ! $this->confirm('Do you wish to continue? [yes|no]'))
return;
}

// Purge assets
$purgeCss = $this->purgeDir($cssDir);
$purgeJs = $this->purgeDir($jsDir);
// Purge directories
$this->comment('Flushing pipeline directories...');
foreach($directories as $dir)
{
$this->output->write("$dir ");

if($this->purgeDir($dir))
$this->info('Ok');
else
$this->error('Error');
}

$this->comment('Done!');
}

/**
* Get the pipeline directories of the groups.
*
* @return array
*/
protected function getPipelineDirectories()
{
// Parse configured groups
$config = config('assets', []);
$groups = (isset($config['default'])) ? $config : ['default' => $config];
if( ! is_null($group = $this->option('group')))
$groups = array_only($groups, $group);

// Parse pipeline directories of each group
$directories = [];
foreach($groups as $group => $config)
{
$pipelineDir = (isset($config['pipeline_dir'])) ? $config['pipeline_dir'] : 'min';

$cssDir = (isset($config['css_dir'])) ? $config['css_dir'] : 'css';
$directories[] = public_path($cssDir . DIRECTORY_SEPARATOR . $pipelineDir);

if( ! $purgeCss or ! $purgeJs)
return $this->error('Something went wrong');

$this->info('Done!');
$jsDir = (isset($config['js_dir'])) ? $config['js_dir'] : 'js';
$directories[] = public_path($jsDir . DIRECTORY_SEPARATOR . $pipelineDir);
}

return array_unique($directories);
}

/**
Expand Down Expand Up @@ -80,6 +114,7 @@ protected function purgeDir($directory)
protected function getOptions()
{
return [
['group', 'g', InputOption::VALUE_REQUIRED, 'Only flush the provided group'],
['force', 'f', InputOption::VALUE_NONE, 'Do not prompt for confirmation'],
];
}
Expand Down

0 comments on commit 69bc6d4

Please sign in to comment.