-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractExtension.php
165 lines (130 loc) · 4.39 KB
/
AbstractExtension.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
declare(strict_types=1);
/*
* This file is part of the "Extended Base Class Library for Symphony CMS" repository.
*
* Copyright 2020-2021 Alannah Kearney <hi@alannahkearney.com>
*
* For the full copyright and license information, please view the LICENCE
* file that was distributed with this source code.
*/
namespace pointybeard\Symphony\Extended;
use Extension;
use pointybeard\Helpers\Functions\Json;
use pointybeard\Symphony\ExtensionAssetManagement\Iterators;
use pointybeard\Symphony\SectionBuilder;
abstract class AbstractExtension extends Extension implements Interfaces\ExtensionInterface
{
private $about = null;
public function init(): void
{
return;
}
public function enable()
{
static::enableAllAssets();
return true;
}
public function disable()
{
static::disableAllAssets();
return true;
}
public function uninstall()
{
// @todo: Check that other extensions don't depend on this one
// @todo: Remove any sections created during install
static::uninstallAllAssets();
return true;
}
public function install()
{
if (true == isset($this->about()->require) && false == empty($this->about()->require)) {
foreach ($this->about()->require as $handle) {
if (false == $this->checkDependency($handle)) {
throw new \Exception("Extenson '{$handle}' must be installed before '{$this->about()->name}' can be installed.");
}
}
}
if (true == file_exists(self::dir().'/src/Install/sections.json')) {
SectionBuilder\Import::fromJsonFile(
self::dir().'/src/Install/sections.json',
SectionBuilder\Import::FLAG_SKIP_ORDERING
);
}
static::installAllAssets();
return true;
}
protected function enableAllAssets()
{
foreach (static::getAssetIterators() as $item) {
$item->enable();
}
}
protected function disableAllAssets()
{
foreach (static::getAssetIterators() as $item) {
$item->disable();
}
}
protected function installAllAssets()
{
foreach (static::getAssetIterators() as $item) {
$item->install();
}
}
protected function uninstallAllAssets()
{
foreach (static::getAssetIterators() as $item) {
$item->uninstall();
}
}
protected function getAssetIterators(): \AppendIterator
{
$it = new \AppendIterator();
if (true == is_dir(self::dir().'/src/Includes/Datasources')) {
$it->append(new Iterators\DatasourceIterator(self::dir().'/src/Includes/Datasources'));
}
if (true == is_dir(self::dir().'/src/Includes/Events')) {
$it->append(new Iterators\EventIterator(self::dir().'/src/Includes/Events'));
}
if (true == is_dir(self::dir().'/src/Includes/Fields')) {
$it->append(new Iterators\FieldIterator(self::dir().'/src/Includes/Fields'));
}
if (true == is_dir(self::dir().'/src/Includes/Content')) {
$it->append(new Iterators\ContentIterator(self::dir().'/src/Includes/Content'));
}
if (true == is_dir(self::dir().'/src/Includes/Commands')) {
$it->append(new Iterators\CommandIterator(self::dir().'/src/Includes/Commands'));
}
return $it;
}
final public static function status(): string
{
\ExtensionManager::about(static::handle())['status'];
}
final public static function handle(): string
{
return basename(self::dir());
}
final public static function about(): \stdClass
{
try {
return Json\json_decode_file(self::dir().'/extension.json');
} catch (\JsonException $ex) {
throw new \Exception('Unable to call the about method on extension '.static::handle().'. Returned: '.$ex->getMessage());
}
}
private static function dir(): string
{
return dirname((new \ReflectionClass(static::class))->getFileName());
}
private function checkDependency(string $handle): bool
{
$about = \ExtensionManager::about($handle);
if (true == empty($about) || false == in_array(\Extension::EXTENSION_ENABLED, $about['status'])) {
return false;
}
return true;
}
}