Forráskód Böngészése

Implement a bootable, event-listening Service Provider.

Frederic G. MARAND 6 éve
szülő
commit
c83fda0bc9
3 módosított fájl, 73 hozzáadás és 5 törlés
  1. 7 2
      src/app.php
  2. 11 3
      src/demo/Services/NavBuilder.php
  3. 55 0
      src/demo/Services/NavProvider.php

+ 7 - 2
src/app.php

@@ -2,7 +2,7 @@
 
 use demo\AppRoute;
 use demo\Logger;
-use demo\Services\NavBuilder;
+use demo\Services\NavProvider;
 use demo\Services\Timer;
 use Demo\UserConverter;
 use Silex\Application;
@@ -20,7 +20,12 @@ $app['timer'] = Timer::create();
 
 // Closures are the basic form of Pimple services.
 $app['logger'] = function () { return new Logger(); };
-$app['nav_builder'] = function ($app) { return NavBuilder::create($app); };
+
+// Service providers allow a closs to define multiple configurable services.
+$app->register(new NavProvider(), ['nav' => ['blog_label' => 'Blog list']]);
+// Notice how someone else could tweak the provided service configuration:
+$app['nav'] = ['blog_label' => 'Blogs list'];
+
 $app->register(new ServiceControllerServiceProvider());
 $app->register(new AssetServiceProvider());
 $app->register(new TwigServiceProvider());

+ 11 - 3
src/demo/Services/NavBuilder.php

@@ -7,6 +7,11 @@ use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
 
 class NavBuilder {
 
+  /**
+   * @var array
+   */
+  protected $config;
+
   /**
    * @var \Twig_Environment
    */
@@ -19,8 +24,10 @@ class NavBuilder {
 
   public function __construct(
     UrlGeneratorInterface $urlGenerator,
-    \Twig_Environment $twig
+    \Twig_Environment $twig,
+    array $config
   ) {
+    $this->config = $config;
     $this->twig = $twig;
     $this->urlGenerator = $urlGenerator;
   }
@@ -28,7 +35,8 @@ class NavBuilder {
   public static function create(Application $app) {
     $urlGenerator = $app['url_generator'];
     $twig = $app['twig'];
-    return new static($urlGenerator, $twig);
+    $config = $app['nav'];
+    return new static($urlGenerator, $twig, $config);
   }
 
   public function build() {
@@ -36,7 +44,7 @@ class NavBuilder {
     $blog = $this->urlGenerator->generate('blog_list');
     $result =  [
       'home' => ['url' => $home, 'text' => 'Accueil'],
-      'blog' => ['url' => $blog, 'text' => 'Blogs'],
+      'blog' => ['url' => $blog, 'text' => $this->config['blog_label']],
     ];
     return $result;
   }

+ 55 - 0
src/demo/Services/NavProvider.php

@@ -0,0 +1,55 @@
+<?php
+
+namespace demo\Services;
+
+use Pimple\Container;
+use Pimple\ServiceProviderInterface;
+use Silex\Api\BootableProviderInterface;
+use Silex\Api\EventListenerProviderInterface;
+use Silex\Application;
+use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
+use Symfony\Component\EventDispatcher\EventDispatcherInterface;
+use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
+use Symfony\Component\HttpKernel\Event\GetResponseEvent;
+use Symfony\Component\HttpKernel\KernelEvents;
+
+class NavProvider implements BootableProviderInterface, EventListenerProviderInterface, ServiceProviderInterface {
+
+  /**
+   * Bootstraps the application.
+   *
+   * This method is called after all services are registered
+   * and should be used for "dynamic" configuration (whenever
+   * a service must be requested).
+   *
+   * @param Application $app
+   */
+  public function boot(Application $app) {
+    return;
+  }
+
+  public function subscribe(
+    Container $app,
+    EventDispatcherInterface $dispatcher
+  ) {
+    $dispatcher->addListener(KernelEvents::RESPONSE, [$this, 'onResponse']);
+  }
+
+  /**
+   * Registers services on the given container.
+   *
+   * This method should only be used to configure services and parameters.
+   * It should not get services.
+   *
+   * @param Container $dic A container instance
+   */
+  public function register(Container $dic) {
+    $dic['nav_builder'] = function ($app) {
+      return NavBuilder::create($app);
+    };
+  }
+
+  public function onResponse(FilterResponseEvent $event, string $eventName, EventDispatcherInterface $eventDispatcher) {
+    echo "<p>Got a " . $event->getResponse()->getStatusCode() . " response</p>\n";
+  }
+}