-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostInstallActions.php
135 lines (117 loc) · 3.44 KB
/
PostInstallActions.php
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
<?php
/**
* Arikaim
*
* @link http://www.arikaim.com
* @copyright Copyright (c) Konstantin Atanasov <[email protected]>
* @license http://www.arikaim.com/license
*
*/
namespace Arikaim\Core\App;
use Closure;
/**
* Post install actions
*/
class PostInstallActions
{
/**
* Run post install actions
*
* @param Closure|null $onProgress
* @param Closure|null $onProgressError
* @return bool
*/
public static function run(?Closure $onProgress = null, ?Closure $onProgressError = null): bool
{
global $arikaim;
// Run post install actions on all extensions
$extensionManager = $arikaim->get('packages')->create('extension');
$extensionManager->postInstallAllPackages();
if (Self::hasActions() == false) {
if (\is_callable($onProgress) == true) {
$onProgress('Done.');
}
return true;
}
$actions = $arikaim->get('config')->loadJsonConfigFile('post-install.json');
$errors = 0;
foreach ($actions as $action) {
$result = Self::runAction($action);
if ($result === false) {
if (\is_callable($onProgressError) == true) {
$onProgressError(Self::getPackageName($action));
}
$errors++;
} else {
if (\is_callable($onProgress) == true) {
$onProgress(Self::getPackageName($action));
}
}
}
return ($errors == 0);
}
/**
* Check if pst actions fiel exist
*
* @return boolean
*/
public static function hasActions(): bool
{
global $arikaim;
return (bool)$arikaim->get('config')->hasConfigFile('post-install.json');
}
/**
* Get action package name
*
* @param array $action
* @return string
*/
protected static function getPackageName(array $action): string
{
$theme = $action['theme'] ?? null;
$extension = $action['extension'] ?? null;
return (empty($extension) == false) ? $extension : $theme;
}
/**
* Run action
*
* @param array $action
* @return boolean
*/
public static function runAction(array $action)
{
$command = $action['command'] ?? null;
if (empty($command) == true) {
return false;
}
$extension = $action['extension'] ?? null;
$packageName = Self::getPackageName($action);
$packageType = (empty($extension) == false) ? 'extension' : 'template';
switch($command) {
case 'set-primary': {
return Self::setPrimaryPackage($packageName,$packageType);
}
}
return false;
}
/**
* Run set primary package action
*
* @param string $name
* @param string $type
* @return boolean
*/
public static function setPrimaryPackage(string $name, string $type = 'extension'): bool
{
global $arikaim;
$packageManager = $arikaim->get('packages')->create($type);
if ($packageManager->hasPackage($name) == false) {
return false;
}
$package = $packageManager->createPackage($name);
$package->unInstall();
$package->install(true);
$result = $package->setPrimary();
return $result;
}
}