1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Mappers; |
4
|
|
|
|
5
|
|
|
use Craft; |
6
|
|
|
use NerdsAndCompany\Schematic\Schematic; |
7
|
|
|
use NerdsAndCompany\Schematic\Interfaces\MapperInterface; |
8
|
|
|
use yii\base\Component as BaseComponent; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Schematic Plugin Mapper. |
12
|
|
|
* |
13
|
|
|
* Sync Craft Setups. |
14
|
|
|
* |
15
|
|
|
* @author Nerds & Company |
16
|
|
|
* @copyright Copyright (c) 2015-2019, Nerds & Company |
17
|
|
|
* @license MIT |
18
|
|
|
* |
19
|
|
|
* @see http://www.nerds.company |
20
|
|
|
*/ |
21
|
|
|
class PluginMapper extends BaseComponent implements MapperInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
1 |
|
public function export(array $plugins): array |
27
|
|
|
{ |
28
|
1 |
|
$pluginDefinitions = []; |
29
|
1 |
|
foreach ($plugins as $handle => $pluginInfo) { |
30
|
1 |
|
$pluginDefinitions[$handle] = $this->getPluginDefinition($handle, $pluginInfo); |
31
|
|
|
} |
32
|
1 |
|
ksort($pluginDefinitions); |
33
|
|
|
|
34
|
1 |
|
return $pluginDefinitions; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $handle |
39
|
|
|
* @param array $pluginInfo |
40
|
|
|
* |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
1 |
|
private function getPluginDefinition(string $handle, array $pluginInfo): array |
44
|
|
|
{ |
45
|
1 |
|
$settings = null; |
46
|
1 |
|
$plugin = Craft::$app->plugins->getPlugin($handle); |
47
|
1 |
|
if ($plugin) { |
48
|
1 |
|
$settings = $plugin->getSettings(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return [ |
52
|
1 |
|
'isEnabled' => $pluginInfo['isEnabled'], |
53
|
1 |
|
'isInstalled' => $pluginInfo['isInstalled'], |
54
|
1 |
|
'settings' => $settings ? $settings->attributes : [], |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
6 |
|
public function import(array $pluginDefinitions, array $plugins): array |
62
|
|
|
{ |
63
|
6 |
|
$imported = []; |
64
|
6 |
|
foreach ($pluginDefinitions as $handle => $definition) { |
65
|
5 |
|
if (!array_key_exists($handle, $plugins)) { |
66
|
1 |
|
Schematic::error(' - Plugin info not found for '.$handle.', make sure it is installed with composer'); |
67
|
1 |
|
continue; |
68
|
|
|
} |
69
|
4 |
|
if (!$definition['isInstalled']) { |
70
|
1 |
|
continue; |
71
|
|
|
} |
72
|
3 |
|
Schematic::info('- Installing plugin '.$handle); |
73
|
3 |
|
$pluginInfo = $plugins[$handle]; |
74
|
3 |
|
if ($this->savePlugin($handle, $definition, $pluginInfo)) { |
75
|
2 |
|
$imported[] = Craft::$app->plugins->getPlugin($handle); |
76
|
|
|
} |
77
|
2 |
|
unset($plugins[$handle]); |
78
|
|
|
} |
79
|
|
|
|
80
|
5 |
|
if (Schematic::$force) { |
81
|
1 |
|
foreach (array_keys($plugins) as $handle) { |
82
|
1 |
|
if ($plugins[$handle]['isInstalled']) { |
83
|
1 |
|
Schematic::info('- Uninstalling plugin '.$handle); |
84
|
1 |
|
Craft::$app->plugins->uninstallPlugin($handle); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
5 |
|
return $imported; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Install, enable, disable and/or update plugin. |
94
|
|
|
* |
95
|
|
|
* @param string $handle |
96
|
|
|
* @param array $definition |
97
|
|
|
* @param array $pluginInfo |
98
|
|
|
* |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
3 |
|
private function savePlugin(string $handle, array $definition, array $pluginInfo): bool |
102
|
|
|
{ |
103
|
3 |
|
if (!$pluginInfo['isInstalled']) { |
104
|
3 |
|
Craft::$app->plugins->installPlugin($handle); |
105
|
|
|
} |
106
|
2 |
|
if ($definition['isEnabled']) { |
107
|
1 |
|
Craft::$app->plugins->enablePlugin($handle); |
108
|
|
|
} else { |
109
|
1 |
|
Craft::$app->plugins->disablePlugin($handle); |
110
|
|
|
} |
111
|
2 |
|
$plugin = Craft::$app->plugins->getPlugin($handle); |
112
|
|
|
|
113
|
2 |
|
if ($plugin && $plugin->getSettings()) { |
114
|
2 |
|
return Craft::$app->plugins->savePluginSettings($plugin, $definition['settings']); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|