Repository navigation
Expand file tree
/
Copy pathComponentHelper.php
More file actions
108 lines (81 loc) · 3.03 KB
/
Copy pathComponentHelper.php
File metadata and controls
108 lines (81 loc) · 3.03 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
<?php namespace RainLab\Builder\Classes;
use Cache;
use Input;
use Exception;
use October\Rain\Support\Traits\Singleton;
use ApplicationException;
/**
* Provides helper methods for Builder CMS components.
*
* @package rainlab\builder
* @author Alexey Bobkov, Samuel Georges
*/
class ComponentHelper
{
use Singleton;
protected $modelListCache = null;
public function listGlobalModels()
{
if ($this->modelListCache !== null) {
return $this->modelListCache;
}
$key = 'builder-global-model-list';
$cached = Cache::get($key, false);
if ($cached !== false && ($cached = @unserialize($cached)) !== false) {
return $this->modelListCache = $cached;
}
$plugins = PluginBaseModel::listAllPluginCodes();
$result = [];
foreach ($plugins as $pluginCode) {
try {
$pluginCodeObj = new PluginCode($pluginCode);
$models = ModelModel::listPluginModels($pluginCodeObj);
$pluginCodeStr = $pluginCodeObj->toCode();
$pluginModelsNamespace = $pluginCodeObj->toPluginNamespace().'\\Models\\';
foreach ($models as $model) {
$fullClassName = $pluginModelsNamespace.$model->className;
$result[$fullClassName] = $pluginCodeStr.' - '.$model->className;
}
}
catch (Exception $ex) {
// Ignore invalid plugins and models
}
}
Cache::put($key, serialize($result), 1);
return $this->modelListCache = $result;
}
public function getModelClassDesignTime()
{
$modelClass = trim(Input::get('modelClass'));
if ($modelClass && !is_scalar($modelClass)) {
throw new ApplicationException('Model class name should be a string.');
}
if (!strlen($modelClass)) {
$models = $this->listGlobalModels();
$modelClass = key($models);
}
if (!ModelModel::validateModelClassName($modelClass)) {
throw new ApplicationException('Invalid model class name.');
}
return $modelClass;
}
public function listModelColumnNames()
{
$modelClass = $this->getModelClassDesignTime();
$key = md5('builder-global-model-list-'.$modelClass);
$cached = Cache::get($key, false);
if ($cached !== false && ($cached = @unserialize($cached)) !== false) {
return $cached;
}
$pluginCodeObj = PluginCode::createFromNamespace($modelClass);
$modelClassParts = explode('\\', $modelClass); // The full class name is already validated in PluginCode::createFromNamespace()
$modelClass = array_pop($modelClassParts);
$columnNames = ModelModel::getModelFields($pluginCodeObj, $modelClass);
$result = [];
foreach ($columnNames as $columnName) {
$result[$columnName] = $columnName;
}
Cache::put($key, serialize($result), 1);
return $result;
}
}