Repository navigation
Expand file tree
/
Copy pathPluginBaseModel.php
More file actions
217 lines (176 loc) · 5.78 KB
/
Copy pathPluginBaseModel.php
File metadata and controls
217 lines (176 loc) · 5.78 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php namespace RainLab\Builder\Classes;
use RainLab\Builder\Models\Settings as PluginSettings;
use System\Classes\UpdateManager;
use System\Classes\PluginManager;
use Exception;
use File;
/**
* Manages plugin basic information.
*
* @package rainlab\builder
* @author Alexey Bobkov, Samuel Georges
*/
class PluginBaseModel extends PluginYamlModel
{
public $name;
public $namespace;
public $description;
public $author;
public $icon;
public $author_namespace;
public $homepage;
protected $localizedName;
protected $localizedDescription;
protected $yamlSection = "plugin";
protected static $fillable = [
'name',
'author',
'namespace',
'author_namespace',
'description',
'icon',
'homepage'
];
protected $validationRules = [
'name' => 'required',
'author' => ['required'],
'namespace' => ['required', 'regex:/^[a-z]+[a-z0-9]+$/i', 'reserved'],
'author_namespace' => ['required', 'regex:/^[a-z]+[a-z0-9]+$/i', 'reserved'],
'homepage' => 'url'
];
public function getIconOptions()
{
return IconList::getList();
}
public function initDefaults()
{
$settings = PluginSettings::instance();
$this->author = $settings->author_name;
$this->author_namespace = $settings->author_namespace;
}
public function getPluginCode()
{
return $this->author_namespace.'.'.$this->namespace;
}
public static function listAllPluginCodes()
{
$plugins = PluginManager::instance()->getPlugins();
return array_keys($plugins);
}
protected function initPropertiesFromPluginCodeObject($pluginCodeObj)
{
$this->author_namespace = $pluginCodeObj->getAuthorCode();
$this->namespace = $pluginCodeObj->getPluginCode();
}
/**
* Converts the model's data to an array before it's saved to a YAML file.
* @return array
*/
protected function modelToYamlArray()
{
return [
'name' => $this->name,
'description' => $this->description,
'author' => $this->author,
'icon' => $this->icon,
'homepage' => $this->homepage
];
}
/**
* Load the model's data from an array.
* @param array $array An array to load the model fields from.
*/
protected function yamlArrayToModel($array)
{
$this->name = $this->getArrayKeySafe($array, 'name');
$this->description = $this->getArrayKeySafe($array, 'description');
$this->author = $this->getArrayKeySafe($array, 'author');
$this->icon = $this->getArrayKeySafe($array, 'icon');
$this->homepage = $this->getArrayKeySafe($array, 'homepage');
}
protected function beforeCreate()
{
$this->localizedName = $this->name;
$this->localizedDescription = $this->description;
$pluginCode = strtolower($this->author_namespace.'.'.$this->namespace);
$this->name = $pluginCode.'::lang.plugin.name';
$this->description = $pluginCode.'::lang.plugin.description';
}
protected function afterCreate()
{
try {
$this->initPluginStructure();
$this->forcePluginRegistration();
$this->initBuilderSettings();
}
catch (Exception $ex) {
$this->rollbackPluginCreation();
throw $ex;
}
}
protected function initPluginStructure()
{
$basePath = $this->getPluginPath();
$defaultLanguage = LocalizationModel::getDefaultLanguage();
$structure = [
$basePath.'/Plugin.php' => 'plugin.php.tpl',
$basePath.'/updates/version.yaml' => 'version.yaml.tpl',
$basePath.'/classes',
$basePath.'/lang/'.$defaultLanguage.'/lang.php' => 'lang.php.tpl'
];
$variables = [
'authorNamespace' => $this->author_namespace,
'pluginNamespace' => $this->namespace,
'pluginNameSanitized' => $this->sanitizePHPString($this->localizedName),
'pluginDescriptionSanitized' => $this->sanitizePHPString($this->localizedDescription),
];
$generator = new FilesystemGenerator('$', $structure, '$/rainlab/builder/classes/pluginbasemodel/templates');
$generator->setVariables($variables);
$generator->generate();
}
protected function forcePluginRegistration()
{
PluginManager::instance()->loadPlugins();
UpdateManager::instance()->update();
}
protected function rollbackPluginCreation()
{
$basePath = '$/'.$this->getPluginPath();
$basePath = File::symbolizePath($basePath);
if (basename($basePath) == strtolower($this->namespace)) {
File::deleteDirectory($basePath);
}
}
protected function sanitizePHPString($str)
{
return str_replace("'", "\'", $str);
}
/**
* Returns a file path to save the model to.
* @return string Returns a path.
*/
protected function getFilePath()
{
return $this->getPluginPathObj()->toPluginFilePath();
}
protected function getPluginPath()
{
return $this->getPluginPathObj()->toFilesystemPath();
}
protected function getPluginPathObj()
{
return new PluginCode($this->getPluginCode());
}
protected function initBuilderSettings()
{
// Initialize Builder configuration - author name and namespace
// if it was not set yet.
$settings = PluginSettings::instance();
if (strlen($settings->author_name) || strlen($settings->author_namespace)) {
return;
}
$settings->author_name = $this->author;
$settings->author_namespace = $this->author_namespace;
$settings->save();
}
}