-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
factories use container as service locator and uses classnames as keys.. #23
Comments
@ppetermann thanks your feedback
|
1) injection of the container into the factoriesI just realized from what you wrote, and from looking at how you suggest using this with slim, that you don't intent to use the factory as a registered service, but that the factory is the registration of the service it provides (meaning, it replaces the function () {return new Service();}), and that is why you inject the container where you do (as that is how pimple can deal with it) 2) example for interface rather than classnameSure
say you use a key like <?php
// set it
$container[JavascriptRenderer::class] = function($c) { return new JavascriptRenderer();}
// so when you use it somewhere you'd do:
$container[PhpDebugBarMiddleware::class] = function ($c) {
return new PhpDebugBarMiddleware($container->get(JavascriptRenderer::class));
}
class PhpDebugBarMiddleware
{
public function __construct(JavacscriptRenderer $debugbarRenderer)
{
}
} So have you really decoupled from Javascript renderer here? nope, because now if PhpDebugBar was a well designed Software, it would provide a RendererInterface, which sadly, it is not, so for solving the dependency we should clean this up by <?php
interface RendererInterface
{
public function render(); // i know there is more, but lets keep the example short
}
class WrappedJavascriptRenderer implements RendererInterface
{
private $delegate;
public function __construct()
{
// we could inject this too, but since this is the replaceable component, no point in that
$this->delegate = new JavascriptRenderer();
}
public function render()
{
return $this->delegate->render();
}
} Now instead of depending on JavascriptRenderer, the middleware can depend on a replaceable component whose interface is known <?php
class PhpDebugBarMiddleware
{
public function __construct(RendererInterface $debugbarRenderer)
{
}
} so from this moment on the PhpDebugMiddleware would be actually decoupled, but wait! there is more! its still using the classname for lookups, which means <?php
$container[RendererInterface::class] = function($c) { return new WrappedJavascriptRenderer();}
$container[PhpDebugBarMiddleware::class] = function ($c) {
return new PhpDebugBarMiddleware($container->get(RendererInterface::class));
} ... which leaves you with a lot cleaner setup, where things are easy to replace, while keeping a consistent and correct name. I've left out namespaces, and i might have typos in the code above. however if i forgot anything else, don't hesitate to ask 3) config "service", and ConfigProviderYeah, i think for ConfigProvider i fell into the same trap as in 1), I didn't notice it was Zend specific. |
I just stumbled over the middleware and was surprised that the docs said it supports psr-11, wondering what it would load through it.
A quick look at the code revealed several issues:
The text was updated successfully, but these errors were encountered: