2

I'm in the process of building a plugin that'll have extensible 'Providers' to allow developers to build connections to other services. These 'Providers' will just be classes that are built to an interface I've included in the plugin. (It's currently in an 'interfaces' directory of my plugin; I'm not sure about the best-practice for where interfaces should be stored.)

My question: What I'm having trouble with is how to include the developer's Provider class when I don't know what the name will be. Here's what I'm doing now:

require_once(CRAFT_PLUGINS_PATH . 'location/providers/' . $this->settings->provider . 'Provider.php');

$provider = '\Craft\\' . $this->settings->provider . 'Provider';

return new $provider();

$this->settings->provider just returns the 'handle' of their Provider, like sampleService. The file they place in the providers directory would be sampleServiceProvider.php, in that case.

If something doesn't make sense, I'll gladly clarify. Any help is greatly appreciated!

1

Rather than assuming that the providers will live in a specific location, I'd be inclined to implement a custom hook, which allows third party plugins to register a new provider.

This also allows third parties to do any initialisation of their provider, prior to handing it to you.

For example, your plugin could do something like this:

$providers = craft()->plugins->call('registerProviders');

And a third party could then do something like this, in its main plugin file:

public function registerProviders()
{
    $instance = new MyProvider('custom', 'dependencies');

    return ['myProvider' => $instance];
}

Greatly simplified, but hopefully you get the idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.