One of the coolest things that it does is use the 'Assetic' library to handle assets. What's awesome about this is that you can apply filters to the assets that it's handling allowing you to modify the output of them. It has a lot of built in filters which are very handy but require external files to work.
The two I was most interested in was the use of 'YUI Compressor' to compress (minify) css/javascript files, and 'Google Closure' which not only compresses Javascript files, but can also improve (speed up) the code in them.
Awesome.
Now according to the symfony documentation in order to actually use them requires you to manually download the relevant 'jar' files, install them in the "/app/Resources/java/" folder and then modify your config.yml file to point the filters to the correct paths.
With Symfony we use composer to make our lives easier to download third party libraries and use them in our projects without having to commit them to version control. However, this isn't possible with those external Jar files... or is it?
As it turns out you can create custom repositories in composer meaning that anyone running a simple 'composer update' command will get the data contained within those repositories, even if they're external website's zip files. You can apply this to almost any zip file or static URL'd file that's on the internet.
The first step in getting this to work is working out what files we need to download (we require direct links to the files), and what types of file they are (jar, zip, etc).
So, in my example above for the "YUI Compressor" and "Google Closure" we end up with the following:
YUI Compressor
- http://cloud.github.com/downloads/yui/yuicompressor/yuicompressor-2.4.7.zip
- Zip File
Google Closure
- http://dl.google.com/closure-compiler/compiler-latest.zip
- Zip File
Now we know what files we need to download to get the assetic filters to work, the next step is creating the entries in the 'composer.json' file. The individual entries should be as follows:
YUI Compressor
Google Closure
Now we put them into the composer.json file, under the "repositories" section, as follows:
I've put this section in mine between the "autoload" and "require" sections, but I don't think it should make a difference where in the file it actually goes.
I'll just quickly explain the information above, for each of the entries the information is basically the same.
- The 'name' should match the format "vendor/packagename"
- The 'version' should match the version number of the package you want where possible (in Google Closure's case I don't know the version number so i've gone with 1.0 as default.. and if we know it's updated we can increment this and composer should uninstall the old version and put the new one in its place)
- The 'url' should be self explainatory
- The 'type' should match the type of file you're downloading, 'zip' will automatically unzip to the vendor folder, if you want to straight copy a file (in the case of downloading a raw 'jar' file) then just use the type 'file'
- The 'reference' I believe should match up with the 'version' number above (not 100% sure on this, but it seems to work on the copy I've got
That's pretty much all there is to setting the repositories up, you can add as many as you want to this and just follow the same pattern to make them available for everyone.
To get them to install in symfony, you need to continue to modify the 'composer.json' file, and add them into the "require" section, as follows:
From:
To:
Make sure the version numbers you specify match up with the ones you've set in the "repositories" section, and the name section need to match again with what you've previously specified in the "repositories" section.
Now if you run "composer update" you should find that it creates the folders "google/closure" and "yui/compressor" in your "vendors" directory of your symfony2 install.
The final step in getting this all to work is to enable the assetic filters in the "app/config/config.yml" file, scroll down to the "assetic" section, and find the sub-section labeled "filters", you might find the 'yui_css' and 'closure' ones already in there but commented out with # symbols.
You'll need to either uncomment or add them in yourself and specify the paths to the jar files, as follows:
You'll see that I start the path in the %kernel.root_dir% folder, this is your /app/ folder, as the vendor folder is the same level as that we go back up one level '../' then we can access the vendor folder, now its simply a case of adding the paths to the jar files.
Hopefully if you've followed this then you just need to add the filters onto your {% stylesheet %} and {% javascript %} blocks to use them to minify your front end code.
Any questions or thoughts on how this could be improved just drop me a comment below
No comments:
Post a Comment