vlucas / bulletphp

A resource-oriented micro PHP framework
http://bulletphp.com
BSD 3-Clause "New" or "Revised" License
418 stars 50 forks source link

Add Plugin/Service Provider Class #39

Closed vlucas closed 10 years ago

vlucas commented 10 years ago

The more apps I create with Bullet, the more I wish certain ways I configure and use Bullet were more modular and portable across other Bullet apps. I think it's time to look at creating a more serious/structured way to register a service with Bullet. I think a class file and interface should be used so that: (1) It is easy to register and autoload, and (2) so that it has predicable methods that we can call to add the desired functionality.

Example use cases:

Example Class file

use Bullet\Plugin, Bullet\PluginInterface;

class TwigPlugin extends Plugin implements PluginInterface
{
    public function run()
    {
        // do stuff with $this->app (instance of Bullet that is injected in constructor)
    }
}

Thoughts?

mackenza commented 10 years ago

this seems consistent with other frameworks (who tend to refer to these as ServiceProviders rather then Plugins but semantics aren't important... to me at least... heh).

Basically you need a way to inject the App container into the plugin so that it can make use of its functionality. For instance, a Twig plugin could use template path from $app for the Twig_Loader_Filesystem() in Twig.

Would the plugin also need to create stuff back in the Bullet App? I am trying to think of a use case where this might be needed but should part of the PluginInterface be something that does an App::addMethod?

vlucas commented 10 years ago

I only choose Plugin because it's a more common name that people might search for - i.e. "bullet twig plugin" vs "bullet twig service provider".

vlucas commented 10 years ago

Looks like Pimple beat me to it and added a ServiceProvider to Pimple in v2.1: https://github.com/fabpot/Pimple/pull/124

I will upgrade to Pimple v2 in the next major point release.

sam2332 commented 10 years ago

when will this major point be do you have a eta for the milestone?

vlucas commented 10 years ago

No ETA, just haven't pulled the trigger yet because going from Pimple 1.x -> 2.x will be a breaking change.

sam2332 commented 10 years ago

oh alright

shadowhand commented 10 years ago

@vlucas do you have a branch that uses Pimple v2?

edit: it appears that Pimple is now at v3, as of one month ago.

vlucas commented 10 years ago

Yes indeed. I will get this updated to v3 now :)

vlucas commented 10 years ago

I just tagged a version 1.4.0 release that uses Pimple 3.x. Note that this is a BREAKING CHANGE, so this will be a lesson to all of you who don't version lock your composer dependencies :)

shadowhand commented 10 years ago

:+1: