1
|
|
|
<?php |
2
|
|
|
namespace Redaxscript\Module; |
3
|
|
|
|
4
|
|
|
use Redaxscript\Config; |
5
|
|
|
use Redaxscript\Installer; |
6
|
|
|
use Redaxscript\Language; |
7
|
|
|
use Redaxscript\Model; |
8
|
|
|
use Redaxscript\Registry; |
9
|
|
|
use Redaxscript\Request; |
10
|
|
|
use function array_key_exists; |
11
|
|
|
use function array_merge; |
12
|
|
|
use function is_array; |
13
|
|
|
use function is_dir; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* parent class to create a module |
17
|
|
|
* |
18
|
|
|
* @since 2.2.0 |
19
|
|
|
* |
20
|
|
|
* @package Redaxscript |
21
|
|
|
* @category Module |
22
|
|
|
* @author Henry Ruhs |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
class Module |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* array of the module |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
protected static array $_moduleArray = []; |
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* constructor of the class |
37
|
|
|
* |
38
|
|
|
* @since 3.0.0 |
39
|
|
|
* |
40
|
|
|
* @param Registry $_registry instance of the registry class |
41
|
|
|
* @param Request $_request instance of the request class |
42
|
|
|
* @param Language $_language instance of the language class |
43
|
|
|
* @param Config $_config instance of the config class |
44
|
|
|
*/ |
45
|
|
|
|
46
|
|
|
public function __construct(protected Registry $_registry, protected Request $_request, protected Language $_language, protected Config $_config) |
47
|
|
|
{ |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* init the class |
52
|
|
|
* |
53
|
|
|
* @since 2.4.0 |
54
|
|
|
* |
55
|
|
|
* @param array $moduleArray custom module setup |
56
|
|
|
*/ |
57
|
|
|
|
58
|
|
|
public function init(array $moduleArray = []) : void |
59
|
|
|
{ |
60
|
|
|
static::$_moduleArray = array_merge(static::$_moduleArray, $moduleArray); |
61
|
|
|
|
62
|
|
|
/* load the language */ |
63
|
|
|
|
64
|
|
|
if (is_array(static::$_moduleArray) && array_key_exists('alias', static::$_moduleArray)) |
65
|
|
|
{ |
66
|
|
|
$this->_language->load( |
67
|
|
|
[ |
68
|
|
|
'modules' . DIRECTORY_SEPARATOR . static::$_moduleArray['alias'] . DIRECTORY_SEPARATOR . 'languages' . DIRECTORY_SEPARATOR . 'en.json', |
69
|
|
|
'modules' . DIRECTORY_SEPARATOR . static::$_moduleArray['alias'] . DIRECTORY_SEPARATOR . 'languages' . DIRECTORY_SEPARATOR . $this->_registry->get('language') . '.json' |
70
|
|
|
]); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* install the module |
76
|
|
|
* |
77
|
|
|
* @since 2.6.0 |
78
|
3 |
|
* |
79
|
|
|
* @return bool |
80
|
3 |
|
*/ |
81
|
3 |
|
|
82
|
3 |
|
public function install() : bool |
83
|
3 |
|
{ |
84
|
3 |
|
if (is_array(static::$_moduleArray) && array_key_exists('alias', static::$_moduleArray)) |
85
|
|
|
{ |
86
|
|
|
$moduleModel = new Model\Module(); |
87
|
|
|
$moduleModel->createByArray(static::$_moduleArray); |
88
|
|
|
|
89
|
|
|
/* create from sql */ |
90
|
|
|
|
91
|
|
|
$directory = 'modules' . DIRECTORY_SEPARATOR . static::$_moduleArray['alias'] . DIRECTORY_SEPARATOR . 'database'; |
92
|
|
|
if (is_dir($directory)) |
93
|
|
|
{ |
94
|
3 |
|
$installer = new Installer($this->_registry, $this->_request, $this->_language, $this->_config); |
95
|
|
|
$installer->init($directory); |
96
|
3 |
|
$installer->rawCreate(); |
97
|
|
|
} |
98
|
|
|
$moduleModel->clearCache(); |
99
|
|
|
return $moduleModel->query()->where('alias', static::$_moduleArray['alias'])->count() === 1; |
100
|
3 |
|
} |
101
|
|
|
return false; |
102
|
2 |
|
} |
103
|
|
|
|
104
|
2 |
|
/** |
105
|
2 |
|
* uninstall the module |
106
|
|
|
* |
107
|
|
|
* @since 2.6.0 |
108
|
3 |
|
* |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
|
112
|
|
|
public function uninstall() : bool |
113
|
|
|
{ |
114
|
|
|
if (is_array(static::$_moduleArray) && array_key_exists('alias', static::$_moduleArray)) |
115
|
|
|
{ |
116
|
|
|
$moduleModel = new Model\Module(); |
117
|
|
|
$moduleModel->deleteByAlias(static::$_moduleArray['alias']); |
118
|
2 |
|
|
119
|
|
|
/* drop from sql */ |
120
|
2 |
|
|
121
|
|
|
$directory = 'modules' . DIRECTORY_SEPARATOR . static::$_moduleArray['alias'] . DIRECTORY_SEPARATOR . 'database'; |
122
|
1 |
|
if (is_dir($directory)) |
123
|
1 |
|
{ |
124
|
|
|
$installer = new Installer($this->_registry, $this->_request, $this->_language, $this->_config); |
125
|
|
|
$installer->init($directory); |
126
|
|
|
$installer->rawDrop(); |
127
|
1 |
|
} |
128
|
1 |
|
$moduleModel->clearCache(); |
129
|
|
|
return $moduleModel->query()->where('alias', static::$_moduleArray['alias'])->count() === 0; |
130
|
1 |
|
} |
131
|
1 |
|
return false; |
132
|
1 |
|
} |
133
|
|
|
|
134
|
1 |
|
/** |
135
|
1 |
|
* reinstall the module |
136
|
|
|
* |
137
|
1 |
|
* @since 4.3.0 |
138
|
|
|
* |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
|
|
|
142
|
|
|
public function reinstall() : bool |
143
|
|
|
{ |
144
|
|
|
return $this->uninstall() && $this->install(); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|