Change DispatcherAware to accept concrete instances

Dispatcher is already instanciated when invoking controller. So useless to lazy load it.
This commit is contained in:
Benoît Burnichon
2015-05-14 19:27:58 +02:00
parent 55190dffd0
commit e25cd4097c
11 changed files with 15 additions and 44 deletions

View File

@@ -17,14 +17,12 @@ trait DispatcherAware
private $dispatcher; private $dispatcher;
/** /**
* Set Locator to use to locate event dispatcher * @param EventDispatcherInterface $dispatcher
*
* @param callable $locator
* @return $this * @return $this
*/ */
public function setDispatcherLocator(callable $locator) public function setDispatcher(EventDispatcherInterface $dispatcher)
{ {
$this->dispatcher = $locator; $this->dispatcher = $dispatcher;
return $this; return $this;
} }
@@ -34,24 +32,10 @@ trait DispatcherAware
*/ */
public function getDispatcher() public function getDispatcher()
{ {
if ($this->dispatcher instanceof EventDispatcherInterface) {
return $this->dispatcher;
}
if (null === $this->dispatcher) { if (null === $this->dispatcher) {
throw new \LogicException('Dispatcher locator was not set'); throw new \LogicException('Dispatcher was not set');
} }
$dispatcher = call_user_func($this->dispatcher);
if (!$dispatcher instanceof EventDispatcherInterface) {
throw new \LogicException(sprintf(
'Expects locator to return instance of "%s", got "%s"',
EventDispatcherInterface::class,
is_object($dispatcher) ? get_class($dispatcher) : gettype($dispatcher)
));
}
$this->dispatcher = $dispatcher;
return $this->dispatcher; return $this->dispatcher;
} }

View File

@@ -10,6 +10,7 @@
namespace Alchemy\Phrasea\Controller\Prod; namespace Alchemy\Phrasea\Controller\Prod;
use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Application\Helper\DispatcherAware;
use Alchemy\Phrasea\Application\Helper\FirewallAware; use Alchemy\Phrasea\Application\Helper\FirewallAware;
use Alchemy\Phrasea\Controller\Controller; use Alchemy\Phrasea\Controller\Controller;
use Alchemy\Phrasea\Controller\RecordsRequest; use Alchemy\Phrasea\Controller\RecordsRequest;
@@ -26,6 +27,7 @@ use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class FeedController extends Controller class FeedController extends Controller
{ {
use DispatcherAware;
use FirewallAware; use FirewallAware;
public function publishRecordsAction(Application $app, Request $request) public function publishRecordsAction(Application $app, Request $request)

View File

@@ -26,9 +26,7 @@ class TaskManager implements ControllerProviderInterface, ServiceProviderInterfa
{ {
$app['controller.admin.task'] = $app->share(function (\Alchemy\Phrasea\Application $app) { $app['controller.admin.task'] = $app->share(function (\Alchemy\Phrasea\Application $app) {
return (new TaskManagerController($app)) return (new TaskManagerController($app))
->setDispatcherLocator(function () use ($app) { ->setDispatcher($app['dispatcher']);
return $app['dispatcher'];
});
}); });
} }

View File

@@ -24,9 +24,7 @@ class OAuth2 implements ControllerProviderInterface, ServiceProviderInterface
{ {
$app['controller.oauth2'] = $app->share(function (PhraseaApplication $app) { $app['controller.oauth2'] = $app->share(function (PhraseaApplication $app) {
return (new OAuth2Controller($app)) return (new OAuth2Controller($app))
->setDispatcherLocator(function () use ($app) { ->setDispatcher($app['dispatcher']);
return $app['dispatcher'];
});
}); });
} }

View File

@@ -32,9 +32,7 @@ class V1 implements ControllerProviderInterface, ServiceProviderInterface
{ {
$app['controller.api.v1'] = $app->share(function (PhraseaApplication $app) { $app['controller.api.v1'] = $app->share(function (PhraseaApplication $app) {
return (new V1Controller($app)) return (new V1Controller($app))
->setDispatcherLocator(function () use ($app) { ->setDispatcher($app['dispatcher']);
return $app['dispatcher'];
});
}); });
} }

View File

@@ -29,9 +29,7 @@ class Lightbox implements ControllerProviderInterface, ServiceProviderInterface
{ {
$app['controller.lightbox'] = $app->share(function (PhraseaApplication $app) { $app['controller.lightbox'] = $app->share(function (PhraseaApplication $app) {
return (new LightboxController($app)) return (new LightboxController($app))
->setDispatcherLocator(function () use ($app) { ->setDispatcher($app['dispatcher']);
return $app['dispatcher'];
});
}); });
} }

View File

@@ -26,9 +26,7 @@ class DoDownload implements ControllerProviderInterface, ServiceProviderInterfac
{ {
$app['controller.prod.do-download'] = $app->share(function (PhraseaApplication $app) { $app['controller.prod.do-download'] = $app->share(function (PhraseaApplication $app) {
return (new DoDownloadController($app)) return (new DoDownloadController($app))
->setDispatcherLocator(function () use ($app) { ->setDispatcher($app['dispatcher']);
return $app['dispatcher'];
});
}); });
} }

View File

@@ -26,9 +26,7 @@ class Download implements ControllerProviderInterface, ServiceProviderInterface
{ {
$app['controller.prod.download'] = $app->share(function (PhraseaApplication $app) { $app['controller.prod.download'] = $app->share(function (PhraseaApplication $app) {
return (new DownloadController($app)) return (new DownloadController($app))
->setDispatcherLocator(function () use ($app) { ->setDispatcher($app['dispatcher']);
return $app['dispatcher'];
});
}); });
} }

View File

@@ -26,9 +26,7 @@ class Edit implements ControllerProviderInterface, ServiceProviderInterface
{ {
$app['controller.prod.edit'] = $app->share(function (PhraseaApplication $app) { $app['controller.prod.edit'] = $app->share(function (PhraseaApplication $app) {
return (new EditController($app)) return (new EditController($app))
->setDispatcherLocator(function () use ($app) { ->setDispatcher($app['dispatcher']);
return $app['dispatcher'];
});
}); });
} }

View File

@@ -26,9 +26,7 @@ class Export implements ControllerProviderInterface, ServiceProviderInterface
{ {
$app['controller.prod.export'] = $app->share(function (PhraseaApplication $app) { $app['controller.prod.export'] = $app->share(function (PhraseaApplication $app) {
return (new ExportController($app)) return (new ExportController($app))
->setDispatcherLocator(function () use ($app) { ->setDispatcher($app['dispatcher'])
return $app['dispatcher'];
})
->setFileSystemLocator(function () use ($app) { ->setFileSystemLocator(function () use ($app) {
return $app['filesystem']; return $app['filesystem'];
}) })

View File

@@ -26,6 +26,7 @@ class Feed implements ControllerProviderInterface, ServiceProviderInterface
{ {
$app['controller.prod.feed'] = $app->share(function (PhraseaApplication $app) { $app['controller.prod.feed'] = $app->share(function (PhraseaApplication $app) {
return (new FeedController($app)) return (new FeedController($app))
->setDispatcher($app['dispatcher'])
->setFirewall($app['firewall']); ->setFirewall($app['firewall']);
}); });
} }