From 064ed1977ff945c13ad0c0c27f1055d4eae008c7 Mon Sep 17 00:00:00 2001 From: Stolz Date: Wed, 30 Dec 2015 16:34:43 +0800 Subject: [PATCH 1/2] Use modern array syntax --- README.md | 32 +++++++++++++++++++------------- src/Laravel/config.php | 8 ++++---- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 9551744..57811a3 100644 --- a/README.md +++ b/README.md @@ -53,9 +53,11 @@ In your project base directory run Then edit `config/app.php` and add the service provider within the `providers` array. - 'providers' => array( - ... + 'providers' => [ + //... 'Stolz\Assets\Laravel\ServiceProvider', + //... + ], There is no need to add the Facade, the package will bind it to the IoC for you. @@ -84,7 +86,7 @@ Basically all you have to do to add and asset, no matter if it's CSS or JS or a Add more than one asset at once - Assets::add(array('another/file.js', 'one/more.css')); + Assets::add(['another/file.js', 'one/more.css']); Add an asset from a local package @@ -130,7 +132,7 @@ A collection is a named set of assets, that is, a set of JavaScript and CSS file To register a collection on run time for later use: - Assets::registerCollection($collectionName, array('some', 'awesome', 'assets')); + Assets::registerCollection($collectionName, ['some', 'awesome', 'assets']); To preconfigure collections using the config file: @@ -219,7 +221,7 @@ Finally, if you happen to use NGINX with the [gzip_static](http://nginx.org/en/d For a **full list of all the available config options** please read the provided [`API.md`](https://github.com/Stolz/Assets/blob/master/API.md) file. -- `'autoload' => array(),` +- `'autoload' => [],` Here you may set which assets (CSS files, JavaScript files or collections) should be loaded by default. - `'css_dir' => 'css',` @@ -234,7 +236,7 @@ For a **full list of all the available config options** please read the provided It is possible to **change any config options on the fly** by passing an array of settings to the `config()` method. Useful if some assets use a different base directory or if you want to pipeline some assets and skip others from the pipeline. i.e: echo Assets::reset()->add('do-not-pipeline-this.js')->js(), - Assets::reset()->add('please-pipeline-this.js')->config(array('pipeline' => true))->js(); + Assets::reset()->add('please-pipeline-this.js')->config(['pipeline' => true])->js(); ### Multitenancy @@ -287,13 +289,13 @@ You can use the library without using static methods. The signature of all metho require __DIR__ . '/vendor/autoload.php'; // Set config options - $config = array( - 'collections' => array(...), - 'autoload' => array(...), + $config = [ + 'collections' => [...], + 'autoload' => [...], 'pipeline' => true, 'public_dir' => '/absolute/path/to/your/webroot/public/dir' ... - ); + ]; // Instantiate the library $assets = new \Stolz\Assets\Manager($config); @@ -375,8 +377,12 @@ i.e: Assuming the next scenario: Then to load the assets you should run: - Assets::add(['foo.css', 'bar.js', 'somevendor/somepackage:lorem.css', 'anothervendor/anotherpackage:ipsum.js']); - + Assets::add([ + 'foo.css', + 'bar.js', + 'somevendor/somepackage:lorem.css', + 'anothervendor/anotherpackage:ipsum.js' + ]); ### Why assets work for the main page but not for subpages? @@ -401,7 +407,7 @@ Yes you can but there is no need. You better use the [multitenancy feature](#mul Yes you can. There is a `config()` public method to change settings on the fly. This allows you to use same instance of the library with different settings. i.e: echo Assets::add('jquery-cdn')->js(); - echo Assets::reset()->add(array('custom.js', 'main.js'))->config(array('pipeline' => true))->js(); + echo Assets::reset()->add(['custom.js', 'main.js'])->config(['pipeline' => true])->js(); If you want the different settings to be permanent, then use the [multitenancy feature](#multitenancy). diff --git a/src/Laravel/config.php b/src/Laravel/config.php index cee4447..4c6aec1 100644 --- a/src/Laravel/config.php +++ b/src/Laravel/config.php @@ -9,11 +9,11 @@ |--------------------------------------------------------------------------- */ -return array( +return [ // Configuration for the default group. Feel free to add more groups. // Each group can have different settings. - 'default' => array( + 'default' => [ /** * Regex to match against a filename/url to determine if it is an asset. @@ -167,5 +167,5 @@ */ //'autoload' => array('jquery-cdn'), - ), // End of default group -); + ], // End of default group +]; From 69bc6d4fc29bb57ccbdbd354ea0f6c799678f41e Mon Sep 17 00:00:00 2001 From: Stolz Date: Wed, 6 Jan 2016 11:10:38 +0800 Subject: [PATCH 2/2] Fixed artisan command. Fixes #82 --- phpcodesniffer.xml | 5 +++ src/Laravel/FlushPipelineCommand.php | 63 +++++++++++++++++++++------- 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/phpcodesniffer.xml b/phpcodesniffer.xml index 22a719f..a7c6912 100644 --- a/phpcodesniffer.xml +++ b/phpcodesniffer.xml @@ -23,6 +23,11 @@ + + + /src/Laravel/ + + diff --git a/src/Laravel/FlushPipelineCommand.php b/src/Laravel/FlushPipelineCommand.php index 502ad23..3f48357 100644 --- a/src/Laravel/FlushPipelineCommand.php +++ b/src/Laravel/FlushPipelineCommand.php @@ -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); } /** @@ -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'], ]; }