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!