[ticket/12620] Uses a cache directory per environment

PHPBB3-12620
This commit is contained in:
Tristan Darricau 2014-09-29 16:06:56 +02:00
parent 998b4baa71
commit aa061aa7c9
4 changed files with 69 additions and 41 deletions

View file

@ -1,5 +1,5 @@
parameters: parameters:
core.template.cache_path: %core.root_path%cache/twig/ core.template.cache_path: %core.root_path%cache/%environment%/twig/
services: services:
template.twig.environment: template.twig.environment:

View file

@ -11,6 +11,7 @@
* *
*/ */
use Symfony\Component\Config\ConfigCache;
use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper; use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper;
use Symfony\Component\Routing\Matcher\UrlMatcher; use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext; use Symfony\Component\Routing\RequestContext;
@ -34,14 +35,10 @@ if (!defined('IN_PHPBB'))
*/ */
function phpbb_get_url_matcher(\phpbb\extension\manager $manager, RequestContext $context, $root_path, $php_ext) function phpbb_get_url_matcher(\phpbb\extension\manager $manager, RequestContext $context, $root_path, $php_ext)
{ {
if (defined('DEBUG')) $config_cache = new ConfigCache($root_path . 'cache/' . PHPBB_ENVIRONMENT . '/url_matcher.' . $php_ext, defined('DEBUG'));
if (!$config_cache->isFresh())
{ {
return phpbb_create_url_matcher($manager, $context, $root_path); phpbb_create_dumped_url_matcher($manager, $root_path, $config_cache);
}
if (!phpbb_url_matcher_dumped($root_path, $php_ext))
{
phpbb_create_dumped_url_matcher($manager, $root_path, $php_ext);
} }
return phpbb_load_url_matcher($context, $root_path, $php_ext); return phpbb_load_url_matcher($context, $root_path, $php_ext);
@ -52,10 +49,10 @@ function phpbb_get_url_matcher(\phpbb\extension\manager $manager, RequestContext
* *
* @param \phpbb\extension\manager $manager Extension manager * @param \phpbb\extension\manager $manager Extension manager
* @param string $root_path Root path * @param string $root_path Root path
* @param string $php_ext PHP file extension * @param ConfigCache $config_cache The config cache
* @return null * @return null
*/ */
function phpbb_create_dumped_url_matcher(\phpbb\extension\manager $manager, $root_path, $php_ext) function phpbb_create_dumped_url_matcher(\phpbb\extension\manager $manager, $root_path, $config_cache)
{ {
$provider = new \phpbb\controller\provider($root_path); $provider = new \phpbb\controller\provider($root_path);
$provider->find_routing_files($manager->all_enabled()); $provider->find_routing_files($manager->all_enabled());
@ -65,7 +62,7 @@ function phpbb_create_dumped_url_matcher(\phpbb\extension\manager $manager, $roo
'class' => 'phpbb_url_matcher', 'class' => 'phpbb_url_matcher',
)); ));
file_put_contents($root_path . 'cache/url_matcher.' . $php_ext, $cached_url_matcher_dump); $config_cache->write($cached_url_matcher_dump, $routes->getResources());
} }
/** /**
@ -93,20 +90,6 @@ function phpbb_create_url_matcher(\phpbb\extension\manager $manager, RequestCont
*/ */
function phpbb_load_url_matcher(RequestContext $context, $root_path, $php_ext) function phpbb_load_url_matcher(RequestContext $context, $root_path, $php_ext)
{ {
require($root_path . 'cache/url_matcher.' . $php_ext); require($root_path . 'cache/' . PHPBB_ENVIRONMENT . '/url_matcher.' . $php_ext);
return new phpbb_url_matcher($context); return new phpbb_url_matcher($context);
} }
/**
* Determine whether we have our dumped URL matcher
*
* The class is automatically dumped to the cache directory
*
* @param string $root_path Root path
* @param string $php_ext PHP file extension
* @return bool True if it exists, false if not
*/
function phpbb_url_matcher_dumped($root_path, $php_ext)
{
return file_exists($root_path . 'cache/url_matcher.' . $php_ext);
}

View file

@ -28,7 +28,12 @@ class file extends \phpbb\cache\driver\base
function __construct($cache_dir = null) function __construct($cache_dir = null)
{ {
global $phpbb_root_path; global $phpbb_root_path;
$this->cache_dir = !is_null($cache_dir) ? $cache_dir : $phpbb_root_path . 'cache/'; $this->cache_dir = !is_null($cache_dir) ? $cache_dir : $phpbb_root_path . 'cache/' . PHPBB_ENVIRONMENT . '/';
if (!is_dir($this->cache_dir))
{
@mkdir($this->cache_dir, 0777, true);
}
} }
/** /**

View file

@ -13,16 +13,21 @@
namespace phpbb\di; namespace phpbb\di;
use Symfony\Component\Config\ConfigCache;
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper; use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
use Symfony\Component\HttpKernel\DependencyInjection\RegisterListenersPass; use Symfony\Component\HttpKernel\DependencyInjection\RegisterListenersPass;
class container_builder class container_builder
{ {
/** @var string phpBB Root Path */ /**
* @var string phpBB Root Path
*/
protected $phpbb_root_path; protected $phpbb_root_path;
/** @var string php file extension */ /**
* @var string php file extension
*/
protected $php_ext; protected $php_ext;
/** /**
@ -111,6 +116,11 @@ class container_builder
*/ */
protected $config_php_file; protected $config_php_file;
/**
* @var string
*/
protected $cache_dir;
/** /**
* Constructor * Constructor
* *
@ -133,18 +143,16 @@ class container_builder
public function get_container() public function get_container()
{ {
$container_filename = $this->get_container_filename(); $container_filename = $this->get_container_filename();
if (!defined('DEBUG_CONTAINER') && $this->dump_container && file_exists($container_filename)) $config_cache = new ConfigCache($container_filename, defined('DEBUG'));
if ($this->dump_container && $config_cache->isFresh())
{ {
require($container_filename); require($container_filename);
$this->container = new \phpbb_cache_container(); $this->container = new \phpbb_cache_container();
} }
else else
{ {
if ($this->config_path === null)
{ $container_extensions = array(new \phpbb\di\extension\core($this->get_config_path()));
$this->config_path = $this->phpbb_root_path . 'config';
}
$container_extensions = array(new \phpbb\di\extension\core($this->config_path));
if ($this->use_extensions) if ($this->use_extensions)
{ {
@ -178,9 +186,9 @@ class container_builder
$this->container->compile(); $this->container->compile();
} }
if ($this->dump_container && !defined('DEBUG_CONTAINER')) if ($this->dump_container)
{ {
$this->dump_container($container_filename); $this->dump_container($config_cache);
} }
} }
@ -266,6 +274,16 @@ class container_builder
$this->config_path = $config_path; $this->config_path = $config_path;
} }
/**
* Returns the path to the container configuration (default: root_path/config)
*
* @return string
*/
protected function get_config_path()
{
return $this->config_path ?: $this->phpbb_root_path . 'config';
}
/** /**
* Set custom parameters to inject into the container. * Set custom parameters to inject into the container.
* *
@ -276,12 +294,32 @@ class container_builder
$this->custom_parameters = $custom_parameters; $this->custom_parameters = $custom_parameters;
} }
/**
* Set the path to the cache directory.
*
* @param string $cache_dir Path to the cache directory
*/
public function set_cache_dir($cache_dir)
{
$this->cache_dir = $cache_dir;
}
/**
* Returns the path to the cache directory (default: root_path/cache/environment).
*
* @return string Path to the cache directory.
*/
protected function get_cache_dir()
{
return $this->cache_dir ?: $this->phpbb_root_path . 'cache/' . PHPBB_ENVIRONMENT . '/';
}
/** /**
* Dump the container to the disk. * Dump the container to the disk.
* *
* @param string $container_filename The name of the file. * @param ConfigCache $cache The config cache
*/ */
protected function dump_container($container_filename) protected function dump_container($cache)
{ {
$dumper = new PhpDumper($this->container); $dumper = new PhpDumper($this->container);
$cached_container_dump = $dumper->dump(array( $cached_container_dump = $dumper->dump(array(
@ -289,7 +327,7 @@ class container_builder
'base_class' => 'Symfony\\Component\\DependencyInjection\\ContainerBuilder', 'base_class' => 'Symfony\\Component\\DependencyInjection\\ContainerBuilder',
)); ));
file_put_contents($container_filename, $cached_container_dump); $cache->write($cached_container_dump, $this->container->getResources());
} }
/** /**
@ -386,6 +424,8 @@ class container_builder
); );
} }
$this->custom_parameters['environment'] = PHPBB_ENVIRONMENT;
foreach ($this->custom_parameters as $key => $value) foreach ($this->custom_parameters as $key => $value)
{ {
$this->container->setParameter($key, $value); $this->container->setParameter($key, $value);
@ -400,6 +440,6 @@ class container_builder
protected function get_container_filename() protected function get_container_filename()
{ {
$filename = str_replace(array('/', '.'), array('slash', 'dot'), $this->phpbb_root_path); $filename = str_replace(array('/', '.'), array('slash', 'dot'), $this->phpbb_root_path);
return $this->phpbb_root_path . 'cache/container_' . $filename . '.' . $this->php_ext; return $this->get_cache_dir() . 'container_' . $filename . '.' . $this->php_ext;
} }
} }