2014-07-25 17:51:36 +10:00
|
|
|
<?php namespace System\Console;
|
|
|
|
|
|
|
|
use File;
|
|
|
|
use Config;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
2015-01-12 20:08:31 +11:00
|
|
|
use System\Classes\CombineAssets;
|
2014-07-25 17:51:36 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility command
|
|
|
|
*
|
|
|
|
* Supported commands:
|
|
|
|
*
|
|
|
|
* - purge thumbs: Deletes all thumbnail files in the uploads directory.
|
|
|
|
*/
|
|
|
|
class OctoberUtil extends Command
|
|
|
|
{
|
|
|
|
|
|
|
|
use \Illuminate\Console\ConfirmableTrait;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command name.
|
|
|
|
*/
|
|
|
|
protected $name = 'october:util';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*/
|
|
|
|
protected $description = 'Utility commands for October';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new command instance.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*/
|
|
|
|
public function fire()
|
|
|
|
{
|
|
|
|
$command = implode(' ', (array) $this->argument('name'));
|
|
|
|
$method = 'util'.studly_case($command);
|
|
|
|
|
|
|
|
if (!method_exists($this, $method)) {
|
|
|
|
$this->error(sprintf('<error>Utility command "%s" does not exist!</error>', $command));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->$method();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the console command arguments.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getArguments()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
['name', InputArgument::IS_ARRAY, 'A utility command to perform.'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the console command options.
|
|
|
|
*/
|
|
|
|
protected function getOptions()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Utilties
|
|
|
|
//
|
|
|
|
|
2015-01-12 20:08:31 +11:00
|
|
|
protected function utilCompileJs()
|
|
|
|
{
|
|
|
|
$this->utilCompileAssets('js');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function utilCompileLess()
|
|
|
|
{
|
|
|
|
$this->utilCompileAssets('less');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function utilCompileAssets($type = null)
|
|
|
|
{
|
|
|
|
$this->comment('Compiling registered asset bundles...');
|
|
|
|
|
|
|
|
Config::set('cms.enableAssetMinify', true);
|
|
|
|
$combiner = CombineAssets::instance();
|
|
|
|
$bundles = $combiner->getBundles($type);
|
|
|
|
|
|
|
|
if (!$bundles){
|
|
|
|
$this->comment('Nothing to compile!');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type) {
|
|
|
|
$bundles = [$bundles];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($bundles as $bundleType) {
|
|
|
|
foreach ($bundleType as $destination => $assets) {
|
|
|
|
$destination = File::symbolizePath($destination);
|
|
|
|
$publicDest = File::localToPublic(realpath(dirname($destination))) . '/' . basename($destination);
|
|
|
|
|
|
|
|
$combiner->combineToFile($assets, $destination);
|
|
|
|
$shortAssets = implode(', ', array_map('basename', $assets));
|
|
|
|
$this->comment($shortAssets);
|
|
|
|
$this->comment(sprintf(' -> %s', $publicDest));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-25 17:51:36 +10:00
|
|
|
protected function utilPurgeThumbs()
|
|
|
|
{
|
2014-10-18 11:58:50 +02:00
|
|
|
if (!$this->confirmToProceed('This will PERMANENTLY DELETE all thumbs in the uploads directory.')) {
|
2014-07-25 17:51:36 +10:00
|
|
|
return;
|
2014-10-18 11:58:50 +02:00
|
|
|
}
|
2014-07-25 17:51:36 +10:00
|
|
|
|
|
|
|
$totalCount = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recursive function to scan the directory for files beginning
|
|
|
|
* with "thumb_" and repeat itself on directories.
|
|
|
|
*/
|
2014-10-18 11:58:50 +02:00
|
|
|
$purgeFunc = function ($targetDir) use (&$purgeFunc, &$totalCount) {
|
2014-07-25 17:51:36 +10:00
|
|
|
if ($files = File::glob($targetDir.'/thumb_*')) {
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$this->info('Purged: '. basename($file));
|
|
|
|
$totalCount++;
|
|
|
|
@unlink($file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($dirs = File::directories($targetDir)) {
|
|
|
|
foreach ($dirs as $dir) {
|
|
|
|
$purgeFunc($dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-02-17 20:58:38 +11:00
|
|
|
$purgeFunc(uploads_path());
|
2014-07-25 17:51:36 +10:00
|
|
|
|
2014-10-18 11:58:50 +02:00
|
|
|
if ($totalCount > 0) {
|
2014-07-25 17:51:36 +10:00
|
|
|
$this->comment(sprintf('Successfully deleted %s thumbs', $totalCount));
|
2014-11-01 12:00:45 +11:00
|
|
|
}
|
|
|
|
else {
|
2014-07-25 17:51:36 +10:00
|
|
|
$this->comment('No thumbs found to delete');
|
2014-10-18 11:58:50 +02:00
|
|
|
}
|
2014-07-25 17:51:36 +10:00
|
|
|
}
|
2014-10-18 11:58:50 +02:00
|
|
|
}
|