1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Services; |
4
|
|
|
|
5
|
|
|
use Craft\Craft; |
6
|
|
|
use Craft\Exception; |
7
|
|
|
use Craft\BasePlugin; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Schematic Plugins Service. |
11
|
|
|
* |
12
|
|
|
* Sync Craft Setups. |
13
|
|
|
* |
14
|
|
|
* @author Nerds & Company |
15
|
|
|
* @copyright Copyright (c) 2015-2017, Nerds & Company |
16
|
|
|
* @license MIT |
17
|
|
|
* |
18
|
|
|
* @link http://www.nerds.company |
19
|
|
|
*/ |
20
|
|
|
class Plugins extends Base |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @return PluginsService |
24
|
|
|
*/ |
25
|
6 |
|
protected function getPluginService() |
26
|
|
|
{ |
27
|
6 |
|
return Craft::app()->plugins; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return UpdatesService |
32
|
|
|
*/ |
33
|
5 |
|
protected function getUpdatesService() |
34
|
|
|
{ |
35
|
5 |
|
return Craft::app()->updates; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Installs plugin by handle. |
40
|
|
|
* |
41
|
|
|
* @param string $handle |
42
|
|
|
*/ |
43
|
3 |
|
protected function installPluginByHandle($handle) |
44
|
|
|
{ |
45
|
3 |
|
Craft::log(Craft::t('Installing plugin {handle}', ['handle' => $handle])); |
46
|
|
|
|
47
|
|
|
try { |
48
|
3 |
|
$this->getPluginService()->installPlugin($handle); |
49
|
3 |
|
} catch (\Exception $e) { |
50
|
1 |
|
$this->addError($e->getMessage()); |
51
|
|
|
} |
52
|
3 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Uninstalls plugin by handle. |
56
|
|
|
* |
57
|
|
|
* @param $handle |
58
|
|
|
*/ |
59
|
1 |
|
protected function uninstallPluginByHandle($handle) |
60
|
|
|
{ |
61
|
1 |
|
$this->getPluginService()->uninstallPlugin($handle); |
62
|
1 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns plugin by handle. |
66
|
|
|
* |
67
|
|
|
* @param string $handle |
68
|
|
|
* |
69
|
|
|
* @return BasePlugin|null |
70
|
|
|
*/ |
71
|
5 |
|
protected function getPlugin($handle) |
72
|
|
|
{ |
73
|
5 |
|
$plugin = $this->getPluginService()->getPlugin($handle, false); |
74
|
5 |
|
if (!$plugin) { |
75
|
1 |
|
$this->addError(Craft::t("Plugin {handle} could not be found, make sure it's files are located in the plugins folder", ['handle' => $handle])); |
76
|
1 |
|
} |
77
|
|
|
|
78
|
5 |
|
return $plugin; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Toggles plugin based on enabled flag. |
83
|
|
|
* |
84
|
|
|
* @param string $handle |
85
|
|
|
* @param bool $isEnabled |
86
|
|
|
*/ |
87
|
3 |
|
protected function togglePluginByHandle($handle, $isEnabled) |
88
|
|
|
{ |
89
|
3 |
|
if ($isEnabled) { |
90
|
2 |
|
$this->getPluginService()->enablePlugin($handle); |
91
|
2 |
|
} else { |
92
|
1 |
|
$this->getPluginService()->disablePlugin($handle); |
93
|
|
|
} |
94
|
3 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param BasePlugin $plugin |
98
|
|
|
* |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
1 |
|
private function getPluginDefinition(BasePlugin $plugin) |
102
|
|
|
{ |
103
|
|
|
return [ |
104
|
1 |
|
'isInstalled' => $plugin->isInstalled, |
105
|
1 |
|
'isEnabled' => $plugin->isEnabled, |
106
|
1 |
|
'settings' => $plugin->getSettings()->attributes, |
107
|
1 |
|
]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array $pluginDefinitions |
112
|
|
|
* @param bool $force |
113
|
|
|
* |
114
|
|
|
* @return Result |
115
|
|
|
*/ |
116
|
5 |
|
public function import(array $pluginDefinitions, $force = false) |
117
|
|
|
{ |
118
|
5 |
|
Craft::log(Craft::t('Updating Craft')); |
119
|
5 |
|
if ($this->getUpdatesService()->isCraftDbMigrationNeeded()) { |
120
|
|
|
$result = $this->getUpdatesService()->updateDatabase('craft'); |
121
|
|
|
if (!$result['success']) { |
122
|
|
|
throw new Exception($result['message']); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
5 |
|
Craft::log(Craft::t('Importing Plugins')); |
127
|
5 |
|
foreach ($pluginDefinitions as $handle => $pluginDefinition) { |
128
|
5 |
|
Craft::log(Craft::t('Applying definitions for {handle}', ['handle' => $handle])); |
129
|
|
|
|
130
|
5 |
|
if ($plugin = $this->getPlugin($handle)) { |
|
|
|
|
131
|
4 |
|
if ($pluginDefinition['isInstalled']) { |
132
|
3 |
|
$isNewPlugin = !$plugin->isInstalled; |
133
|
3 |
|
if ($isNewPlugin) { |
134
|
3 |
|
$this->installPluginByHandle($handle); |
135
|
3 |
|
} |
136
|
|
|
|
137
|
3 |
|
$this->togglePluginByHandle($handle, $pluginDefinition['isEnabled']); |
138
|
|
|
|
139
|
3 |
|
if (!$isNewPlugin && $plugin->isEnabled) { |
140
|
|
|
$this->getUpdatesService()->updateDatabase($handle); |
141
|
|
|
} |
142
|
|
|
|
143
|
3 |
|
if (array_key_exists('settings', $pluginDefinition)) { |
144
|
3 |
|
Craft::log(Craft::t('Saving plugin settings for {handle}', ['handle' => $handle])); |
145
|
|
|
|
146
|
3 |
|
$this->getPluginService()->savePluginSettings($plugin, $pluginDefinition['settings']); |
147
|
3 |
|
} |
148
|
3 |
|
} else { |
149
|
1 |
|
$this->uninstallPluginByHandle($handle); |
150
|
|
|
} |
151
|
4 |
|
} |
152
|
5 |
|
} |
153
|
|
|
|
154
|
5 |
|
return $this->getResultModel(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param array $data |
159
|
|
|
* |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
1 |
|
public function export(array $data = []) |
163
|
|
|
{ |
164
|
1 |
|
Craft::log(Craft::t('Exporting Plugins')); |
165
|
|
|
|
166
|
1 |
|
$plugins = $this->getPluginService()->getPlugins(false); |
167
|
1 |
|
$pluginDefinitions = []; |
168
|
|
|
|
169
|
1 |
|
foreach ($plugins as $plugin) { |
170
|
1 |
|
$handle = preg_replace('/^Craft\\\\(.*?)Plugin$/', '$1', get_class($plugin)); |
171
|
1 |
|
$pluginDefinitions[$handle] = $this->getPluginDefinition($plugin); |
172
|
1 |
|
} |
173
|
1 |
|
ksort($pluginDefinitions); |
174
|
|
|
|
175
|
1 |
|
return $pluginDefinitions; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.