jadb.io personal tidbits

CakePHP Plugins Development

Today, CakePHP’s plugin installer has been upgraded and the changes while not minimal should be transparent (or almost) to everyone.

The reason for the changes was explained here - read that.

Now, some people are getting mixed up about the way plugins are now split into different directories (vendor/ and plugins/). That’s because, as described in the PR I linked you to above, they should have never been in the same directory to start with.

So, how does one now deal with plugins being developed? In vendor/? In plugins/?

Best practice would suggest to develop plugins in TDD - so no need for an application environment to be running. I will assume that if you’re doing that, then you know what you’re doing :)

Another way is to develop plugins inside a sample (or development) application. In those cases, stick to adding your plugin to plugins/ for development (until it’s released on Github at least). The reason being that the vendor/ directory isn’t one you track in your repository (it’s auto-generated by composer) and so changes you make there are not part of your repo (unless you use --force).

Now, with your plugin in plugins/YourPlugin, it’s time to tell Composer about the namespace it needs to auto-load for it:

  "autoload": {
    "psr-4": {
      "YourPlugin\\": "plugins/YourPlugin/src"

Of course, just append your plugin’s namespace to the already defined ones in your app.

And now tell composer to update the autoloader class:

$ composer dumpautoload

For the rest, do as you would usually do: load the plugin in bootstrap, etc.

This so far assumed that your plugin was part of your application repository and not a standalone repository. In case you have already pushed the repository to Github (or any other VCS), the workflow is a little different.

First, ignore the namespace changes above and instead, make sure that you plugin is part of your application’s composer dependencies:

  "require": {
    "name/plugin": "dev-master"

Now, add your Github repository to the your application’s composer.json like so:

  "repositories": [
      "type": "vcs"
      , "url": "https://github.com/name/plugin"

and run composer install. This will install your plugin in vendor/ and add it’s absolute path to vendor/cakephp-plugins.php (nothing you should really care about).

As you develop and make changes to your plugin (which is a separate repository in this case), go to vendor/name/plugin and commit your changes from time to time (push of course).

Once you are done with the development stage of your plugin and have released it to packagist, just delete the custom repository you have setup for the plugin in your application’s composer.json and it will automatically start fetching your plugin from Packagist instead!

That’s it, if you have questions/comments just let me know below.

comments powered by Disqus