Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Stolz committed Jan 6, 2016
2 parents 11eb5e8 + 69bc6d4 commit 4414cd2
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 31 deletions.
32 changes: 19 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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:

Expand Down Expand Up @@ -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',`
Expand All @@ -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();

<a id="multitenancy"></a>
### Multitenancy
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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'
]);

<a id="faq_base"></a>
### Why assets work for the main page but not for subpages?
Expand All @@ -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).

Expand Down
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
8 changes: 4 additions & 4 deletions src/Laravel/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -167,5 +167,5 @@
*/
//'autoload' => array('jquery-cdn'),

), // End of default group
);
], // End of default group
];

0 comments on commit 4414cd2

Please sign in to comment.