Jelajahi Sumber

merged vicb/kernel

Fabien Potencier 14 tahun lalu
induk
melakukan
4da43df963

+ 7 - 12
src/Symfony/Bundle/FrameworkBundle/Tests/Kernel.php

@@ -20,13 +20,13 @@ class Kernel extends BaseKernel
 {
     public function __construct()
     {
-        $this->tmpDir = sys_get_temp_dir().'/sf2_'.rand(1, 9999);
-        if (!is_dir($this->tmpDir)) {
-            if (false === @mkdir($this->tmpDir)) {
-                die(sprintf('Unable to create a temporary directory (%s)', $this->tmpDir));
+        $this->rootDir = sys_get_temp_dir().'/sf2_'.rand(1, 9999);
+        if (!is_dir($this->rootDir)) {
+            if (false === @mkdir($this->rootDir)) {
+                die(sprintf('Unable to create a temporary directory (%s)', $this->rootDir));
             }
-        } elseif (!is_writable($this->tmpDir)) {
-            die(sprintf('Unable to write in a temporary directory (%s)', $this->tmpDir));
+        } elseif (!is_writable($this->rootDir)) {
+            die(sprintf('Unable to write in a temporary directory (%s)', $this->rootDir));
         }
 
         parent::__construct('env', true);
@@ -42,12 +42,7 @@ class Kernel extends BaseKernel
     public function __destruct()
     {
         $fs = new Filesystem();
-        $fs->remove($this->tmpDir);
-    }
-
-    public function registerRootDir()
-    {
-        return $this->tmpDir;
+        $fs->remove($this->rootDir);
     }
 
     public function registerBundles()

+ 13 - 1
src/Symfony/Component/HttpKernel/HttpKernel.php

@@ -41,7 +41,19 @@ class HttpKernel implements HttpKernelInterface
     }
 
     /**
-     * {@inheritdoc}
+     * Handles a Request to convert it to a Response.
+     *
+     * When $catch is true, the implementation must catch all exceptions
+     * and do its best to convert them to a Response instance.
+     *
+     * @param  Request $request A Request instance
+     * @param  integer $type    The type of the request
+     *                          (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
+     * @param  Boolean $catch   Whether to catch exceptions or not
+     *
+     * @return Response A Response instance
+     *
+     * @throws \Exception When an Exception occurs during processing
      */
     public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
     {

+ 50 - 6
src/Symfony/Component/HttpKernel/Kernel.php

@@ -61,8 +61,7 @@ abstract class Kernel implements KernelInterface
         $this->environment = $environment;
         $this->debug = (Boolean) $debug;
         $this->booted = false;
-        $this->rootDir = realpath($this->registerRootDir());
-        $this->name = preg_replace('/[^a-zA-Z0-9_]+/', '', basename($this->rootDir));
+        $this->name = preg_replace('/[^a-zA-Z0-9_]+/', '', basename($this->getRootDir()));
 
         if ($this->debug) {
             ini_set('display_errors', 1);
@@ -263,6 +262,11 @@ abstract class Kernel implements KernelInterface
         throw new \InvalidArgumentException(sprintf('Unable to find file "@%s".', $name));
     }
 
+    /**
+     * Gets the name of the kernel
+     *
+     * @return string The kernel name
+     */
     public function getName()
     {
         return $this->name;
@@ -295,6 +299,11 @@ abstract class Kernel implements KernelInterface
      */
     public function getRootDir()
     {
+        if (null === $this->rootDir) {
+            $r = new \ReflectionObject($this);
+            $this->rootDir = dirname($r->getFileName());
+        }
+        
         return $this->rootDir;
     }
 
@@ -325,7 +334,7 @@ abstract class Kernel implements KernelInterface
      */
     public function getCacheDir()
     {
-        return $this->rootDir.'/cache/'.$this->environment;
+        return $this->getRootDir().'/cache/'.$this->environment;
     }
 
     /**
@@ -335,7 +344,7 @@ abstract class Kernel implements KernelInterface
      */
     public function getLogDir()
     {
-        return $this->rootDir.'/logs';
+        return $this->getRootDir().'/logs';
     }
 
     /**
@@ -346,7 +355,6 @@ abstract class Kernel implements KernelInterface
      * @throws \LogicException if two bundles share a common name
      * @throws \LogicException if a bundle tries to extend a non-registered bundle
      * @throws \LogicException if two bundles extend the same ancestor
-     *
      */
     protected function initializeBundles()
     {
@@ -407,6 +415,12 @@ abstract class Kernel implements KernelInterface
         return $this->name.ucfirst($this->environment).($this->debug ? 'Debug' : '').'ProjectContainer';
     }
 
+    /**
+     * Initializes the DI container
+     *
+     * The cached version of the DI container is used when fresh, otherwise the
+     * container is built.
+     */
     protected function initializeContainer()
     {
         $class = $this->getContainerClass();
@@ -429,6 +443,11 @@ abstract class Kernel implements KernelInterface
         }
     }
 
+    /**
+     * Returns the kernel parameters
+     *
+     * @return array An array of kernel parameters
+     */
     protected function getKernelParameters()
     {
         $bundles = array();
@@ -438,7 +457,7 @@ abstract class Kernel implements KernelInterface
 
         return array_merge(
             array(
-                'kernel.root_dir'        => $this->rootDir,
+                'kernel.root_dir'        => $this->getRootDir(),
                 'kernel.environment'     => $this->environment,
                 'kernel.debug'           => $this->debug,
                 'kernel.name'            => $this->name,
@@ -452,6 +471,13 @@ abstract class Kernel implements KernelInterface
         );
     }
 
+    /**
+     * Gets the environment parameters
+     *
+     * Only the parameters starting with "SYMFONY__" are considered
+     *
+     * @return array An array of parameters
+     */
     protected function getEnvParameters()
     {
         $parameters = array();
@@ -464,6 +490,11 @@ abstract class Kernel implements KernelInterface
         return $parameters;
     }
 
+    /**
+     * Builds the DI container
+     *
+     * @return ContainerBuilder The compiled DI container
+     */
     protected function buildContainer()
     {
         $parameterBag = new ParameterBag($this->getKernelParameters());
@@ -499,6 +530,13 @@ abstract class Kernel implements KernelInterface
         return $container;
     }
 
+    /**
+     * Dumps the DI container to PHP code in the cache
+     * 
+     * @param ConfigCache       $cache      The config cache
+     * @param ContainerBuilder  $container  The DI container
+     * @param string            $class      The name of the class to generate
+     */
     protected function dumpContainer(ConfigCache $cache, ContainerBuilder $container, $class)
     {
         // cache the container
@@ -511,6 +549,12 @@ abstract class Kernel implements KernelInterface
         $cache->write($content, $container->getResources());
     }
 
+    /**
+     * Returns a loader for the container
+     * 
+     * @param ContainerInterface $container The DI container
+     * @return DelegatingLoader The loader
+     */
     protected function getContainerLoader(ContainerInterface $container)
     {
         $locator = new FileLocator($this);

+ 8 - 12
src/Symfony/Component/HttpKernel/KernelInterface.php

@@ -25,15 +25,6 @@ use Symfony\Component\Config\Loader\LoaderInterface;
  */
 interface KernelInterface extends HttpKernelInterface, \Serializable
 {
-    /**
-     * Returns the root directory of this application.
-     *
-     * Most of the time, this is just __DIR__.
-     *
-     * @return string A directory path
-     */
-    function registerRootDir();
-
     /**
      * Returns an array of bundles to registers.
      *
@@ -77,12 +68,12 @@ interface KernelInterface extends HttpKernelInterface, \Serializable
     function isClassInActiveBundle($class);
 
     /**
-     * Returns a bundle by its name.
+     * Returns a bundle and optionally its descendants by its name.
      *
      * @param string  $name  Bundle name
-     * @param Boolean $first Whether to return the first bundle or all bundles matching this name
+     * @param Boolean $first Whether to return the first bundle only or together with its descendants
      *
-     * @return BundleInterface A BundleInterface instance
+     * @return BundleInterface|Array A BundleInterface instance or an array of BundleInterface instances if $first is false
      *
      * @throws \InvalidArgumentException when the bundle is not enabled
      */
@@ -116,6 +107,11 @@ interface KernelInterface extends HttpKernelInterface, \Serializable
      */
     function locateResource($name, $dir = null, $first = true);
 
+    /**
+     * Gets the name of the kernel
+     *
+     * @return string The kernel name
+     */
     function getName();
 
     /**