1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-01-17 22:28:46 +01:00

Merge pull request #2671 from Nicofuma/ticket/12775

[ticket/12775] Replace functions_container with a container_builder class.

* Nicofuma/ticket/12775: (34 commits)
  [ticket/12775] Set dbal.conn.driver as synthetic during installation
  [ticket/12775] Add the definition of dbal.conn in fixtures/config/services.yml
  [ticket/12775] Inject the connection when created in the container
  [ticket/12775] Extract the vars later in install/install_update.php
  [ticket/12775] Rename config_php_handler to config_php_file container_builder
  [ticket/12775] Set defined_vars as a property of config_php_file
  [ticket/12775] Fix doc blocks in the container builder
  [ticket/12775] Remove useless includes of config.php
  [ticket/12775] Move phpbb_convert_30_dbms_to_31 into the config file class
  [ticket/12775] Fix comments
  [ticket/12775] Update doc blocks
  [ticket/12775] Fix container_builder
  [ticket/12775] Rename config_php to config_php_file
  [ticket/12775] Renamed to \phpbb\di\container_builder
  [ticket/12775] Remove the last include of functions_container
  [ticket/12775] Fix unit tests
  [ticket/12775] Add tests for \phpbb\config_php
  [ticket/12775] Add tests for the container factory
  [ticket/12775] Use a field instead of a local var in load_config_var()
  [ticket/12775] Update container and config in install/
  ...
This commit is contained in:
Andreas Fischer 2014-07-11 11:49:51 +02:00
commit b2a883d504
28 changed files with 919 additions and 482 deletions

View File

@ -22,20 +22,27 @@ define('IN_PHPBB', true);
$phpbb_root_path = __DIR__ . '/../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
require($phpbb_root_path . 'includes/startup.' . $phpEx);
require($phpbb_root_path . 'config.' . $phpEx);
require($phpbb_root_path . 'includes/constants.' . $phpEx);
require($phpbb_root_path . 'includes/functions.' . $phpEx);
require($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
require($phpbb_root_path . 'includes/functions_container.' . $phpEx);
require($phpbb_root_path . 'includes/utf/utf_tools.' . $phpEx);
require($phpbb_root_path . 'phpbb/class_loader.' . $phpEx);
$phpbb_class_loader = new \phpbb\class_loader('phpbb\\', "{$phpbb_root_path}phpbb/", $phpEx);
$phpbb_class_loader->register();
$phpbb_config_php_file = new \phpbb\config_php_file($phpbb_root_path, $phpEx);
extract($phpbb_config_php_file->get_all());
require($phpbb_root_path . 'includes/constants.' . $phpEx);
require($phpbb_root_path . 'includes/functions.' . $phpEx);
require($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
require($phpbb_root_path . 'includes/utf/utf_tools.' . $phpEx);
$phpbb_class_loader_ext = new \phpbb\class_loader('\\', "{$phpbb_root_path}ext/", $phpEx);
$phpbb_class_loader_ext->register();
$phpbb_container = phpbb_create_update_container($phpbb_root_path, $phpEx, "$phpbb_root_path/config");
$phpbb_container_builder = new \phpbb\di\container_builder($phpbb_config_php_file, $phpbb_root_path, $phpEx);
$phpbb_container_builder->set_use_extensions(false);
$phpbb_container_builder->set_dump_container(false);
$phpbb_container = $phpbb_container_builder->get_container();
$phpbb_container->get('request')->enable_super_globals();
require($phpbb_root_path . 'includes/compatibility_globals.' . $phpEx);

View File

@ -21,11 +21,13 @@ if (!defined('IN_PHPBB'))
}
require($phpbb_root_path . 'includes/startup.' . $phpEx);
require($phpbb_root_path . 'phpbb/class_loader.' . $phpEx);
if (file_exists($phpbb_root_path . 'config.' . $phpEx))
{
require($phpbb_root_path . 'config.' . $phpEx);
}
$phpbb_class_loader = new \phpbb\class_loader('phpbb\\', "{$phpbb_root_path}phpbb/", $phpEx);
$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_INSTALLED'))
{
@ -76,11 +78,8 @@ $phpbb_adm_relative_path = (isset($phpbb_adm_relative_path)) ? $phpbb_adm_relati
$phpbb_admin_path = (defined('PHPBB_ADMIN_PATH')) ? PHPBB_ADMIN_PATH : $phpbb_root_path . $phpbb_adm_relative_path;
// Include files
require($phpbb_root_path . 'phpbb/class_loader.' . $phpEx);
require($phpbb_root_path . 'includes/functions.' . $phpEx);
require($phpbb_root_path . 'includes/functions_content.' . $phpEx);
require($phpbb_root_path . 'includes/functions_container.' . $phpEx);
include($phpbb_root_path . 'includes/functions_compatibility.' . $phpEx);
require($phpbb_root_path . 'includes/constants.' . $phpEx);
@ -89,16 +88,14 @@ require($phpbb_root_path . 'includes/utf/utf_tools.' . $phpEx);
// Set PHP error handler to ours
set_error_handler(defined('PHPBB_MSG_HANDLER') ? PHPBB_MSG_HANDLER : 'msg_handler');
// Setup class loader first
$phpbb_class_loader = new \phpbb\class_loader('phpbb\\', "{$phpbb_root_path}phpbb/", $phpEx);
$phpbb_class_loader->register();
$phpbb_class_loader_ext = new \phpbb\class_loader('\\', "{$phpbb_root_path}ext/", $phpEx);
$phpbb_class_loader_ext->register();
phpbb_load_extensions_autoloaders($phpbb_root_path);
// Set up container
$phpbb_container = phpbb_create_default_container($phpbb_root_path, $phpEx);
$phpbb_container_builder = new \phpbb\di\container_builder($phpbb_config_php_file, $phpbb_root_path, $phpEx);
$phpbb_container = $phpbb_container_builder->get_container();
$phpbb_class_loader->set_cache($phpbb_container->get('cache.driver'));
$phpbb_class_loader_ext->set_cache($phpbb_container->get('cache.driver'));

View File

@ -39,9 +39,6 @@ services:
cache.driver:
class: %cache.driver.class%
cache.driver.install:
class: phpbb\cache\driver\file
class_loader:
class: phpbb\class_loader
arguments:
@ -69,6 +66,9 @@ services:
- @cache.driver
- %tables.config%
config.php:
synthetic: true
config_text:
class: phpbb\config\db_text
arguments:

View File

@ -38,31 +38,31 @@ else if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'
if (isset($_GET['avatar']))
{
require($phpbb_root_path . 'includes/startup.' . $phpEx);
require($phpbb_root_path . 'config.' . $phpEx);
require($phpbb_root_path . 'phpbb/class_loader.' . $phpEx);
$phpbb_class_loader = new \phpbb\class_loader('phpbb\\', "{$phpbb_root_path}phpbb/", $phpEx);
$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_INSTALLED') || empty($dbms) || empty($acm_type))
{
exit;
}
require($phpbb_root_path . 'phpbb/class_loader.' . $phpEx);
require($phpbb_root_path . 'includes/constants.' . $phpEx);
require($phpbb_root_path . 'includes/functions.' . $phpEx);
require($phpbb_root_path . 'includes/functions_container.' . $phpEx);
require($phpbb_root_path . 'includes/functions_download' . '.' . $phpEx);
require($phpbb_root_path . 'includes/utf/utf_tools.' . $phpEx);
// Setup class loader first
$phpbb_class_loader = new \phpbb\class_loader('phpbb\\', "{$phpbb_root_path}phpbb/", $phpEx);
$phpbb_class_loader->register();
$phpbb_class_loader_ext = new \phpbb\class_loader('\\', "{$phpbb_root_path}ext/", $phpEx);
$phpbb_class_loader_ext->register();
phpbb_load_extensions_autoloaders($phpbb_root_path);
// Set up container
$phpbb_container = phpbb_create_default_container($phpbb_root_path, $phpEx);
$phpbb_container_builder = new \phpbb\di\container_builder($phpbb_config_php_file, $phpbb_root_path, $phpEx);
$phpbb_container = $phpbb_container_builder->get_container();
$phpbb_class_loader->set_cache($phpbb_container->get('cache.driver'));
$phpbb_class_loader_ext->set_cache($phpbb_container->get('cache.driver'));

View File

@ -5326,52 +5326,6 @@ function phpbb_to_numeric($input)
return ($input > PHP_INT_MAX) ? (float) $input : (int) $input;
}
/**
* Convert either 3.0 dbms or 3.1 db driver class name to 3.1 db driver class name.
*
* If $dbms is a valid 3.1 db driver class name, returns it unchanged.
* Otherwise prepends phpbb\db\driver\ to the dbms to convert a 3.0 dbms
* to 3.1 db driver class name.
*
* @param string $dbms dbms parameter
* @return db driver class
*/
function phpbb_convert_30_dbms_to_31($dbms)
{
// Note: this check is done first because mysqli extension
// supplies a mysqli class, and class_exists($dbms) would return
// true for mysqli class.
// However, per the docblock any valid 3.1 driver name should be
// recognized by this function, and have priority over 3.0 dbms.
if (strpos($dbms, 'phpbb\db\driver') === false && class_exists('phpbb\db\driver\\' . $dbms))
{
return 'phpbb\db\driver\\' . $dbms;
}
if (class_exists($dbms))
{
// Additionally we could check that $dbms extends phpbb\db\driver\driver.
// http://php.net/manual/en/class.reflectionclass.php
// Beware of possible performance issues:
// http://stackoverflow.com/questions/294582/php-5-reflection-api-performance
// We could check for interface implementation in all paths or
// only when we do not prepend phpbb\db\driver\.
/*
$reflection = new \ReflectionClass($dbms);
if ($reflection->isSubclassOf('phpbb\db\driver\driver'))
{
return $dbms;
}
*/
return $dbms;
}
throw new \RuntimeException("You have specified an invalid dbms driver: $dbms");
}
/**
* Get the board contact details (e.g. for emails)
*

View File

@ -1,297 +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\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
/**
* Get DB connection from config.php.
*
* Used to bootstrap the container.
*
* @param string $config_file
* @return \phpbb\db\driver\driver_interface
*/
function phpbb_bootstrap_db_connection($config_file)
{
require($config_file);
$dbal_driver_class = phpbb_convert_30_dbms_to_31($dbms);
$db = new $dbal_driver_class();
$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, defined('PHPBB_DB_NEW_LINK'));
return $db;
}
/**
* Get table prefix from config.php.
*
* Used to bootstrap the container.
*
* @param string $config_file
* @return string table prefix
*/
function phpbb_bootstrap_table_prefix($config_file)
{
require($config_file);
return $table_prefix;
}
/**
* Get enabled extensions.
*
* Used to bootstrap the container.
*
* @param string $config_file
* @param string $phpbb_root_path
* @param \phpbb\db\driver\driver_interface $db The generated connection
* @return array enabled extensions
*/
function phpbb_bootstrap_enabled_exts($config_file, $phpbb_root_path, &$db)
{
$db = phpbb_bootstrap_db_connection($config_file);
$table_prefix = phpbb_bootstrap_table_prefix($config_file);
$extension_table = $table_prefix.'ext';
$sql = 'SELECT *
FROM ' . $extension_table . '
WHERE ext_active = 1';
$result = $db->sql_query($sql);
$rows = $db->sql_fetchrowset($result);
$db->sql_freeresult($result);
$exts = array();
foreach ($rows as $row)
{
$exts[$row['ext_name']] = $phpbb_root_path . 'ext/' . $row['ext_name'] . '/';
}
return $exts;
}
/**
* Create the ContainerBuilder object
*
* @param array $extensions Array of Container extension objects
* @param string $phpbb_root_path Root path
* @param string $php_ext PHP Extension
* @return ContainerBuilder object
*/
function phpbb_create_container(array $extensions, $phpbb_root_path, $php_ext)
{
$container = new ContainerBuilder();
foreach ($extensions as $extension)
{
$container->registerExtension($extension);
$container->loadFromExtension($extension->getAlias());
}
$container->setParameter('core.root_path', $phpbb_root_path);
$container->setParameter('core.php_ext', $php_ext);
return $container;
}
/**
* Create installer container
*
* @param string $phpbb_root_path Root path
* @param string $php_ext PHP Extension
* @return ContainerBuilder object
*/
function phpbb_create_install_container($phpbb_root_path, $php_ext)
{
$other_config_path = $phpbb_root_path . 'install/update/new/config/';
$config_path = file_exists($other_config_path . 'services.yml') ? $other_config_path : $phpbb_root_path . 'config/';
$core = new \phpbb\di\extension\core($config_path);
$container = phpbb_create_container(array($core), $phpbb_root_path, $php_ext);
$container->setParameter('core.root_path', $phpbb_root_path);
$container->setParameter('core.adm_relative_path', $phpbb_adm_relative_path);
$container->setParameter('core.php_ext', $php_ext);
$container->setParameter('core.table_prefix', '');
$container->register('dbal.conn.driver')->setSynthetic(true);
$container->setAlias('cache.driver', 'cache.driver.install');
$container->compile();
return $container;
}
/**
* Create updater container
*
* @param string $phpbb_root_path Root path
* @param string $php_ext PHP Extension
* @param array $config_path Path to config directory
* @return ContainerBuilder object (compiled)
*/
function phpbb_create_update_container($phpbb_root_path, $php_ext, $config_path)
{
$config_file = $phpbb_root_path . 'config.' . $php_ext;
return phpbb_create_compiled_container(
$config_file,
array(
new phpbb\di\extension\config($config_file),
new phpbb\di\extension\core($config_path),
),
array(
new phpbb\di\pass\collection_pass(),
new phpbb\di\pass\kernel_pass(),
),
$phpbb_root_path,
$php_ext
);
}
/**
* Create a compiled ContainerBuilder object
*
* @param array $extensions Array of Container extension objects
* @param array $passes Array of Compiler Pass objects
* @param string $phpbb_root_path Root path
* @param string $php_ext PHP Extension
* @return ContainerBuilder object (compiled)
*/
function phpbb_create_compiled_container($config_file, array $extensions, array $passes, $phpbb_root_path, $php_ext)
{
// Create the final container to be compiled and cached
$container = phpbb_create_container($extensions, $phpbb_root_path, $php_ext);
// Compile the container
foreach ($passes as $pass)
{
$container->addCompilerPass($pass);
}
$container->compile();
return $container;
}
/**
* Create a compiled and dumped ContainerBuilder object
*
* @param array $extensions Array of Container extension objects
* @param array $passes Array of Compiler Pass objects
* @param string $phpbb_root_path Root path
* @param string $php_ext PHP Extension
* @return ContainerBuilder object (compiled)
*/
function phpbb_create_dumped_container($config_file, array $extensions, array $passes, $phpbb_root_path, $php_ext)
{
// Check for our cached container; if it exists, use it
$container_filename = phpbb_container_filename($phpbb_root_path, $php_ext);
if (file_exists($container_filename))
{
require($container_filename);
return new phpbb_cache_container();
}
$container = phpbb_create_compiled_container($config_file, $extensions, $passes, $phpbb_root_path, $php_ext);
// Lastly, we create our cached container class
$dumper = new PhpDumper($container);
$cached_container_dump = $dumper->dump(array(
'class' => 'phpbb_cache_container',
'base_class' => 'Symfony\\Component\\DependencyInjection\\ContainerBuilder',
));
file_put_contents($container_filename, $cached_container_dump);
return $container;
}
/**
* Create an environment-specific ContainerBuilder object
*
* If debug is enabled, the container is re-compiled every time.
* This ensures that the latest changes will always be reflected
* during development.
*
* Otherwise it will get the existing dumped container and use
* that one instead.
*
* @param array $extensions Array of Container extension objects
* @param array $passes Array of Compiler Pass objects
* @param string $phpbb_root_path Root path
* @param string $php_ext PHP Extension
* @return ContainerBuilder object (compiled)
*/
function phpbb_create_dumped_container_unless_debug($config_file, array $extensions, array $passes, $phpbb_root_path, $php_ext)
{
$container_factory = defined('DEBUG_CONTAINER') ? 'phpbb_create_compiled_container' : 'phpbb_create_dumped_container';
return $container_factory($config_file, $extensions, $passes, $phpbb_root_path, $php_ext);
}
/**
* Create a default ContainerBuilder object
*
* Contains the default configuration of the phpBB container.
*
* @param array $extensions Array of Container extension objects
* @param array $passes Array of Compiler Pass objects
* @return ContainerBuilder object (compiled)
*/
function phpbb_create_default_container($phpbb_root_path, $php_ext)
{
$config_file = $phpbb_root_path . 'config.' . $php_ext;
$db = null;
$installed_exts = phpbb_bootstrap_enabled_exts($config_file, $phpbb_root_path, $db);
$container = phpbb_create_dumped_container_unless_debug(
$config_file,
array(
new \phpbb\di\extension\config($config_file),
new \phpbb\di\extension\core($phpbb_root_path . 'config'),
new \phpbb\di\extension\ext($installed_exts),
),
array(
new \phpbb\di\pass\collection_pass(),
new \phpbb\di\pass\kernel_pass(),
),
$phpbb_root_path,
$php_ext
);
$container->get('dbal.conn')->set_driver($db);
return $container;
}
/**
* Get the filename under which the dumped container will be stored.
*
* @param string $phpbb_root_path Root path
* @param string $php_ext PHP Extension
* @return Path for dumped container
*/
function phpbb_container_filename($phpbb_root_path, $php_ext)
{
$filename = str_replace(array('/', '.'), array('slash', 'dot'), $phpbb_root_path);
return $phpbb_root_path . 'cache/container_' . $filename . '.' . $php_ext;
}

View File

@ -257,11 +257,12 @@ class phpbb_questionnaire_phpbb_data_provider
*/
function get_data()
{
global $phpbb_root_path, $phpEx;
include("{$phpbb_root_path}config.$phpEx");
global $phpbb_root_path, $phpEx, $phpbb_config_php_file;
extract($phpbb_config_php_file->get_all());
unset($dbhost, $dbport, $dbname, $dbuser, $dbpasswd); // Just a precaution
$dbms = phpbb_convert_30_dbms_to_31($dbms);
$dbms = $phpbb_config_php_file->convert_30_dbms_to_31($dbms);
// Only send certain config vars
$config_vars = array(

View File

@ -25,10 +25,11 @@ if (!defined('IN_PHPBB'))
exit;
}
include($phpbb_root_path . 'config.' . $phpEx);
$phpbb_config_php_file = new \phpbb\config_php_file($phpbb_root_path, $phpEx);
extract($phpbb_config_php_file->get_all());
unset($dbpasswd);
$dbms = phpbb_convert_30_dbms_to_31($dbms);
$dbms = $phpbb_config_php_file->convert_30_dbms_to_31($dbms);
/**
* $convertor_data provides some basic information about this convertor which is

View File

@ -56,8 +56,14 @@ function phpbb_end_update($cache, $config)
}
require($phpbb_root_path . 'includes/startup.' . $phpEx);
require($phpbb_root_path . 'phpbb/class_loader.' . $phpEx);
$phpbb_class_loader = new \phpbb\class_loader('phpbb\\', "{$phpbb_root_path}phpbb/", $phpEx);
$phpbb_class_loader->register();
$phpbb_config_php_file = new \phpbb\config_php_file($phpbb_root_path, $phpEx);
extract($phpbb_config_php_file->get_all());
include($phpbb_root_path . 'config.' . $phpEx);
if (!defined('PHPBB_INSTALLED') || empty($dbms) || empty($acm_type))
{
die("Please read: <a href='../docs/INSTALL.html'>INSTALL.html</a> before attempting to update.");
@ -68,13 +74,9 @@ $phpbb_adm_relative_path = (isset($phpbb_adm_relative_path)) ? $phpbb_adm_relati
$phpbb_admin_path = (defined('PHPBB_ADMIN_PATH')) ? PHPBB_ADMIN_PATH : $phpbb_root_path . $phpbb_adm_relative_path;
// Include files
require($phpbb_root_path . 'phpbb/class_loader.' . $phpEx);
require($phpbb_root_path . 'includes/functions.' . $phpEx);
require($phpbb_root_path . 'includes/functions_content.' . $phpEx);
require($phpbb_root_path . 'includes/functions_container.' . $phpEx);
require($phpbb_root_path . 'config.' . $phpEx);
require($phpbb_root_path . 'includes/constants.' . $phpEx);
include($phpbb_root_path . 'includes/utf/utf_normalizer.' . $phpEx);
require($phpbb_root_path . 'includes/utf/utf_tools.' . $phpEx);
@ -82,26 +84,12 @@ require($phpbb_root_path . 'includes/utf/utf_tools.' . $phpEx);
// Set PHP error handler to ours
set_error_handler(defined('PHPBB_MSG_HANDLER') ? PHPBB_MSG_HANDLER : 'msg_handler');
// Setup class loader first
$phpbb_class_loader = new \phpbb\class_loader('phpbb\\', "{$phpbb_root_path}phpbb/", $phpEx);
$phpbb_class_loader->register();
// Set up container (must be done here because extensions table may not exist)
$container_extensions = array(
new \phpbb\di\extension\config($phpbb_root_path . 'config.' . $phpEx),
new \phpbb\di\extension\core($phpbb_root_path . 'config/'),
);
$container_passes = array(
new \phpbb\di\pass\collection_pass(),
);
$phpbb_container = phpbb_create_container($container_extensions, $phpbb_root_path, $phpEx);
// Compile the container
foreach ($container_passes as $pass)
{
$phpbb_container->addCompilerPass($pass);
}
$phpbb_container->compile();
$phpbb_container_builder = new \phpbb\di\container_builder($phpbb_config_php_file, $phpbb_root_path, $phpEx);
$phpbb_container_builder->set_use_extensions(false);
$phpbb_container_builder->set_use_kernel_pass(false);
$phpbb_container_builder->set_dump_container(false);
$phpbb_container = $phpbb_container_builder->get_container();
// set up caching
$cache = $phpbb_container->get('cache');

View File

@ -102,7 +102,6 @@ $phpbb_admin_path = (defined('PHPBB_ADMIN_PATH')) ? PHPBB_ADMIN_PATH : $phpbb_ro
phpbb_require_updated('phpbb/class_loader.' . $phpEx);
phpbb_require_updated('includes/functions.' . $phpEx);
phpbb_require_updated('includes/functions_container.' . $phpEx);
phpbb_require_updated('includes/functions_content.' . $phpEx, true);
@ -120,7 +119,29 @@ $phpbb_class_loader_ext = new \phpbb\class_loader('\\', "{$phpbb_root_path}ext/"
$phpbb_class_loader_ext->register();
// Set up container
$phpbb_container = phpbb_create_install_container($phpbb_root_path, $phpEx);
$phpbb_config_php_file = new \phpbb\config_php_file($phpbb_root_path, $phpEx);
$phpbb_container_builder = new \phpbb\di\container_builder($phpbb_config_php_file, $phpbb_root_path, $phpEx);
$phpbb_container_builder->set_use_extensions(false);
$phpbb_container_builder->set_dump_container(false);
$phpbb_container_builder->set_use_custom_pass(false);
$phpbb_container_builder->set_inject_config(false);
$phpbb_container_builder->set_compile_container(false);
$other_config_path = $phpbb_root_path . 'install/update/new/config/';
$config_path = file_exists($other_config_path . 'services.yml') ? $other_config_path : $phpbb_root_path . 'config/';
$phpbb_container_builder->set_config_path($config_path);
$phpbb_container_builder->set_custom_parameters(array(
'core.root_path' => $phpbb_root_path,
'core.adm_relative_path' => $phpbb_adm_relative_path,
'core.php_ext' => $phpEx,
'core.table_prefix' => '',
'cache.driver.class' => 'phpbb\cache\driver\file',
));
$phpbb_container = $phpbb_container_builder->get_container();
$phpbb_container->register('dbal.conn.driver')->setSynthetic(true);
$phpbb_container->compile();
$phpbb_class_loader->set_cache($phpbb_container->get('cache.driver'));
$phpbb_class_loader_ext->set_cache($phpbb_container->get('cache.driver'));

View File

@ -107,7 +107,7 @@ class install_convert extends module
function main($mode, $sub)
{
global $lang, $template, $phpbb_root_path, $phpEx, $cache, $config, $language, $table_prefix;
global $convert, $request, $phpbb_container;
global $convert, $request, $phpbb_container, $phpbb_config_php_file;
$this->tpl_name = 'install_convert';
$this->mode = $mode;
@ -127,7 +127,8 @@ class install_convert extends module
// Enable super globals to prevent issues with the new \phpbb\request\request object
$request->enable_super_globals();
// Create a normal container now
$phpbb_container = phpbb_create_default_container($phpbb_root_path, $phpEx);
$phpbb_container_builder = new \phpbb\di\container_builder($phpbb_config_php_file, $phpbb_root_path, $phpEx);
$phpbb_container = $phpbb_container_builder->get_container();
// Create cache
$cache = $phpbb_container->get('cache');
@ -135,11 +136,12 @@ class install_convert extends module
switch ($sub)
{
case 'intro':
require($phpbb_root_path . 'config.' . $phpEx);
extract($phpbb_config_php_file->get_all());
require($phpbb_root_path . 'includes/constants.' . $phpEx);
require($phpbb_root_path . 'includes/functions_convert.' . $phpEx);
$dbms = phpbb_convert_30_dbms_to_31($dbms);
$dbms = $phpbb_config_php_file->convert_30_dbms_to_31($dbms);
$db = new $dbms();
$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true);
@ -224,11 +226,12 @@ class install_convert extends module
// This is for making sure the session get not screwed due to the 3.0.x users table being completely new.
$cache->purge();
require($phpbb_root_path . 'config.' . $phpEx);
extract($phpbb_config_php_file->get_all());
require($phpbb_root_path . 'includes/constants.' . $phpEx);
require($phpbb_root_path . 'includes/functions_convert.' . $phpEx);
$dbms = phpbb_convert_30_dbms_to_31($dbms);
$dbms = $phpbb_config_php_file->convert_30_dbms_to_31($dbms);
$db = new $dbms();
$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true);
@ -370,13 +373,14 @@ class install_convert extends module
*/
function get_convert_settings($sub)
{
global $lang, $language, $template, $db, $phpbb_root_path, $phpEx, $config, $cache;
global $lang, $language, $template, $db, $phpbb_root_path, $phpEx, $config, $cache, $phpbb_config_php_file;
extract($phpbb_config_php_file->get_all());
require($phpbb_root_path . 'config.' . $phpEx);
require($phpbb_root_path . 'includes/constants.' . $phpEx);
require($phpbb_root_path . 'includes/functions_convert.' . $phpEx);
$dbms = phpbb_convert_30_dbms_to_31($dbms);
$dbms = $phpbb_config_php_file->convert_30_dbms_to_31($dbms);
$db = new $dbms();
$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true);
@ -458,7 +462,8 @@ class install_convert extends module
{
$error[] = sprintf($lang['TABLE_PREFIX_SAME'], $src_table_prefix);
}
$src_dbms = phpbb_convert_30_dbms_to_31($src_dbms);
$src_dbms = $phpbb_config_php_file->convert_30_dbms_to_31($src_dbms);
// Check table prefix
if (!sizeof($error))
@ -614,13 +619,14 @@ class install_convert extends module
{
global $template, $user, $phpbb_root_path, $phpEx, $db, $lang, $config, $cache, $auth;
global $convert, $convert_row, $message_parser, $skip_rows, $language;
global $request;
global $request, $phpbb_config_php_file;
extract($phpbb_config_php_file->get_all());
require($phpbb_root_path . 'config.' . $phpEx);
require($phpbb_root_path . 'includes/constants.' . $phpEx);
require($phpbb_root_path . 'includes/functions_convert.' . $phpEx);
$dbms = phpbb_convert_30_dbms_to_31($dbms);
$dbms = $phpbb_config_php_file->convert_30_dbms_to_31($dbms);
$db = new $dbms();
$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true);

View File

@ -51,7 +51,7 @@ class install_install extends module
function main($mode, $sub)
{
global $lang, $template, $language, $phpbb_root_path, $phpEx;
global $phpbb_container, $cache, $phpbb_log, $request;
global $phpbb_container, $cache, $phpbb_log, $request, $phpbb_config_php_file;
switch ($sub)
{
@ -104,7 +104,8 @@ class install_install extends module
$request->enable_super_globals();
// Create a normal container now
$phpbb_container = phpbb_create_default_container($phpbb_root_path, $phpEx);
$phpbb_container_builder = new \phpbb\di\container_builder($phpbb_config_php_file, $phpbb_root_path, $phpEx);
$phpbb_container = $phpbb_container_builder->get_container();
// Sets the global variables
$cache = $phpbb_container->get('cache');

View File

@ -66,7 +66,7 @@ class install_update extends module
function main($mode, $sub)
{
global $template, $phpEx, $phpbb_root_path, $user, $db, $config, $cache, $auth, $language;
global $request, $phpbb_admin_path, $phpbb_adm_relative_path, $phpbb_container;
global $request, $phpbb_admin_path, $phpbb_adm_relative_path, $phpbb_container, $phpbb_config_php_file;
// We must enable super globals, otherwise creating a new instance of the request class,
// using the new container with a dbal connection will fail with the following PHP Notice:
@ -74,14 +74,14 @@ class install_update extends module
$request->enable_super_globals();
// Create a normal container now
$phpbb_container_builder = new \phpbb\di\container_builder($phpbb_config_php_file, $phpbb_root_path, $phpEx);
$phpbb_container_builder->set_dump_container(false);
$phpbb_container_builder->set_use_extensions(false);
if (file_exists($phpbb_root_path . 'install/update/new/config'))
{
$phpbb_container = phpbb_create_update_container($phpbb_root_path, $phpEx, $phpbb_root_path . 'install/update/new/config');
}
else
{
$phpbb_container = phpbb_create_update_container($phpbb_root_path, $phpEx, $phpbb_root_path . 'config');
$phpbb_container_builder->set_config_path($phpbb_root_path . 'install/update/new/config');
}
$phpbb_container = $phpbb_container_builder->get_container();
// Writes into global $cache
$cache = $phpbb_container->get('cache');
@ -93,7 +93,7 @@ class install_update extends module
$this->new_location = $phpbb_root_path . 'install/update/new/';
// Init DB
require($phpbb_root_path . 'config.' . $phpEx);
extract($phpbb_config_php_file->get_all());
require($phpbb_root_path . 'includes/constants.' . $phpEx);
// Special options for conflicts/modified files
@ -102,7 +102,7 @@ class install_update extends module
define('MERGE_NEW_FILE', 3);
define('MERGE_MOD_FILE', 4);
$dbms = phpbb_convert_30_dbms_to_31($dbms);
$dbms = $phpbb_config_php_file->convert_30_dbms_to_31($dbms);
$db = new $dbms();

View File

@ -0,0 +1,175 @@
<?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;
class config_php_file
{
/** @var string phpBB Root Path */
protected $phpbb_root_path;
/** @var string php file extension */
protected $php_ext;
/**
* Indicates whether the php config file has been loaded.
*
* @var bool
*/
protected $config_loaded = false;
/**
* The content of the php config file
*
* @var array
*/
protected $config_data = array();
/**
* The path to the config file. (Default: $phpbb_root_path . 'config.' . $php_ext)
*
* @var string
*/
protected $config_file;
private $defined_vars;
/**
* Constructor
*
* @param string $phpbb_root_path phpBB Root Path
* @param string $php_ext php file extension
*/
function __construct($phpbb_root_path, $php_ext)
{
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext;
$this->config_file = $this->phpbb_root_path . 'config.' . $this->php_ext;
}
/**
* Set the path to the config file.
*
* @param string $config_file
*/
public function set_config_file($config_file)
{
$this->config_file = $config_file;
$this->config_loaded = false;
}
/**
* Returns an associative array containing the variables defined by the config file.
*
* @return bool|array Return the content of the config file or false if the file does not exists.
*/
public function get_all()
{
if (!$this->load_config_file())
{
return false;
}
return $this->config_data;
}
/**
* Return the value of a variable defined into the config.php file and false if the variable does not exist.
*
* @param string $variable The name of the variable
* @return mixed
*/
public function get($variable)
{
if (!$this->load_config_file())
{
return false;
}
return isset($this->config_data[$variable]) ? $this->config_data[$variable] : false;
}
/**
* Load the config file and store the information.
*
* @return bool True if the file was correctly loaded, false otherwise.
*/
protected function load_config_file()
{
if (!$this->config_loaded)
{
if (file_exists($this->config_file))
{
$this->defined_vars = get_defined_vars();
require($this->config_file);
$this->config_data = array_diff_key(get_defined_vars(), $this->defined_vars);
$this->config_loaded = true;
}
else
{
return false;
}
}
return true;
}
/**
* Convert either 3.0 dbms or 3.1 db driver class name to 3.1 db driver class name.
*
* If $dbms is a valid 3.1 db driver class name, returns it unchanged.
* Otherwise prepends phpbb\db\driver\ to the dbms to convert a 3.0 dbms
* to 3.1 db driver class name.
*
* @param string $dbms dbms parameter
* @return string driver class
* @throws \RuntimeException
*/
public function convert_30_dbms_to_31($dbms)
{
// Note: this check is done first because mysqli extension
// supplies a mysqli class, and class_exists($dbms) would return
// true for mysqli class.
// However, per the docblock any valid 3.1 driver name should be
// recognized by this function, and have priority over 3.0 dbms.
if (strpos($dbms, 'phpbb\db\driver') === false && class_exists('phpbb\db\driver\\' . $dbms))
{
return 'phpbb\db\driver\\' . $dbms;
}
if (class_exists($dbms))
{
// Additionally we could check that $dbms extends phpbb\db\driver\driver.
// http://php.net/manual/en/class.reflectionclass.php
// Beware of possible performance issues:
// http://stackoverflow.com/questions/294582/php-5-reflection-api-performance
// We could check for interface implementation in all paths or
// only when we do not prepend phpbb\db\driver\.
/*
$reflection = new \ReflectionClass($dbms);
if ($reflection->isSubclassOf('phpbb\db\driver\driver'))
{
return $dbms;
}
*/
return $dbms;
}
throw new \RuntimeException("You have specified an invalid dbms driver: $dbms");
}
}

View File

@ -0,0 +1,402 @@
<?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;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
class container_builder
{
/** @var string phpBB Root Path */
protected $phpbb_root_path;
/** @var string php file extension */
protected $php_ext;
/**
* The container under construction
*
* @var ContainerBuilder
*/
protected $container;
/**
* @var \phpbb\db\driver\driver_interface
*/
protected $dbal_connection = null;
/**
* @var array the installed extensions
*/
protected $installed_exts = null;
/**
* Indicates whether the php config file should be injected into the container (default to true).
*
* @var bool
*/
protected $inject_config = true;
/**
* Indicates whether extensions should be used (default to true).
*
* @var bool
*/
protected $use_extensions = true;
/**
* Defines a custom path to find the configuration of the container (default to $this->phpbb_root_path . 'config')
*
* @var string
*/
protected $config_path = null;
/**
* Indicates whether the phpBB compile pass should be used (default to true).
*
* @var bool
*/
protected $use_custom_pass = true;
/**
* Indicates whether the kernel compile pass should be used (default to true).
*
* @var bool
*/
protected $use_kernel_pass = true;
/**
* Indicates whether the container should be dumped to the filesystem (default to true).
*
* If DEBUG_CONTAINER is set this option is ignored and a new container is build.
*
* @var bool
*/
protected $dump_container = true;
/**
* Indicates if the container should be compiled automatically (default to true).
*
* @var bool
*/
protected $compile_container = true;
/**
* Custom parameters to inject into the container.
*
* Default to true:
* array(
* 'core.root_path', $this->phpbb_root_path,
* 'core.php_ext', $this->php_ext,
* );
*
* @var array
*/
protected $custom_parameters = null;
/**
* @var \phpbb\config_php_file
*/
protected $config_php_file;
/**
* Constructor
*
* @param \phpbb\config_php_file $config_php_file
* @param string $phpbb_root_path Path to the phpbb includes directory.
* @param string $php_ext php file extension
*/
function __construct(\phpbb\config_php_file $config_php_file, $phpbb_root_path, $php_ext)
{
$this->config_php_file = $config_php_file;
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext;
}
/**
* Build and return a new Container respecting the current configuration
*
* @return \phpbb_cache_container|ContainerBuilder
*/
public function get_container()
{
$container_filename = $this->get_container_filename();
if (!defined('DEBUG_CONTAINER') && $this->dump_container && file_exists($container_filename))
{
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));
if ($this->use_extensions)
{
$installed_exts = $this->get_installed_extensions();
$container_extensions[] = new \phpbb\di\extension\ext($installed_exts);
}
if ($this->inject_config)
{
$container_extensions[] = new \phpbb\di\extension\config($this->config_php_file);
}
$this->container = $this->create_container($container_extensions);
if ($this->use_custom_pass)
{
$this->container->addCompilerPass(new \phpbb\di\pass\collection_pass());
if ($this->use_kernel_pass)
{
$this->container->addCompilerPass(new \phpbb\di\pass\kernel_pass());
}
}
$this->inject_custom_parameters();
if ($this->compile_container)
{
$this->container->compile();
}
if ($this->dump_container && !defined('DEBUG'))
{
$this->dump_container($container_filename);
}
}
$this->container->set('config.php', $this->config_php_file);
if ($this->compile_container)
{
$this->inject_dbal();
}
return $this->container;
}
/**
* Set if the extensions should be used.
*
* @param bool $use_extensions
*/
public function set_use_extensions($use_extensions)
{
$this->use_extensions = $use_extensions;
}
/**
* Set if the phpBB compile pass have to be used.
*
* @param bool $use_custom_pass
*/
public function set_use_custom_pass($use_custom_pass)
{
$this->use_custom_pass = $use_custom_pass;
}
/**
* Set if the kernel compile pass have to be used.
*
* @param bool $use_kernel_pass
*/
public function set_use_kernel_pass($use_kernel_pass)
{
$this->use_kernel_pass = $use_kernel_pass;
}
/**
* Set if the php config file should be injecting into the container.
*
* @param bool $inject_config
*/
public function set_inject_config($inject_config)
{
$this->inject_config = $inject_config;
}
/**
* Set if a dump container should be used.
*
* If DEBUG_CONTAINER is set this option is ignored and a new container is build.
*
* @var bool $dump_container
*/
public function set_dump_container($dump_container)
{
$this->dump_container = $dump_container;
}
/**
* Set if the container should be compiled automatically (default to true).
*
* @var bool $dump_container
*/
public function set_compile_container($compile_container)
{
$this->compile_container = $compile_container;
}
/**
* Set a custom path to find the configuration of the container
*
* @param string $config_path
*/
public function set_config_path($config_path)
{
$this->config_path = $config_path;
}
/**
* Set custom parameters to inject into the container.
*
* @param array $custom_parameters
*/
public function set_custom_parameters($custom_parameters)
{
$this->custom_parameters = $custom_parameters;
}
/**
* Dump the container to the disk.
*
* @param string $container_filename The name of the file.
*/
protected function dump_container($container_filename)
{
$dumper = new PhpDumper($this->container);
$cached_container_dump = $dumper->dump(array(
'class' => 'phpbb_cache_container',
'base_class' => 'Symfony\\Component\\DependencyInjection\\ContainerBuilder',
));
file_put_contents($container_filename, $cached_container_dump);
}
/**
* Inject the connection into the container if one was opened.
*/
protected function inject_dbal()
{
if ($this->dbal_connection !== null)
{
$this->container->get('dbal.conn')->set_driver($this->dbal_connection);
}
}
/**
* Get DB connection.
*
* @return \phpbb\db\driver\driver_interface
*/
protected function get_dbal_connection()
{
if ($this->dbal_connection === null)
{
$dbal_driver_class = $this->config_php_file->convert_30_dbms_to_31($this->config_php_file->get('dbms'));
$this->dbal_connection = new $dbal_driver_class();
$this->dbal_connection->sql_connect(
$this->config_php_file->get('dbhost'),
$this->config_php_file->get('dbuser'),
$this->config_php_file->get('dbpasswd'),
$this->config_php_file->get('dbname'),
$this->config_php_file->get('dbport'),
defined('PHPBB_DB_NEW_LINK') && PHPBB_DB_NEW_LINK
);
}
return $this->dbal_connection;
}
/**
* Get enabled extensions.
*
* @return array enabled extensions
*/
protected function get_installed_extensions()
{
$db = $this->get_dbal_connection();
$extension_table = $this->config_php_file->get('table_prefix') . 'ext';
$sql = 'SELECT *
FROM ' . $extension_table . '
WHERE ext_active = 1';
$result = $db->sql_query($sql);
$rows = $db->sql_fetchrowset($result);
$db->sql_freeresult($result);
$exts = array();
foreach ($rows as $row)
{
$exts[$row['ext_name']] = $this->phpbb_root_path . 'ext/' . $row['ext_name'] . '/';
}
return $exts;
}
/**
* Create the ContainerBuilder object
*
* @param array $extensions Array of Container extension objects
* @return ContainerBuilder object
*/
protected function create_container(array $extensions)
{
$container = new ContainerBuilder();
foreach ($extensions as $extension)
{
$container->registerExtension($extension);
$container->loadFromExtension($extension->getAlias());
}
return $container;
}
/**
* Inject the customs parameters into the container
*/
protected function inject_custom_parameters()
{
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);
}
}
/**
* Get the filename under which the dumped container will be stored.
*
* @return string Path for dumped container
*/
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;
}
}

View File

@ -21,9 +21,12 @@ use Symfony\Component\HttpKernel\DependencyInjection\Extension;
*/
class config extends Extension
{
public function __construct($config_file)
/** @var array */
protected $config_php;
public function __construct(\phpbb\config_php_file $config_php)
{
$this->config_file = $config_file;
$this->config_php = $config_php;
}
/**
@ -36,17 +39,15 @@ class config extends Extension
*/
public function load(array $config, ContainerBuilder $container)
{
require($this->config_file);
$container->setParameter('core.adm_relative_path', (isset($phpbb_adm_relative_path) ? $phpbb_adm_relative_path : 'adm/'));
$container->setParameter('core.table_prefix', $table_prefix);
$container->setParameter('cache.driver.class', $this->convert_30_acm_type($acm_type));
$container->setParameter('dbal.driver.class', phpbb_convert_30_dbms_to_31($dbms));
$container->setParameter('dbal.dbhost', $dbhost);
$container->setParameter('dbal.dbuser', $dbuser);
$container->setParameter('dbal.dbpasswd', $dbpasswd);
$container->setParameter('dbal.dbname', $dbname);
$container->setParameter('dbal.dbport', $dbport);
$container->setParameter('core.adm_relative_path', ($this->config_php->get('phpbb_adm_relative_path') ? $this->config_php->get('phpbb_adm_relative_path') : 'adm/'));
$container->setParameter('core.table_prefix', $this->config_php->get('table_prefix'));
$container->setParameter('cache.driver.class', $this->convert_30_acm_type($this->config_php->get('acm_type')));
$container->setParameter('dbal.driver.class', $this->config_php->convert_30_dbms_to_31($this->config_php->get('dbms')));
$container->setParameter('dbal.dbhost', $this->config_php->get('dbhost'));
$container->setParameter('dbal.dbuser', $this->config_php->get('dbuser'));
$container->setParameter('dbal.dbpasswd', $this->config_php->get('dbpasswd'));
$container->setParameter('dbal.dbname', $this->config_php->get('dbname'));
$container->setParameter('dbal.dbport', $this->config_php->get('dbport'));
$container->setParameter('dbal.new_link', defined('PHPBB_DB_NEW_LINK') && PHPBB_DB_NEW_LINK);
}

View File

@ -0,0 +1,30 @@
<?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.
*
*/
class phpbb_config_php_file_test extends phpbb_test_case
{
public function test_default()
{
$config_php = new \phpbb\config_php_file(dirname( __FILE__ ) . '/fixtures/', 'php');
$this->assertSame('bar', $config_php->get('foo'));
$this->assertSame(array('foo' => 'bar', 'foo_foo' => 'bar bar'), $config_php->get_all());
}
public function test_set_config_file()
{
$config_php = new \phpbb\config_php_file(dirname( __FILE__ ) . '/fixtures/', 'php');
$config_php->set_config_file(dirname( __FILE__ ) . '/fixtures/config_other.php');
$this->assertSame('foo', $config_php->get('bar'));
$this->assertSame(array('bar' => 'foo', 'bar_bar' => 'foo foo'), $config_php->get_all());
}
}

View File

@ -14,47 +14,135 @@
namespace
{
require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
require_once dirname(__FILE__) . '/../../phpBB/includes/functions_container.php';
class phpbb_di_container_test extends phpbb_test_case
class phpbb_di_container_test extends \phpbb_test_case
{
public function test_phpbb_create_container()
{
$phpbb_root_path = __DIR__ . '/../../phpBB/';
$extensions = array(
new \phpbb\di\extension\config(__DIR__ . '/fixtures/config.php'),
new \phpbb\di\extension\core($phpbb_root_path . 'config'),
);
$container = phpbb_create_container($extensions, $phpbb_root_path, 'php');
protected $config_php;
$this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerBuilder', $container);
/**
* @var \phpbb\di\container_builder
*/
protected $builder;
protected $phpbb_root_path;
protected $filename;
public function setUp()
{
$this->phpbb_root_path = dirname(__FILE__) . '/';
$this->config_php = new \phpbb\config_php_file($this->phpbb_root_path . 'fixtures/', 'php');
$this->builder = new phpbb_mock_phpbb_di_container_builder($this->config_php, $this->phpbb_root_path . 'fixtures/', 'php');
$this->filename = $this->phpbb_root_path . '../tmp/container.php';
if (is_file($this->filename))
{
unlink($this->filename);
}
parent::setUp();
}
public function test_phpbb_create_install_container()
public function test_default_container()
{
$phpbb_root_path = __DIR__ . '/../../phpBB/';
$extensions = array(
new \phpbb\di\extension\config(__DIR__ . '/fixtures/config.php'),
new \phpbb\di\extension\core($phpbb_root_path . 'config'),
);
$container = phpbb_create_install_container($phpbb_root_path, 'php');
$container = $this->builder->get_container();
$this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerBuilder', $container);
// Checks the core services
$this->assertTrue($container->hasParameter('core'));
// Checks compile_container
$this->assertTrue($container->isFrozen());
// Checks inject_config
$this->assertTrue($container->hasParameter('dbal.dbhost'));
// Checks use_extensions
$this->assertTrue($container->hasParameter('enabled'));
$this->assertFalse($container->hasParameter('disabled'));
$this->assertFalse($container->hasParameter('available'));
// Checks set_custom_parameters
$this->assertTrue($container->hasParameter('core.root_path'));
// Checks dump_container
$this->assertTrue(is_file($this->filename));
// Checks the construction of a dumped container
$container = $this->builder->get_container();
$this->assertInstanceOf('phpbb_cache_container', $container);
$this->assertFalse($container->isFrozen());
$container->getParameterBag(); // needed, otherwise the container is not marked as frozen
$this->assertTrue($container->isFrozen());
}
public function test_dump_container()
{
$this->builder->set_dump_container(false);
$container = $this->builder->get_container();
$this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerBuilder', $container);
// Checks dump_container
$this->assertFalse(is_file($this->filename));
// Checks the construction of a dumped container
$container = $this->builder->get_container();
$this->assertNotInstanceOf('phpbb_cache_container', $container);
$this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerBuilder', $container);
$this->assertTrue($container->isFrozen());
}
public function test_phpbb_create_compiled_container()
public function test_use_extensions()
{
$phpbb_root_path = __DIR__ . '/../../phpBB/';
$config_file = __DIR__ . '/fixtures/config.php';
$extensions = array(
new \phpbb\di\extension\config(__DIR__ . '/fixtures/config.php'),
new \phpbb\di\extension\core($phpbb_root_path . 'config'),
);
$container = phpbb_create_compiled_container($config_file, $extensions, array(), $phpbb_root_path, 'php');
$this->builder->set_use_extensions(false);
$container = $this->builder->get_container();
$this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerBuilder', $container);
$this->assertTrue($container->isFrozen());
// Checks the core services
$this->assertTrue($container->hasParameter('core'));
// Checks use_extensions
$this->assertFalse($container->hasParameter('enabled'));
$this->assertFalse($container->hasParameter('disabled'));
$this->assertFalse($container->hasParameter('available'));
}
public function test_compile_container()
{
$this->builder->set_compile_container(false);
$container = $this->builder->get_container();
$this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerBuilder', $container);
// Checks compile_container
$this->assertFalse($container->isFrozen());
}
public function test_inject_config()
{
$this->builder->set_inject_config(false);
$container = $this->builder->get_container();
$this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerBuilder', $container);
// Checks inject_config
$this->assertFalse($container->hasParameter('dbal.dbhost'));
}
public function test_set_config_path()
{
$this->builder->set_config_path($this->phpbb_root_path . 'fixtures/other_config/');
$container = $this->builder->get_container();
$this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerBuilder', $container);
$this->assertTrue($container->hasParameter('other_config'));
$this->assertFalse($container->hasParameter('core'));
}
public function test_set_custom_parameters()
{
$this->builder->set_custom_parameters(array('my_parameter' => true));
$container = $this->builder->get_container();
$this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerBuilder', $container);
$this->assertTrue($container->hasParameter('my_parameter'));
$this->assertFalse($container->hasParameter('core.root_path'));
}
}
}
@ -102,5 +190,12 @@ namespace phpbb\db\driver
function sql_like_expression($expression)
{
}
function sql_fetchrowset($query_id = false)
{
return array(
array('ext_name' => 'vendor/enabled'),
);
}
}
}

View File

@ -0,0 +1,14 @@
parameters:
core: true
services:
config.php:
synthetic: true
dbal.conn:
class: phpbb\db\driver\factory
arguments:
- @service_container
dispatcher:
class: phpbb\db\driver\container_mock

View File

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

View File

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

View File

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

View File

@ -0,0 +1,14 @@
parameters:
other_config: true
services:
config.php:
synthetic: true
dbal.conn:
class: phpbb\db\driver\factory
arguments:
- @service_container
dispatcher:
class: phpbb\db\driver\container_mock

3
tests/fixtures/config.php vendored Normal file
View File

@ -0,0 +1,3 @@
<?php
$foo = 'bar';
$foo_foo = 'bar bar';

3
tests/fixtures/config_other.php vendored Normal file
View File

@ -0,0 +1,3 @@
<?php
$bar = 'foo';
$bar_bar = 'foo foo';

View File

@ -36,7 +36,8 @@ class phpbb_convert_30_dbms_to_31_test extends phpbb_test_case
{
$expected = "phpbb\\db\\driver\\$input";
$output = phpbb_convert_30_dbms_to_31($input);
$config_php_file = new \phpbb\config_php_file('', '');
$output = $config_php_file->convert_30_dbms_to_31($input);
$this->assertEquals($expected, $output);
}

View File

@ -0,0 +1,20 @@
<?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.
*
*/
class phpbb_mock_phpbb_di_container_builder extends \phpbb\di\container_builder
{
protected function get_container_filename()
{
return $this->phpbb_root_path . '../../tmp/container.' . $this->php_ext;
}
}

View File

@ -142,17 +142,16 @@ class phpbb_test_case_helpers
$test_config = dirname(__FILE__) . '/../test_config.php';
}
$config_php_file = new \phpbb\config_php_file('', '');
if (file_exists($test_config))
{
include($test_config);
if (!function_exists('phpbb_convert_30_dbms_to_31'))
{
require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
}
$config_php_file->set_config_file($test_config);
extract($config_php_file->get_all());
unset($dbpasswd);
$config = array_merge($config, array(
'dbms' => phpbb_convert_30_dbms_to_31($dbms),
'dbms' => $config_php_file->convert_30_dbms_to_31($dbms),
'dbhost' => $dbhost,
'dbport' => $dbport,
'dbname' => $dbname,
@ -183,13 +182,8 @@ class phpbb_test_case_helpers
if (isset($_SERVER['PHPBB_TEST_DBMS']))
{
if (!function_exists('phpbb_convert_30_dbms_to_31'))
{
require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
}
$config = array_merge($config, array(
'dbms' => isset($_SERVER['PHPBB_TEST_DBMS']) ? phpbb_convert_30_dbms_to_31($_SERVER['PHPBB_TEST_DBMS']) : '',
'dbms' => isset($_SERVER['PHPBB_TEST_DBMS']) ? $config_php_file->convert_30_dbms_to_31($_SERVER['PHPBB_TEST_DBMS']) : '',
'dbhost' => isset($_SERVER['PHPBB_TEST_DBHOST']) ? $_SERVER['PHPBB_TEST_DBHOST'] : '',
'dbport' => isset($_SERVER['PHPBB_TEST_DBPORT']) ? $_SERVER['PHPBB_TEST_DBPORT'] : '',
'dbname' => isset($_SERVER['PHPBB_TEST_DBNAME']) ? $_SERVER['PHPBB_TEST_DBNAME'] : '',