Improve plugin loading
This commit is contained in:
@@ -5,8 +5,8 @@ namespace Kanboard\Core\Plugin;
|
|||||||
/**
|
/**
|
||||||
* Plugin Base class
|
* Plugin Base class
|
||||||
*
|
*
|
||||||
* @package plugin
|
* @package Kanboard\Core\Plugin
|
||||||
* @author Frederic Guillot
|
* @author Frederic Guillot
|
||||||
*/
|
*/
|
||||||
abstract class Base extends \Kanboard\Core\Base
|
abstract class Base extends \Kanboard\Core\Base
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ namespace Kanboard\Core\Plugin;
|
|||||||
/**
|
/**
|
||||||
* Plugin Hooks Handler
|
* Plugin Hooks Handler
|
||||||
*
|
*
|
||||||
* @package plugin
|
* @package Kanboard\Core\Plugin
|
||||||
* @author Frederic Guillot
|
* @author Frederic Guillot
|
||||||
*/
|
*/
|
||||||
class Hook
|
class Hook
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -4,26 +4,17 @@ namespace Kanboard\Core\Plugin;
|
|||||||
|
|
||||||
use Composer\Autoload\ClassLoader;
|
use Composer\Autoload\ClassLoader;
|
||||||
use DirectoryIterator;
|
use DirectoryIterator;
|
||||||
use PDOException;
|
|
||||||
use LogicException;
|
use LogicException;
|
||||||
use RuntimeException;
|
|
||||||
use Kanboard\Core\Tool;
|
use Kanboard\Core\Tool;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Plugin Loader
|
* Plugin Loader
|
||||||
*
|
*
|
||||||
* @package plugin
|
* @package Kanboard\Core\Plugin
|
||||||
* @author Frederic Guillot
|
* @author Frederic Guillot
|
||||||
*/
|
*/
|
||||||
class Loader extends \Kanboard\Core\Base
|
class Loader extends \Kanboard\Core\Base
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Schema version table for plugins
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
const TABLE_SCHEMA = 'plugin_schema_versions';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Plugin instances
|
* Plugin instances
|
||||||
*
|
*
|
||||||
@@ -32,6 +23,17 @@ class Loader extends \Kanboard\Core\Base
|
|||||||
*/
|
*/
|
||||||
public $plugins = array();
|
public $plugins = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get list of loaded plugins
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getPlugins()
|
||||||
|
{
|
||||||
|
return $this->plugins;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Scan plugin folder and load plugins
|
* Scan plugin folder and load plugins
|
||||||
*
|
*
|
||||||
@@ -46,120 +48,65 @@ class Loader extends \Kanboard\Core\Base
|
|||||||
|
|
||||||
$dir = new DirectoryIterator(PLUGINS_DIR);
|
$dir = new DirectoryIterator(PLUGINS_DIR);
|
||||||
|
|
||||||
foreach ($dir as $fileinfo) {
|
foreach ($dir as $fileInfo) {
|
||||||
if (! $fileinfo->isDot() && $fileinfo->isDir()) {
|
if ($fileInfo->isDir() && substr($fileInfo->getFilename(), 0, 1) !== '.') {
|
||||||
$plugin = $fileinfo->getFilename();
|
$pluginName = $fileInfo->getFilename();
|
||||||
$this->loadSchema($plugin);
|
$this->loadSchema($pluginName);
|
||||||
$this->load($plugin);
|
$this->initializePlugin($this->loadPlugin($pluginName));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load plugin schema
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param string $pluginName
|
||||||
|
*/
|
||||||
|
public function loadSchema($pluginName)
|
||||||
|
{
|
||||||
|
if (SchemaHandler::hasSchema($pluginName)) {
|
||||||
|
$schemaHandler = new SchemaHandler($this->container);
|
||||||
|
$schemaHandler->loadSchema($pluginName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load plugin
|
* Load plugin
|
||||||
*
|
*
|
||||||
* @access public
|
* @access public
|
||||||
* @throws LogicException
|
* @throws LogicException
|
||||||
* @param string $plugin
|
* @param string $pluginName
|
||||||
|
* @return Base
|
||||||
*/
|
*/
|
||||||
public function load($plugin)
|
public function loadPlugin($pluginName)
|
||||||
{
|
{
|
||||||
$class = '\Kanboard\Plugin\\'.$plugin.'\\Plugin';
|
$className = '\Kanboard\Plugin\\'.$pluginName.'\\Plugin';
|
||||||
|
|
||||||
if (! class_exists($class)) {
|
if (! class_exists($className)) {
|
||||||
throw new LogicException('Unable to load this plugin class '.$class);
|
throw new LogicException('Unable to load this plugin class '.$className);
|
||||||
}
|
}
|
||||||
|
|
||||||
$instance = new $class($this->container);
|
return new $className($this->container);
|
||||||
|
|
||||||
Tool::buildDIC($this->container, $instance->getClasses());
|
|
||||||
Tool::buildDICHelpers($this->container, $instance->getHelpers());
|
|
||||||
|
|
||||||
$instance->initialize();
|
|
||||||
|
|
||||||
if (method_exists($instance, 'onStartup')) {
|
|
||||||
$this->dispatcher->addListener('app.bootstrap', array($instance, 'onStartup'));
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->plugins[] = $instance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load plugin schema
|
* Initialize plugin
|
||||||
*
|
*
|
||||||
* @access public
|
* @access public
|
||||||
* @param string $plugin
|
* @param Base $plugin
|
||||||
*/
|
*/
|
||||||
public function loadSchema($plugin)
|
public function initializePlugin(Base $plugin)
|
||||||
{
|
{
|
||||||
$filename = PLUGINS_DIR.'/'.$plugin.'/Schema/'.ucfirst(DB_DRIVER).'.php';
|
if (method_exists($plugin, 'onStartup')) {
|
||||||
|
$this->dispatcher->addListener('app.bootstrap', array($plugin, 'onStartup'));
|
||||||
if (file_exists($filename)) {
|
|
||||||
require_once($filename);
|
|
||||||
$this->migrateSchema($plugin);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
Tool::buildDIC($this->container, $plugin->getClasses());
|
||||||
* Execute plugin schema migrations
|
Tool::buildDICHelpers($this->container, $plugin->getHelpers());
|
||||||
*
|
|
||||||
* @access public
|
|
||||||
* @param string $plugin
|
|
||||||
*/
|
|
||||||
public function migrateSchema($plugin)
|
|
||||||
{
|
|
||||||
$last_version = constant('\Kanboard\Plugin\\'.$plugin.'\Schema\VERSION');
|
|
||||||
$current_version = $this->getSchemaVersion($plugin);
|
|
||||||
|
|
||||||
try {
|
$plugin->initialize();
|
||||||
$this->db->startTransaction();
|
$this->plugins[] = $plugin;
|
||||||
$this->db->getDriver()->disableForeignKeys();
|
|
||||||
|
|
||||||
for ($i = $current_version + 1; $i <= $last_version; $i++) {
|
|
||||||
$function_name = '\Kanboard\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();
|
|
||||||
throw new RuntimeException('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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
122
app/Core/Plugin/SchemaHandler.php
Normal file
122
app/Core/Plugin/SchemaHandler.php
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Kanboard\Core\Plugin;
|
||||||
|
|
||||||
|
use PDOException;
|
||||||
|
use RuntimeException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class SchemaHandler
|
||||||
|
*
|
||||||
|
* @package Kanboard\Core\Plugin
|
||||||
|
* @author Frederic Guillot
|
||||||
|
*/
|
||||||
|
class SchemaHandler extends \Kanboard\Core\Base
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Schema version table for plugins
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
const TABLE_SCHEMA = 'plugin_schema_versions';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get schema filename
|
||||||
|
*
|
||||||
|
* @static
|
||||||
|
* @access public
|
||||||
|
* @param string $pluginName
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function getSchemaFilename($pluginName)
|
||||||
|
{
|
||||||
|
return PLUGINS_DIR.'/'.$pluginName.'/Schema/'.ucfirst(DB_DRIVER).'.php';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return true if the plugin has schema
|
||||||
|
*
|
||||||
|
* @static
|
||||||
|
* @access public
|
||||||
|
* @param string $pluginName
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public static function hasSchema($pluginName)
|
||||||
|
{
|
||||||
|
return file_exists(self::getSchemaFilename($pluginName));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load plugin schema
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param string $pluginName
|
||||||
|
*/
|
||||||
|
public function loadSchema($pluginName)
|
||||||
|
{
|
||||||
|
require_once self::getSchemaFilename($pluginName);
|
||||||
|
$this->migrateSchema($pluginName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute plugin schema migrations
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param string $pluginName
|
||||||
|
*/
|
||||||
|
public function migrateSchema($pluginName)
|
||||||
|
{
|
||||||
|
$lastVersion = constant('\Kanboard\Plugin\\'.$pluginName.'\Schema\VERSION');
|
||||||
|
$currentVersion = $this->getSchemaVersion($pluginName);
|
||||||
|
|
||||||
|
try {
|
||||||
|
$this->db->startTransaction();
|
||||||
|
$this->db->getDriver()->disableForeignKeys();
|
||||||
|
|
||||||
|
for ($i = $currentVersion + 1; $i <= $lastVersion; $i++) {
|
||||||
|
$functionName = '\Kanboard\Plugin\\'.$pluginName.'\Schema\version_'.$i;
|
||||||
|
|
||||||
|
if (function_exists($functionName)) {
|
||||||
|
call_user_func($functionName, $this->db->getConnection());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->db->getDriver()->enableForeignKeys();
|
||||||
|
$this->db->closeTransaction();
|
||||||
|
$this->setSchemaVersion($pluginName, $i - 1);
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
$this->db->cancelTransaction();
|
||||||
|
$this->db->getDriver()->enableForeignKeys();
|
||||||
|
throw new RuntimeException('Unable to migrate schema for the plugin: '.$pluginName.' => '.$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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,7 +8,6 @@ use Symfony\Component\EventDispatcher\EventDispatcher;
|
|||||||
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher;
|
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher;
|
||||||
use Symfony\Component\Stopwatch\Stopwatch;
|
use Symfony\Component\Stopwatch\Stopwatch;
|
||||||
use SimpleLogger\Logger;
|
use SimpleLogger\Logger;
|
||||||
use SimpleLogger\File;
|
|
||||||
use Kanboard\Core\Session\FlashMessage;
|
use Kanboard\Core\Session\FlashMessage;
|
||||||
use Kanboard\Core\Session\SessionStorage;
|
use Kanboard\Core\Session\SessionStorage;
|
||||||
use Kanboard\ServiceProvider\ActionProvider;
|
use Kanboard\ServiceProvider\ActionProvider;
|
||||||
@@ -91,9 +90,4 @@ abstract class Base extends PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$this->container['db']->closeConnection();
|
$this->container['db']->closeConnection();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isWindows()
|
|
||||||
{
|
|
||||||
return substr(PHP_OS, 0, 3) === 'WIN';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,13 +2,13 @@
|
|||||||
|
|
||||||
require_once __DIR__.'/../../Base.php';
|
require_once __DIR__.'/../../Base.php';
|
||||||
|
|
||||||
use Kanboard\Core\Plugin\Loader;
|
use Kanboard\Core\Plugin\SchemaHandler;
|
||||||
|
|
||||||
class LoaderTest extends Base
|
class SchemaHandlerTest extends Base
|
||||||
{
|
{
|
||||||
public function testGetSchemaVersion()
|
public function testGetSchemaVersion()
|
||||||
{
|
{
|
||||||
$p = new Loader($this->container);
|
$p = new SchemaHandler($this->container);
|
||||||
$this->assertEquals(0, $p->getSchemaVersion('not_found'));
|
$this->assertEquals(0, $p->getSchemaVersion('not_found'));
|
||||||
|
|
||||||
$this->assertTrue($p->setSchemaVersion('plugin1', 1));
|
$this->assertTrue($p->setSchemaVersion('plugin1', 1));
|
||||||
Reference in New Issue
Block a user