From 38da529997df649a02e05d5b9238bb2a7faa55ca Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Thu, 24 Feb 2011 14:02:28 +0100 Subject: [PATCH] [Kernel] Get rid of Kernel::registerRootDir() --- .../Bundle/FrameworkBundle/Tests/Kernel.php | 19 +++---- .../Component/HttpKernel/HttpKernel.php | 14 ++++- src/Symfony/Component/HttpKernel/Kernel.php | 56 +++++++++++++++++-- .../Component/HttpKernel/KernelInterface.php | 20 +++---- 4 files changed, 78 insertions(+), 31 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Kernel.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Kernel.php index 7498a4ea7decb..7b30525a00720 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Kernel.php +++ b/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 @@ public function __construct() public function __destruct() { $fs = new Filesystem(); - $fs->remove($this->tmpDir); - } - - public function registerRootDir() - { - return $this->tmpDir; + $fs->remove($this->rootDir); } public function registerBundles() diff --git a/src/Symfony/Component/HttpKernel/HttpKernel.php b/src/Symfony/Component/HttpKernel/HttpKernel.php index 513aa364bf325..849020b251665 100644 --- a/src/Symfony/Component/HttpKernel/HttpKernel.php +++ b/src/Symfony/Component/HttpKernel/HttpKernel.php @@ -41,7 +41,19 @@ public function __construct(EventDispatcherInterface $dispatcher, ControllerReso } /** - * {@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) { diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index 0e1502a1e3f4e..52d24b2df9335 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -60,8 +60,7 @@ public function __construct($environment, $debug) $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); @@ -262,6 +261,11 @@ public function locateResource($name, $dir = null, $first = true) 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; @@ -294,6 +298,11 @@ public function isDebug() */ public function getRootDir() { + if (null === $this->rootDir) { + $r = new \ReflectionObject($this); + $this->rootDir = dirname($r->getFileName()); + } + return $this->rootDir; } @@ -324,7 +333,7 @@ public function getStartTime() */ public function getCacheDir() { - return $this->rootDir.'/cache/'.$this->environment; + return $this->getRootDir().'/cache/'.$this->environment; } /** @@ -334,7 +343,7 @@ public function getCacheDir() */ public function getLogDir() { - return $this->rootDir.'/logs'; + return $this->getRootDir().'/logs'; } /** @@ -345,7 +354,6 @@ public function getLogDir() * @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() { @@ -396,6 +404,12 @@ protected function initializeBundles() } + /** + * 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->name.ucfirst($this->environment).($this->debug ? 'Debug' : '').'ProjectContainer'; @@ -418,6 +432,11 @@ protected function initializeContainer() } } + /** + * Returns the kernel parameters + * + * @return array An array of kernel parameters + */ protected function getKernelParameters() { $bundles = array(); @@ -427,7 +446,7 @@ protected function getKernelParameters() 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, @@ -440,6 +459,13 @@ protected function getKernelParameters() ); } + /** + * Gets the environment parameters + * + * Only the parameters starting with "SYMFONY__" are considered + * + * @return array An array of parameters + */ protected function getEnvParameters() { $parameters = array(); @@ -452,6 +478,11 @@ protected function getEnvParameters() return $parameters; } + /** + * Builds the DI container + * + * @return ContainerBuilder The compiled DI container + */ protected function buildContainer() { $parameterBag = new ParameterBag($this->getKernelParameters()); @@ -474,6 +505,13 @@ protected function buildContainer() 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) { foreach (array('cache', 'logs') as $name) { @@ -497,6 +535,12 @@ protected function dumpContainer(ConfigCache $cache, ContainerBuilder $container $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) { $resolver = new LoaderResolver(array( diff --git a/src/Symfony/Component/HttpKernel/KernelInterface.php b/src/Symfony/Component/HttpKernel/KernelInterface.php index 59d8f78b23e2d..8bc162680897b 100644 --- a/src/Symfony/Component/HttpKernel/KernelInterface.php +++ b/src/Symfony/Component/HttpKernel/KernelInterface.php @@ -25,15 +25,6 @@ */ 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 @@ function getBundles(); 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 @@ function getBundle($name, $first = true); */ function locateResource($name, $dir = null, $first = true); + /** + * Gets the name of the kernel + * + * @return string The kernel name + */ function getName(); /**