summaryrefslogtreecommitdiff
path: root/app/Core/Plugin/Loader.php
diff options
context:
space:
mode:
authorFrederic Guillot <fred@kanboard.net>2015-09-20 13:11:41 -0400
committerFrederic Guillot <fred@kanboard.net>2015-09-20 13:11:41 -0400
commita0124b45f9dab8a0f7d4879d4ea147b414b25bf2 (patch)
tree4f1a419104374591e269e9f32838ca500245fe1a /app/Core/Plugin/Loader.php
parentfe57edd9e87832dbd14ea8ffd2dc2f16ac1ceb6f (diff)
Add sub namespace for plugins
Diffstat (limited to 'app/Core/Plugin/Loader.php')
-rw-r--r--app/Core/Plugin/Loader.php138
1 files changed, 138 insertions, 0 deletions
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 @@
+<?php
+
+namespace Core\Plugin;
+
+use DirectoryIterator;
+use PDOException;
+use Core\Tool;
+
+/**
+ * Plugin Loader
+ *
+ * @package plugin
+ * @author Frederic Guillot
+ */
+class Loader extends \Core\Base
+{
+ /**
+ * Schema version table for plugins
+ *
+ * @var string
+ */
+ const TABLE_SCHEMA = 'plugin_schema_versions';
+
+ /**
+ * Scan plugin folder and load plugins
+ *
+ * @access public
+ */
+ public function scan()
+ {
+ if (file_exists(__DIR__.'/../../../plugins')) {
+ $dir = new DirectoryIterator(__DIR__.'/../../../plugins');
+
+ foreach ($dir as $fileinfo) {
+ if (! $fileinfo->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);
+ }
+}