|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://flipboxfactory.com/software/flux/license |
|
6
|
|
|
* @link https://www.flipboxfactory.com/software/flux/ |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\flux\cp\services; |
|
10
|
|
|
|
|
11
|
|
|
use craft\helpers\StringHelper; |
|
12
|
|
|
use flipbox\flux\Flux; |
|
13
|
|
|
use flipbox\flux\records\Transformer; |
|
14
|
|
|
use yii\base\Component; |
|
15
|
|
|
use yii\base\Event; |
|
16
|
|
|
use yii\db\Query; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Flipbox Factory <[email protected]> |
|
20
|
|
|
* @since 1.0.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
class Transformers extends Component |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* |
|
26
|
|
|
*/ |
|
27
|
|
|
const EVENT_REGISTER_TRANSFORMERS = \flipbox\flux\services\Transformers::EVENT_REGISTER_TRANSFORMERS; |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @inheritdoc |
|
32
|
|
|
*/ |
|
33
|
|
|
public function init() |
|
34
|
|
|
{ |
|
35
|
|
|
parent::init(); |
|
36
|
|
|
|
|
37
|
|
|
// Init the transformer to ensure events are triggered |
|
38
|
|
|
Flux::getInstance()->getTransformers(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Returns an array of classes that have been registered for a particular scope. |
|
43
|
|
|
* @param string $scope |
|
44
|
|
|
* @return array |
|
45
|
|
|
*/ |
|
46
|
|
|
public function findAllClassesByScope(string $scope): array |
|
47
|
|
|
{ |
|
48
|
|
|
if (!Flux::getInstance()->isValidScope($scope)) { |
|
49
|
|
|
return []; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$classes = (new Query()) |
|
53
|
|
|
->select(['class']) |
|
54
|
|
|
->from([Transformer::tableName()]) |
|
55
|
|
|
->andWhere([ |
|
56
|
|
|
'scope' => $scope |
|
57
|
|
|
]) |
|
58
|
|
|
->column(); |
|
59
|
|
|
|
|
60
|
|
|
foreach ($this->eventReflection() as $name => $event) { |
|
61
|
|
|
$events = $this->scopeEvents($name, $event); |
|
62
|
|
|
|
|
63
|
|
|
// Are there events AND is it the scope we're looking for |
|
64
|
|
|
if (empty($events) || !array_key_exists($scope, $events)) { |
|
65
|
|
|
continue; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$classes = array_merge( |
|
69
|
|
|
$classes, |
|
70
|
|
|
array_keys($events[$scope]) |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $classes; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Returns an array of classes that have been registered for a particular scope. |
|
79
|
|
|
* @param string $scope |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
|
|
public function findAllHandlesByScopeAndClass(string $scope, string $class): array |
|
83
|
|
|
{ |
|
84
|
|
|
if (!Flux::getInstance()->isValidScope($scope)) { |
|
85
|
|
|
return []; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$transformers = Flux::getInstance()->getTransformers()->resolveAllByScopeAndClass( |
|
89
|
|
|
$scope, |
|
90
|
|
|
$class |
|
91
|
|
|
); |
|
92
|
|
|
|
|
93
|
|
|
return array_keys($transformers); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get all registered events |
|
99
|
|
|
* |
|
100
|
|
|
* @return array |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function eventReflection(): array |
|
103
|
|
|
{ |
|
104
|
|
|
try { |
|
105
|
|
|
$reflection = new \ReflectionClass(Event::class); |
|
106
|
|
|
|
|
107
|
|
|
$eventsProperty = $reflection->getProperty('_events'); |
|
108
|
|
|
$eventsProperty->setAccessible(true); |
|
109
|
|
|
|
|
110
|
|
|
return $eventsProperty->getValue(); |
|
111
|
|
|
|
|
112
|
|
|
} catch (\ReflectionException $exception) { |
|
113
|
|
|
Flux::warning( |
|
114
|
|
|
"Unabel to resolve events.", |
|
115
|
|
|
__METHOD__ |
|
116
|
|
|
); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return []; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param $name |
|
124
|
|
|
* @param $classEvents |
|
125
|
|
|
* @return array |
|
126
|
|
|
*/ |
|
127
|
|
|
protected function scopeEvents($name, $classEvents): array |
|
128
|
|
|
{ |
|
129
|
|
|
if (!StringHelper::matchWildcard(self::EVENT_REGISTER_TRANSFORMERS . ':*', $name)) { |
|
130
|
|
|
return []; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$scope = StringHelper::removeLeft($name, self::EVENT_REGISTER_TRANSFORMERS . ':'); |
|
134
|
|
|
|
|
135
|
|
|
// The scope must be valid |
|
136
|
|
|
if (!Flux::getInstance()->isValidScope($scope)) { |
|
137
|
|
|
return []; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return [$scope => $classEvents]; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|