I created my first ZF2 (Zend Framework 2) module called BgOauthProvider. I versioned it on GitHub and added it to modules.zendframework.com. All was well. But at the back of my mind it was niggling me that it couldn’t be deployed and managed via composer.
I’d had very little experience with composer (other than it was working), so I found the whole concept of publishing to packagist quite daunting, and couldn’t find any posts explaining the process. I bit the bullet and wen’t ahead and did it.
It’s actually very straight forward
So I thought I’d blog about my experience, and maybe it’ll spur someone else on to do it too. Composer/packagist, and other projects like them are really moving PHP forward.
Step one – register
Register on packagist.org, try and get the same user name as you have on github. This will not only help create symmetry between your packagist and github accounts but will also help later on it the Service Hooks.
Step two – composer.json
Next, before submitting your module to packagist you’ll need a composer.json in the root of your project on github. Here’s mine, I’ll explain it line by line as best I can.
"description": "ZF2 OAuth 1.0a Provider module",
"keywords": ["oauth","oauth provider", "zf2"],
"name": "Brian Gallagher",
"email": "[email protected]",
- name: The name for your package. The first half is the vendor name, this will usually be your github username, the second half will be your module name.
- type: ZF2 doesn’t have a specific package type, so just use “library”.
- description: A description.
- keywords: Like tags.
- homepage: Probably just your modules github address.
- licence: The Licence used to distribute.
- authors: An array of authors.
- require: Your modules dependancies – other packages.
- autoload: The location of your classes in your project. In ZF2, the convention is to put your PSR0 namespaced classes in a “src” directory. The classmap is needed, so that Zend can load the Module.php file, as now it’ll be one level deeper in the vendors directors. Eg my Module.php file now resides in vendor/bgallagher/bgoauthprovider/Module.php.
Step three – submitting project
Once you have your composer.json file in your project, you can submit your github address. Packagist.org will load in the composer.json file we created above and have you approve the desired name. Once you confirm your project is now listed on packagist.
Step four – auto-updating
It’s suggested that you set up a service hook so that when you push to github, your packagist repo is automatically updated.
Copy your API Token from https://packagist.org/profile/.
Then from your github project go to Settings > Service Hooks > Packagist. If your packagist username is the same as your github username, you don’t have to fill in user, just paste your API Token from packagist.