PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/kriswallsmith/assetic/README.md

https://gitlab.com/matijabelec/bigpandadev
Markdown | 343 lines | 259 code | 84 blank | 0 comment | 0 complexity | 72b8cf93c1554df39279280818eaef71 MD5 | raw file
  1. # Assetic [![Build Status](https://travis-ci.org/kriswallsmith/assetic.png?branch=master)](https://travis-ci.org/kriswallsmith/assetic) ![project status](http://stillmaintained.com/kriswallsmith/assetic.png) #
  2. Assetic is an asset management framework for PHP.
  3. ``` php
  4. <?php
  5. use Assetic\Asset\AssetCollection;
  6. use Assetic\Asset\FileAsset;
  7. use Assetic\Asset\GlobAsset;
  8. $js = new AssetCollection(array(
  9. new GlobAsset('/path/to/js/*'),
  10. new FileAsset('/path/to/another.js'),
  11. ));
  12. // the code is merged when the asset is dumped
  13. echo $js->dump();
  14. ```
  15. Assets
  16. ------
  17. An Assetic asset is something with filterable content that can be loaded and
  18. dumped. An asset also includes metadata, some of which can be manipulated and
  19. some of which is immutable.
  20. | **Property** | **Accessor** | **Mutator** |
  21. |--------------|-----------------|---------------|
  22. | content | getContent | setContent |
  23. | mtime | getLastModified | n/a |
  24. | source root | getSourceRoot | n/a |
  25. | source path | getSourcePath | n/a |
  26. | target path | getTargetPath | setTargetPath |
  27. The "target path" property denotes where an asset (or an collection of assets) should be dumped.
  28. Filters
  29. -------
  30. Filters can be applied to manipulate assets.
  31. ``` php
  32. <?php
  33. use Assetic\Asset\AssetCollection;
  34. use Assetic\Asset\FileAsset;
  35. use Assetic\Asset\GlobAsset;
  36. use Assetic\Filter\LessFilter;
  37. use Assetic\Filter\Yui;
  38. $css = new AssetCollection(array(
  39. new FileAsset('/path/to/src/styles.less', array(new LessFilter())),
  40. new GlobAsset('/path/to/css/*'),
  41. ), array(
  42. new Yui\CssCompressorFilter('/path/to/yuicompressor.jar'),
  43. ));
  44. // this will echo CSS compiled by LESS and compressed by YUI
  45. echo $css->dump();
  46. ```
  47. The filters applied to the collection will cascade to each asset leaf if you
  48. iterate over it.
  49. ``` php
  50. <?php
  51. foreach ($css as $leaf) {
  52. // each leaf is compressed by YUI
  53. echo $leaf->dump();
  54. }
  55. ```
  56. The core provides the following filters in the `Assetic\Filter` namespace:
  57. * `AutoprefixerFilter`: Parse and update vendor-specific properties using autoprefixer
  58. * `CoffeeScriptFilter`: compiles CoffeeScript into Javascript
  59. * `CompassFilter`: Compass CSS authoring framework
  60. * `CssEmbedFilter`: embeds image data in your stylesheets
  61. * `CssImportFilter`: inlines imported stylesheets
  62. * `CssMinFilter`: minifies CSS
  63. * `CssRewriteFilter`: fixes relative URLs in CSS assets when moving to a new URL
  64. * `DartFilter`: compiles Javascript using dart2js
  65. * `EmberPrecompileFilter`: precompiles Handlebars templates into Javascript for use in the Ember.js framework
  66. * `GoogleClosure\CompilerApiFilter`: compiles Javascript using the Google Closure Compiler API
  67. * `GoogleClosure\CompilerJarFilter`: compiles Javascript using the Google Closure Compiler JAR
  68. * `GssFilter`: compliles CSS using the Google Closure Stylesheets Compiler
  69. * `HandlebarsFilter`: compiles Handlebars templates into Javascript
  70. * `JpegoptimFilter`: optimize your JPEGs
  71. * `JpegtranFilter`: optimize your JPEGs
  72. * `JSMinFilter`: minifies Javascript
  73. * `JSMinPlusFilter`: minifies Javascript
  74. * `JSqueezeFilter`: compresses Javascript
  75. * `LessFilter`: parses LESS into CSS (using less.js with node.js)
  76. * `LessphpFilter`: parses LESS into CSS (using lessphp)
  77. * `OptiPngFilter`: optimize your PNGs
  78. * `PackagerFilter`: parses Javascript for packager tags
  79. * `PackerFilter`: compresses Javascript using Dean Edwards's Packer
  80. * `PhpCssEmbedFilter`: embeds image data in your stylesheet
  81. * `PngoutFilter`: optimize your PNGs
  82. * `ReactJsxFilter`: compiles React JSX into JavaScript
  83. * `Sass\SassFilter`: parses SASS into CSS
  84. * `Sass\ScssFilter`: parses SCSS into CSS
  85. * `ScssphpFilter`: parses SCSS using scssphp
  86. * `SeparatorFilter`: inserts a separator between assets to prevent merge failures
  87. * `SprocketsFilter`: Sprockets Javascript dependency management
  88. * `StylusFilter`: parses STYL into CSS
  89. * `TypeScriptFilter`: parses TypeScript into Javascript
  90. * `UglifyCssFilter`: minifies CSS
  91. * `UglifyJs2Filter`: minifies Javascript
  92. * `UglifyJsFilter`: minifies Javascript
  93. * `Yui\CssCompressorFilter`: compresses CSS using the YUI compressor
  94. * `Yui\JsCompressorFilter`: compresses Javascript using the YUI compressor
  95. Asset Manager
  96. -------------
  97. An asset manager is provided for organizing assets.
  98. ``` php
  99. <?php
  100. use Assetic\AssetManager;
  101. use Assetic\Asset\FileAsset;
  102. use Assetic\Asset\GlobAsset;
  103. $am = new AssetManager();
  104. $am->set('jquery', new FileAsset('/path/to/jquery.js'));
  105. $am->set('base_css', new GlobAsset('/path/to/css/*'));
  106. ```
  107. The asset manager can also be used to reference assets to avoid duplication.
  108. ``` php
  109. <?php
  110. use Assetic\Asset\AssetCollection;
  111. use Assetic\Asset\AssetReference;
  112. use Assetic\Asset\FileAsset;
  113. $am->set('my_plugin', new AssetCollection(array(
  114. new AssetReference($am, 'jquery'),
  115. new FileAsset('/path/to/jquery.plugin.js'),
  116. )));
  117. ```
  118. Filter Manager
  119. --------------
  120. A filter manager is also provided for organizing filters.
  121. ``` php
  122. <?php
  123. use Assetic\FilterManager;
  124. use Assetic\Filter\Sass\SassFilter;
  125. use Assetic\Filter\Yui;
  126. $fm = new FilterManager();
  127. $fm->set('sass', new SassFilter('/path/to/parser/sass'));
  128. $fm->set('yui_css', new Yui\CssCompressorFilter('/path/to/yuicompressor.jar'));
  129. ```
  130. Asset Factory
  131. -------------
  132. If you'd rather not create all these objects by hand, you can use the asset
  133. factory, which will do most of the work for you.
  134. ``` php
  135. <?php
  136. use Assetic\Factory\AssetFactory;
  137. $factory = new AssetFactory('/path/to/asset/directory/');
  138. $factory->setAssetManager($am);
  139. $factory->setFilterManager($fm);
  140. $factory->setDebug(true);
  141. $css = $factory->createAsset(array(
  142. '@reset', // load the asset manager's "reset" asset
  143. 'css/src/*.scss', // load every scss files from "/path/to/asset/directory/css/src/"
  144. ), array(
  145. 'scss', // filter through the filter manager's "scss" filter
  146. '?yui_css', // don't use this filter in debug mode
  147. ));
  148. echo $css->dump();
  149. ```
  150. The `AssetFactory` is constructed with a root directory which is used as the base directory for relative asset paths.
  151. Prefixing a filter name with a question mark, as `yui_css` is here, will cause
  152. that filter to be omitted when the factory is in debug mode.
  153. You can also register [Workers](src/Assetic/Factory/Worker/WorkerInterface.php) on the factory and all assets created
  154. by it will be passed to the worker's `process()` method before being returned. See _Cache Busting_ below for an example.
  155. Dumping Assets to static files
  156. ------------------------------
  157. You can dump all the assets an AssetManager holds to files in a directory. This will probably be below your webserver's document root
  158. so the files can be served statically.
  159. ``` php
  160. <?php
  161. use Assetic\AssetWriter;
  162. $writer = new AssetWriter('/path/to/web');
  163. $writer->writeManagerAssets($am);
  164. ```
  165. This will make use of the assets' target path.
  166. Cache Busting
  167. -------------
  168. If you serve your assets from static files as just described, you can use the CacheBustingWorker to rewrite the target
  169. paths for assets. It will insert an identifier before the filename extension that is unique for a particular version
  170. of the asset.
  171. This identifier is based on the modification time of the asset and will also take depended-on assets into
  172. consideration if the applied filters support it.
  173. ``` php
  174. <?php
  175. use Assetic\Factory\AssetFactory;
  176. use Assetic\Factory\Worker\CacheBustingWorker;
  177. $factory = new AssetFactory('/path/to/asset/directory/');
  178. $factory->setAssetManager($am);
  179. $factory->setFilterManager($fm);
  180. $factory->setDebug(true);
  181. $factory->addWorker(new CacheBustingWorker());
  182. $css = $factory->createAsset(array(
  183. '@reset', // load the asset manager's "reset" asset
  184. 'css/src/*.scss', // load every scss files from "/path/to/asset/directory/css/src/"
  185. ), array(
  186. 'scss', // filter through the filter manager's "scss" filter
  187. '?yui_css', // don't use this filter in debug mode
  188. ));
  189. echo $css->dump();
  190. ```
  191. Internal caching
  192. -------
  193. A simple caching mechanism is provided to avoid unnecessary work.
  194. ``` php
  195. <?php
  196. use Assetic\Asset\AssetCache;
  197. use Assetic\Asset\FileAsset;
  198. use Assetic\Cache\FilesystemCache;
  199. use Assetic\Filter\Yui;
  200. $yui = new Yui\JsCompressorFilter('/path/to/yuicompressor.jar');
  201. $js = new AssetCache(
  202. new FileAsset('/path/to/some.js', array($yui)),
  203. new FilesystemCache('/path/to/cache')
  204. );
  205. // the YUI compressor will only run on the first call
  206. $js->dump();
  207. $js->dump();
  208. $js->dump();
  209. ```
  210. Twig
  211. ----
  212. To use the Assetic [Twig][3] extension you must register it to your Twig
  213. environment:
  214. ``` php
  215. <?php
  216. $twig->addExtension(new AsseticExtension($factory));
  217. ```
  218. Once in place, the extension exposes a stylesheets and a javascripts tag with a syntax similar
  219. to what the asset factory uses:
  220. ``` html+jinja
  221. {% stylesheets '/path/to/sass/main.sass' filter='sass,?yui_css' output='css/all.css' %}
  222. <link href="{{ asset_url }}" type="text/css" rel="stylesheet" />
  223. {% endstylesheets %}
  224. ```
  225. This example will render one `link` element on the page that includes a URL
  226. where the filtered asset can be found.
  227. When the extension is in debug mode, this same tag will render multiple `link`
  228. elements, one for each asset referenced by the `css/src/*.sass` glob. The
  229. specified filters will still be applied, unless they are marked as optional
  230. using the `?` prefix.
  231. This behavior can also be triggered by setting a `debug` attribute on the tag:
  232. ``` html+jinja
  233. {% stylesheets 'css/*' debug=true %} ... {% stylesheets %}
  234. ```
  235. These assets need to be written to the web directory so these URLs don't
  236. return 404 errors.
  237. ``` php
  238. <?php
  239. use Assetic\AssetWriter;
  240. use Assetic\Extension\Twig\TwigFormulaLoader;
  241. use Assetic\Extension\Twig\TwigResource;
  242. use Assetic\Factory\LazyAssetManager;
  243. $am = new LazyAssetManager($factory);
  244. // enable loading assets from twig templates
  245. $am->setLoader('twig', new TwigFormulaLoader($twig));
  246. // loop through all your templates
  247. foreach ($templates as $template) {
  248. $resource = new TwigResource($twigLoader, $template);
  249. $am->addResource($resource, 'twig');
  250. }
  251. $writer = new AssetWriter('/path/to/web');
  252. $writer->writeManagerAssets($am);
  253. ```
  254. ---
  255. Assetic is based on the Python [webassets][1] library (available on
  256. [GitHub][2]).
  257. [1]: http://elsdoerfer.name/docs/webassets
  258. [2]: https://github.com/miracle2k/webassets
  259. [3]: http://twig.sensiolabs.org