Skip to content

Using 3rd party libraries in Composer projects

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:


// 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 |<