1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-06-04 21:44:57 +02:00

Merge pull request #2883 from Nicofuma/ticket/12620

[ticket/12620] Allow the user to define multiples environments
This commit is contained in:
Nils Adermann 2014-11-21 18:20:19 +01:00
commit 040d451dcc
76 changed files with 774 additions and 407 deletions

View File

@ -283,6 +283,8 @@
<delete file="${dir}/config.php" />
<delete dir="${dir}/develop" />
<delete dir="${dir}/install/data" />
<delete dir="${dir}/config/development" />
<delete dir="${dir}/config/test" />
<phingcall target="clean-vendor-dir">
<property name="dir" value="${dir}" />

View File

@ -21,7 +21,6 @@ define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_url_matcher.' . $phpEx);
// Start session management
$user->session_begin();

View File

@ -21,6 +21,12 @@ if (php_sapi_name() != 'cli')
}
define('IN_PHPBB', true);
if (!defined('PHPBB_ENVIRONMENT'))
{
@define('PHPBB_ENVIRONMENT', 'production');
}
$phpbb_root_path = __DIR__ . '/../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
require($phpbb_root_path . 'includes/startup.' . $phpEx);

View File

@ -29,6 +29,11 @@ $phpbb_class_loader->register();
$phpbb_config_php_file = new \phpbb\config_php_file($phpbb_root_path, $phpEx);
extract($phpbb_config_php_file->get_all());
if (!defined('PHPBB_ENVIRONMENT'))
{
@define('PHPBB_ENVIRONMENT', 'production');
}
if (!defined('PHPBB_INSTALLED'))
{
// Redirect the user to the installer
@ -94,8 +99,18 @@ $phpbb_class_loader_ext->register();
phpbb_load_extensions_autoloaders($phpbb_root_path);
// Set up container
$phpbb_container_builder = new \phpbb\di\container_builder($phpbb_config_php_file, $phpbb_root_path, $phpEx);
$phpbb_container = $phpbb_container_builder->get_container();
try
{
$phpbb_container_builder = new \phpbb\di\container_builder($phpbb_config_php_file, $phpbb_root_path, $phpEx);
$phpbb_container = $phpbb_container_builder->get_container();
}
catch (InvalidArgumentException $e)
{
trigger_error(
'The requested environment ' . PHPBB_ENVIRONMENT . ' is not available.',
E_USER_ERROR
);
}
$phpbb_class_loader->set_cache($phpbb_container->get('cache.driver'));
$phpbb_class_loader_ext->set_cache($phpbb_container->get('cache.driver'));

View File

View File

@ -1,19 +1,19 @@
imports:
- { resource: auth.yml }
- { resource: avatar.yml }
- { resource: captcha.yml }
- { resource: console.yml }
- { resource: content.yml }
- { resource: cron.yml }
- { resource: db.yml }
- { resource: event.yml }
- { resource: feed.yml }
- { resource: mimetype_guesser.yml }
- { resource: notification.yml }
- { resource: password.yml }
- { resource: profilefield.yml }
- { resource: twig.yml }
- { resource: user.yml }
- { resource: services_auth.yml }
- { resource: services_avatar.yml }
- { resource: services_captcha.yml }
- { resource: services_console.yml }
- { resource: services_content.yml }
- { resource: services_cron.yml }
- { resource: services_db.yml }
- { resource: services_event.yml }
- { resource: services_feed.yml }
- { resource: services_mimetype_guesser.yml }
- { resource: services_notification.yml }
- { resource: services_password.yml }
- { resource: services_profilefield.yml }
- { resource: services_twig.yml }
- { resource: services_user.yml }
- { resource: tables.yml }
- { resource: parameters.yml }
@ -157,6 +157,7 @@ services:
- @ext.manager
- %core.root_path%
- %core.php_ext%
- %core.environment%
router.listener:
class: Symfony\Component\HttpKernel\EventListener\RouterListener

View File

@ -0,0 +1,60 @@
services:
migrator:
class: phpbb\db\migrator
arguments:
- @config
- @dbal.conn
- @dbal.tools
- %tables.migrations%
- %core.root_path%
- %core.php_ext%
- %core.table_prefix%
- @migrator.tool_collection
- @migrator.helper
migrator.helper:
class: phpbb\db\migration\helper
migrator.tool_collection:
class: phpbb\di\service_collection
arguments:
- @service_container
tags:
- { name: service_collection, tag: migrator.tool }
migrator.tool.config:
class: phpbb\db\migration\tool\config
arguments:
- @config
tags:
- { name: migrator.tool }
migrator.tool.config_text:
class: phpbb\db\migration\tool\config_text
arguments:
- @config_text
tags:
- { name: migrator.tool }
migrator.tool.module:
class: phpbb\db\migration\tool\module
arguments:
- @dbal.conn
- @cache
- @user
- %core.root_path%
- %core.php_ext%
- %tables.modules%
tags:
- { name: migrator.tool }
migrator.tool.permission:
class: phpbb\db\migration\tool\permission
arguments:
- @dbal.conn
- @cache
- @auth
- %core.root_path%
- %core.php_ext%
tags:
- { name: migrator.tool }

View File

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

View File

@ -0,0 +1,5 @@
imports:
- { resource: ../default/config.yml }
core:
require_dev_dependencies: true

View File

@ -0,0 +1,3 @@
imports:
- { resource: services.yml }
- { resource: parameters.yml }

View File

@ -0,0 +1,2 @@
imports:
- { resource: ../../default/container/parameters.yml }

View File

@ -0,0 +1,2 @@
imports:
- { resource: ../../default/container/services.yml }

View File

@ -0,0 +1,2 @@
core.default:
resource: "../../default/routing/routing.yml"

View File

@ -0,0 +1,2 @@
imports:
- { resource: ../default/config.yml }

View File

@ -0,0 +1,3 @@
imports:
- { resource: services.yml }
- { resource: parameters.yml }

View File

@ -0,0 +1,2 @@
imports:
- { resource: ../../default/container/parameters.yml }

View File

@ -0,0 +1,2 @@
imports:
- { resource: ../../default/container/services.yml }

View File

@ -0,0 +1,2 @@
core.default:
resource: "../../default/routing/routing.yml"

View File

@ -0,0 +1,5 @@
imports:
- { resource: ../default/config.yml }
core:
require_dev_dependencies: true

View File

@ -0,0 +1,3 @@
imports:
- { resource: services.yml }
- { resource: parameters.yml }

View File

@ -0,0 +1,2 @@
imports:
- { resource: ../../default/container/parameters.yml }

View File

@ -0,0 +1,2 @@
imports:
- { resource: ../../default/container/services.yml }

View File

@ -0,0 +1,2 @@
core.default:
resource: "../../default/routing/routing.yml"

View File

@ -42,6 +42,11 @@ if (isset($_GET['avatar']))
$phpbb_config_php_file = new \phpbb\config_php_file($phpbb_root_path, $phpEx);
extract($phpbb_config_php_file->get_all());
if (!defined('PHPBB_ENVIRONMENT'))
{
@define('PHPBB_ENVIRONMENT', 'production');
}
if (!defined('PHPBB_INSTALLED') || empty($dbms) || empty($acm_type))
{
exit;

View File

@ -451,13 +451,17 @@ function phpbb_create_config_file_data($data, $dbms, $debug = false, $debug_cont
$config_data .= "\n@define('PHPBB_INSTALLED', true);\n";
$config_data .= "// @define('PHPBB_DISPLAY_LOAD_TIME', true);\n";
if ($debug)
if ($debug_test)
{
$config_data .= "@define('DEBUG', true);\n";
$config_data .= "@define('PHPBB_ENVIRONMENT', 'test');\n";
}
else if ($debug)
{
$config_data .= "@define('PHPBB_ENVIRONMENT', 'development');\n";
}
else
{
$config_data .= "// @define('DEBUG', true);\n";
$config_data .= "@define('PHPBB_ENVIRONMENT', 'production');\n";
}
if ($debug_container)
@ -472,6 +476,7 @@ function phpbb_create_config_file_data($data, $dbms, $debug = false, $debug_cont
if ($debug_test)
{
$config_data .= "@define('DEBUG_TEST', true);\n";
$config_data .= "@define('DEBUG', true);\n"; // Mandatory for the functional tests, will be removed by PHPBB3-12623
}
return $config_data;

View File

@ -1,112 +0,0 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
/**
* Create a new UrlMatcher class and dump it into the cache file
*
* @param \phpbb\extension\manager $manager Extension manager
* @param RequestContext $context Symfony RequestContext object
* @param string $root_path Root path
* @param string $php_ext PHP file extension
* @return null
*/
function phpbb_get_url_matcher(\phpbb\extension\manager $manager, RequestContext $context, $root_path, $php_ext)
{
if (defined('DEBUG'))
{
return phpbb_create_url_matcher($manager, $context, $root_path);
}
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);
}
/**
* Create a new UrlMatcher class and dump it into the cache file
*
* @param \phpbb\extension\manager $manager Extension manager
* @param string $root_path Root path
* @param string $php_ext PHP file extension
* @return null
*/
function phpbb_create_dumped_url_matcher(\phpbb\extension\manager $manager, $root_path, $php_ext)
{
$provider = new \phpbb\controller\provider();
$provider->find_routing_files($manager->get_finder());
$routes = $provider->find($root_path)->get_routes();
$dumper = new PhpMatcherDumper($routes);
$cached_url_matcher_dump = $dumper->dump(array(
'class' => 'phpbb_url_matcher',
));
file_put_contents($root_path . 'cache/url_matcher.' . $php_ext, $cached_url_matcher_dump);
}
/**
* Create a non-cached UrlMatcher
*
* @param \phpbb\extension\manager $manager Extension manager
* @param RequestContext $context Symfony RequestContext object
* @return UrlMatcher
*/
function phpbb_create_url_matcher(\phpbb\extension\manager $manager, RequestContext $context, $root_path)
{
$provider = new \phpbb\controller\provider();
$provider->find_routing_files($manager->get_finder());
$routes = $provider->find($root_path)->get_routes();
return new UrlMatcher($routes, $context);
}
/**
* Load the cached phpbb_url_matcher class
*
* @param RequestContext $context Symfony RequestContext object
* @param string $root_path Root path
* @param string $php_ext PHP file extension
* @return phpbb_url_matcher
*/
function phpbb_load_url_matcher(RequestContext $context, $root_path, $php_ext)
{
require($root_path . 'cache/url_matcher.' . $php_ext);
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

@ -16,6 +16,7 @@
*/
define('IN_PHPBB', true);
define('IN_INSTALL', true);
define('PHPBB_ENVIRONMENT', 'production');
/**#@-*/
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './../';

View File

@ -27,8 +27,14 @@ class file extends \phpbb\cache\driver\base
*/
function __construct($cache_dir = null)
{
global $phpbb_root_path;
$this->cache_dir = !is_null($cache_dir) ? $cache_dir : $phpbb_root_path . 'cache/';
global $phpbb_root_path, $phpbb_container;
$this->cache_dir = !is_null($cache_dir) ? $cache_dir : $phpbb_root_path . 'cache/' . $phpbb_container->getParameter('core.environment') . '/';
if (!is_dir($this->cache_dir))
{
@mkdir($this->cache_dir, 0777, true);
}
}
/**

View File

@ -41,8 +41,9 @@ class helper
protected $config;
/**
* @var \phpbb\routing\router phpBB router
*/
* phpBB router
* @var \phpbb\routing\router
*/
protected $router;
/* @var \phpbb\symfony_request */

View File

@ -13,16 +13,25 @@
namespace phpbb\di;
use Symfony\Component\Config\ConfigCache;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
use Symfony\Component\HttpKernel\DependencyInjection\RegisterListenersPass;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
use Symfony\Component\HttpKernel\DependencyInjection\MergeExtensionConfigurationPass;
class container_builder
{
/** @var string phpBB Root Path */
/**
* @var string phpBB Root Path
*/
protected $phpbb_root_path;
/** @var string php file extension */
/**
* @var string php file extension
*/
protected $php_ext;
/**
@ -111,6 +120,11 @@ class container_builder
*/
protected $config_php_file;
/**
* @var string
*/
protected $cache_dir;
/**
* Constructor
*
@ -133,23 +147,30 @@ class container_builder
public function get_container()
{
$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);
$this->container = new \phpbb_cache_container();
}
else
{
if ($this->config_path === null)
{
$this->config_path = $this->phpbb_root_path . 'config';
}
$container_extensions = array(new \phpbb\di\extension\core($this->config_path));
$container_extensions = array(new \phpbb\di\extension\core($this->get_config_path()));
if ($this->use_extensions)
{
$installed_exts = $this->get_installed_extensions();
$container_extensions[] = new \phpbb\di\extension\ext($installed_exts);
foreach ($installed_exts as $ext_name => $path)
{
$extension_class = '\\' . str_replace('/', '\\', $ext_name) . '\\di\\extension';
if (!class_exists($extension_class))
{
$extension_class = '\phpbb\extension\di\extension_base';
}
$container_extensions[] = new $extension_class($ext_name, $path);
}
}
if ($this->inject_config)
@ -171,6 +192,9 @@ class container_builder
}
}
$loader = new YamlFileLoader($this->container, new FileLocator(phpbb_realpath($this->get_config_path())));
$loader->load($this->container->getParameter('core.environment') . '/config.yml');
$this->inject_custom_parameters();
if ($this->compile_container)
@ -178,9 +202,9 @@ class container_builder
$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 +290,16 @@ class container_builder
$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.
*
@ -276,12 +310,32 @@ class container_builder
$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/' . $this->get_environment() . '/';
}
/**
* 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);
$cached_container_dump = $dumper->dump(array(
@ -289,7 +343,7 @@ class container_builder
'base_class' => 'Symfony\\Component\\DependencyInjection\\ContainerBuilder',
));
file_put_contents($container_filename, $cached_container_dump);
$cache->write($cached_container_dump, $this->container->getResources());
}
/**
@ -362,34 +416,73 @@ class container_builder
*/
protected function create_container(array $extensions)
{
$container = new ContainerBuilder();
$container = new ContainerBuilder(new ParameterBag($this->get_core_parameters()));
$extensions_alias = array();
foreach ($extensions as $extension)
{
$container->registerExtension($extension);
$container->loadFromExtension($extension->getAlias());
$extensions_alias[] = $extension->getAlias();
//$container->loadFromExtension($extension->getAlias());
}
$container->getCompilerPassConfig()->setMergePass(new MergeExtensionConfigurationPass($extensions_alias));
return $container;
}
/**
* Inject the customs parameters into the container
*/
* Inject the customs parameters into the container
*/
protected function inject_custom_parameters()
{
if ($this->custom_parameters === null)
if ($this->custom_parameters !== null)
{
$this->custom_parameters = array(
'core.root_path' => $this->phpbb_root_path,
'core.php_ext' => $this->php_ext,
);
foreach ($this->custom_parameters as $key => $value)
{
$this->container->setParameter($key, $value);
}
}
}
/**
* Returns the core parameters.
*
* @return array An array of core parameters
*/
protected function get_core_parameters()
{
return array_merge(
array(
'core.root_path' => $this->phpbb_root_path,
'core.php_ext' => $this->php_ext,
'core.environment' => $this->get_environment(),
'core.debug' => DEBUG,
),
$this->get_env_parameters()
);
}
/**
* Gets the environment parameters.
*
* Only the parameters starting with "PHPBB__" are considered.
*
* @return array An array of parameters
*/
protected function get_env_parameters()
{
$parameters = array();
foreach ($_SERVER as $key => $value)
{
if (0 === strpos($key, 'PHPBB__'))
{
$parameters[strtolower(str_replace('__', '.', substr($key, 9)))] = $value;
}
}
foreach ($this->custom_parameters as $key => $value)
{
$this->container->setParameter($key, $value);
}
return $parameters;
}
/**
@ -400,6 +493,16 @@ class container_builder
protected function get_container_filename()
{
$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;
}
/**
* Return the name of the current environment.
*
* @return string
*/
protected function get_environment()
{
return PHPBB_ENVIRONMENT;
}
}

View File

@ -0,0 +1,38 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\di\extension;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class container_configuration implements ConfigurationInterface
{
/**
* Generates the configuration tree builder.
*
* @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('core');
$rootNode
->children()
->booleanNode('require_dev_dependencies')->defaultValue(false)->end()
->end()
;
return $treeBuilder;
}
}

View File

@ -13,10 +13,11 @@
namespace phpbb\di\extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
/**
* Container core extension
@ -24,42 +25,68 @@ use Symfony\Component\Config\FileLocator;
class core extends Extension
{
/**
* Config path
* @var string
*/
* Config path
* @var string
*/
protected $config_path;
/**
* Constructor
*
* @param string $config_path Config path
*/
* Constructor
*
* @param string $config_path Config path
*/
public function __construct($config_path)
{
$this->config_path = $config_path;
}
/**
* Loads a specific configuration.
*
* @param array $config An array of configuration values
* @param ContainerBuilder $container A ContainerBuilder instance
*
* @throws \InvalidArgumentException When provided tag is not defined in this extension
*/
public function load(array $config, ContainerBuilder $container)
* Loads a specific configuration.
*
* @param array $configs An array of configuration values
* @param ContainerBuilder $container A ContainerBuilder instance
*
* @throws \InvalidArgumentException When provided tag is not defined in this extension
*/
public function load(array $configs, ContainerBuilder $container)
{
$loader = new YamlFileLoader($container, new FileLocator(phpbb_realpath($this->config_path)));
$loader->load('services.yml');
$loader->load($container->getParameter('core.environment') . '/container/environment.yml');
$config = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($config, $configs);
if ($config['require_dev_dependencies'])
{
if (!class_exists('Goutte\Client', true))
{
trigger_error(
'Composer development dependencies have not been set up for the ' . $container->getParameter('core.environment') . ' environment yet, run ' .
"'php ../composer.phar install --dev' from the phpBB directory to do so.",
E_USER_ERROR
);
}
}
}
/**
* Returns the recommended alias to use in XML.
*
* This alias is also the mandatory prefix to use when using YAML.
*
* @return string The alias
*/
* {@inheritdoc}
*/
public function getConfiguration(array $config, ContainerBuilder $container)
{
$r = new \ReflectionClass('\phpbb\di\extension\container_configuration');
$container->addResource(new FileResource($r->getFileName()));
return new container_configuration();
}
/**
* Returns the recommended alias to use in XML.
*
* This alias is also the mandatory prefix to use when using YAML.
*
* @return string The alias
*/
public function getAlias()
{
return 'core';

View File

@ -1,67 +0,0 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\di\extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
/**
* Container ext extension
*/
class ext extends Extension
{
protected $paths = array();
public function __construct($enabled_extensions)
{
foreach ($enabled_extensions as $ext => $path)
{
$this->paths[] = $path;
}
}
/**
* Loads a specific configuration.
*
* @param array $config An array of configuration values
* @param ContainerBuilder $container A ContainerBuilder instance
*
* @throws \InvalidArgumentException When provided tag is not defined in this extension
*/
public function load(array $config, ContainerBuilder $container)
{
foreach ($this->paths as $path)
{
if (file_exists($path . '/config/services.yml'))
{
$loader = new YamlFileLoader($container, new FileLocator(phpbb_realpath($path . '/config')));
$loader->load('services.yml');
}
}
}
/**
* Returns the recommended alias to use in XML.
*
* This alias is also the mandatory prefix to use when using YAML.
*
* @return string The alias
*/
public function getAlias()
{
return 'ext';
}
}

View File

@ -0,0 +1,137 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\extension\di;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
/**
* Container core extension
*/
class extension_base extends Extension
{
/**
* Name of the extension (vendor/name)
*
* @var string
*/
protected $extension_name;
/**
* Path to the extension.
*
* @var string
*/
protected $ext_path;
/**
* Constructor
*
* @param string $extension_name Name of the extension (vendor/name)
* @param string $ext_path Path to the extension
*/
public function __construct($extension_name, $ext_path)
{
$this->extension_name = $extension_name;
$this->ext_path = $ext_path;
}
/**
* Loads a specific configuration.
*
* @param array $configs An array of configuration values
* @param ContainerBuilder $container A ContainerBuilder instance
*
* @throws \InvalidArgumentException When provided tag is not defined in this extension
*/
public function load(array $configs, ContainerBuilder $container)
{
$this->load_services($container);
}
/**
* Loads the services.yml file.
*
* @param ContainerBuilder $container A ContainerBuilder instance
*/
protected function load_services(ContainerBuilder $container)
{
$services_directory = false;
$services_file = false;
if (file_exists($this->ext_path . 'config/' . $container->getParameter('core.environment') . '/container/environment.yml'))
{
$services_directory = $this->ext_path . 'config/' . $container->getParameter('core.environment') . '/container/';
$services_file = 'environment.yml';
}
else if (!is_dir($this->ext_path . 'config/' . $container->getParameter('core.environment')))
{
if (file_exists($this->ext_path . 'config/default/container/environment.yml'))
{
$services_directory = $this->ext_path . 'config/default/container/';
$services_file = 'environment.yml';
}
else if (!is_dir($this->ext_path . 'config/default') && file_exists($this->ext_path . '/config/services.yml'))
{
$services_directory = $this->ext_path . 'config';
$services_file = 'services.yml';
}
}
if ($services_directory && $services_file)
{
$loader = new YamlFileLoader($container, new FileLocator(phpbb_realpath($services_directory)));
$loader->load($services_file);
}
}
/**
* {@inheritdoc}
*/
public function getConfiguration(array $config, ContainerBuilder $container)
{
$reflected = new \ReflectionClass($this);
$namespace = $reflected->getNamespaceName();
$class = $namespace . '\\di\configuration';
if (class_exists($class))
{
$r = new \ReflectionClass($class);
$container->addResource(new FileResource($r->getFileName()));
if (!method_exists($class, '__construct'))
{
$configuration = new $class();
return $configuration;
}
}
}
/**
* Returns the recommended alias to use in XML.
*
* This alias is also the mandatory prefix to use when using YAML.
*
* @return string The alias
*/
public function getAlias()
{
return str_replace('/', '_', $this->extension_name);
}
}

View File

@ -25,95 +25,121 @@ use Symfony\Component\Config\FileLocator;
use phpbb\extension\manager;
/**
* Integration of all pieces of the routing system for easier use.
*/
* Integration of all pieces of the routing system for easier use.
*/
class router implements RouterInterface
{
/**
* @var manager Extension manager
*/
* Extension manager
*
* @var manager
*/
protected $extension_manager;
/**
* @var string phpBB root path
*/
* phpBB root path
*
* @var string
*/
protected $phpbb_root_path;
/**
* @var string PHP file extensions
*/
* PHP file extensions
*
* @var string
*/
protected $php_ext;
/**
* @var array YAML file(s) containing route information
*/
* Name of the current environment
*
* @var string
*/
protected $environment;
/**
* YAML file(s) containing route information
*
* @var array
*/
protected $routing_files;
/**
* @var \Symfony\Component\Routing\Matcher\UrlMatcherInterface|null
*/
* @var \Symfony\Component\Routing\Matcher\UrlMatcherInterface|null
*/
protected $matcher;
/**
* @var \Symfony\Component\Routing\Generator\UrlGeneratorInterface|null
*/
* @var \Symfony\Component\Routing\Generator\UrlGeneratorInterface|null
*/
protected $generator;
/**
* @var RequestContext
*/
* @var RequestContext
*/
protected $context;
/**
* @var RouteCollection|null
*/
* @var RouteCollection|null
*/
protected $route_collection;
/**
* Construct method
*
* @param manager $extension_manager The extension manager
* @param string $phpbb_root_path phpBB root path
* @param string $php_ext PHP file extension
* @param array $routing_files Array of strings containing paths to YAML files holding route information
*/
public function __construct(manager $extension_manager, $phpbb_root_path, $php_ext, $routing_files = array())
* Construct method
*
* @param manager $extension_manager Extension manager
* @param string $phpbb_root_path phpBB root path
* @param string $php_ext PHP file extension
* @param string $environment Name of the current environment
* @param array $routing_files Array of strings containing paths to YAML files holding route information
*/
public function __construct(manager $extension_manager, $phpbb_root_path, $php_ext, $environment, $routing_files = array())
{
$this->extension_manager = $extension_manager;
$this->routing_files = $routing_files;
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext;
$this->environment = $environment;
$this->context = new RequestContext();
}
/**
* Find the list of routing files
*
* @param \phpbb\finder $finder
* @return router
*/
public function find_routing_files(\phpbb\finder $finder)
* Find the list of routing files
*
* @param array $paths Array of paths where to look for routing files.
* @return router
*/
public function find_routing_files(array $paths)
{
if ($this->routing_files === null || empty($this->routing_files))
$this->routing_files = array($this->phpbb_root_path . 'config/' . $this->environment . '/routing/environment.yml');
foreach ($paths as $path)
{
// We hardcode the path to the core config directory
// because the finder cannot find it
$this->routing_files = array_merge($this->routing_files, array('config/routing.yml'), array_keys($finder
->directory('/config')
->suffix('routing.yml')
->find()
));
if (file_exists($path . 'config/' . $this->environment . '/routing/environment.yml'))
{
$this->routing_files[] = $path . 'config/' . $this->environment . '/routing/environment.yml';
}
else if (!is_dir($path . 'config/' . $this->environment))
{
if (file_exists($path . 'config/default/routing/environment.yml'))
{
$this->routing_files[] = $path . 'config/default/routing/environment.yml';
}
else if (!is_dir($path . 'config/default/routing') && file_exists($path . 'config/routing.yml'))
{
$this->routing_files[] = $path . 'config/routing.yml';
}
}
}
return $this;
}
/**
* Find a list of controllers
*
* @param string $base_path Base path to prepend to file paths
* @return router
*/
* Find a list of controllers
*
* @param string $base_path Base path to prepend to file paths
* @return router
*/
public function find($base_path = '')
{
if ($this->route_collection === null || $this->route_collection->count() === 0)
@ -130,15 +156,15 @@ class router implements RouterInterface
}
/**
* Get the list of routes
*
* @return RouteCollection Get the route collection
*/
* Get the list of routes
*
* @return RouteCollection Get the route collection
*/
public function get_routes()
{
if ($this->route_collection == null || empty($this->routing_files))
{
$this->find_routing_files($this->extension_manager->get_finder())
$this->find_routing_files($this->extension_manager->all_enabled())
->find($this->phpbb_root_path);
}
@ -146,16 +172,16 @@ class router implements RouterInterface
}
/**
* {@inheritdoc}
*/
* {@inheritdoc}
*/
public function getRouteCollection()
{
return $this->get_routes();
}
/**
* {@inheritdoc}
*/
* {@inheritdoc}
*/
public function setContext(RequestContext $context)
{
$this->context = $context;
@ -171,34 +197,34 @@ class router implements RouterInterface
}
/**
* {@inheritdoc}
*/
* {@inheritdoc}
*/
public function getContext()
{
return $this->context;
}
/**
* {@inheritdoc}
*/
* {@inheritdoc}
*/
public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
{
return $this->get_generator()->generate($name, $parameters, $referenceType);
}
/**
* {@inheritdoc}
*/
* {@inheritdoc}
*/
public function match($pathinfo)
{
return $this->get_matcher()->match($pathinfo);
}
/**
* Gets the UrlMatcher instance associated with this Router.
*
* @return \Symfony\Component\Routing\Matcher\UrlMatcherInterface A UrlMatcherInterface instance
*/
* Gets the UrlMatcher instance associated with this Router.
*
* @return \Symfony\Component\Routing\Matcher\UrlMatcherInterface A UrlMatcherInterface instance
*/
public function get_matcher()
{
if ($this->matcher !== null)
@ -218,8 +244,8 @@ class router implements RouterInterface
return $this->matcher;
}
/**
* Creates a new dumped URL Matcher (dump it if necessary)
*/
* Creates a new dumped URL Matcher (dump it if necessary)
*/
protected function create_dumped_url_matcher()
{
if (!file_exists($this->phpbb_root_path . 'cache/url_matcher.' . $this->php_ext))
@ -240,18 +266,18 @@ class router implements RouterInterface
}
/**
* Creates a new URL Matcher
*/
* Creates a new URL Matcher
*/
protected function create_new_url_matcher()
{
$this->matcher = new UrlMatcher($this->get_routes(), $this->context);
}
/**
* Gets the UrlGenerator instance associated with this Router.
*
* @return \Symfony\Component\Routing\Generator\UrlGeneratorInterface A UrlGeneratorInterface instance
*/
* Gets the UrlGenerator instance associated with this Router.
*
* @return \Symfony\Component\Routing\Generator\UrlGeneratorInterface A UrlGeneratorInterface instance
*/
public function get_generator()
{
if ($this->generator !== null)
@ -272,8 +298,8 @@ class router implements RouterInterface
}
/**
* Creates a new dumped URL Generator (dump it if necessary)
*/
* Creates a new dumped URL Generator (dump it if necessary)
*/
protected function create_dumped_url_generator()
{
if (!file_exists($this->phpbb_root_path . 'cache/url_generator.' . $this->php_ext))
@ -294,8 +320,8 @@ class router implements RouterInterface
}
/**
* Creates a new URL Generator
*/
* Creates a new URL Generator
*/
protected function create_new_url_generator()
{
$this->generator = new UrlGenerator($this->get_routes(), $this->context);

View File

@ -1,26 +1,26 @@
<?php
namespace phpbb\avatar\driver;
class barfoo extends \phpbb\avatar\driver\driver
{
public function get_data($row)
{
return array();
}
public function prepare_form($request, $template, $user, $row, &$error)
{
return false;
}
public function process_form($request, $template, $user, $row, &$error)
{
return false;
}
public function get_template_name()
{
return 'barfoo.html';
}
}
<?php
namespace phpbb\avatar\driver;
class barfoo extends \phpbb\avatar\driver\driver
{
public function get_data($row)
{
return array();
}
public function prepare_form($request, $template, $user, $row, &$error)
{
return false;
}
public function process_form($request, $template, $user, $row, &$error)
{
return false;
}
public function get_template_name()
{
return 'barfoo.html';
}
}

View File

@ -1,26 +1,26 @@
<?php
namespace phpbb\avatar\driver;
class foobar extends \phpbb\avatar\driver\driver
{
public function get_data($row)
{
return array();
}
public function prepare_form($request, $template, $user, $row, &$error)
{
return false;
}
public function process_form($request, $template, $user, $row, &$error)
{
return false;
}
public function get_template_name()
{
return 'foobar.html';
}
}
<?php
namespace phpbb\avatar\driver;
class foobar extends \phpbb\avatar\driver\driver
{
public function get_data($row)
{
return array();
}
public function prepare_form($request, $template, $user, $row, &$error)
{
return false;
}
public function process_form($request, $template, $user, $row, &$error)
{
return false;
}
public function get_template_name()
{
return 'foobar.html';
}
}

View File

@ -12,6 +12,8 @@
*/
define('IN_PHPBB', true);
define('PHPBB_ENVIRONMENT', 'test');
$phpbb_root_path = 'phpBB/';
$phpEx = 'php';
require_once $phpbb_root_path . 'includes/startup.php';

View File

@ -113,14 +113,8 @@ abstract class phpbb_controller_common_helper_route extends phpbb_test_case
)
);
$finder = new \phpbb\finder(
new \phpbb\filesystem(),
dirname(__FILE__) . '/',
new phpbb_mock_cache()
);
$finder->set_extensions(array_keys($this->extension_manager->all_enabled()));
$this->router = new phpbb_mock_router($this->extension_manager, dirname(__FILE__) . '/', 'php');
$this->router->find_routing_files($finder);
$this->router = new phpbb_mock_router($this->extension_manager, dirname(__FILE__) . '/', 'php', PHPBB_ENVIRONMENT);
$this->router->find_routing_files($this->extension_manager->all_enabled());
$this->router->find(dirname(__FILE__) . '/');
// Set correct current phpBB root path
$this->root_path = $this->get_phpbb_root_path();
@ -325,7 +319,7 @@ abstract class phpbb_controller_common_helper_route extends phpbb_test_case
$this->helper = new phpbb_mock_controller_helper($this->template, $this->user, $this->config, $this->router, $this->symfony_request, $this->request, $this->filesystem, $this->root_path, 'php', dirname(__FILE__) . '/');
$this->assertEquals($expected, $this->helper->route($route, $params, $is_amp, $session_id, UrlGeneratorInterface::NETWORK_PATH), $description);
}
//TODO
public function helper_url_data_absolute_with_rewrite()
{
return array(

View File

@ -30,13 +30,18 @@ class phpbb_controller_controller_test extends phpbb_test_case
'ext_active' => '1',
'ext_path' => 'ext/vendor2/foo/',
),
'vendor2/bar' => array(
'ext_name' => 'vendor2/bar',
'ext_active' => '1',
'ext_path' => 'ext/vendor2/bar/',
),
));
}
public function test_router_find_files()
{
$router = new \phpbb\routing\router($this->extension_manager, dirname(__FILE__) . '/', 'php');
$router->find_routing_files($this->extension_manager->get_finder());
$router = new \phpbb\routing\router($this->extension_manager, dirname(__FILE__) . '/', 'php', PHPBB_ENVIRONMENT);
$router->find_routing_files($this->extension_manager->all_enabled());
$routes = $router->find(__DIR__)->get_routes();
// This will need to be updated if any new routes are defined
@ -49,6 +54,9 @@ class phpbb_controller_controller_test extends phpbb_test_case
$this->assertInstanceOf('Symfony\Component\Routing\Route', $routes->get('controller2'));
$this->assertEquals('/foo/bar', $routes->get('controller2')->getPath());
$this->assertInstanceOf('Symfony\Component\Routing\Route', $routes->get('controller3'));
$this->assertEquals('/bar', $routes->get('controller3')->getPath());
$this->assertNull($routes->get('controller_noroute'));
}

View File

@ -0,0 +1,3 @@
services:
bar.controller:
class: bar\controller

View File

@ -0,0 +1,3 @@
controller3:
pattern: /bar
defaults: { _controller: bar.controller:handle }

View File

@ -0,0 +1,18 @@
<?php
namespace bar;
use Symfony\Component\HttpFoundation\Response;
class controller
{
/**
* Handle method
*
* @return null
*/
public function handle()
{
return new Response('Test', 200);
}
}

View File

@ -14,6 +14,7 @@
namespace
{
require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
require_once dirname(__FILE__) . '/fixtures/ext/vendor/enabled_4/di/extension.php';
class phpbb_di_container_test extends \phpbb_test_case
{
@ -57,6 +58,9 @@ namespace
// Checks use_extensions
$this->assertTrue($container->hasParameter('enabled'));
$this->assertTrue($container->hasParameter('enabled_2'));
$this->assertTrue($container->hasParameter('enabled_3'));
$this->assertTrue($container->hasParameter('enabled_4'));
$this->assertFalse($container->hasParameter('disabled'));
$this->assertFalse($container->hasParameter('available'));
@ -142,7 +146,6 @@ namespace
$this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerBuilder', $container);
$this->assertTrue($container->hasParameter('my_parameter'));
$this->assertFalse($container->hasParameter('core.root_path'));
}
}
}
@ -199,6 +202,9 @@ namespace phpbb\db\driver
{
return array(
array('ext_name' => 'vendor/enabled'),
array('ext_name' => 'vendor/enabled-2'),
array('ext_name' => 'vendor/enabled-3'),
array('ext_name' => 'vendor/enabled_4'),
);
}
}

View File

@ -0,0 +1,2 @@
core:
require_dev_dependencies: true

View File

@ -0,0 +1,2 @@
parameters:
enabled_2: true

View File

@ -0,0 +1,2 @@
parameters:
enabled_3: true

View File

@ -0,0 +1,31 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace vendor\enabled_4\di;
use phpbb\extension\di\extension_base;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
/**
* Container core extension
*/
class extension extends extension_base
{
protected function load_services(ContainerBuilder $container)
{
$loader = new YamlFileLoader($container, new FileLocator(phpbb_realpath($this->ext_path)));
$loader->load('environment.yml');
}
}

View File

@ -0,0 +1,2 @@
parameters:
enabled_4: true

View File

@ -0,0 +1,2 @@
core:
require_dev_dependencies: true

View File

@ -36,16 +36,10 @@ class phpbb_pagination_pagination_test extends phpbb_template_template_test_case
$filesystem = new \phpbb\filesystem();
$manager = new phpbb_mock_extension_manager(dirname(__FILE__) . '/', array());
$finder = new \phpbb\finder(
$filesystem,
dirname(__FILE__) . '/',
new phpbb_mock_cache()
);
$finder->set_extensions(array_keys($manager->all_enabled()));
$this->config = new \phpbb\config\config(array('enable_mod_rewrite' => '1'));
$router = new phpbb_mock_router($manager, dirname(__FILE__) . '/', 'php');
$router->find_routing_files($finder);
$router = new phpbb_mock_router($manager, dirname(__FILE__) . '/', 'php', PHPBB_ENVIRONMENT);
$router->find_routing_files($manager->all_enabled());
$router->find(dirname(__FILE__) . '/');
$request = new phpbb_mock_request();

View File

@ -96,6 +96,7 @@ class phpbb_session_testable_factory
'auth.provider.db',
new phpbb_mock_auth_provider()
);
$phpbb_container->setParameter('core.environment', PHPBB_ENVIRONMENT);
$provider_collection = new \phpbb\auth\provider_collection($phpbb_container, $config);
$provider_collection->add('auth.provider.db');
$phpbb_container->set(

View File

@ -204,6 +204,11 @@ class phpbb_functional_test_case extends phpbb_test_case
{
if (!$this->cache)
{
global $phpbb_container;
$phpbb_container = new phpbb_mock_container_builder();
$phpbb_container->setParameter('core.environment', PHPBB_ENVIRONMENT);
$this->cache = new \phpbb\cache\driver\file;
}