1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Models\SmartProperties\Definitions\Definition; |
4
|
|
|
|
5
|
|
|
use Nip\Utility\Str; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Trait HasPropertiesNamespaces |
9
|
|
|
* @package ByTIC\Models\SmartProperties\Definitions\Definition |
10
|
|
|
*/ |
11
|
|
|
trait HasPropertiesNamespaces |
12
|
|
|
{ |
13
|
|
|
protected $propertiesNamespaces = null; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @return null |
17
|
|
|
*/ |
18
|
|
|
public function getPropertiesNamespaces() |
19
|
|
|
{ |
20
|
|
|
if ($this->propertiesNamespaces === null) { |
21
|
|
|
$this->initPropertiesNamespaces(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
return $this->propertiesNamespaces; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
protected function initPropertiesNamespaces() |
28
|
|
|
{ |
29
|
|
|
$this->propertiesNamespaces = $this->generatePropertiesNamespaces(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected function generatePropertiesNamespaces() |
33
|
|
|
{ |
34
|
|
|
$directories = (array)$this->generateItemsDirectory(); |
35
|
|
|
$rootNamespace = $this->getPropertyItemsRootNamespace(); |
36
|
|
|
|
37
|
|
|
$propertiesNamespaces = []; |
38
|
|
|
foreach ($directories as $namespace => $directory) { |
39
|
|
|
$namespace = Str::contains($namespace, '\\') ? $namespace : $rootNamespace; |
40
|
|
|
$namespace = trim($namespace, '\\'); |
41
|
|
|
$propertiesNamespaces[$namespace.'\\'] = $directory; |
42
|
|
|
} |
43
|
|
|
return $propertiesNamespaces; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
protected function generateItemsDirectory() |
50
|
|
|
{ |
51
|
|
|
$methodName = 'get' . $this->getName() . 'ItemsDirectory'; |
|
|
|
|
52
|
|
|
if (method_exists($this->manager, $methodName)) { |
53
|
|
|
return $this->manager->$methodName(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$methodName = 'get' . Str::plural($this->getName()) . 'Directory'; |
57
|
|
|
if (method_exists($this->manager, $methodName)) { |
58
|
|
|
return $this->manager->$methodName(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $this->generateManagerDirectory() . DIRECTORY_SEPARATOR . $this->generatePropertyDirectory(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
protected function generateManagerDirectory() |
68
|
|
|
{ |
69
|
|
|
$reflector = new \ReflectionObject($this->manager); |
70
|
|
|
|
71
|
|
|
return dirname($reflector->getFileName()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
protected function generatePropertyDirectory() |
78
|
|
|
{ |
79
|
|
|
return $this->getLabel(); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
protected function getPropertyItemsRootNamespace(): string |
86
|
|
|
{ |
87
|
|
|
$manager = $this->getManager(); |
|
|
|
|
88
|
|
|
$method = 'get' . $this->getName() . 'ItemsRootNamespace'; |
89
|
|
|
if (method_exists($manager, $method)) { |
90
|
|
|
return $manager->{$method}(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$method = 'get' . $this->getName() . 'Namespace'; |
94
|
|
|
if (method_exists($manager, $method)) { |
95
|
|
|
return $manager->{$method}(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $manager->getModelNamespace() . $this->getLabel() . '\\'; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|