|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CakeCMS Core |
|
4
|
|
|
* |
|
5
|
|
|
* This file is part of the of the simple cms based on CakePHP 3. |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
* |
|
9
|
|
|
* @package Core |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
* @copyright MIT License http://www.opensource.org/licenses/mit-license.php |
|
12
|
|
|
* @link https://github.com/CakeCMS/Core". |
|
13
|
|
|
* @author Sergey Kalistratov <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Core\Element; |
|
17
|
|
|
|
|
18
|
|
|
use JBZoo\Utils\FS; |
|
19
|
|
|
use JBZoo\Utils\Str; |
|
20
|
|
|
use Cake\Utility\Hash; |
|
21
|
|
|
use Cake\Utility\Text; |
|
22
|
|
|
use Cake\Core\Configure; |
|
23
|
|
|
use Cake\Core\ClassLoader; |
|
24
|
|
|
use Core\ORM\Entity\Element as EntityElement; |
|
25
|
|
|
use Core\Element\Exception\ElementException; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Class Manager |
|
29
|
|
|
* |
|
30
|
|
|
* @package Core\Element |
|
31
|
|
|
*/ |
|
32
|
|
|
class Manager |
|
33
|
|
|
{ |
|
34
|
|
|
|
|
35
|
|
|
const DEFAULT_GROUP = 'Item'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Create element instance. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $type |
|
41
|
|
|
* @param string $group |
|
42
|
|
|
* @param array $config |
|
43
|
|
|
* @param EntityElement|null $entity |
|
44
|
|
|
* @return Element |
|
45
|
|
|
*/ |
|
46
|
|
|
public function create($type, $group = self::DEFAULT_GROUP, array $config = [], EntityElement $entity = null) |
|
47
|
|
|
{ |
|
48
|
|
|
$type = $this->_normalize($type); |
|
49
|
|
|
if (!$type) { |
|
50
|
|
|
throw new ElementException(__d('core_dev', 'Element type "{0}" is empty', $type)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$group = $this->_normalize($group); |
|
54
|
|
|
if (!$group) { |
|
55
|
|
|
throw new ElementException(__d('core_dev', 'Element group "{0}" is empty', $group)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$this->_findElement($group, $type); |
|
59
|
|
|
$className = $this->_getClassName($group, $type); |
|
60
|
|
|
|
|
61
|
|
|
if (!class_exists($className)) { |
|
62
|
|
|
throw new ElementException(__d('core_dev', 'Element class "{0}" not found!', $className)); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** @var Element $element */ |
|
66
|
|
|
$element = new $className($type, $group); |
|
67
|
|
|
|
|
68
|
|
|
$config = Hash::merge($config, [ |
|
69
|
|
|
'id' => $element->isCore() ? '_' . Str::low($type) : Text::uuid(), |
|
70
|
|
|
'name' => $element->getName(), |
|
71
|
|
|
'type' => $type, |
|
72
|
|
|
'group' => $group, |
|
73
|
|
|
'description' => '', |
|
74
|
|
|
]); |
|
75
|
|
|
|
|
76
|
|
|
$element->id = $config['id']; |
|
77
|
|
|
$element->setConfig($config); |
|
78
|
|
|
|
|
79
|
|
|
if ($entity !== null) { |
|
80
|
|
|
$element->setEntity($entity); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$element->initialize(); |
|
84
|
|
|
|
|
85
|
|
|
return $element; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Find and load element. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $group |
|
92
|
|
|
* @param string $type |
|
93
|
|
|
* @return bool|string |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function _findElement($group, $type) |
|
96
|
|
|
{ |
|
97
|
|
|
$className = $this->_getClassName($group, $type); |
|
98
|
|
|
|
|
99
|
|
|
if (!class_exists($className)) { |
|
100
|
|
|
$loader = new ClassLoader(); |
|
101
|
|
|
$paths = Configure::read('App.paths.elements'); |
|
102
|
|
|
foreach ($paths as $path) { |
|
103
|
|
|
$path = FS::clean($path . '/' . $group . '/' . $type, '/'); |
|
104
|
|
|
$loader->addNamespace("\\{$group}\\{$type}", $path); |
|
105
|
|
|
$result = $loader->loadClass("{$group}\\{$type}\\{$type}Element"); |
|
106
|
|
|
if ($result !== false) { |
|
107
|
|
|
return $result; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return false; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Get current class name. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $group |
|
119
|
|
|
* @param string $type |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function _getClassName($group, $type) |
|
123
|
|
|
{ |
|
124
|
|
|
return "\\Elements\\{$group}\\{$type}Element"; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Normalize string format. |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $string |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
|
|
protected function _normalize($string) |
|
134
|
|
|
{ |
|
135
|
|
|
return ucfirst(Str::low($string)); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|