Skip to content

Commit 11eb5e8

Browse files
committed
Merge branch 'develop'
2 parents 6c1a4c2 + d3ebca4 commit 11eb5e8

File tree

7 files changed

+279
-198
lines changed

7 files changed

+279
-198
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
*.kate-swp
22
*.nfs00*
33
*.old
4+
*.orig
45
*~
56
.*~
67
/doc
78
/vendor
89
composer.lock
9-
tests/cli.php

README.md

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ An ultra-simple-to-use assets management PHP library.
1111
- [Usage](#usage).
1212
- [Views](#views).
1313
- [Controllers](#controllers).
14-
- [API](#api).
1514
- [Configuration](#configuration).
1615
- [Collections](#collections).
1716
- [Pipeline](#pipeline).
1817
- [More options](#options).
18+
- [Multitenancy](#multitenancy).
1919
- [Non static interface usage](#nonstatic).
2020
- [Sample collections](#samples).
2121
- [Troubleshooting / F.A.Q.](#troubleshooting).
@@ -28,21 +28,21 @@ An ultra-simple-to-use assets management PHP library.
2828

2929
- **Very easy to use**.
3030
- Autogenerates HTML tags for including your JavaScript and CSS files.
31+
- Automatically detects type of asset (CSS, JavaScript or collection).
3132
- Supports programmatically adding assets on the fly.
3233
- Supports local (**including packages**) or remote assets.
3334
- Prevents from loading duplicated assets.
3435
- Included assets **pipeline** (*concatenate and minify all your assets to a single file*) with URL **timestamps** and **gzip** support.
3536
- Automatically prefixes local assets with a configurable folder name or url.
3637
- Supports secure (*https*) and protocol agnostic (*//*) links.
37-
- Supports **collections** (*named groups of assets*) that can be nested, allowing assets dependencies definitions.
38-
- Automatically detects type of asset (CSS, JavaScript or collection).
38+
- Supports **collections** (*named sets of assets*) that can be nested, allowing assets dependencies definitions.
39+
- Supports **multitenancy** (multiple independent configurations) for different groups of assets (*this feature is only available for Laravel >= 5.0*).
3940
- Allows autoloading by default preconfigured assets and collections.
4041

41-
4242
<a id="frameworks"></a>
4343
## Supported frameworks
4444

45-
The library is framework agnostic and it should work well with any framework or naked PHP application. Nevertheless, the following instructions have been tailored for **Laravel 5** framework ([still on Laravel 4?](https://github.com/Stolz/Assets/issues/55#issuecomment-73024822)). If you want to use the library in any other scenario please read the [non static interface](#nonstatic) instructions.
45+
The library is framework agnostic and it should work well with any framework or naked PHP application. Nevertheless, sice the library is most popular between Laravel users the following instructions have been tailored for **Laravel 5** framework ([still on Laravel 4?](https://github.com/Stolz/Assets/issues/55#issuecomment-73024822)). If you want to use the library in any other scenario please read the [non static interface](#nonstatic) instructions.
4646

4747
<a id="installation"></a>
4848
## Installation
@@ -79,7 +79,8 @@ Basically all you have to do to add and asset, no matter if it's CSS or JS or a
7979

8080
Assets::add('filename');
8181

82-
*For more advanced uses keep reading ...*
82+
>For more advanced uses keep reading but please note that there are some more methods not documented here. For a **full list of all the available methods** please read the provided [`API.md`](https://github.com/Stolz/Assets/blob/master/API.md) file.
83+
8384

8485
Add more than one asset at once
8586

@@ -98,8 +99,8 @@ You may add remote assets in the same fashion
9899

99100
If your assets have no extension and autodetection fails, then just use canonical functions *(they accept an array of assets too)*
100101

101-
Assets::addCss('asset.css');
102-
Assets::addJs('asset.js');
102+
Assets::addCss('CSSfile.foo');
103+
Assets::addJs('JavaScriptFile.bar');
103104

104105
If at some point you decide you added the wrong assets you can reset them and start over
105106

@@ -111,11 +112,6 @@ All methods that don't generate output will accept chaining:
111112

112113
Assets::reset()->add('collection')->addJs('file.js')->css();
113114

114-
<a id="api"></a>
115-
### API
116-
117-
There are some methods not documented here. For a **full list of all the available methods** please read the provided [`API.md`](https://github.com/Stolz/Assets/blob/master/API.md) file.
118-
119115
<a id="configuration"></a>
120116
## Configuration
121117

@@ -130,15 +126,15 @@ If you are using the [non static interface](#nonstatic) just pass an associative
130126
<a id="collections"></a>
131127
### Collections
132128

133-
A collection is a named group of assets, that is, a set of JavaScript and CSS files. Any collection may include more collections, allowing dependencies definition and collection nesting. Collections can be created on run time or via config file.
129+
A collection is a named set of assets, that is, a set of JavaScript and CSS files. Any collection may include more collections, allowing dependencies definition and collection nesting. Collections can be created on run time or via config file.
134130

135131
To register a collection on run time for later use:
136132

137133
Assets::registerCollection($collectionName, array('some', 'awesome', 'assets'));
138134

139135
To preconfigure collections using the config file:
140136

141-
// ... config.php ...
137+
// ... File: config/assets.php ...
142138
'collections' => [
143139
'one' => 'one.css',
144140
'two' => ['two.css', 'two.js'],
@@ -240,6 +236,46 @@ It is possible to **change any config options on the fly** by passing an array o
240236
echo Assets::reset()->add('do-not-pipeline-this.js')->js(),
241237
Assets::reset()->add('please-pipeline-this.js')->config(array('pipeline' => true))->js();
242238

239+
<a id="multitenancy"></a>
240+
### Multitenancy
241+
242+
**Note:** *This feature is only available for Laravel >= 5.0*.
243+
244+
Multitenancy is achieved using groups. A group is an isolated container of the library. Each group is totally independent of other groups so it uses its own settings and assets flow. This is useful if you need different approaches for different types of assets (for instance, you may need some assets to be pipelined but some others no). Therefore, when using multiple groups is your responsability to make sure the assets of different groups that depend on eachother are loaded in ther right order.
245+
246+
By default if no groups are defined the default group is used. To define a group just nest your normal settings within an array in the config file. The array key will be the group name. For instance:
247+
248+
249+
// ... File: config/assets.php ...
250+
251+
// Default group
252+
'default' => [
253+
'pipeline' => true,
254+
'js_dir' => 'js',
255+
// ... more options for default group
256+
],
257+
258+
// Other group
259+
'group1' => [
260+
'pipeline' => false,
261+
'public_dir' => '/foo',
262+
// ... more options for group1
263+
],
264+
265+
// Another group
266+
'group2' => [
267+
'pipeline' => false,
268+
'css_dir' => 'css/admin',
269+
// ... more options for group2
270+
],
271+
272+
For choosing which group you want to interact with, use the `group()` method. If no group is specified the 'default' group will be used.
273+
274+
Assets::add('foo.js')->js(); // Uses default group
275+
Assets::group('group1')->add('bar.css')->css(); // Uses the 'group1' group.
276+
277+
Please note the `group()` method is part of the Facade, so it does not accept chaining and it always has to be used at the beginning of each interaction with the library.
278+
243279
----
244280

245281
<a id="nonstatic"></a>
@@ -357,7 +393,7 @@ If you use a massive amount of assets make sure your connection is fast enough a
357393
<a id="faq_config_on_the_fly"></a>
358394
### Can I use multiple instances of the library?
359395

360-
Yes you can but there is no need. Read next question. If you still want to use multiple instances, [read how](https://github.com/Stolz/Assets/issues/37#issuecomment-57676554).
396+
Yes you can but there is no need. You better use the [multitenancy feature](#multitenancy) (*only available for Laravel >= 5.0*).
361397

362398
<a id="faq_instances"></a>
363399
### Can I change settings on the fly?
@@ -367,6 +403,8 @@ Yes you can. There is a `config()` public method to change settings on the fly.
367403
echo Assets::add('jquery-cdn')->js();
368404
echo Assets::reset()->add(array('custom.js', 'main.js'))->config(array('pipeline' => true))->js();
369405

406+
If you want the different settings to be permanent, then use the [multitenancy feature](#multitenancy).
407+
370408
<a id="faq_filter"></a>
371409
### Can I filter/preprocess my assets?
372410

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,5 @@
3232
"optimize-autoloader": true,
3333
"preferred-install": "dist"
3434
},
35-
"minimum-stability": "dev",
3635
"license": "MIT"
3736
}

src/Laravel/Facade.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,25 @@ class Facade extends LaravelFacade
1111
*/
1212
protected static function getFacadeAccessor()
1313
{
14-
return 'stolz.assets';
14+
return 'stolz.assets.group.default';
15+
}
16+
17+
/**
18+
* Get the instance of the assets manager for a given group.
19+
*
20+
* @param string $group
21+
*
22+
* @return \Stolz\Assets\Manager
23+
*
24+
* @throws \RuntimeException
25+
*/
26+
public static function group($group = 'default')
27+
{
28+
$binding = "stolz.assets.group.$group";
29+
30+
if( ! static::$app->bound($binding))
31+
throw new \RuntimeException("Stolz\Assets: Assets group '$group' not found in the config file");
32+
33+
return static::$app[$binding];
1534
}
1635
}

src/Laravel/ServiceProvider.php

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,67 @@
77
class ServiceProvider extends LaravelServiceProvider
88
{
99
/**
10-
* Register bindings in the container.
10+
* Perform post-registration booting of services.
1111
*
1212
* @return void
1313
*/
14-
public function register()
14+
public function boot()
1515
{
16-
// Merge user's configuration
17-
$this->mergeConfigFrom(__DIR__ . '/config.php', 'assets');
18-
19-
// Bind 'stolz.assets' shared component to the IoC container
20-
$this->app->singleton('stolz.assets', function ($app) {
16+
// Register paths to be published by 'vendor:publish' Artisan command
17+
$this->publishes([
18+
__DIR__ . '/config.php' => config_path('assets.php'),
19+
]);
2120

22-
$config = $app['config']['assets'];
23-
if( ! isset($config['public_dir']))
24-
$config['public_dir'] = public_path();
21+
// Add 'Assets' facade alias
22+
AliasLoader::getInstance()->alias('Assets', 'Stolz\Assets\Laravel\Facade');
2523

26-
return new Assets($config);
27-
});
24+
// Register the Artisan command
25+
$this->commands('stolz.assets.command.flush');
26+
}
2827

29-
// Bind 'stolz.assets.command.flush' component to the IoC container
28+
/**
29+
* Register bindings in the container.
30+
*
31+
* @return void
32+
*/
33+
public function register()
34+
{
35+
// Register the Artisan command binding
3036
$this->app->bind('stolz.assets.command.flush', function ($app) {
3137
return new FlushPipelineCommand();
3238
});
39+
40+
// Merge user's configuration with the default package config file
41+
//$this->mergeConfigFrom(__DIR__ . '/config.php', 'assets');
42+
$config = $this->app['config']->get('assets', []);
43+
44+
// Register the library instances bindings ...
45+
46+
// No groups defined. Assume the config is for the default group.
47+
if( ! isset($config['default']))
48+
return $this->registerAssetsManagerInstance('default', $config);
49+
50+
// Multiple groups
51+
foreach($config as $groupName => $groupConfig)
52+
$this->registerAssetsManagerInstance($groupName, (array) $groupConfig);
3353
}
3454

3555
/**
36-
* Perform post-registration booting of services.
56+
* Register an instance of the assets manager library in the IoC container.
57+
*
58+
* @param string $name Name of the group
59+
* @param array $config Config of the group
3760
*
3861
* @return void
3962
*/
40-
public function boot()
63+
protected function registerAssetsManagerInstance($name, array $config)
4164
{
42-
// Register paths to be published by 'vendor:publish' artisan command
43-
$this->publishes([
44-
__DIR__ . '/config.php' => config_path('assets.php'),
45-
]);
65+
$this->app->singleton("stolz.assets.group.$name", function ($app) use ($config) {
4666

47-
// Add 'Assets' facade alias
48-
AliasLoader::getInstance()->alias('Assets', 'Stolz\Assets\Laravel\Facade');
67+
if( ! isset($config['public_dir']))
68+
$config['public_dir'] = public_path();
4969

50-
// Add artisan command
51-
$this->commands('stolz.assets.command.flush');
70+
return new Assets($config);
71+
});
5272
}
5373
}

0 commit comments

Comments
 (0)