blob: 4769e5cb38285861481e9d6d2dd64faa65df2cc3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
<?php
namespace Kanboard\Core\Plugin;
use DirectoryIterator;
use PDOException;
use RuntimeException;
use Kanboard\Core\Tool;
/**
* Plugin Loader
*
* @package plugin
* @author Frederic Guillot
*/
class Loader extends \Kanboard\Core\Base
{
/**
* Schema version table for plugins
*
* @var string
*/
const TABLE_SCHEMA = 'plugin_schema_versions';
/**
* Plugin instances
*
* @access public
* @var array
*/
public $plugins = array();
/**
* Scan plugin folder and load plugins
*
* @access public
*/
public function scan()
{
if (file_exists(PLUGINS_DIR)) {
$dir = new DirectoryIterator(PLUGINS_DIR);
foreach ($dir as $fileinfo) {
if (! $fileinfo->isDot() && $fileinfo->isDir()) {
$plugin = $fileinfo->getFilename();
$this->loadSchema($plugin);
$this->load($plugin);
}
}
}
}
/**
* Load plugin
*
* @access public
* @param string $plugin
*/
public function load($plugin)
{
$class = '\Kanboard\Plugin\\'.$plugin.'\\Plugin';
$instance = new $class($this->container);
Tool::buildDic($this->container, $instance->getClasses());
$instance->initialize();
$this->plugins[] = $instance;
}
/**
* Load plugin schema
*
* @access public
* @param string $plugin
*/
public function loadSchema($plugin)
{
$filename = PLUGINS_DIR.'/'.$plugin.'/Schema/'.ucfirst(DB_DRIVER).'.php';
if (file_exists($filename)) {
require_once($filename);
$this->migrateSchema($plugin);
}
}
/**
* Execute plugin schema migrations
*
* @access public
* @param string $plugin
*/
public function migrateSchema($plugin)
{
$last_version = constant('\Kanboard\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 = '\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);
}
}
|