Permalink
Browse files

Merge pull request #24 from henrikbjorn/plugin-silex

Add Silex plugin
  • Loading branch information...
2 parents 8c59b61 + 4aaff7d commit d0eccdf5827ef3504ee263d0471cd1d7b25f10e3 @beberlei committed Dec 18, 2012
View
@@ -241,6 +241,36 @@ the EntityManager:
- ``LiteCQRS\Plugin\Doctrine\ORMRepository``
+### Silex
+
+Silex plugin ships with a CommandBus that knows how to get services out of your Silex application
+aswell as a ServiceProvider. The ServiceProvider adds the most basic services to get LiteCQRS to
+run.
+
+To enable the service provider register it on your application:
+
+``` php
+<?php
+$app->register(new LiteCQRS\Plugin\Silex\Provider\LiteCQRSServiceProvider());
+```
+
+`lite_cqrs.commands` is automatically injected into the `ApplicationCommandBus`. So to add Command's to
+the bus extend the service with:
+
+``` php
+<?php
+
+$app['lite_cqrs.commands'] = $app->extend(function ($commands) {
+ $commands['MyCustom\\SearchCommand'] = 'search_handler';
+
+ return $commands;
+})
+```
+
+Remember that the key have to be the Command class and the value must be the service id that have the right
+handler method implemented.
+
+
### Symfony
Inside symfony you can use LiteCQRS by registering services with
@@ -0,0 +1,52 @@
+<?php
+
+namespace LiteCQRS\Plugin\Silex;
+
+use Silex\Application;
+
+/**
+ * @package LiteCQRS
+ */
+class ApplicationCommandBus extends \LiteCQRS\Bus\SequentialCommandBus
+{
+ protected $application;
+ protected $commandServices = array();
+
+ /**
+ * @param Application $application
+ * @param array $proxyFactories
+ */
+ public function __construct(Application $application, array $proxyFactories = array())
+ {
+ parent::__construct($proxyFactories);
+
+ $this->application = $application;
+ }
+
+ /**
+ * @param array $commandServices
+ */
+ public function registerServices(array $commandServices)
+ {
+ $this->commandServices = $commandServices;
+ }
+
+ /**
+ * @param string $commandType
+ * @return object
+ */
+ public function getService($commandType)
+ {
+ if (false == isset($this->commandServices[$commandType])) {
+ throw new \RuntimeException("No command handler exists for command '" . $commandType . "'");
+ }
+
+ $serviceId = $this->commandServices[$commandType];
+
+ if (false == isset($this->application[$serviceId])) {
+ throw new \RuntimeException("Silex Application has no service '" . $serviceId . "' that is registered for command '" . $commandType . "'");
+ }
+
+ return $this->application[$serviceId];
+ }
+}
@@ -0,0 +1,49 @@
+<?php
+
+namespace LiteCQRS\Plugin\Silex\Provider;
+
+use LiteCQRS\Plugin\Silex\ApplicationCommandBus;
+use LiteCQRS\Bus;
+use Silex\Application;
+
+/**
+ * @package LiteCQRS
+ */
+class LiteCQRSServiceProvider implements \Silex\ServiceProviderInterface
+{
+ /**
+ * @param Application $app
+ */
+ public function register(Application $app)
+ {
+ $app['command_bus'] = $app->share(function (Application $app) {
+ return new ApplicationCommandBus($app, $app['lite_cqrs.command_proxy_factories']);
+ });
+
+ $app['lite_cqrs.commands'] = array();
+
+ $app['lite_cqrs.identity_map'] = $app->share(function () {
+ return new Bus\SimpleIdentityMap();
+ });
+
+ $app['lite_cqrs.event_message_bus'] = $app->share(function () {
+ return new Bus\InMemoryEventMessageBus();
+ });
+
+ $app['lite_cqrs.event_message_handler'] = $app->share(function (Application $app) {
+ return new Bus\EventMessageHandlerFactory($app['lite_cqrs.event_message_bus'], $app['lite_cqrs.identity_map']);
+ });
+
+ $app['lite_cqrs.command_proxy_factories'] = $app->share(function (Application $app) {
+ return array($app['lite_cqrs.event_message_handler']);
+ });
+ }
+
+ /**
+ * @param Application $app
+ */
+ public function boot(Application $app)
+ {
+ $app['command_bus']->registerServices($app['lite_cqrs.commands']);
+ }
+}

0 comments on commit d0eccdf

Please sign in to comment.