Propel, custom Behaviors, Composer and Git – how to get it all working

After having spent a bit of time trying to work out how to get Propel and Composer to play nicely together, I thought it would be helpful to write up a little aid for those going through similar pains…

The end game is to have your custom behaviors ‘live’ in their own Git repo to be pulled in via Composer – so you can then lean on the PSR-4 autoload support in composer to bring in your custom behaviors.

First off the docs on the Propel site don’t exactly make this easy to follow. What you need are two parts set up just right in your composer.json:

  • Declare that your package is indeed a Propel behavior and not just some other Php package;
  • Put in a bit of magic to allow Propel to map your behavior name to the class that implements it.

So in your composer.json you want the following:

{
 "name" : "REPOGROUP/MY-behavior",
 "keywords": [ "propel", "behavior" ],
 "type" : "propel-behavior",
 "require" : {
 "php" : ">=5.0.0"
 },
 "minimum-stability": "dev",
 "autoload" : {
 "psr-4" : {
 "MYCLASSPATH\\": "src/"
 }
 },
 "extra" : {
 "name" : "attribute",
 "class" : "MYCLASSPATH\\MyBehavior"
 }
}

Replace REPOGROUP, MY and MYCLASSPATH with what you need. I’m assuming the code sits in src/ – the above composer.json will give you a dev version (so you just commit and push, then where you need it just do a ‘composer update’). The type is what tells propel where to look and the extra section does the name to class mapping.

That’s it. Assuming your propel install is working, you should be able to develop. If you want to you can also ‘bind’ the repository defn in the using composer.json to be just local – if you don’t want to keep kicking into git code under development.