Last time I talked about Deploying a symfony2 app with capifony and bower.
Working with this setup for a few months I found a few problems:
- Deploys get a lot slower
- Strange errors with cache clear and cache warmup
- Why not use composer as we already use it for other dependencies
Looking for a better solution I found out most of my dependencies were already on packagist, so I could just include them in my composer.json
1 2 3 4 5 |
|
Now while running composer.phar install you’ll get the following output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Composer uses the robloach/component-installer to install the components in /vendor/components along with assetic and symfony process which it uses to generate a require.js file you can directly use in your project. This require.js (and require.css) can be found in /components/require.js
If you’re using Symfony2 and want to use assetic instead of the require.js file, you can configure that in your config.yml like so:
1 2 3 4 5 6 7 |
|
Usage is then just using this asset in your template files:
1 2 3 4 5 6 |
|
Ok, so what packages are available on packagist?
Just fill in components on packagist which already has 44 pages. Ok, so not all of those are real components, there are some other php packages as well, but most popular libs are already available this way.
I need component X and it’s not on packagist, what to do?
Just add your own repository to composer.json and figure out which specific files you need in your project. The following is an example with jqplot
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
- L6: Add a name for the package, which you use later on in the require
- L9 Add a repository with a dist to a zipfile (jqplot is on bitbucket but works as well for github or normal http urls)
- l7 specify type: ‘component’
- L25 require the robloach/component-installer so it is handled by the component installer by composer
L32 require your package with the name you’ve given in L6 and the version in L8
Lines 13 till 24 are important as they define which files will be copied to the /components/jqplot directory from the /vendords/components/jqplot directory. In this case we’ll need excanvas, the jquery.jqplot.js file and all plugins. And we include the jquery.jqplot.css style as well.
Check out the documentation on robloach/component-installer for more information.
Conclusion
As most js and css packages are on packagist anyway and composer is really good integrated into capifony, this solution works a lot better than depending on bower. Adding a package not on packagist is a bit more work, but not that difficult. Try it out yourself!