Przeglądaj źródła

[Foundation] added a way to reboot the kernel

Fabien Potencier 15 lat temu
rodzic
commit
ee50040a79

+ 4 - 0
src/Symfony/Foundation/Bundle/Bundle.php

@@ -29,4 +29,8 @@ abstract class Bundle implements BundleInterface
   public function boot(ContainerInterface $container)
   {
   }
+
+  public function shutdown(ContainerInterface $container)
+  {
+  }
 }

+ 2 - 0
src/Symfony/Foundation/Bundle/BundleInterface.php

@@ -25,4 +25,6 @@ interface BundleInterface
   public function buildContainer(ContainerInterface $container);
 
   public function boot(ContainerInterface $container);
+
+  public function shutdown(ContainerInterface $container);
 }

+ 31 - 1
src/Symfony/Foundation/Kernel.php

@@ -113,7 +113,7 @@ abstract class Kernel implements \Serializable
     $this->container = $this->initializeContainer();
     $this->container->setService('kernel', $this);
 
-    // boot bundles (in reverse order)
+    // boot bundles
     foreach ($this->bundles as $bundle)
     {
       $bundle->boot($this->container);
@@ -124,6 +124,36 @@ abstract class Kernel implements \Serializable
     return $this;
   }
 
+  /**
+   * Shutdowns the kernel.
+   *
+   * This method is mainly useful when doing functional testing.
+   */
+  public function shutdown()
+  {
+    $this->booted = false;
+
+    foreach ($this->bundles as $bundle)
+    {
+      $bundle->shutdown($this->container);
+    }
+
+    $this->container = null;
+  }
+
+  /**
+   * Reboots the kernel.
+   *
+   * This method is mainly useful when doing functional testing.
+   *
+   * It is a shortcut for the call to shutdown() and boot().
+   */
+  public function reboot()
+  {
+    $this->shutdown();
+    $this->boot();
+  }
+
   public function run()
   {
     $this->handle()->send();

+ 36 - 10
src/Symfony/Foundation/bootstrap.php

@@ -1,4 +1,4 @@
-<?php
+<?php 
 
 namespace Symfony\Foundation\Bundle;
 
@@ -16,6 +16,10 @@ abstract class Bundle implements BundleInterface
   public function boot(ContainerInterface $container)
   {
   }
+
+  public function shutdown(ContainerInterface $container)
+  {
+  }
 }
 
 
@@ -31,6 +35,8 @@ interface BundleInterface
   public function buildContainer(ContainerInterface $container);
 
   public function boot(ContainerInterface $container);
+
+  public function shutdown(ContainerInterface $container);
 }
 
 
@@ -145,7 +151,7 @@ class KernelExtension extends LoaderExtension
     return $configuration;
   }
 
-
+  
   public function getXsdValidationBasePath()
   {
     return false;
@@ -182,7 +188,7 @@ class ErrorHandler
 
   protected $level;
 
-
+  
   public function __construct($level = null)
   {
     $this->level = null === $level ? error_reporting() : $level;
@@ -193,7 +199,7 @@ class ErrorHandler
     set_error_handler(array($this, 'handle'));
   }
 
-
+  
   public function handle($level, $message, $file, $line, $context)
   {
     if (0 === $this->level)
@@ -218,7 +224,7 @@ namespace Symfony\Foundation;
 
 class ClassCollectionLoader
 {
-
+  
   static public function load($classes, $cacheDir, $name, $autoReload)
   {
     $cache = $cacheDir.'/'.$name.'.php';
@@ -336,7 +342,7 @@ abstract class Kernel implements \Serializable
 
   const VERSION = '2.0.0-DEV';
 
-
+  
   public function __construct($environment, $debug)
   {
     $this->debug = (Boolean) $debug;
@@ -373,13 +379,13 @@ abstract class Kernel implements \Serializable
 
   abstract public function registerRoutes();
 
-
+  
   public function isBooted()
   {
     return $this->booted;
   }
 
-
+  
   public function boot()
   {
     if (true === $this->booted)
@@ -400,6 +406,26 @@ abstract class Kernel implements \Serializable
     return $this;
   }
 
+  
+  public function shutdown()
+  {
+    $this->booted = false;
+
+    foreach ($this->bundles as $bundle)
+    {
+      $bundle->shutdown($this->container);
+    }
+
+    $this->container = null;
+  }
+
+  
+  public function reboot()
+  {
+    $this->shutdown();
+    $this->boot();
+  }
+
   public function run()
   {
     $this->handle()->send();
@@ -683,7 +709,7 @@ class EventDispatcher extends BaseEventDispatcher
 {
   protected $container;
 
-
+  
   public function __construct(ContainerInterface $container)
   {
     $this->container = $container;
@@ -700,7 +726,7 @@ class EventDispatcher extends BaseEventDispatcher
     }
   }
 
-
+  
   public function getListeners($name)
   {
     if (!isset($this->listeners[$name]))