From c99ed6447371fc548442f8b44312954c43bfbcb6 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Wed, 14 Jan 2015 11:04:42 +0100 Subject: [ticket/13489] Disable the event dispatcher in the migrator PHPBB3-13489 --- phpBB/phpbb/db/migrator.php | 52 ++++++++++++++++++++++++++---- phpBB/phpbb/event/dispatcher.php | 35 ++++++++++++++++++++ phpBB/phpbb/event/dispatcher_interface.php | 10 ++++++ 3 files changed, 91 insertions(+), 6 deletions(-) diff --git a/phpBB/phpbb/db/migrator.php b/phpBB/phpbb/db/migrator.php index d03496eae3..7fc3e787e2 100644 --- a/phpBB/phpbb/db/migrator.php +++ b/phpBB/phpbb/db/migrator.php @@ -13,11 +13,19 @@ namespace phpbb\db; +use Symfony\Component\DependencyInjection\ContainerAwareInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; + /** * The migrator is responsible for applying new migrations in the correct order. */ class migrator { + /** + * @var ContainerInterface + */ + protected $container; + /** @var \phpbb\config\config */ protected $config; @@ -77,15 +85,16 @@ class migrator /** * The output handler. A null handler is configured by default. * - * @var migrator_output_handler + * @var migrator_output_handler_interface */ public $output_handler; /** * Constructor of the database migrator */ - public function __construct(\phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\db\tools $db_tools, $migrations_table, $phpbb_root_path, $php_ext, $table_prefix, $tools, \phpbb\db\migration\helper $helper) + public function __construct(ContainerInterface $container, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\db\tools $db_tools, $migrations_table, $phpbb_root_path, $php_ext, $table_prefix, $tools, \phpbb\db\migration\helper $helper) { + $this->container = $container; $this->config = $config; $this->db = $db; $this->db_tools = $db_tools; @@ -171,6 +180,18 @@ class migrator * @return null */ public function update() + { + $this->container->get('dispatcher')->disable(); + $this->update_do(); + $this->container->get('dispatcher')->enable(); + } + + /** + * Effectively runs a single update step from the next migration to be applied. + * + * @return null + */ + protected function update_do() { foreach ($this->migrations as $name) { @@ -317,7 +338,7 @@ class migrator catch (\phpbb\db\migration\exception $e) { // Revert the schema changes - $this->revert($name); + $this->revert_do($name); // Rethrow exception throw $e; @@ -337,9 +358,21 @@ class migrator * check if revert() needs to be called again use the migration_state() method. * * @param string $migration String migration name to revert (including any that depend on this migration) - * @return null */ public function revert($migration) + { + $this->container->get('dispatcher')->disable(); + $this->revert_do($migration); + $this->container->get('dispatcher')->enable(); + } + + /** + * Effectively runs a single revert step from the last migration installed + * + * @param string $migration String migration name to revert (including any that depend on this migration) + * @return null + */ + protected function revert_do($migration) { if (!isset($this->migration_state[$migration])) { @@ -351,7 +384,7 @@ class migrator { if (!empty($state['migration_depends_on']) && in_array($migration, $state['migration_depends_on'])) { - $this->revert($name); + $this->revert_do($name); } } @@ -742,7 +775,14 @@ class migrator */ protected function get_migration($name) { - return new $name($this->config, $this->db, $this->db_tools, $this->phpbb_root_path, $this->php_ext, $this->table_prefix); + $migration = new $name($this->config, $this->db, $this->db_tools, $this->phpbb_root_path, $this->php_ext, $this->table_prefix); + + if ($migration instanceof ContainerAwareInterface) + { + $migration->setContainer($this->container); + } + + return $migration; } /** diff --git a/phpBB/phpbb/event/dispatcher.php b/phpBB/phpbb/event/dispatcher.php index 9a786022c2..1c4abeb108 100644 --- a/phpBB/phpbb/event/dispatcher.php +++ b/phpBB/phpbb/event/dispatcher.php @@ -14,6 +14,7 @@ namespace phpbb\event; use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher; +use Symfony\Component\EventDispatcher\Event; /** * Extension of the Symfony2 EventDispatcher @@ -31,6 +32,11 @@ use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher; */ class dispatcher extends ContainerAwareEventDispatcher implements dispatcher_interface { + /** + * @var bool + */ + protected $disabled = false; + /** * {@inheritdoc} */ @@ -40,4 +46,33 @@ class dispatcher extends ContainerAwareEventDispatcher implements dispatcher_int $this->dispatch($eventName, $event); return $event->get_data_filtered(array_keys($data)); } + + /** + * {@inheritdoc} + */ + public function dispatch($eventName, Event $event = null) + { + if ($this->disabled) + { + return $event; + } + + return parent::dispatch($eventName, $event); + } + + /** + * {@inheritdoc} + */ + public function disable() + { + $this->disabled = true; + } + + /** + * {@inheritdoc} + */ + public function enable() + { + $this->disabled = false; + } } diff --git a/phpBB/phpbb/event/dispatcher_interface.php b/phpBB/phpbb/event/dispatcher_interface.php index 50a3ef9101..c66aa98260 100644 --- a/phpBB/phpbb/event/dispatcher_interface.php +++ b/phpBB/phpbb/event/dispatcher_interface.php @@ -37,4 +37,14 @@ interface dispatcher_interface extends \Symfony\Component\EventDispatcher\EventD * @return mixed */ public function trigger_event($eventName, $data = array()); + + /** + * Disable the event dispatcher. + */ + public function disable(); + + /** + * Enable the event dispatcher. + */ + public function enable(); } -- cgit v1.2.1 From 4df125625c7df93a5c3727a3cfd9517df4ba0ba6 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Wed, 14 Jan 2015 11:37:10 +0100 Subject: [ticket/13489] Fix tests PHPBB3-13489 --- tests/dbal/migrator_test.php | 8 +++++--- tests/extension/manager_test.php | 4 +++- tests/extension/metadata_manager_test.php | 4 +++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/tests/dbal/migrator_test.php b/tests/dbal/migrator_test.php index 10a9444d63..4c4306888c 100644 --- a/tests/dbal/migrator_test.php +++ b/tests/dbal/migrator_test.php @@ -46,7 +46,10 @@ class phpbb_dbal_migrator_test extends phpbb_database_test_case new \phpbb\db\migration\tool\config($this->config), ); + $container = new phpbb_mock_container_builder(); + $this->migrator = new \phpbb\db\migrator( + $container, $this->config, $this->db, $this->db_tools, @@ -57,9 +60,8 @@ class phpbb_dbal_migrator_test extends phpbb_database_test_case $tools, new \phpbb\db\migration\helper() ); - - $container = new phpbb_mock_container_builder(); - $container->set('migrator', $migrator); + $container->set('migrator', $this->migrator); + $container->set('dispatcher', new phpbb_mock_event_dispatcher()); $user = new \phpbb\user('\phpbb\datetime'); $this->extension_manager = new \phpbb\extension\manager( diff --git a/tests/extension/manager_test.php b/tests/extension/manager_test.php index 5c7cad89f6..0eeb060936 100644 --- a/tests/extension/manager_test.php +++ b/tests/extension/manager_test.php @@ -156,7 +156,10 @@ class phpbb_extension_manager_test extends phpbb_database_test_case $table_prefix = 'phpbb_'; $user = new \phpbb\user('\phpbb\user'); + $container = new phpbb_mock_container_builder(); + $migrator = new \phpbb\db\migrator( + $container, $config, $db, $db_tools, @@ -167,7 +170,6 @@ class phpbb_extension_manager_test extends phpbb_database_test_case array(), new \phpbb\db\migration\helper() ); - $container = new phpbb_mock_container_builder(); $container->set('migrator', $migrator); return new \phpbb\extension\manager( diff --git a/tests/extension/metadata_manager_test.php b/tests/extension/metadata_manager_test.php index fab1d3af3a..2a746d3792 100644 --- a/tests/extension/metadata_manager_test.php +++ b/tests/extension/metadata_manager_test.php @@ -62,7 +62,10 @@ class phpbb_extension_metadata_manager_test extends phpbb_database_test_case new \phpbb\template\context() ); + $container = new phpbb_mock_container_builder(); + $this->migrator = new \phpbb\db\migrator( + $container, $this->config, $this->db, $this->db_tools, @@ -73,7 +76,6 @@ class phpbb_extension_metadata_manager_test extends phpbb_database_test_case array(), new \phpbb\db\migration\helper() ); - $container = new phpbb_mock_container_builder(); $container->set('migrator', $this->migrator); $this->extension_manager = new \phpbb\extension\manager( -- cgit v1.2.1 From ce47170c29f4bd7b6a9a4dc74f95be5b4241872d Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Wed, 14 Jan 2015 12:07:53 +0100 Subject: [ticket/13489] Update migrations PHPBB3-13489 --- .../db/migration/data/v30x/release_3_0_5_rc1.php | 22 +++++++++++--- .../data/v310/soft_delete_mod_convert.php | 34 +++++++++++++--------- .../db/migration/profilefield_base_migration.php | 21 +++++++++++-- 3 files changed, 57 insertions(+), 20 deletions(-) diff --git a/phpBB/phpbb/db/migration/data/v30x/release_3_0_5_rc1.php b/phpBB/phpbb/db/migration/data/v30x/release_3_0_5_rc1.php index 2cc7786046..dc79071e16 100644 --- a/phpBB/phpbb/db/migration/data/v30x/release_3_0_5_rc1.php +++ b/phpBB/phpbb/db/migration/data/v30x/release_3_0_5_rc1.php @@ -13,8 +13,16 @@ namespace phpbb\db\migration\data\v30x; -class release_3_0_5_rc1 extends \phpbb\db\migration\migration +use Symfony\Component\DependencyInjection\ContainerAwareInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; + +class release_3_0_5_rc1 extends \phpbb\db\migration\migration implements ContainerAwareInterface { + /** + * @var ContainerInterface + */ + protected $container; + public function effectively_installed() { return phpbb_version_compare($this->config['version'], '3.0.5-RC1', '>='); @@ -55,9 +63,7 @@ class release_3_0_5_rc1 extends \phpbb\db\migration\migration public function hash_old_passwords() { - global $phpbb_container; - - $passwords_manager = $phpbb_container->get('passwords.manager'); + $passwords_manager = $this->container->get('passwords.manager'); $sql = 'SELECT user_id, user_password FROM ' . $this->table_prefix . 'users WHERE user_pass_convert = 1'; @@ -130,4 +136,12 @@ class release_3_0_5_rc1 extends \phpbb\db\migration\migration } } } + + /** + * {@inheritdoc} + */ + public function setContainer(ContainerInterface $container = null) + { + $this->container = $container; + } } diff --git a/phpBB/phpbb/db/migration/data/v310/soft_delete_mod_convert.php b/phpBB/phpbb/db/migration/data/v310/soft_delete_mod_convert.php index 58845b88ec..c21b75b881 100644 --- a/phpBB/phpbb/db/migration/data/v310/soft_delete_mod_convert.php +++ b/phpBB/phpbb/db/migration/data/v310/soft_delete_mod_convert.php @@ -13,13 +13,21 @@ namespace phpbb\db\migration\data\v310; +use Symfony\Component\DependencyInjection\ContainerAwareInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; + /** * Migration to convert the Soft Delete MOD for 3.0 * * https://www.phpbb.com/customise/db/mod/soft_delete/ */ -class soft_delete_mod_convert extends \phpbb\db\migration\migration +class soft_delete_mod_convert extends \phpbb\db\migration\migration implements ContainerAwareInterface { + /** + * @var ContainerInterface + */ + protected $container; + static public function depends_on() { return array( @@ -115,19 +123,19 @@ class soft_delete_mod_convert extends \phpbb\db\migration\migration } } + /** + * @return \phpbb\content_visibility + */ protected function get_content_visibility() { - return new \phpbb\content_visibility( - new \phpbb\auth\auth(), - $this->config, - $this->db, - new \phpbb\user('\phpbb\datetime'), - $this->phpbb_root_path, - $this->php_ext, - $this->table_prefix . 'forums', - $this->table_prefix . 'posts', - $this->table_prefix . 'topics', - $this->table_prefix . 'users' - ); + return $this->container->get('content.visibility'); + } + + /** + * {@inheritdoc} + */ + public function setContainer(ContainerInterface $container = null) + { + $this->container = $container; } } diff --git a/phpBB/phpbb/db/migration/profilefield_base_migration.php b/phpBB/phpbb/db/migration/profilefield_base_migration.php index 9000949a7d..02954d109c 100644 --- a/phpBB/phpbb/db/migration/profilefield_base_migration.php +++ b/phpBB/phpbb/db/migration/profilefield_base_migration.php @@ -13,7 +13,10 @@ namespace phpbb\db\migration; -abstract class profilefield_base_migration extends \phpbb\db\migration\migration +use Symfony\Component\DependencyInjection\ContainerAwareInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; + +abstract class profilefield_base_migration extends \phpbb\db\migration\migration implements ContainerAwareInterface { protected $profilefield_name; @@ -40,6 +43,11 @@ abstract class profilefield_base_migration extends \phpbb\db\migration\migration protected $user_column_name; + /** + * @var ContainerInterface + */ + protected $container; + public function effectively_installed() { return $this->db_tools->sql_column_exists($this->table_prefix . 'profile_fields_data', 'pf_' . $this->profilefield_name); @@ -237,11 +245,18 @@ abstract class profilefield_base_migration extends \phpbb\db\migration\migration if ($profile_row === null) { - global $phpbb_container; - $manager = $phpbb_container->get('profilefields.manager'); + $manager = $this->container->get('profilefields.manager'); $profile_row = $manager->build_insert_sql_array(array()); } return $profile_row; } + + /** + * {@inheritdoc} + */ + public function setContainer(ContainerInterface $container = null) + { + $this->container = $container; + } } -- cgit v1.2.1 From dab07283573689fcc6eb2c4e270ac37c3a573959 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Wed, 14 Jan 2015 12:32:47 +0100 Subject: [ticket/13489] Fix service configuration PHPBB3-13489 --- phpBB/config/db.yml | 1 + .../db/migration/container_aware_migration.php | 36 ++++++++++++++++++++++ .../db/migration/data/v30x/release_3_0_5_rc1.php | 18 ++--------- .../data/v310/soft_delete_mod_convert.php | 18 ++--------- .../db/migration/profilefield_base_migration.php | 18 +---------- .../test_framework/phpbb_functional_test_case.php | 4 ++- 6 files changed, 45 insertions(+), 50 deletions(-) create mode 100644 phpBB/phpbb/db/migration/container_aware_migration.php diff --git a/phpBB/config/db.yml b/phpBB/config/db.yml index b3f1b485ea..d11669d8a3 100644 --- a/phpBB/config/db.yml +++ b/phpBB/config/db.yml @@ -18,6 +18,7 @@ services: migrator: class: phpbb\db\migrator arguments: + - @service_container - @config - @dbal.conn - @dbal.tools diff --git a/phpBB/phpbb/db/migration/container_aware_migration.php b/phpBB/phpbb/db/migration/container_aware_migration.php new file mode 100644 index 0000000000..3b4b49b04b --- /dev/null +++ b/phpBB/phpbb/db/migration/container_aware_migration.php @@ -0,0 +1,36 @@ + +* @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\db\migration; + +use Symfony\Component\DependencyInjection\ContainerAwareInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; + +/** +* Abstract base class for container aware database migrations. +*/ +abstract class container_aware_migration extends migration implements ContainerAwareInterface +{ + /** + * @var ContainerInterface + */ + protected $container; + + /** + * {@inheritdoc} + */ + public function setContainer(ContainerInterface $container = null) + { + $this->container = $container; + } +} diff --git a/phpBB/phpbb/db/migration/data/v30x/release_3_0_5_rc1.php b/phpBB/phpbb/db/migration/data/v30x/release_3_0_5_rc1.php index dc79071e16..c3e887841f 100644 --- a/phpBB/phpbb/db/migration/data/v30x/release_3_0_5_rc1.php +++ b/phpBB/phpbb/db/migration/data/v30x/release_3_0_5_rc1.php @@ -13,16 +13,10 @@ namespace phpbb\db\migration\data\v30x; -use Symfony\Component\DependencyInjection\ContainerAwareInterface; -use Symfony\Component\DependencyInjection\ContainerInterface; +use phpbb\db\migration\container_aware_migration; -class release_3_0_5_rc1 extends \phpbb\db\migration\migration implements ContainerAwareInterface +class release_3_0_5_rc1 extends container_aware_migration { - /** - * @var ContainerInterface - */ - protected $container; - public function effectively_installed() { return phpbb_version_compare($this->config['version'], '3.0.5-RC1', '>='); @@ -136,12 +130,4 @@ class release_3_0_5_rc1 extends \phpbb\db\migration\migration implements Contain } } } - - /** - * {@inheritdoc} - */ - public function setContainer(ContainerInterface $container = null) - { - $this->container = $container; - } } diff --git a/phpBB/phpbb/db/migration/data/v310/soft_delete_mod_convert.php b/phpBB/phpbb/db/migration/data/v310/soft_delete_mod_convert.php index c21b75b881..85b90da5fa 100644 --- a/phpBB/phpbb/db/migration/data/v310/soft_delete_mod_convert.php +++ b/phpBB/phpbb/db/migration/data/v310/soft_delete_mod_convert.php @@ -13,21 +13,15 @@ namespace phpbb\db\migration\data\v310; -use Symfony\Component\DependencyInjection\ContainerAwareInterface; -use Symfony\Component\DependencyInjection\ContainerInterface; +use phpbb\db\migration\container_aware_migration; /** * Migration to convert the Soft Delete MOD for 3.0 * * https://www.phpbb.com/customise/db/mod/soft_delete/ */ -class soft_delete_mod_convert extends \phpbb\db\migration\migration implements ContainerAwareInterface +class soft_delete_mod_convert extends container_aware_migration { - /** - * @var ContainerInterface - */ - protected $container; - static public function depends_on() { return array( @@ -130,12 +124,4 @@ class soft_delete_mod_convert extends \phpbb\db\migration\migration implements C { return $this->container->get('content.visibility'); } - - /** - * {@inheritdoc} - */ - public function setContainer(ContainerInterface $container = null) - { - $this->container = $container; - } } diff --git a/phpBB/phpbb/db/migration/profilefield_base_migration.php b/phpBB/phpbb/db/migration/profilefield_base_migration.php index 02954d109c..da1a38e2fa 100644 --- a/phpBB/phpbb/db/migration/profilefield_base_migration.php +++ b/phpBB/phpbb/db/migration/profilefield_base_migration.php @@ -13,10 +13,7 @@ namespace phpbb\db\migration; -use Symfony\Component\DependencyInjection\ContainerAwareInterface; -use Symfony\Component\DependencyInjection\ContainerInterface; - -abstract class profilefield_base_migration extends \phpbb\db\migration\migration implements ContainerAwareInterface +abstract class profilefield_base_migration extends container_aware_migration { protected $profilefield_name; @@ -43,11 +40,6 @@ abstract class profilefield_base_migration extends \phpbb\db\migration\migration protected $user_column_name; - /** - * @var ContainerInterface - */ - protected $container; - public function effectively_installed() { return $this->db_tools->sql_column_exists($this->table_prefix . 'profile_fields_data', 'pf_' . $this->profilefield_name); @@ -251,12 +243,4 @@ abstract class profilefield_base_migration extends \phpbb\db\migration\migration return $profile_row; } - - /** - * {@inheritdoc} - */ - public function setContainer(ContainerInterface $container = null) - { - $this->container = $container; - } } diff --git a/tests/test_framework/phpbb_functional_test_case.php b/tests/test_framework/phpbb_functional_test_case.php index 51bae7a723..6c045712ab 100644 --- a/tests/test_framework/phpbb_functional_test_case.php +++ b/tests/test_framework/phpbb_functional_test_case.php @@ -227,7 +227,9 @@ class phpbb_functional_test_case extends phpbb_test_case $db = $this->get_db(); $db_tools = new \phpbb\db\tools($db); + $container = new phpbb_mock_container_builder(); $migrator = new \phpbb\db\migrator( + $container, $config, $db, $db_tools, @@ -238,8 +240,8 @@ class phpbb_functional_test_case extends phpbb_test_case array(), new \phpbb\db\migration\helper() ); - $container = new phpbb_mock_container_builder(); $container->set('migrator', $migrator); + $container->set('dispatcher', new phpbb_mock_event_dispatcher()); $user = new \phpbb\user('\phpbb\datetime'); $extension_manager = new \phpbb\extension\manager( -- cgit v1.2.1