1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Models\SmartProperties\Definitions\Builders; |
4
|
|
|
|
5
|
|
|
use ByTIC\Models\SmartProperties\Definitions\Definition; |
6
|
|
|
use Nip\Records\AbstractModels\RecordManager; |
7
|
|
|
use Nip\Utility\Str; |
8
|
|
|
use RecursiveDirectoryIterator; |
9
|
|
|
use RecursiveIteratorIterator; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class FolderBuilders |
13
|
|
|
* @package ByTIC\Models\SmartProperties\Definitions\Builders |
14
|
|
|
*/ |
15
|
|
|
class FolderBuilders |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var RecordManager |
19
|
|
|
*/ |
20
|
|
|
protected $manager; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Definition |
24
|
|
|
*/ |
25
|
|
|
protected $definition; |
26
|
|
|
|
27
|
|
|
protected $itemsDirectory = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param RecordManager $manager |
31
|
|
|
* @param string $field |
32
|
|
|
* @param null|string $name |
33
|
|
|
*/ |
34
|
|
|
public static function create($manager, $field, $name = null): Definition |
35
|
|
|
{ |
36
|
|
|
$builder = new static(); |
37
|
|
|
$builder->manager = $manager; |
38
|
|
|
$builder->definition = $builder->newDefinition($manager); |
39
|
|
|
$builder->definition->setField($field); |
40
|
|
|
if ($name) { |
41
|
|
|
$builder->definition->setName($name); |
42
|
|
|
} |
43
|
|
|
$builder->definition->setPlaces($builder->buildPlaces()); |
44
|
|
|
return $builder->definition; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param $manager |
50
|
|
|
* @return Definition |
51
|
|
|
*/ |
52
|
|
|
protected function newDefinition($manager): Definition |
53
|
|
|
{ |
54
|
|
|
$definition = new Definition(); |
55
|
|
|
$definition->setManager($manager); |
56
|
|
|
|
57
|
|
|
return $definition; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
protected function buildPlaces() |
64
|
|
|
{ |
65
|
|
|
$names = $this->getItemsNamesFromManager(); |
66
|
|
|
|
67
|
|
|
$names = $names ?: $this->getItemsNamesFromFiles(); |
68
|
|
|
|
69
|
|
|
foreach ($names as $key => $name) { |
70
|
|
|
if ($this->isAbstractItemName($name)) { |
71
|
|
|
unset($names[$key]); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
return $names; |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
protected function getItemsNamesFromFiles(): array |
82
|
|
|
{ |
83
|
|
|
$directory = $this->getItemsDirectory(); |
84
|
|
|
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)); |
85
|
|
|
$names = []; |
86
|
|
|
foreach ($files as $file) { |
87
|
|
|
if ($file->isDir()) { |
88
|
|
|
continue; |
89
|
|
|
} |
90
|
|
|
$name = str_replace($directory, '', $file->getPathname()); |
91
|
|
|
$name = str_replace('.php', '', $name); |
92
|
|
|
$names[] = trim($name, DIRECTORY_SEPARATOR . '\\'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return array_unique($names); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return array|boolean |
100
|
|
|
*/ |
101
|
|
|
protected function getItemsNamesFromManager() |
102
|
|
|
{ |
103
|
|
|
$methodName = 'get' . $this->definition->getName() . 'Names'; |
104
|
|
|
if (method_exists($this->manager, $methodName)) { |
105
|
|
|
return $this->manager->$methodName(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return false; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $name |
113
|
|
|
* |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
protected function isAbstractItemName(string $name): bool |
117
|
|
|
{ |
118
|
|
|
if (in_array($name, ['Abstract', 'Generic'])) { |
119
|
|
|
return true; |
120
|
|
|
} |
121
|
|
|
if (strpos($name, 'Abstract') === 0) { |
122
|
|
|
return true; |
123
|
|
|
} |
124
|
|
|
if (Str::endsWith($name, 'Trait')) { |
125
|
|
|
return true; |
126
|
|
|
} |
127
|
|
|
if (strpos($name, '\Abstract') !== false) { |
128
|
|
|
return true; |
129
|
|
|
} |
130
|
|
|
if (strpos($name, DIRECTORY_SEPARATOR . 'Abstract') !== false) { |
131
|
|
|
return true; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return false; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return null|string |
139
|
|
|
*/ |
140
|
|
|
protected function getItemsDirectory() |
141
|
|
|
{ |
142
|
|
|
if ($this->itemsDirectory == null) { |
143
|
|
|
$this->initItemsDirectory(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $this->itemsDirectory; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param $dir |
151
|
|
|
*/ |
152
|
|
|
protected function setItemsDirectory($dir) |
153
|
|
|
{ |
154
|
|
|
$this->itemsDirectory = $dir; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
protected function initItemsDirectory() |
158
|
|
|
{ |
159
|
|
|
$this->setItemsDirectory($this->generateItemsDirectory()); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
protected function generateItemsDirectory() |
166
|
|
|
{ |
167
|
|
|
$methodName = 'get' . $this->definition->getName() . 'ItemsDirectory'; |
168
|
|
|
if (method_exists($this->manager, $methodName)) { |
169
|
|
|
return $this->manager->$methodName(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$methodName = 'get' . Str::plural($this->definition->getName()) . 'Directory'; |
|
|
|
|
173
|
|
|
if (method_exists($this->manager, $methodName)) { |
174
|
|
|
return $this->manager->$methodName(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $this->generateManagerDirectory() . DIRECTORY_SEPARATOR . $this->generatePropertyDirectory(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @return string |
182
|
|
|
*/ |
183
|
|
|
protected function generateManagerDirectory() |
184
|
|
|
{ |
185
|
|
|
$reflector = new \ReflectionObject($this->manager); |
186
|
|
|
|
187
|
|
|
return dirname($reflector->getFileName()); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return string |
192
|
|
|
*/ |
193
|
|
|
protected function generatePropertyDirectory() |
194
|
|
|
{ |
195
|
|
|
return $this->definition->getLabel(); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|