From fb2642bbc6360dacfd4a3cc9f7e9447b02cb46a1 Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Mon, 10 Jan 2011 02:27:18 +0100 Subject: [PATCH 1/6] [task/config-class] Implemented a config class to replace the global array. There is a phpbb_config class which simply holds an array and does not persist any data. It implements ArrayAccess, Countable and IteratorAggregate to allow regular use of configuration as if it was still an array. The phpbb_config_db class depends on an instance of the dbal and a cache driver. It obtains the configuration data from cache and database as necessary and persists data to the database. The functions set_config and set_config_count remain for backward compatability but they only call methods on the new config class now instead of directly manipulating the database and cache. PHPBB3-9988 --- phpBB/common.php | 4 +- phpBB/download/file.php | 5 +- phpBB/includes/cache/service.php | 45 ----- .../plugins/phpbb_captcha_gd_plugin.php | 9 +- phpBB/includes/config/config.php | 157 ++++++++++++++++ phpBB/includes/config/db.php | 175 ++++++++++++++++++ phpBB/includes/db/dbal.php | 22 +++ phpBB/includes/db/firebird.php | 16 ++ phpBB/includes/db/postgres.php | 16 ++ phpBB/includes/functions.php | 72 ++++--- phpBB/install/database_update.php | 14 +- phpBB/install/index.php | 4 +- phpBB/install/install_convert.php | 34 +--- phpBB/install/install_install.php | 13 +- phpBB/install/install_update.php | 13 +- phpBB/style.php | 5 +- tests/config/config_test.php | 112 +++++++++++ tests/config/db_test.php | 117 ++++++++++++ tests/config/fixtures/config.xml | 18 ++ tests/mock/cache.php | 6 + 20 files changed, 703 insertions(+), 154 deletions(-) create mode 100644 phpBB/includes/config/config.php create mode 100644 phpBB/includes/config/db.php create mode 100644 tests/config/config_test.php create mode 100644 tests/config/db_test.php create mode 100644 tests/config/fixtures/config.xml diff --git a/phpBB/common.php b/phpBB/common.php index 7b6a407c94..0116516213 100644 --- a/phpBB/common.php +++ b/phpBB/common.php @@ -227,7 +227,9 @@ $db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, defined(' unset($dbpasswd); // Grab global variables, re-cache if necessary -$config = $cache->obtain_config(); +$config = new phpbb_config_db($db, $cache_factory->get_driver()); +set_config(null, null, null, $config); +set_config_count(null, null, null, $config); // Add own hook handler require($phpbb_root_path . 'includes/hooks/index.' . $phpEx); diff --git a/phpBB/download/file.php b/phpBB/download/file.php index a7e8b9f06c..985a24e572 100644 --- a/phpBB/download/file.php +++ b/phpBB/download/file.php @@ -70,7 +70,10 @@ if (isset($_GET['avatar'])) // worst-case default $browser = (!empty($_SERVER['HTTP_USER_AGENT'])) ? htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT']) : 'msie 6.0'; - $config = $cache->obtain_config(); + $config = new phpbb_config_db($db, $cache_factory->get_driver()); + set_config(null, null, null, $config); + set_config_count(null, null, null, $config); + $filename = $_GET['avatar']; $avatar_group = false; $exit = false; diff --git a/phpBB/includes/cache/service.php b/phpBB/includes/cache/service.php index 424606bfc8..bcbb0ce872 100644 --- a/phpBB/includes/cache/service.php +++ b/phpBB/includes/cache/service.php @@ -39,51 +39,6 @@ class phpbb_cache_service return call_user_func_array(array($this->acm, $method), $arguments); } - /** - * Get config values - */ - function obtain_config() - { - global $db; - - if (($config = $this->acm->get('config')) !== false) - { - $sql = 'SELECT config_name, config_value - FROM ' . CONFIG_TABLE . ' - WHERE is_dynamic = 1'; - $result = $db->sql_query($sql); - - while ($row = $db->sql_fetchrow($result)) - { - $config[$row['config_name']] = $row['config_value']; - } - $db->sql_freeresult($result); - } - else - { - $config = $cached_config = array(); - - $sql = 'SELECT config_name, config_value, is_dynamic - FROM ' . CONFIG_TABLE; - $result = $db->sql_query($sql); - - while ($row = $db->sql_fetchrow($result)) - { - if (!$row['is_dynamic']) - { - $cached_config[$row['config_name']] = $row['config_value']; - } - - $config[$row['config_name']] = $row['config_value']; - } - $db->sql_freeresult($result); - - $this->acm->put('config', $cached_config); - } - - return $config; - } - /** * Obtain list of naughty words and build preg style replacement arrays for use by the * calling script diff --git a/phpBB/includes/captcha/plugins/phpbb_captcha_gd_plugin.php b/phpBB/includes/captcha/plugins/phpbb_captcha_gd_plugin.php index e9cc1f57d2..1c82065a90 100644 --- a/phpBB/includes/captcha/plugins/phpbb_captcha_gd_plugin.php +++ b/phpBB/includes/captcha/plugins/phpbb_captcha_gd_plugin.php @@ -152,9 +152,16 @@ class phpbb_captcha_gd extends phpbb_default_captcha global $config; $config_old = $config; + + $config = new phpbb_config(array()); + foreach ($old_config as $key => $value) + { + $config->set($key, $value); + } + foreach ($this->captcha_vars as $captcha_var => $template_var) { - $config[$captcha_var] = request_var($captcha_var, (int) $config[$captcha_var]); + $config->set($captcha_var, request_var($captcha_var, (int) $config[$captcha_var])); } parent::execute_demo(); $config = $config_old; diff --git a/phpBB/includes/config/config.php b/phpBB/includes/config/config.php new file mode 100644 index 0000000000..64fef28cfa --- /dev/null +++ b/phpBB/includes/config/config.php @@ -0,0 +1,157 @@ + string) + */ + protected $config; + + /** + * Creates a configuration container with a default set of values + * + * @param array(string => string) $config The configuration data. + */ + public function __construct(array $config) + { + $this->config = $config; + } + + /** + * Retrieves an ArrayIterator over the configuration values. + * + * @return ArrayIterator An iterator over all config data + */ + public function getIterator() + { + return new ArrayIterator($this->config); + } + + /** + * Checks if the specified config value exists. + * + * @param string $key The configuration option's name. + * @return bool Whether the configuration option exists. + */ + public function offsetExists($key) + { + return isset($this->config[$key]); + } + + /** + * Retrieves a configuration value. + * + * @param string $key The configuration option's name. + * @return string The configuration value + */ + public function offsetGet($key) + { + return (isset($this->config[$key])) ? $this->config[$key] : ''; + } + + /** + * Temporarily overwrites the value of a configuration variable. + * + * The configuration change will not persist. It will be lost + * after the request. + * + * @param string $key The configuration option's name. + * @param string $value The temporary value. + */ + public function offsetSet($key, $value) + { + $this->config[$key] = $value; + } + + /** + * Called when deleting a configuration value directly, triggers an error. + * + * @param string $key The configuration option's name. + */ + public function offsetUnset($key) + { + trigger_error('Config values have to be deleted explicitly with the phpbb_config::delete($key) method.', E_USER_ERROR); + } + + /** + * Retrieves the number of configuration options currently set. + * + * @return int Number of config options + */ + public function count() + { + return count($this->config); + } + + /** + * Sets a configuration option's value + * + * @param string $key The configuration option's name + * @param string $value New configuration value + * @param bool $cache Whether this variable should be cached or if it + * changes too frequently to be efficiently cached. + */ + public function set($key, $value, $cache = true) + { + $this->config[$key] = $value; + } + + /** + * Sets a configuration option's value only if the old_value matches the + * current configuration value or the configuration value does not exist yet. + * + * @param string $key The configuration option's name + * @param string $old_value Current configuration value + * @param string $value New configuration value + * @param bool $cache Whether this variable should be cached or if it + * changes too frequently to be efficiently cached. + * @return bool True if the value was changed, false otherwise. + */ + public function set_atomic($key, $old_value, $value, $cache = true) + { + if (!isset($this->config[$key]) || $this->config[$key] == $old_value) + { + $this->config[$key] = $value; + return true; + } + return false; + } + + /** + * Increments an integer configuration value. + * + * @param string $key The configuration option's name + * @param int $increment Amount to increment by + * @param bool $cache Whether this variable should be cached or if it + * changes too frequently to be efficiently cached. + */ + function increment($key, $increment, $cache = true) + { + if (!isset($this->config[$key])) + { + $this->config[$key] = 0; + } + + $this->config[$key] += $increment; + } +} diff --git a/phpBB/includes/config/db.php b/phpBB/includes/config/db.php new file mode 100644 index 0000000000..e6b7bffb73 --- /dev/null +++ b/phpBB/includes/config/db.php @@ -0,0 +1,175 @@ +db = $db; + $this->cache = $cache; + $this->table = $table; + + if (($config = $cache->get('config')) !== false) + { + $sql = 'SELECT config_name, config_value + FROM ' . $this->table . ' + WHERE is_dynamic = 1'; + $result = $this->db->sql_query($sql); + $config += $this->db->sql_fetchrowset($result); + $this->db->sql_freeresult($result); + } + else + { + $config = $cached_config = array(); + + $sql = 'SELECT config_name, config_value, is_dynamic + FROM ' . $this->table; + $result = $this->db->sql_query($sql); + + while ($row = $this->db->sql_fetchrow($result)) + { + if (!$row['is_dynamic']) + { + $cached_config[$row['config_name']] = $row['config_value']; + } + + $config[$row['config_name']] = $row['config_value']; + } + $this->db->sql_freeresult($result); + + $cache->put('config', $cached_config); + } + + parent::__construct($config); + } + + /** + * Sets a configuration option's value + * + * @param string $key The configuration option's name + * @param string $value New configuration value + * @param bool $cache Whether this variable should be cached or if it + * changes too frequently to be efficiently cached. + */ + public function set($key, $value, $cache = true) + { + $this->set_atomic($key, false, $value, $cache); + } + + /** + * Sets a configuration option's value only if the old_value matches the + * current configuration value or the configuration value does not exist yet. + * + * @param string $key The configuration option's name + * @param mixed $old_value Current configuration value or false to ignore + * the old value + * @param string $new_value New configuration value + * @param bool $cache Whether this variable should be cached or if it + * changes too frequently to be efficiently cached + * @return bool True if the value was changed, false otherwise + */ + public function set_atomic($key, $old_value, $new_value, $cache = true) + { + $sql = 'UPDATE ' . $this->table . " + SET config_value = '" . $this->db->sql_escape($new_value) . "' + WHERE config_name = '" . $this->db->sql_escape($key) . "'"; + + if ($old_value !== false) + { + $sql .= " AND config_value = '" . $this->db->sql_escape($old_value) . "'"; + } + + $result = $this->db->sql_query($sql); + + if (!$this->db->sql_affectedrows($result) && isset($this->config[$key])) + { + return false; + } + + if (!isset($this->config[$key])) + { + $sql = 'INSERT INTO ' . $this->table . ' ' . $this->db->sql_build_array('INSERT', array( + 'config_name' => $key, + 'config_value' => $new_value, + 'is_dynamic' => ($cache) ? 0 : 1)); + $this->db->sql_query($sql); + } + + if ($cache) + { + $this->cache->destroy('config'); + } + + $this->config[$key] = $new_value; + return true; + } + + /** + * Increments an integer config value directly in the database. + * + * Using this method instead of setting the new value directly avoids race + * conditions and unlike set_atomic it cannot fail. + * + * @param string $key The configuration option's name + * @param int $increment Amount to increment by + * @param bool $cache Whether this variable should be cached or if it + * changes too frequently to be efficiently cached. + */ + function increment($key, $increment, $cache = true) + { + if (!isset($this->config[$key])) + { + $this->set($key, '0', $cache); + } + + $sql_update = $this->db->cast_expr_to_string($this->db->cast_expr_to_bigint('config_value') . ' + ' . (int) $increment); + + $this->db->sql_query('UPDATE ' . $this->table . ' + SET config_value = ' . $sql_update . " + WHERE config_name = '" . $this->db->sql_escape($key) . "'"); + + if ($cache) + { + $this->cache->destroy('config'); + } + + $this->config[$key] += $increment; + } +} diff --git a/phpBB/includes/db/dbal.php b/phpBB/includes/db/dbal.php index fd0464814f..cfebb61440 100644 --- a/phpBB/includes/db/dbal.php +++ b/phpBB/includes/db/dbal.php @@ -447,6 +447,28 @@ class dbal return $column_name . ' | ' . (1 << $bit) . (($compare) ? ' ' . $compare : ''); } + /** + * Returns SQL string to cast a string expression to an int. + * + * @param string $expression An expression evaluating to string + * @return string Expression returning an int + */ + function cast_expr_to_bigint($expression) + { + return $expression; + } + + /** + * Returns SQL string to cast an integer expression to a string. + * + * @param string $expression An expression evaluating to int + * @return string Expression returning a string + */ + function cast_expr_to_string($expression) + { + return $expression; + } + /** * Run more than one insert statement. * diff --git a/phpBB/includes/db/firebird.php b/phpBB/includes/db/firebird.php index 68069ab6cf..69476f79f8 100644 --- a/phpBB/includes/db/firebird.php +++ b/phpBB/includes/db/firebird.php @@ -465,6 +465,22 @@ class dbal_firebird extends dbal return 'BIN_OR(' . $column_name . ', ' . (1 << $bit) . ')' . (($compare) ? ' ' . $compare : ''); } + /** + * @inheritdoc + */ + function cast_expr_to_bigint($expression) + { + return 'CAST(' . $expression . ' as DECIMAL(255, 0))'; + } + + /** + * @inheritdoc + */ + function cast_expr_to_string($expression) + { + return 'CAST(' . $expression . ' as VARCHAR(255))'; + } + /** * return sql error array * @access private diff --git a/phpBB/includes/db/postgres.php b/phpBB/includes/db/postgres.php index eb297b8db5..424ce12245 100644 --- a/phpBB/includes/db/postgres.php +++ b/phpBB/includes/db/postgres.php @@ -365,6 +365,22 @@ class dbal_postgres extends dbal return $expression; } + /** + * @inheritdoc + */ + function cast_expr_to_bigint($expression) + { + return 'CAST(' . $expression . ' as DECIMAL(255, 0))'; + } + + /** + * @inheritdoc + */ + function cast_expr_to_string($expression) + { + return 'CAST(' . $expression . ' as VARCHAR(255))'; + } + /** * return sql error array * @access private diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index c55931a2fb..12e4ebc597 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -17,7 +17,11 @@ if (!defined('IN_PHPBB')) } // Common global functions - +/** +* Casts a variable to the given type. +* +* @deprecated +*/ function set_var(&$result, $var, $type, $multibyte = false) { // no need for dependency injection here, if you have the object, call the method yourself! @@ -30,6 +34,7 @@ function set_var(&$result, $var, $type, $multibyte = false) * See {@link phpbb_request_interface::variable phpbb_request_interface::variable} for * documentation of this function's use. * +* @deprecated * @param mixed $var_name The form variable's name from which data shall be retrieved. * If the value is an array this may be an array of indizes which will give * direct access to a value at any depth. E.g. if the value of "var" is array(1 => "a") @@ -78,59 +83,46 @@ function request_var($var_name, $default, $multibyte = false, $cookie = false, p /** * Set config value. Creates missing config entry. +* +* @deprecated */ -function set_config($config_name, $config_value, $is_dynamic = false) +function set_config($config_name, $config_value, $is_dynamic = false, phpbb_config $set_config = null) { - global $db, $cache, $config; + static $config = null; - $sql = 'UPDATE ' . CONFIG_TABLE . " - SET config_value = '" . $db->sql_escape($config_value) . "' - WHERE config_name = '" . $db->sql_escape($config_name) . "'"; - $db->sql_query($sql); - - if (!$db->sql_affectedrows() && !isset($config[$config_name])) + if ($set_config !== null) { - $sql = 'INSERT INTO ' . CONFIG_TABLE . ' ' . $db->sql_build_array('INSERT', array( - 'config_name' => $config_name, - 'config_value' => $config_value, - 'is_dynamic' => ($is_dynamic) ? 1 : 0)); - $db->sql_query($sql); + $config = $set_config; + + if (empty($config_name)) + { + return; + } } - $config[$config_name] = $config_value; - - if (!$is_dynamic) - { - $cache->destroy('config'); - } + $config->set($config_name, $config_value, !$is_dynamic); } /** * Set dynamic config value with arithmetic operation. +* +* @deprecated */ -function set_config_count($config_name, $increment, $is_dynamic = false) +function set_config_count($config_name, $increment, $is_dynamic = false, phpbb_config $set_config = null) { - global $db, $cache; + static $config = null; - switch ($db->sql_layer) + if ($set_config !== null) { - case 'firebird': - case 'postgres': - $sql_update = 'CAST(CAST(config_value as DECIMAL(255, 0)) + ' . (int) $increment . ' as VARCHAR(255))'; - break; + $config = $set_config; - // MySQL, SQlite, mssql, mssql_odbc, oracle - default: - $sql_update = 'config_value + ' . (int) $increment; - break; + if (empty($config_name)) + { + return; + } } - $db->sql_query('UPDATE ' . CONFIG_TABLE . ' SET config_value = ' . $sql_update . " WHERE config_name = '" . $db->sql_escape($config_name) . "'"); - - if (!$is_dynamic) - { - $cache->destroy('config'); - } + $config->increment($config_name, $increment, !$is_dynamic); } /** @@ -3546,7 +3538,7 @@ function phpbb_checkdnsrr($host, $type = 'MX') // but until 5.3.3 it only works for MX records // See: http://bugs.php.net/bug.php?id=51844 - // Call checkdnsrr() if + // Call checkdnsrr() if // we're looking for an MX record or // we're not on Windows or // we're running a PHP version where #51844 has been fixed @@ -3566,7 +3558,7 @@ function phpbb_checkdnsrr($host, $type = 'MX') // dns_get_record() is available since PHP 5; since PHP 5.3 also on Windows, // but on Windows it does not work reliable for AAAA records before PHP 5.3.1 - // Call dns_get_record() if + // Call dns_get_record() if // we're not looking for an AAAA record or // we're not on Windows or // we're running a PHP version where AAAA lookups work reliable @@ -3596,7 +3588,7 @@ function phpbb_checkdnsrr($host, $type = 'MX') foreach ($resultset as $result) { if ( - isset($result['host']) && $result['host'] == $host && + isset($result['host']) && $result['host'] == $host && isset($result['type']) && $result['type'] == $type ) { diff --git a/phpBB/install/database_update.php b/phpBB/install/database_update.php index f73f7472f0..cecd565fa5 100644 --- a/phpBB/install/database_update.php +++ b/phpBB/install/database_update.php @@ -165,17 +165,9 @@ include($phpbb_root_path . 'language/' . $language . '/install.' . $phpEx); $inline_update = (request_var('type', 0)) ? true : false; // To let set_config() calls succeed, we need to make the config array available globally -$config = array(); - -$sql = 'SELECT * - FROM ' . CONFIG_TABLE; -$result = $db->sql_query($sql); - -while ($row = $db->sql_fetchrow($result)) -{ - $config[$row['config_name']] = $row['config_value']; -} -$db->sql_freeresult($result); +$config = new phpbb_config_db($db, $cache_factory->get_driver()); +set_config(null, null, null, $config); +set_config_count(null, null, null, $config); // We do not include DB Tools here, because we can not be sure the file is up-to-date ;) // Instead, this file defines a clean db_tools version (we are also not able to provide a different file, else the database update will not work standalone) diff --git a/phpBB/install/index.php b/phpBB/install/index.php index 653268ba68..18521e27d0 100644 --- a/phpBB/install/index.php +++ b/phpBB/install/index.php @@ -281,9 +281,9 @@ else } // Set some standard variables we want to force -$config = array( +$config = new phpbb_config(array( 'load_tplcompile' => '1' -); +)); $template->set_custom_template('../adm/style', 'admin'); $template->assign_var('T_TEMPLATE_PATH', '../adm/style'); diff --git a/phpBB/install/install_convert.php b/phpBB/install/install_convert.php index 31c675c201..7aae5bf28c 100644 --- a/phpBB/install/install_convert.php +++ b/phpBB/install/install_convert.php @@ -130,16 +130,7 @@ class install_convert extends module unset($dbpasswd); // We need to fill the config to let internal functions correctly work - $sql = 'SELECT * - FROM ' . CONFIG_TABLE; - $result = $db->sql_query($sql); - - $config = array(); - while ($row = $db->sql_fetchrow($result)) - { - $config[$row['config_name']] = $row['config_value']; - } - $db->sql_freeresult($result); + $config = new phpbb_config_db($db, new phpbb_cache_driver_null); // Detect if there is already a conversion in progress at this point and offer to resume // It's quite possible that the user will get disconnected during a large conversion so they need to be able to resume it @@ -350,16 +341,7 @@ class install_convert extends module $this->page_title = $lang['STAGE_SETTINGS']; // We need to fill the config to let internal functions correctly work - $sql = 'SELECT * - FROM ' . CONFIG_TABLE; - $result = $db->sql_query($sql); - - $config = array(); - while ($row = $db->sql_fetchrow($result)) - { - $config[$row['config_name']] = $row['config_value']; - } - $db->sql_freeresult($result); + $config = new phpbb_config_db($db, new phpbb_cache_driver_null); $convertor_tag = request_var('tag', ''); @@ -597,16 +579,8 @@ class install_convert extends module $db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true); unset($dbpasswd); - $sql = 'SELECT * - FROM ' . CONFIG_TABLE; - $result = $db->sql_query($sql); - - $config = array(); - while ($row = $db->sql_fetchrow($result)) - { - $config[$row['config_name']] = $row['config_value']; - } - $db->sql_freeresult($result); + // We need to fill the config to let internal functions correctly work + $config = new phpbb_config_db($db, new phpbb_cache_driver_null); // Override a couple of config variables for the duration $config['max_quote_depth'] = 0; diff --git a/phpBB/install/install_install.php b/phpBB/install/install_install.php index 8143ea7737..e47c8b8c01 100644 --- a/phpBB/install/install_install.php +++ b/phpBB/install/install_install.php @@ -1460,17 +1460,8 @@ class install_install extends module include_once($phpbb_root_path . 'includes/constants.' . $phpEx); include_once($phpbb_root_path . 'includes/search/fulltext_native.' . $phpEx); - // Fill the config array - it is needed by those functions we call - $sql = 'SELECT * - FROM ' . CONFIG_TABLE; - $result = $db->sql_query($sql); - - $config = array(); - while ($row = $db->sql_fetchrow($result)) - { - $config[$row['config_name']] = $row['config_value']; - } - $db->sql_freeresult($result); + // We need to fill the config to let internal functions correctly work + $config = new phpbb_config_db($db, new phpbb_cache_driver_null); $error = false; $search = new fulltext_native($error); diff --git a/phpBB/install/install_update.php b/phpBB/install/install_update.php index 2e78803b74..7c91b112a2 100644 --- a/phpBB/install/install_update.php +++ b/phpBB/install/install_update.php @@ -101,17 +101,8 @@ class install_update extends module // We do not need this any longer, unset for safety purposes unset($dbpasswd); - $config = array(); - - $sql = 'SELECT config_name, config_value - FROM ' . CONFIG_TABLE; - $result = $db->sql_query($sql); - - while ($row = $db->sql_fetchrow($result)) - { - $config[$row['config_name']] = $row['config_value']; - } - $db->sql_freeresult($result); + // We need to fill the config to let internal functions correctly work + $config = new phpbb_config_db($db, new phpbb_cache_driver_null); // Force template recompile $config['load_tplcompile'] = 1; diff --git a/phpBB/style.php b/phpBB/style.php index cff91a2312..09340992c5 100644 --- a/phpBB/style.php +++ b/phpBB/style.php @@ -82,7 +82,10 @@ if ($id) } unset($dbpasswd); - $config = $cache->obtain_config(); + $config = new phpbb_config_db($db, $cache_factory->get_driver()); + set_config(null, null, null, $config); + set_config_count(null, null, null, $config); + $user = false; // try to get a session ID from REQUEST array diff --git a/tests/config/config_test.php b/tests/config/config_test.php new file mode 100644 index 0000000000..73a365c847 --- /dev/null +++ b/tests/config/config_test.php @@ -0,0 +1,112 @@ + 'bar')); + + $this->assertTrue(isset($config['foo'])); + $this->assertFalse(isset($config['foobar'])); + } + + public function test_offset_get() + { + $config = new phpbb_config(array('foo' => 'bar')); + $this->assertEquals('bar', $config['foo']); + } + + public function test_offset_get_missing() + { + $config = new phpbb_config(array()); + $this->assertEquals('', $config['foo']); + } + + public function test_offset_set() + { + $config = new phpbb_config(array()); + $config['foo'] = 'x'; + $this->assertEquals('x', $config['foo']); + } + + public function test_offset_unset_fails() + { + $this->setExpectedTriggerError(E_USER_ERROR); + $config = new phpbb_config(array('foo' => 'x')); + unset($config['foo']); + } + + public function test_count() + { + $config = new phpbb_config(array('foo' => 'bar')); + $this->assertEquals(1, count($config)); + } + + public function test_iterate() + { + $vars = array('foo' => '23', 'bar' => '42'); + $config = new phpbb_config($vars); + + $count = 0; + foreach ($config as $key => $value) + { + $this->assertTrue(isset($vars[$key])); + $this->assertEquals($vars[$key], $value); + + $count++; + } + + $this->assertEquals(count($vars), $count); + } + + public function test_set_overwrite() + { + $config = new phpbb_config(array('foo' => 'x')); + $config->set('foo', 'bar'); + $this->assertEquals('bar', $config['foo']); + } + + public function test_set_new() + { + $config = new phpbb_config(array()); + $config->set('foo', 'bar'); + $this->assertEquals('bar', $config['foo']); + } + + public function test_set_atomic_overwrite() + { + $config = new phpbb_config(array('foo' => 'bar')); + $this->assertTrue($config->set_atomic('foo', 'bar', '23')); + $this->assertEquals('23', $config['foo']); + } + + public function test_set_atomic_new() + { + $config = new phpbb_config(array()); + $this->assertTrue($config->set_atomic('foo', false, '23')); + $this->assertEquals('23', $config['foo']); + } + + public function test_set_atomic_failure() + { + $config = new phpbb_config(array('foo' => 'bar')); + $this->assertFalse($config->set_atomic('foo', 'wrong', '23')); + $this->assertEquals('bar', $config['foo']); + } + + public function test_increment() + { + $config = new phpbb_config(array('foo' => '23')); + $config->increment('foo', 3); + $this->assertEquals(26, $config['foo']); + $config->increment('foo', 1); + $this->assertEquals(27, $config['foo']); + } +} diff --git a/tests/config/db_test.php b/tests/config/db_test.php new file mode 100644 index 0000000000..04277e869b --- /dev/null +++ b/tests/config/db_test.php @@ -0,0 +1,117 @@ +createXMLDataSet(__DIR__ . '/fixtures/config.xml'); + } + + public function setUp() + { + parent::setUp(); + + $this->cache = new phpbb_mock_cache; + $this->db = $this->new_dbal(); + $this->config = new phpbb_config_db($this->cache, $this->db, 'phpbb_config'); + } + + public function test_load_config() + { + $this->assertEquals('23', $this->config['foo']); + $this->assertEquals('42', $this->config['bar']); + } + + public function test_offset_set() + { + $this->config['foo'] = 'x'; // temporary set + $this->assertEquals('x', $this->config['foo']); + + $config2 = new phpbb_config_db($this->cache, $this->db, 'phpbb_config'); + $this->assertEquals('23', $config2['foo']); + } + + public function test_set_overwrite() + { + $this->config->set('foo', '17'); + $this->assertEquals('17', $this->config['foo']); + + // re-read config and populate cache + $config2 = new phpbb_config_db($this->cache, $this->db, 'phpbb_config'); + $this->cache->checkVar($this, 'config', array('foo' => '17')); + } + + public function test_set_overwrite_uncached() + { + $this->config->set('bar', '17', false); + + // re-read config and populate cache + $config2 = new phpbb_config_db($this->cache, $this->db, 'phpbb_config'); + $this->cache->checkVar($this, 'config', array('foo' => '23')); + } + + public function test_set_new() + { + $this->config->set('foobar', '5'); + $this->assertEquals('5', $this->config['foobar']); + + // re-read config and populate cache + $config2 = new phpbb_config_db($this->cache, $this->db, 'phpbb_config'); + $this->cache->checkVar($this, 'config', array('foo' => '23', 'foobar' => '5')); + } + + public function test_set_new_uncached() + { + $this->config->set('foobar', '5', false); + $this->assertEquals('5', $this->config['foobar']); + + // re-read config and populate cache + $config2 = new phpbb_config_db($this->cache, $this->db, 'phpbb_config'); + $this->cache->checkVar($this, 'config', array('foo' => '23')); + } + + public function test_set_atomic_overwrite() + { + $this->assertTrue($this->config->set_atomic('foo', '23', '17')); + $this->assertEquals('17', $this->config['foo']); + } + + public function test_set_atomic_new() + { + $this->assertTrue($this->config->set_atomic('foobar', false, '5')); + $this->assertEquals('5', $this->config['foobar']); + } + + public function test_set_atomic_failure() + { + $this->assertFalse($this->config->set_atomic('foo', 'wrong', '17')); + $this->assertEquals('23', $this->config['foo']); + } + + public function test_increment() + { + $this->config->increment('foo', 3); + $this->assertEquals(26, $this->config['foo']); + $this->config->increment('foo', 1); + $this->assertEquals(27, $this->config['foo']); + } + + public function test_increment_new() + { + $this->config->increment('foobar', 3); + $this->assertEquals(3, $this->config['foobar']);; + } +} diff --git a/tests/config/fixtures/config.xml b/tests/config/fixtures/config.xml new file mode 100644 index 0000000000..9d395b685c --- /dev/null +++ b/tests/config/fixtures/config.xml @@ -0,0 +1,18 @@ + + + + config_name + config_value + is_dynamic + + foo + 23 + 0 + + + bar + 42 + 1 + +
+
diff --git a/tests/mock/cache.php b/tests/mock/cache.php index 3bfb31f1be..dd29e0e9e3 100644 --- a/tests/mock/cache.php +++ b/tests/mock/cache.php @@ -37,6 +37,11 @@ class phpbb_mock_cache implements phpbb_cache_driver_interface $test->assertEquals($data, $this->data[$var_name]); } + public function checkVarUnset(PHPUnit_Framework_Assert $test, $var_name) + { + $test->assertFalse(isset($this->data[$var_name])); + } + public function check(PHPUnit_Framework_Assert $test, $data) { $test->assertEquals($data, $this->data); @@ -59,6 +64,7 @@ class phpbb_mock_cache implements phpbb_cache_driver_interface } function destroy($var_name, $table = '') { + unset($this->data[$var_name]); } public function _exists($var_name) { From 106f6800d4d898f079129e85fd6763c199dae9ec Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Tue, 11 Jan 2011 19:38:10 +0100 Subject: [PATCH 2/6] [task/config-class] Fix db config constructor param order PHPBB3-9988 --- phpBB/includes/config/db.php | 4 ++-- tests/config/db_test.php | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/phpBB/includes/config/db.php b/phpBB/includes/config/db.php index e6b7bffb73..1e07e1f67f 100644 --- a/phpBB/includes/config/db.php +++ b/phpBB/includes/config/db.php @@ -36,11 +36,11 @@ class phpbb_config_db extends phpbb_config /** * Creates a configuration container with a default set of values * - * @param phpbb_cache_driver_interface $cache Cache instance * @param dbal $db Database connection + * @param phpbb_cache_driver_interface $cache Cache instance * @param string $table Configuration table name */ - public function __construct(phpbb_cache_driver_interface $cache, dbal $db, $table) + public function __construct(dbal $db, phpbb_cache_driver_interface $cache, $table) { $this->db = $db; $this->cache = $cache; diff --git a/tests/config/db_test.php b/tests/config/db_test.php index 04277e869b..07f3bf8d5a 100644 --- a/tests/config/db_test.php +++ b/tests/config/db_test.php @@ -26,7 +26,7 @@ class phpbb_config_db_test extends phpbb_database_test_case $this->cache = new phpbb_mock_cache; $this->db = $this->new_dbal(); - $this->config = new phpbb_config_db($this->cache, $this->db, 'phpbb_config'); + $this->config = new phpbb_config_db($this->db, $this->cache, 'phpbb_config'); } public function test_load_config() @@ -40,7 +40,7 @@ class phpbb_config_db_test extends phpbb_database_test_case $this->config['foo'] = 'x'; // temporary set $this->assertEquals('x', $this->config['foo']); - $config2 = new phpbb_config_db($this->cache, $this->db, 'phpbb_config'); + $config2 = new phpbb_config_db($this->db, $this->cache, 'phpbb_config'); $this->assertEquals('23', $config2['foo']); } @@ -50,7 +50,7 @@ class phpbb_config_db_test extends phpbb_database_test_case $this->assertEquals('17', $this->config['foo']); // re-read config and populate cache - $config2 = new phpbb_config_db($this->cache, $this->db, 'phpbb_config'); + $config2 = new phpbb_config_db($this->db, $this->cache, 'phpbb_config'); $this->cache->checkVar($this, 'config', array('foo' => '17')); } @@ -59,7 +59,7 @@ class phpbb_config_db_test extends phpbb_database_test_case $this->config->set('bar', '17', false); // re-read config and populate cache - $config2 = new phpbb_config_db($this->cache, $this->db, 'phpbb_config'); + $config2 = new phpbb_config_db($this->db, $this->cache, 'phpbb_config'); $this->cache->checkVar($this, 'config', array('foo' => '23')); } @@ -69,7 +69,7 @@ class phpbb_config_db_test extends phpbb_database_test_case $this->assertEquals('5', $this->config['foobar']); // re-read config and populate cache - $config2 = new phpbb_config_db($this->cache, $this->db, 'phpbb_config'); + $config2 = new phpbb_config_db($this->db, $this->cache, 'phpbb_config'); $this->cache->checkVar($this, 'config', array('foo' => '23', 'foobar' => '5')); } @@ -79,7 +79,7 @@ class phpbb_config_db_test extends phpbb_database_test_case $this->assertEquals('5', $this->config['foobar']); // re-read config and populate cache - $config2 = new phpbb_config_db($this->cache, $this->db, 'phpbb_config'); + $config2 = new phpbb_config_db($this->db, $this->cache, 'phpbb_config'); $this->cache->checkVar($this, 'config', array('foo' => '23')); } From 6b1e343d8dae618afd0cb04173749f005c10e376 Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Tue, 11 Jan 2011 23:25:51 +0100 Subject: [PATCH 3/6] [task/config-class] Always specify the config table to use. PHPBB3-9988 --- phpBB/common.php | 2 +- phpBB/download/file.php | 2 +- .../plugins/phpbb_captcha_gd_plugin.php | 2 +- phpBB/install/database_update.php | 2 +- phpBB/install/install_convert.php | 12 +++++-- phpBB/install/install_install.php | 36 ++++++------------- phpBB/install/install_update.php | 4 ++- phpBB/style.php | 2 +- 8 files changed, 28 insertions(+), 34 deletions(-) diff --git a/phpBB/common.php b/phpBB/common.php index 0116516213..0c052ae415 100644 --- a/phpBB/common.php +++ b/phpBB/common.php @@ -227,7 +227,7 @@ $db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, defined(' unset($dbpasswd); // Grab global variables, re-cache if necessary -$config = new phpbb_config_db($db, $cache_factory->get_driver()); +$config = new phpbb_config_db($db, $cache_factory->get_driver(), CONFIG_TABLE); set_config(null, null, null, $config); set_config_count(null, null, null, $config); diff --git a/phpBB/download/file.php b/phpBB/download/file.php index 985a24e572..84abd3538e 100644 --- a/phpBB/download/file.php +++ b/phpBB/download/file.php @@ -70,7 +70,7 @@ if (isset($_GET['avatar'])) // worst-case default $browser = (!empty($_SERVER['HTTP_USER_AGENT'])) ? htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT']) : 'msie 6.0'; - $config = new phpbb_config_db($db, $cache_factory->get_driver()); + $config = new phpbb_config_db($db, $cache_factory->get_driver(), CONFIG_TABLE); set_config(null, null, null, $config); set_config_count(null, null, null, $config); diff --git a/phpBB/includes/captcha/plugins/phpbb_captcha_gd_plugin.php b/phpBB/includes/captcha/plugins/phpbb_captcha_gd_plugin.php index 1c82065a90..add8c3959f 100644 --- a/phpBB/includes/captcha/plugins/phpbb_captcha_gd_plugin.php +++ b/phpBB/includes/captcha/plugins/phpbb_captcha_gd_plugin.php @@ -154,7 +154,7 @@ class phpbb_captcha_gd extends phpbb_default_captcha $config_old = $config; $config = new phpbb_config(array()); - foreach ($old_config as $key => $value) + foreach ($config_old as $key => $value) { $config->set($key, $value); } diff --git a/phpBB/install/database_update.php b/phpBB/install/database_update.php index cecd565fa5..0969654084 100644 --- a/phpBB/install/database_update.php +++ b/phpBB/install/database_update.php @@ -165,7 +165,7 @@ include($phpbb_root_path . 'language/' . $language . '/install.' . $phpEx); $inline_update = (request_var('type', 0)) ? true : false; // To let set_config() calls succeed, we need to make the config array available globally -$config = new phpbb_config_db($db, $cache_factory->get_driver()); +$config = new phpbb_config_db($db, $cache_factory->get_driver(), CONFIG_TABLE); set_config(null, null, null, $config); set_config_count(null, null, null, $config); diff --git a/phpBB/install/install_convert.php b/phpBB/install/install_convert.php index 7aae5bf28c..7837353cfa 100644 --- a/phpBB/install/install_convert.php +++ b/phpBB/install/install_convert.php @@ -130,7 +130,9 @@ class install_convert extends module unset($dbpasswd); // We need to fill the config to let internal functions correctly work - $config = new phpbb_config_db($db, new phpbb_cache_driver_null); + $config = new phpbb_config_db($db, new phpbb_cache_driver_null, CONFIG_TABLE); + set_config(null, null, null, $config); + set_config_count(null, null, null, $config); // Detect if there is already a conversion in progress at this point and offer to resume // It's quite possible that the user will get disconnected during a large conversion so they need to be able to resume it @@ -341,7 +343,9 @@ class install_convert extends module $this->page_title = $lang['STAGE_SETTINGS']; // We need to fill the config to let internal functions correctly work - $config = new phpbb_config_db($db, new phpbb_cache_driver_null); + $config = new phpbb_config_db($db, new phpbb_cache_driver_null, CONFIG_TABLE); + set_config(null, null, null, $config); + set_config_count(null, null, null, $config); $convertor_tag = request_var('tag', ''); @@ -580,7 +584,9 @@ class install_convert extends module unset($dbpasswd); // We need to fill the config to let internal functions correctly work - $config = new phpbb_config_db($db, new phpbb_cache_driver_null); + $config = new phpbb_config_db($db, new phpbb_cache_driver_null, CONFIG_TABLE); + set_config(null, null, null, $config); + set_config_count(null, null, null, $config); // Override a couple of config variables for the duration $config['max_quote_depth'] = 0; diff --git a/phpBB/install/install_install.php b/phpBB/install/install_install.php index e47c8b8c01..01ed223c27 100644 --- a/phpBB/install/install_install.php +++ b/phpBB/install/install_install.php @@ -1461,7 +1461,9 @@ class install_install extends module include_once($phpbb_root_path . 'includes/search/fulltext_native.' . $phpEx); // We need to fill the config to let internal functions correctly work - $config = new phpbb_config_db($db, new phpbb_cache_driver_null); + $config = new phpbb_config_db($db, new phpbb_cache_driver_null, CONFIG_TABLE); + set_config(null, null, null, $config); + set_config_count(null, null, null, $config); $error = false; $search = new fulltext_native($error); @@ -1821,17 +1823,10 @@ class install_install extends module // Obtain any submitted data $data = $this->get_submitted_data(); - // Fill the config array - it is needed by those functions we call - $sql = 'SELECT * - FROM ' . CONFIG_TABLE; - $result = $db->sql_query($sql); - - $config = array(); - while ($row = $db->sql_fetchrow($result)) - { - $config[$row['config_name']] = $row['config_value']; - } - $db->sql_freeresult($result); + // We need to fill the config to let internal functions correctly work + $config = new phpbb_config_db($db, new phpbb_cache_driver_null, CONFIG_TABLE); + set_config(null, null, null, $config); + set_config_count(null, null, null, $config); $sql = 'SELECT group_id FROM ' . GROUPS_TABLE . " @@ -1898,19 +1893,10 @@ class install_install extends module $this->page_title = $lang['STAGE_FINAL']; - // Obtain any submitted data - $data = $this->get_submitted_data(); - - $sql = 'SELECT * - FROM ' . CONFIG_TABLE; - $result = $db->sql_query($sql); - - $config = array(); - while ($row = $db->sql_fetchrow($result)) - { - $config[$row['config_name']] = $row['config_value']; - } - $db->sql_freeresult($result); + // We need to fill the config to let internal functions correctly work + $config = new phpbb_config_db($db, new phpbb_cache_driver_null, CONFIG_TABLE); + set_config(null, null, null, $config); + set_config_count(null, null, null, $config); $user->session_begin(); $auth->login($data['admin_name'], $data['admin_pass1'], false, true, true); diff --git a/phpBB/install/install_update.php b/phpBB/install/install_update.php index 7c91b112a2..5c3a38d237 100644 --- a/phpBB/install/install_update.php +++ b/phpBB/install/install_update.php @@ -102,7 +102,9 @@ class install_update extends module unset($dbpasswd); // We need to fill the config to let internal functions correctly work - $config = new phpbb_config_db($db, new phpbb_cache_driver_null); + $config = new phpbb_config_db($db, new phpbb_cache_driver_null, CONFIG_TABLE); + set_config(null, null, null, $config); + set_config_count(null, null, null, $config); // Force template recompile $config['load_tplcompile'] = 1; diff --git a/phpBB/style.php b/phpBB/style.php index 09340992c5..88b6a54cbc 100644 --- a/phpBB/style.php +++ b/phpBB/style.php @@ -82,7 +82,7 @@ if ($id) } unset($dbpasswd); - $config = new phpbb_config_db($db, $cache_factory->get_driver()); + $config = new phpbb_config_db($db, $cache_factory->get_driver(), CONFIG_TABLE); set_config(null, null, null, $config); set_config_count(null, null, null, $config); From 129d3c53bc6f826ae9a9d484fda4f38f6963b375 Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Tue, 11 Jan 2011 23:55:10 +0100 Subject: [PATCH 4/6] [task/config-class] Correctly merge cached config with dynamically loaded data. PHPBB3-9988 --- phpBB/includes/config/db.php | 6 +++++- tests/config/db_test.php | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/phpBB/includes/config/db.php b/phpBB/includes/config/db.php index 1e07e1f67f..a753d12cd0 100644 --- a/phpBB/includes/config/db.php +++ b/phpBB/includes/config/db.php @@ -52,7 +52,11 @@ class phpbb_config_db extends phpbb_config FROM ' . $this->table . ' WHERE is_dynamic = 1'; $result = $this->db->sql_query($sql); - $config += $this->db->sql_fetchrowset($result); + + while ($row = $this->db->sql_fetchrow($result)) + { + $config[$row['config_name']] = $row['config_value']; + } $this->db->sql_freeresult($result); } else diff --git a/tests/config/db_test.php b/tests/config/db_test.php index 07f3bf8d5a..e291c05c8f 100644 --- a/tests/config/db_test.php +++ b/tests/config/db_test.php @@ -35,6 +35,17 @@ class phpbb_config_db_test extends phpbb_database_test_case $this->assertEquals('42', $this->config['bar']); } + public function test_load_cached() + { + $cache = new phpbb_mock_cache(array('config' => array('x' => 'y'))); + $this->config = new phpbb_config_db($this->db, $cache, 'phpbb_config'); + + $this->assertTrue(!isset($this->config['foo'])); + $this->assertEquals('42', $this->config['bar']); + + $this->assertEquals('y', $this->config['x']); + } + public function test_offset_set() { $this->config['foo'] = 'x'; // temporary set From 8e26f14eb619c35cabfbf548e0d20926c4dd2d01 Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Wed, 12 Jan 2011 00:16:01 +0100 Subject: [PATCH 5/6] [task/config-class] Add an attribute for the table name in phpbb_config_db. PHPBB3-9988 --- phpBB/includes/config/db.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/phpBB/includes/config/db.php b/phpBB/includes/config/db.php index a753d12cd0..74fb0504ce 100644 --- a/phpBB/includes/config/db.php +++ b/phpBB/includes/config/db.php @@ -33,6 +33,12 @@ class phpbb_config_db extends phpbb_config */ protected $db; + /** + * Name of the database table used for configuration. + * @var string + */ + protected $table; + /** * Creates a configuration container with a default set of values * From f11579549d0250733f4a2bd1759adc2db6d587d3 Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Wed, 12 Jan 2011 00:28:12 +0100 Subject: [PATCH 6/6] [task/config-class] Do not create multiple cache driver instances. Retrieve the driver from the service instead of creating new ones over and over from the factory. PHPBB3-9988 --- phpBB/common.php | 4 +- phpBB/download/file.php | 4 +- phpBB/includes/cache/service.php | 65 ++++++++++++++++++++----------- phpBB/install/database_update.php | 4 +- phpBB/install/index.php | 2 +- phpBB/style.php | 4 +- 6 files changed, 51 insertions(+), 32 deletions(-) diff --git a/phpBB/common.php b/phpBB/common.php index 0c052ae415..0ac7cbbd86 100644 --- a/phpBB/common.php +++ b/phpBB/common.php @@ -207,8 +207,8 @@ $class_loader->register(); // set up caching $cache_factory = new phpbb_cache_factory($acm_type); -$class_loader->set_cache($cache_factory->get_driver()); $cache = $cache_factory->get_service(); +$class_loader->set_cache($cache->get_driver()); // Instantiate some basic classes $request = new phpbb_request(); @@ -227,7 +227,7 @@ $db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, defined(' unset($dbpasswd); // Grab global variables, re-cache if necessary -$config = new phpbb_config_db($db, $cache_factory->get_driver(), CONFIG_TABLE); +$config = new phpbb_config_db($db, $cache->get_driver(), CONFIG_TABLE); set_config(null, null, null, $config); set_config_count(null, null, null, $config); diff --git a/phpBB/download/file.php b/phpBB/download/file.php index 84abd3538e..a169136734 100644 --- a/phpBB/download/file.php +++ b/phpBB/download/file.php @@ -55,8 +55,8 @@ if (isset($_GET['avatar'])) // set up caching $cache_factory = new phpbb_cache_factory($acm_type); - $class_loader->set_cache($cache_factory->get_driver()); $cache = $cache_factory->get_service(); + $class_loader->set_cache($cache->get_driver()); $db = new $sql_db(); @@ -70,7 +70,7 @@ if (isset($_GET['avatar'])) // worst-case default $browser = (!empty($_SERVER['HTTP_USER_AGENT'])) ? htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT']) : 'msie 6.0'; - $config = new phpbb_config_db($db, $cache_factory->get_driver(), CONFIG_TABLE); + $config = new phpbb_config_db($db, $cache->get_driver(), CONFIG_TABLE); set_config(null, null, null, $config); set_config_count(null, null, null, $config); diff --git a/phpBB/includes/cache/service.php b/phpBB/includes/cache/service.php index bcbb0ce872..68026c8647 100644 --- a/phpBB/includes/cache/service.php +++ b/phpBB/includes/cache/service.php @@ -2,7 +2,6 @@ /** * * @package acm -* @version $Id$ * @copyright (c) 2005 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * @@ -22,21 +21,41 @@ if (!defined('IN_PHPBB')) */ class phpbb_cache_service { - private $acm; + private $driver; - public function __construct(phpbb_cache_driver_interface $acm = null) + /** + * Creates a cache service around a cache driver + * + * @param phpbb_cache_driver_interface $driver The cache driver + */ + public function __construct(phpbb_cache_driver_interface $driver = null) { - $this->set_acm($acm); + $this->set_driver($driver); } - public function set_acm(phpbb_cache_driver_interface $acm) + /** + * Returns the cache driver used by this cache service. + * + * @return phpbb_cache_driver_interface The cache driver + */ + public function get_driver() { - $this->acm = $acm; + return $this->driver; + } + + /** + * Replaces the cache driver used by this cache service. + * + * @param phpbb_cache_driver_interface $driver The cache driver + */ + public function set_driver(phpbb_cache_driver_interface $driver) + { + $this->driver = $driver; } public function __call($method, $arguments) { - return call_user_func_array(array($this->acm, $method), $arguments); + return call_user_func_array(array($this->driver, $method), $arguments); } /** @@ -47,7 +66,7 @@ class phpbb_cache_service { global $db; - if (($censors = $this->acm->get('_word_censors')) === false) + if (($censors = $this->driver->get('_word_censors')) === false) { $sql = 'SELECT word, replacement FROM ' . WORDS_TABLE; @@ -61,7 +80,7 @@ class phpbb_cache_service } $db->sql_freeresult($result); - $this->acm->put('_word_censors', $censors); + $this->driver->put('_word_censors', $censors); } return $censors; @@ -72,7 +91,7 @@ class phpbb_cache_service */ function obtain_icons() { - if (($icons = $this->acm->get('_icons')) === false) + if (($icons = $this->driver->get('_icons')) === false) { global $db; @@ -92,7 +111,7 @@ class phpbb_cache_service } $db->sql_freeresult($result); - $this->acm->put('_icons', $icons); + $this->driver->put('_icons', $icons); } return $icons; @@ -103,7 +122,7 @@ class phpbb_cache_service */ function obtain_ranks() { - if (($ranks = $this->acm->get('_ranks')) === false) + if (($ranks = $this->driver->get('_ranks')) === false) { global $db; @@ -133,7 +152,7 @@ class phpbb_cache_service } $db->sql_freeresult($result); - $this->acm->put('_ranks', $ranks); + $this->driver->put('_ranks', $ranks); } return $ranks; @@ -148,7 +167,7 @@ class phpbb_cache_service */ function obtain_attach_extensions($forum_id) { - if (($extensions = $this->acm->get('_extensions')) === false) + if (($extensions = $this->driver->get('_extensions')) === false) { global $db; @@ -192,7 +211,7 @@ class phpbb_cache_service } $db->sql_freeresult($result); - $this->acm->put('_extensions', $extensions); + $this->driver->put('_extensions', $extensions); } // Forum post @@ -253,7 +272,7 @@ class phpbb_cache_service */ function obtain_bots() { - if (($bots = $this->acm->get('_bots')) === false) + if (($bots = $this->driver->get('_bots')) === false) { global $db; @@ -292,7 +311,7 @@ class phpbb_cache_service } $db->sql_freeresult($result); - $this->acm->put('_bots', $bots); + $this->driver->put('_bots', $bots); } return $bots; @@ -313,7 +332,7 @@ class phpbb_cache_service foreach ($parsed_items as $key => $parsed_array) { - $parsed_array = $this->acm->get('_cfg_' . $key . '_' . $theme[$key . '_path']); + $parsed_array = $this->driver->get('_cfg_' . $key . '_' . $theme[$key . '_path']); if ($parsed_array === false) { @@ -339,7 +358,7 @@ class phpbb_cache_service $parsed_array = parse_cfg_file($filename); $parsed_array['filetime'] = @filemtime($filename); - $this->acm->put('_cfg_' . $key . '_' . $theme[$key . '_path'], $parsed_array); + $this->driver->put('_cfg_' . $key . '_' . $theme[$key . '_path'], $parsed_array); } $parsed_items[$key] = $parsed_array; } @@ -352,7 +371,7 @@ class phpbb_cache_service */ function obtain_disallowed_usernames() { - if (($usernames = $this->acm->get('_disallowed_usernames')) === false) + if (($usernames = $this->driver->get('_disallowed_usernames')) === false) { global $db; @@ -367,7 +386,7 @@ class phpbb_cache_service } $db->sql_freeresult($result); - $this->acm->put('_disallowed_usernames', $usernames); + $this->driver->put('_disallowed_usernames', $usernames); } return $usernames; @@ -380,7 +399,7 @@ class phpbb_cache_service { global $phpbb_root_path, $phpEx; - if (($hook_files = $this->acm->get('_hooks')) === false) + if (($hook_files = $this->driver->get('_hooks')) === false) { $hook_files = array(); @@ -399,7 +418,7 @@ class phpbb_cache_service closedir($dh); } - $this->acm->put('_hooks', $hook_files); + $this->driver->put('_hooks', $hook_files); } return $hook_files; diff --git a/phpBB/install/database_update.php b/phpBB/install/database_update.php index 0969654084..4eedf7aa33 100644 --- a/phpBB/install/database_update.php +++ b/phpBB/install/database_update.php @@ -96,8 +96,8 @@ $class_loader->register(); // set up caching $cache_factory = new phpbb_cache_factory($acm_type); -$class_loader->set_cache($cache_factory->get_driver()); $cache = $cache_factory->get_service(); +$class_loader->set_cache($cache->get_driver()); $request = new phpbb_request(); $user = new user(); @@ -165,7 +165,7 @@ include($phpbb_root_path . 'language/' . $language . '/install.' . $phpEx); $inline_update = (request_var('type', 0)) ? true : false; // To let set_config() calls succeed, we need to make the config array available globally -$config = new phpbb_config_db($db, $cache_factory->get_driver(), CONFIG_TABLE); +$config = new phpbb_config_db($db, $cache->get_driver(), CONFIG_TABLE); set_config(null, null, null, $config); set_config_count(null, null, null, $config); diff --git a/phpBB/install/index.php b/phpBB/install/index.php index 18521e27d0..c8c38ba0fa 100644 --- a/phpBB/install/index.php +++ b/phpBB/install/index.php @@ -172,8 +172,8 @@ $class_loader->register(); // set up caching $cache_factory = new phpbb_cache_factory('file'); -$class_loader->set_cache($cache_factory->get_driver()); $cache = $cache_factory->get_service(); +$class_loader->set_cache($cache->get_driver()); $request = new phpbb_request(); diff --git a/phpBB/style.php b/phpBB/style.php index 88b6a54cbc..9f8b77c1f5 100644 --- a/phpBB/style.php +++ b/phpBB/style.php @@ -66,8 +66,8 @@ if ($id) // set up caching $cache_factory = new phpbb_cache_factory($acm_type); - $class_loader->set_cache($cache_factory->get_driver()); $cache = $cache_factory->get_service(); + $class_loader->set_cache($cache->get_driver()); $request = new phpbb_request(); $db = new $sql_db(); @@ -82,7 +82,7 @@ if ($id) } unset($dbpasswd); - $config = new phpbb_config_db($db, $cache_factory->get_driver(), CONFIG_TABLE); + $config = new phpbb_config_db($db, $cache->get_driver(), CONFIG_TABLE); set_config(null, null, null, $config); set_config_count(null, null, null, $config);