|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Jitamin. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (C) Jitamin Team |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Jitamin\Foundation\Plugin; |
|
13
|
|
|
|
|
14
|
|
|
use PDOException; |
|
15
|
|
|
use RuntimeException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class SchemaHandler. |
|
19
|
|
|
*/ |
|
20
|
|
|
class SchemaHandler extends \Jitamin\Foundation\Base |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Schema version table for plugins. |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
const TABLE_SCHEMA = 'plugin_schema_versions'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Get schema filename. |
|
31
|
|
|
* |
|
32
|
|
|
* @static |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $pluginName |
|
35
|
|
|
* |
|
36
|
|
|
* @return string |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function getSchemaFilename($pluginName) |
|
39
|
|
|
{ |
|
40
|
|
|
return PLUGINS_DIR.'/'.$pluginName.'/Schema/'.ucfirst(DB_DRIVER).'.php'; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Return true if the plugin has schema. |
|
45
|
|
|
* |
|
46
|
|
|
* @static |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $pluginName |
|
49
|
|
|
* |
|
50
|
|
|
* @return bool |
|
51
|
|
|
*/ |
|
52
|
|
|
public static function hasSchema($pluginName) |
|
53
|
|
|
{ |
|
54
|
|
|
return file_exists(self::getSchemaFilename($pluginName)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Load plugin schema. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $pluginName |
|
61
|
|
|
*/ |
|
62
|
|
|
public function loadSchema($pluginName) |
|
63
|
|
|
{ |
|
64
|
|
|
require_once self::getSchemaFilename($pluginName); |
|
65
|
|
|
$this->migrateSchema($pluginName); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Execute plugin schema migrations. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $pluginName |
|
72
|
|
|
*/ |
|
73
|
|
|
public function migrateSchema($pluginName) |
|
74
|
|
|
{ |
|
75
|
|
|
$lastVersion = constant('\Jitamin\Plugin\\'.$pluginName.'\Schema\VERSION'); |
|
76
|
|
|
$currentVersion = $this->getSchemaVersion($pluginName); |
|
77
|
|
|
|
|
78
|
|
|
try { |
|
79
|
|
|
$this->db->startTransaction(); |
|
|
|
|
|
|
80
|
|
|
$this->db->getDriver()->disableForeignKeys(); |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
for ($i = $currentVersion + 1; $i <= $lastVersion; $i++) { |
|
83
|
|
|
$functionName = '\Jitamin\Plugin\\'.$pluginName.'\Schema\version_'.$i; |
|
84
|
|
|
|
|
85
|
|
|
if (function_exists($functionName)) { |
|
86
|
|
|
call_user_func($functionName, $this->db->getConnection()); |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$this->db->getDriver()->enableForeignKeys(); |
|
|
|
|
|
|
91
|
|
|
$this->db->closeTransaction(); |
|
|
|
|
|
|
92
|
|
|
$this->setSchemaVersion($pluginName, $i - 1); |
|
93
|
|
|
} catch (PDOException $e) { |
|
94
|
|
|
$this->db->cancelTransaction(); |
|
|
|
|
|
|
95
|
|
|
$this->db->getDriver()->enableForeignKeys(); |
|
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
throw new RuntimeException('Unable to migrate schema for the plugin: '.$pluginName.' => '.$e->getMessage()); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get current plugin schema version. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $plugin |
|
105
|
|
|
* |
|
106
|
|
|
* @return int |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getSchemaVersion($plugin) |
|
109
|
|
|
{ |
|
110
|
|
|
return (int) $this->db->table(self::TABLE_SCHEMA)->eq('plugin', strtolower($plugin))->findOneColumn('version'); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Save last plugin schema version. |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $plugin |
|
117
|
|
|
* @param int $version |
|
118
|
|
|
* |
|
119
|
|
|
* @return bool |
|
120
|
|
|
*/ |
|
121
|
|
|
public function setSchemaVersion($plugin, $version) |
|
122
|
|
|
{ |
|
123
|
|
|
$dictionary = [ |
|
124
|
|
|
strtolower($plugin) => $version, |
|
125
|
|
|
]; |
|
126
|
|
|
|
|
127
|
|
|
return $this->db->getDriver()->upsert(self::TABLE_SCHEMA, 'plugin', 'version', $dictionary); |
|
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.