|
0
|
1 |
In order to use the Assetic OOP API you must first understand the two central |
|
|
2 |
concepts of Assetic: assets and filters. |
|
|
3 |
|
|
|
4 |
### What is an Asset? |
|
|
5 |
|
|
|
6 |
As asset is an object that has content and metadata which can be loaded and |
|
|
7 |
dumped. Your assets will probably fall into three categories: Javascripts, |
|
|
8 |
stylesheets and images. Most assets will be loaded from files in your |
|
|
9 |
filesystem, but they can also be loaded via HTTP, a database, from a string, |
|
|
10 |
or virtually anything else. All that an asset has to do is fulfill Assetic's |
|
|
11 |
basic asset interface. |
|
|
12 |
|
|
|
13 |
### What is a Filter? |
|
|
14 |
|
|
|
15 |
A filter is an object that acts upon an asset's content when that asset is |
|
|
16 |
loaded and/or dumped. Similar to assets, a filter can do virtually anything, |
|
|
17 |
as long as it implements Assetic's filter interface. |
|
|
18 |
|
|
|
19 |
Here is a list of some of the tools that can be applied to assets using a |
|
|
20 |
filter: |
|
|
21 |
|
|
|
22 |
* CoffeeScript |
|
|
23 |
* CssEmbed |
|
|
24 |
* CssMin |
|
|
25 |
* Google Closure Compiler |
|
|
26 |
* jpegoptim |
|
|
27 |
* jpegtran |
|
|
28 |
* Less |
|
|
29 |
* LessPHP |
|
|
30 |
* optipng |
|
|
31 |
* Packager |
|
|
32 |
* pngout |
|
|
33 |
* SASS |
|
|
34 |
* Sprockets (version 1) |
|
|
35 |
* Stylus |
|
|
36 |
* YUI Compressor |
|
|
37 |
|
|
|
38 |
### Using Assets and Filters |
|
|
39 |
|
|
|
40 |
You need to start by creating an asset object. This will probably mean |
|
|
41 |
instantiating a `FileAsset` instance, which takes a filesystem path as its |
|
|
42 |
first argument: |
|
|
43 |
|
|
|
44 |
$asset = new Assetic\Asset\FileAsset('/path/to/main.css'); |
|
|
45 |
|
|
|
46 |
Once you have an asset you can begin adding filters to it by calling |
|
|
47 |
`ensureFilter()`. For example, you can add a filter that applies the YUI |
|
|
48 |
Compressor to the contents of the asset: |
|
|
49 |
|
|
|
50 |
$yui = new Assetic\Filter\Yui\CssCompressorFilter('/path/to/yui.jar'); |
|
|
51 |
$asset->ensureFilter($yui); |
|
|
52 |
|
|
|
53 |
Once you've added as many filters as you'd like you can output the finished |
|
|
54 |
asset to the browser: |
|
|
55 |
|
|
|
56 |
header('Content-Type: text/css'); |
|
|
57 |
echo $asset->dump(); |
|
|
58 |
|
|
|
59 |
### Asset Collections |
|
|
60 |
|
|
|
61 |
It is a good idea to combine assets of the same type into a single file to |
|
|
62 |
avoid unnecessary HTTP requests. You can do this in Assetic using the |
|
|
63 |
`AssetCollection` class. This class is just like any other asset in Assetic's |
|
|
64 |
eyes as it implements the asset interface, but under the hood it allows you to |
|
|
65 |
combine multiple assets into one. |
|
|
66 |
|
|
|
67 |
use Assetic\Asset\AssetCollection; |
|
|
68 |
|
|
|
69 |
$asset = new AssetCollection(array( |
|
|
70 |
new FileAsset('/path/to/js/jquery.js'), |
|
|
71 |
new FileAsset('/path/to/js/jquery.plugin.js'), |
|
|
72 |
new FileAsset('/path/to/js/application.js'), |
|
|
73 |
)); |
|
|
74 |
|
|
|
75 |
### Nested Asset Collections |
|
|
76 |
|
|
|
77 |
The collection class implements the asset interface and all assets passed into |
|
|
78 |
a collection must implement the same interface, which means you can easily |
|
|
79 |
nest collections within one another: |
|
|
80 |
|
|
|
81 |
use Assetic\Asset\AssetCollection; |
|
|
82 |
use Assetic\Asset\GlobAsset; |
|
|
83 |
use Assetic\Asset\HttpAsset; |
|
|
84 |
|
|
|
85 |
$asset = new AssetCollection(array( |
|
|
86 |
new HttpAsset('http://example.com/jquery.min.js'), |
|
|
87 |
new GlobAsset('/path/to/js/*'), |
|
|
88 |
)); |
|
|
89 |
|
|
|
90 |
The `HttpAsset` class is a special asset class that loads a file over HTTP; |
|
|
91 |
`GlobAsset` is a special asset collection class that loads files based on a |
|
|
92 |
filesystem glob -- both implement the asset interface. |
|
|
93 |
|
|
|
94 |
This concept of nesting asset collection become even more powerful when you |
|
|
95 |
start applying different sets of filters to each collection. Imagine some of |
|
|
96 |
your application's stylesheets are written in SASS, while some are written in |
|
|
97 |
vanilla CSS. You can combine all of these into one seamless CSS asset: |
|
|
98 |
|
|
|
99 |
use Assetic\Asset\AssetCollection; |
|
|
100 |
use Assetic\Asset\GlobAsset; |
|
|
101 |
use Assetic\Filter\SassFilter; |
|
|
102 |
use Assetic\Filter\Yui\CssCompressorFilter; |
|
|
103 |
|
|
|
104 |
$css = new AssetCollection(array( |
|
|
105 |
new GlobAsset('/path/to/sass/*.sass', array(new SassFilter())), |
|
|
106 |
new GlobAsset('/path/to/css/*.css'), |
|
|
107 |
), array( |
|
|
108 |
new YuiCompressorFilter('/path/to/yuicompressor.jar'), |
|
|
109 |
)); |
|
|
110 |
|
|
|
111 |
You'll notice I've also applied the YUI compressor filter to the combined |
|
|
112 |
asset so all CSS will be minified. |
|
|
113 |
|
|
|
114 |
### Iterating over an Asset Collection |
|
|
115 |
|
|
|
116 |
Once you have an asset collection you can iterate over it like you would a |
|
|
117 |
plain old PHP array: |
|
|
118 |
|
|
|
119 |
echo "Source paths:\n"; |
|
|
120 |
foreach ($collection as $asset) { |
|
|
121 |
echo ' - '.$asset->getSourcePath()."\n"; |
|
|
122 |
} |
|
|
123 |
|
|
|
124 |
The asset collection iterates recursively, which means you will only see the |
|
|
125 |
"leaf" assets during iteration. Iteration also includes a smart filter which |
|
|
126 |
ensures you only see each asset once, even if the same asset has been included |
|
|
127 |
multiple times. |
|
|
128 |
|
|
|
129 |
Next: [Defining Assets "On The Fly"](define.md) |