From a0124b45f9dab8a0f7d4879d4ea147b414b25bf2 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sun, 20 Sep 2015 13:11:41 -0400 Subject: Add sub namespace for plugins --- app/Core/Plugin/Base.php | 47 +++++++++++ app/Core/Plugin/Loader.php | 138 +++++++++++++++++++++++++++++++++ app/Core/PluginBase.php | 47 ----------- app/Core/PluginLoader.php | 137 -------------------------------- app/common.php | 2 +- doc/plugins.markdown | 8 +- tests/units/Core/Plugin/LoaderTest.php | 23 ++++++ tests/units/Core/PluginLoaderTest.php | 23 ------ 8 files changed, 213 insertions(+), 212 deletions(-) create mode 100644 app/Core/Plugin/Base.php create mode 100644 app/Core/Plugin/Loader.php delete mode 100644 app/Core/PluginBase.php delete mode 100644 app/Core/PluginLoader.php create mode 100644 tests/units/Core/Plugin/LoaderTest.php delete mode 100644 tests/units/Core/PluginLoaderTest.php diff --git a/app/Core/Plugin/Base.php b/app/Core/Plugin/Base.php new file mode 100644 index 00000000..580d41ad --- /dev/null +++ b/app/Core/Plugin/Base.php @@ -0,0 +1,47 @@ +container; + + $this->container['dispatcher']->addListener($event, function() use ($container, $callback) { + call_user_func($callback, $container); + }); + } +} diff --git a/app/Core/Plugin/Loader.php b/app/Core/Plugin/Loader.php new file mode 100644 index 00000000..ffead9f6 --- /dev/null +++ b/app/Core/Plugin/Loader.php @@ -0,0 +1,138 @@ +isDot() && $fileinfo->isDir()) { + $plugin = $fileinfo->getFilename(); + $this->loadSchema($plugin); + $this->load($plugin); + } + } + } + } + + /** + * Load plugin + * + * @access public + */ + public function load($plugin) + { + $class = '\Plugin\\'.$plugin.'\\Plugin'; + $instance = new $class($this->container); + + Tool::buildDic($this->container, $instance->getClasses()); + + $instance->initialize(); + } + + /** + * Load plugin schema + * + * @access public + * @param string $plugin + */ + public function loadSchema($plugin) + { + $filename = __DIR__.'/../../../plugins/'.$plugin.'/Schema/'.ucfirst(DB_DRIVER).'.php'; + + if (file_exists($filename)) { + require($filename); + $this->migrateSchema($plugin); + } + } + + /** + * Execute plugin schema migrations + * + * @access public + * @param string $plugin + */ + public function migrateSchema($plugin) + { + $last_version = constant('\Plugin\\'.$plugin.'\Schema\VERSION'); + $current_version = $this->getSchemaVersion($plugin); + + try { + + $this->db->startTransaction(); + $this->db->getDriver()->disableForeignKeys(); + + for ($i = $current_version + 1; $i <= $last_version; $i++) { + $function_name = '\Plugin\\'.$plugin.'\Schema\version_'.$i; + + if (function_exists($function_name)) { + call_user_func($function_name, $this->db->getConnection()); + } + } + + $this->db->getDriver()->enableForeignKeys(); + $this->db->closeTransaction(); + $this->setSchemaVersion($plugin, $i - 1); + } + catch (PDOException $e) { + $this->db->cancelTransaction(); + $this->db->getDriver()->enableForeignKeys(); + die('Unable to migrate schema for the plugin: '.$plugin.' => '.$e->getMessage()); + } + } + + /** + * Get current plugin schema version + * + * @access public + * @param string $plugin + * @return integer + */ + public function getSchemaVersion($plugin) + { + return (int) $this->db->table(self::TABLE_SCHEMA)->eq('plugin', strtolower($plugin))->findOneColumn('version'); + } + + /** + * Save last plugin schema version + * + * @access public + * @param string $plugin + * @param integer $version + * @return boolean + */ + public function setSchemaVersion($plugin, $version) + { + $dictionary = array( + strtolower($plugin) => $version + ); + + return $this->db->getDriver()->upsert(self::TABLE_SCHEMA, 'plugin', 'version', $dictionary); + } +} diff --git a/app/Core/PluginBase.php b/app/Core/PluginBase.php deleted file mode 100644 index 457afa03..00000000 --- a/app/Core/PluginBase.php +++ /dev/null @@ -1,47 +0,0 @@ -container; - - $this->container['dispatcher']->addListener($event, function() use ($container, $callback) { - call_user_func($callback, $container); - }); - } -} diff --git a/app/Core/PluginLoader.php b/app/Core/PluginLoader.php deleted file mode 100644 index c7c254f7..00000000 --- a/app/Core/PluginLoader.php +++ /dev/null @@ -1,137 +0,0 @@ -isDot() && $fileinfo->isDir()) { - $plugin = $fileinfo->getFilename(); - $this->loadSchema($plugin); - $this->load($plugin); - } - } - } - } - - /** - * Load plugin - * - * @access public - */ - public function load($plugin) - { - $class = '\Plugin\\'.$plugin.'\\Plugin'; - $instance = new $class($this->container); - - Tool::buildDic($this->container, $instance->getClasses()); - - $instance->initialize(); - } - - /** - * Load plugin schema - * - * @access public - * @param string $plugin - */ - public function loadSchema($plugin) - { - $filename = __DIR__.'/../../plugins/'.$plugin.'/Schema/'.ucfirst(DB_DRIVER).'.php'; - - if (file_exists($filename)) { - require($filename); - $this->migrateSchema($plugin); - } - } - - /** - * Execute plugin schema migrations - * - * @access public - * @param string $plugin - */ - public function migrateSchema($plugin) - { - $last_version = constant('\Plugin\\'.$plugin.'\Schema\VERSION'); - $current_version = $this->getSchemaVersion($plugin); - - try { - - $this->db->startTransaction(); - $this->db->getDriver()->disableForeignKeys(); - - for ($i = $current_version + 1; $i <= $last_version; $i++) { - $function_name = '\Plugin\\'.$plugin.'\Schema\version_'.$i; - - if (function_exists($function_name)) { - call_user_func($function_name, $this->db->getConnection()); - } - } - - $this->db->getDriver()->enableForeignKeys(); - $this->db->closeTransaction(); - $this->setSchemaVersion($plugin, $i - 1); - } - catch (PDOException $e) { - $this->db->cancelTransaction(); - $this->db->getDriver()->enableForeignKeys(); - die('Unable to migrate schema for the plugin: '.$plugin.' => '.$e->getMessage()); - } - } - - /** - * Get current plugin schema version - * - * @access public - * @param string $plugin - * @return integer - */ - public function getSchemaVersion($plugin) - { - return (int) $this->db->table(self::TABLE_SCHEMA)->eq('plugin', strtolower($plugin))->findOneColumn('version'); - } - - /** - * Save last plugin schema version - * - * @access public - * @param string $plugin - * @param integer $version - * @return boolean - */ - public function setSchemaVersion($plugin, $version) - { - $dictionary = array( - strtolower($plugin) => $version - ); - - return $this->db->getDriver()->upsert(self::TABLE_SCHEMA, 'plugin', 'version', $dictionary); - } -} diff --git a/app/common.php b/app/common.php index ea38ab36..dcd571bc 100644 --- a/app/common.php +++ b/app/common.php @@ -33,5 +33,5 @@ if (ENABLE_URL_REWRITE) { require __DIR__.'/routes.php'; } -$plugin = new Core\PluginLoader($container); +$plugin = new Core\Plugin\Loader($container); $plugin->scan(); diff --git a/doc/plugins.markdown b/doc/plugins.markdown index 7f32cf0a..4388f7c6 100644 --- a/doc/plugins.markdown +++ b/doc/plugins.markdown @@ -47,9 +47,9 @@ Example of `Plugin.php` file (`plugins/Foobar/Plugin.php`): namespace Plugin\Foobar; -use Core\PluginBase; +use Core\Plugin\Base; -class Plugin extends PluginBase +class Plugin extends Plugin\Base { public function initialize() { @@ -58,14 +58,14 @@ class Plugin extends PluginBase } ``` -This file should contains a class `Plugin` defined under the namespace `Plugin\Yourplugin` and extends `Core\PluginBase`. +This file should contains a class `Plugin` defined under the namespace `Plugin\Yourplugin` and extends `Core\Plugin\Base`. The only required method is `initialize()`. This method is called for each request when the plugin is loaded. Plugin methods -------------- -Available methods from `PluginBase`: +Available methods from `Plugin\Base`: - `initialize()`: Executed when the plugin is loaded - `getClasses()`: Return all classes that should be stored in the dependency injection container diff --git a/tests/units/Core/Plugin/LoaderTest.php b/tests/units/Core/Plugin/LoaderTest.php new file mode 100644 index 00000000..40c23fbb --- /dev/null +++ b/tests/units/Core/Plugin/LoaderTest.php @@ -0,0 +1,23 @@ +container); + $this->assertEquals(0, $p->getSchemaVersion('not_found')); + + $this->assertTrue($p->setSchemaVersion('plugin1', 1)); + $this->assertEquals(1, $p->getSchemaVersion('plugin1')); + + $this->assertTrue($p->setSchemaVersion('plugin2', 33)); + $this->assertEquals(33, $p->getSchemaVersion('plugin2')); + + $this->assertTrue($p->setSchemaVersion('plugin1', 2)); + $this->assertEquals(2, $p->getSchemaVersion('plugin1')); + } +} diff --git a/tests/units/Core/PluginLoaderTest.php b/tests/units/Core/PluginLoaderTest.php deleted file mode 100644 index 62327f01..00000000 --- a/tests/units/Core/PluginLoaderTest.php +++ /dev/null @@ -1,23 +0,0 @@ -container); - $this->assertEquals(0, $p->getSchemaVersion('not_found')); - - $this->assertTrue($p->setSchemaVersion('plugin1', 1)); - $this->assertEquals(1, $p->getSchemaVersion('plugin1')); - - $this->assertTrue($p->setSchemaVersion('plugin2', 33)); - $this->assertEquals(33, $p->getSchemaVersion('plugin2')); - - $this->assertTrue($p->setSchemaVersion('plugin1', 2)); - $this->assertEquals(2, $p->getSchemaVersion('plugin1')); - } -} -- cgit v1.2.3