Ver Fonte

[Foundation] made some performance optimizations (if you change a parameter in a .htaccess or httpd.conf, you now need to flush the cache - should not be a big deal)

Fabien Potencier há 15 anos atrás
pai
commit
7c9559bc26
2 ficheiros alterados com 89 adições e 75 exclusões
  1. 45 38
      src/Symfony/Foundation/Kernel.php
  2. 44 37
      src/Symfony/Foundation/bootstrap.php

+ 45 - 38
src/Symfony/Foundation/Kernel.php

@@ -15,6 +15,7 @@ use Symfony\Components\DependencyInjection\ContainerInterface;
 use Symfony\Components\DependencyInjection\Builder;
 use Symfony\Components\DependencyInjection\BuilderConfiguration;
 use Symfony\Components\DependencyInjection\Dumper\PhpDumper;
+use Symfony\Components\DependencyInjection\FileResource;
 use Symfony\Components\RequestHandler\RequestInterface;
 
 /**
@@ -34,7 +35,6 @@ abstract class Kernel
   protected $debug;
   protected $booted;
   protected $name;
-  protected $parameters;
   protected $startTime;
 
   const VERSION = '2.0.0-DEV';
@@ -44,9 +44,8 @@ abstract class Kernel
    *
    * @param string  $environment The environment
    * @param Boolean $debug       Whether to enable debugging or not
-   * @param array   $parameters  An array of parameters to customize the DI container
    */
-  public function __construct($environment, $debug, $parameters = array())
+  public function __construct($environment, $debug)
   {
     $this->debug = (Boolean) $debug;
     if ($this->debug)
@@ -66,7 +65,6 @@ abstract class Kernel
 
     $this->booted = false;
     $this->environment = $environment;
-    $this->parameters = $parameters;
     $this->bundles = $this->registerBundles();
     $this->bundleDirs = $this->registerBundleDirs();
     $this->rootDir = realpath($this->registerRootDir());
@@ -183,12 +181,33 @@ abstract class Kernel
     return $this->debug ? $this->startTime : -INF;
   }
 
-  public function getParameters()
+  public function getCacheDir()
   {
-    return $this->parameters;
+    return $this->rootDir.'/cache/'.$this->environment;
   }
 
-  public function getDefaultParameters()
+  public function getLogDir()
+  {
+    return $this->rootDir.'/logs';
+  }
+
+  protected function initializeContainer()
+  {
+    $class = $this->name.'ProjectContainer';
+    $location = $this->getCacheDir().'/'.$class;
+    $reload = $this->debug ? $this->needsReload($class, $location) : false;
+
+    if ($reload || !file_exists($location.'.php'))
+    {
+      $this->buildContainer($class, $location.'.php');
+    }
+
+    require_once $location.'.php';
+
+    return new $class();
+  }
+
+  public function getKernelParameters()
   {
     $bundles = array();
     foreach ($this->bundles as $bundle)
@@ -202,34 +221,16 @@ abstract class Kernel
         'kernel.environment' => $this->environment,
         'kernel.debug'       => $this->debug,
         'kernel.name'        => $this->name,
-        'kernel.cache_dir'   => $this->rootDir.'/cache/'.$this->environment,
-        'kernel.logs_dir'    => $this->rootDir.'/logs',
+        'kernel.cache_dir'   => $this->getCacheDir(),
+        'kernel.logs_dir'    => $this->getLogDir(),
         'kernel.bundle_dirs' => $this->bundleDirs,
         'kernel.bundles'     => $bundles,
         'kernel.charset'     => 'UTF-8',
       ),
-      $this->getEnvParameters(),
-      $this->parameters
+      $this->getEnvParameters()
     );
   }
 
-  protected function initializeContainer()
-  {
-    $parameters = $this->getDefaultParameters();
-    $class = $this->name.'ProjectContainer';
-    $file = $parameters['kernel.cache_dir'].'/'.$class.'.php';
-    $reload = $this->debug ? $this->needsReload($class, $file, $parameters) : false;
-
-    if ($reload || !file_exists($file))
-    {
-      $this->buildContainer($class, $file, $parameters);
-    }
-
-    require_once $file;
-
-    return new $class();
-  }
-
   protected function getEnvParameters()
   {
     $parameters = array();
@@ -244,17 +245,15 @@ abstract class Kernel
     return $parameters;
   }
 
-  protected function needsReload($class, $file, $parameters)
+  protected function needsReload($class, $location)
   {
-    $metadata = $parameters['kernel.cache_dir'].'/'.$class.'.meta';
-
-    if (!file_exists($metadata) || !file_exists($file))
+    if (!file_exists($location.'.meta') || !file_exists($location.'.php'))
     {
       return true;
     }
 
-    $meta = unserialize(file_get_contents($metadata));
-    $time = filemtime($file);
+    $meta = unserialize(file_get_contents($location.'.meta'));
+    $time = filemtime($location.'.php');
     foreach ($meta as $resource)
     {
       if (!$resource->isUptodate($time))
@@ -266,9 +265,9 @@ abstract class Kernel
     return false;
   }
 
-  protected function buildContainer($class, $file, $parameters)
+  protected function buildContainer($class, $file)
   {
-    $container = new Builder($parameters);
+    $container = new Builder($this->getKernelParameters());
 
     $configuration = new BuilderConfiguration();
     foreach ($this->bundles as $bundle)
@@ -281,7 +280,7 @@ abstract class Kernel
 
     foreach (array('cache', 'logs') as $name)
     {
-      $dir = $parameters[sprintf('kernel.%s_dir', $name)];
+      $dir = $container->getParameter(sprintf('kernel.%s_dir', $name));
       if (!is_dir($dir))
       {
         if (false === @mkdir($dir, 0777, true))
@@ -306,8 +305,16 @@ abstract class Kernel
 
     if ($this->debug)
     {
+      // add the Kernel class hierachy as resources
+      $parent = new \ReflectionObject($this);
+      $configuration->addResource(new FileResource($parent->getFileName()));
+      while ($parent = $parent->getParentClass())
+      {
+        $configuration->addResource(new FileResource($parent->getFileName()));
+      }
+
       // save the resources
-      $this->writeCacheFile($parameters['kernel.cache_dir'].'/'.$class.'.meta', serialize($configuration->getResources()));
+      $this->writeCacheFile($this->getCacheDir().'/'.$class.'.meta', serialize($configuration->getResources()));
     }
   }
 

+ 44 - 37
src/Symfony/Foundation/bootstrap.php

@@ -316,6 +316,7 @@ use Symfony\Components\DependencyInjection\ContainerInterface;
 use Symfony\Components\DependencyInjection\Builder;
 use Symfony\Components\DependencyInjection\BuilderConfiguration;
 use Symfony\Components\DependencyInjection\Dumper\PhpDumper;
+use Symfony\Components\DependencyInjection\FileResource;
 use Symfony\Components\RequestHandler\RequestInterface;
 
 
@@ -329,13 +330,12 @@ abstract class Kernel
   protected $debug;
   protected $booted;
   protected $name;
-  protected $parameters;
   protected $startTime;
 
   const VERSION = '2.0.0-DEV';
 
   
-  public function __construct($environment, $debug, $parameters = array())
+  public function __construct($environment, $debug)
   {
     $this->debug = (Boolean) $debug;
     if ($this->debug)
@@ -355,7 +355,6 @@ abstract class Kernel
 
     $this->booted = false;
     $this->environment = $environment;
-    $this->parameters = $parameters;
     $this->bundles = $this->registerBundles();
     $this->bundleDirs = $this->registerBundleDirs();
     $this->rootDir = realpath($this->registerRootDir());
@@ -459,12 +458,33 @@ abstract class Kernel
     return $this->debug ? $this->startTime : -INF;
   }
 
-  public function getParameters()
+  public function getCacheDir()
   {
-    return $this->parameters;
+    return $this->rootDir.'/cache/'.$this->environment;
   }
 
-  public function getDefaultParameters()
+  public function getLogDir()
+  {
+    return $this->rootDir.'/logs';
+  }
+
+  protected function initializeContainer()
+  {
+    $class = $this->name.'ProjectContainer';
+    $location = $this->getCacheDir().'/'.$class;
+    $reload = $this->debug ? $this->needsReload($class, $location) : false;
+
+    if ($reload || !file_exists($location.'.php'))
+    {
+      $this->buildContainer($class, $location.'.php');
+    }
+
+    require_once $location.'.php';
+
+    return new $class();
+  }
+
+  public function getKernelParameters()
   {
     $bundles = array();
     foreach ($this->bundles as $bundle)
@@ -478,34 +498,16 @@ abstract class Kernel
         'kernel.environment' => $this->environment,
         'kernel.debug'       => $this->debug,
         'kernel.name'        => $this->name,
-        'kernel.cache_dir'   => $this->rootDir.'/cache/'.$this->environment,
-        'kernel.logs_dir'    => $this->rootDir.'/logs',
+        'kernel.cache_dir'   => $this->getCacheDir(),
+        'kernel.logs_dir'    => $this->getLogDir(),
         'kernel.bundle_dirs' => $this->bundleDirs,
         'kernel.bundles'     => $bundles,
         'kernel.charset'     => 'UTF-8',
       ),
-      $this->getEnvParameters(),
-      $this->parameters
+      $this->getEnvParameters()
     );
   }
 
-  protected function initializeContainer()
-  {
-    $parameters = $this->getDefaultParameters();
-    $class = $this->name.'ProjectContainer';
-    $file = $parameters['kernel.cache_dir'].'/'.$class.'.php';
-    $reload = $this->debug ? $this->needsReload($class, $file, $parameters) : false;
-
-    if ($reload || !file_exists($file))
-    {
-      $this->buildContainer($class, $file, $parameters);
-    }
-
-    require_once $file;
-
-    return new $class();
-  }
-
   protected function getEnvParameters()
   {
     $parameters = array();
@@ -520,17 +522,15 @@ abstract class Kernel
     return $parameters;
   }
 
-  protected function needsReload($class, $file, $parameters)
+  protected function needsReload($class, $location)
   {
-    $metadata = $parameters['kernel.cache_dir'].'/'.$class.'.meta';
-
-    if (!file_exists($metadata) || !file_exists($file))
+    if (!file_exists($location.'.meta') || !file_exists($location.'.php'))
     {
       return true;
     }
 
-    $meta = unserialize(file_get_contents($metadata));
-    $time = filemtime($file);
+    $meta = unserialize(file_get_contents($location.'.meta'));
+    $time = filemtime($location.'.php');
     foreach ($meta as $resource)
     {
       if (!$resource->isUptodate($time))
@@ -542,9 +542,9 @@ abstract class Kernel
     return false;
   }
 
-  protected function buildContainer($class, $file, $parameters)
+  protected function buildContainer($class, $file)
   {
-    $container = new Builder($parameters);
+    $container = new Builder($this->getKernelParameters());
 
     $configuration = new BuilderConfiguration();
     foreach ($this->bundles as $bundle)
@@ -557,7 +557,7 @@ abstract class Kernel
 
     foreach (array('cache', 'logs') as $name)
     {
-      $dir = $parameters[sprintf('kernel.%s_dir', $name)];
+      $dir = $container->getParameter(sprintf('kernel.%s_dir', $name));
       if (!is_dir($dir))
       {
         if (false === @mkdir($dir, 0777, true))
@@ -581,7 +581,14 @@ abstract class Kernel
 
     if ($this->debug)
     {
-            $this->writeCacheFile($parameters['kernel.cache_dir'].'/'.$class.'.meta', serialize($configuration->getResources()));
+            $parent = new \ReflectionObject($this);
+      $configuration->addResource(new FileResource($parent->getFileName()));
+      while ($parent = $parent->getParentClass())
+      {
+        $configuration->addResource(new FileResource($parent->getFileName()));
+      }
+
+            $this->writeCacheFile($this->getCacheDir().'/'.$class.'.meta', serialize($configuration->getResources()));
     }
   }