Dear Reader,
Recently, I discussed a lesson I learned about “Managing the Verbosity of symfony’s Command Object With a Trait” while building a project. This particular project has been very instructional to me so I thought I would share something else I learned.
As discussed in the previous post, this is a command line script to move email addresses from eventbrite events into MailChimp lists. I wrote it specifically for use with my NomadPHP project but the way it now works, it operates on all my projects at once.
A problem I ran into when starting this project is that the official MailChimp API wrapper for PHP is NOT a Composer package. Thankfully, the wizards behind Composer have thought this through. To facilitate using non-Composer packages in composer projects, all I had to do is add one line to my “autoload” section of my project:
"autoload": { "psr-0": { "NomadPHP": "app/"}, "classmap": [ "src/"] }
The classmap
section allows me to drop any class file into the directory src/
and then run composer.phar update
. Composer will look at the files in that directory and add them to vendor/composer/autoload_classmap.php
. Mine now looks like this:
<?php // autoload_classmap.php generated by Composer $vendorDir = dirname(dirname(__FILE__)); $baseDir = dirname($vendorDir); return array( 'Eventbrite' => $baseDir . '/src/Eventbrite.php', 'MCAPI' => $baseDir . '/src/MCAPI.class.php', );
Problem solved. As you can see I also used this to be able to use the eventbrite API wrapper as well.
If you are not using Composer for your projects, you really need to start. It’s a great way to speed your PHP development.
Until next time,
I <3 |<
=C=
RT @CalEvans: Using 3rd party libraries in Composer projects: http://t.co/S9ktu0P5Jn
Hi Cal,
Just a couple of quick notes for you:
The “classmap” key should go at the same level as “psr-0”, not inside it, and if you just want to update your classmap, rather than all packages, you should use “composer.phar dump-autoload” which will re-generate the autoloader without updating your dependencies.
Also, you have the option for the “files” key (at the same level as psr-0 and classmap) which can explicitly include files for you, relative to your composer.json. I find this particularly useful for bootstrap files in microphp style apps.
Thanks
Adam