-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSubSystem.php
executable file
·155 lines (135 loc) · 3.72 KB
/
SubSystem.php
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php /** @noinspection ALL */
/**
* @package Goomento_Core
* @link https://github.com/Goomento/Core
*/
declare(strict_types=1);
namespace Goomento\Core;
use Goomento\Core\Helper\HooksHelper;
use Goomento\Core\Helper\State;
use Goomento\Core\Model\Registry;
use Magento\Framework\App\ActionInterface;
use Magento\Framework\App\Request\Http;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\ObjectManagerInterface;
class SubSystem implements SubSystemInterface
{
/**
* @var State
*/
private $stateHelper;
/**
* @var array
*/
private $systems;
/**
* @var ObjectManagerInterface
*/
private $objectManager;
/**
* @var ActionInterface|null
*/
private $action;
/**
* @var Http|null
*/
private $request;
/**
* @var Registry
*/
private $registry;
/**
* SubSystem constructor.
* @param State $stateHelper
* @param ObjectManagerInterface $objectManager
* @param Registry $registry
* @param array $systems
*/
public function __construct(
State $stateHelper,
ObjectManagerInterface $objectManager,
Registry $registry,
array $systems = []
) {
$this->stateHelper = $stateHelper;
$this->objectManager = $objectManager;
$this->systems = $systems;
$this->registry = $registry;
/**
* Construct the sub-system
*/
HooksHelper::doAction('construct');
}
/**
* @inheritdoc
*/
public function init(array $buildSubject)
{
$this->request = $buildSubject['request'];
$this->action = $buildSubject['controller_action'];
$this->registry->register('current_action', $this->action);
$this->registry->register('current_action_name', $this->request->getFullActionName());
foreach ($this->systems as $system) {
/** @var SubSystemInterface $system */
$system = $this->getSubsystem($system);
if ($this->matchingAreaScopes($system)) {
$system->init($buildSubject);
}
}
HooksHelper::doAction('init');
}
/**
* @param SubSystemInterface $subSystem
* @return bool
* @throws LocalizedException
*/
private function matchingAreaScopes(SubSystemInterface $subSystem) : bool
{
$scopes = (array) $subSystem->getAreaScopes();
$scopes = array_flip($scopes);
$currentState = $this->stateHelper->getAreaCode();
$fullActionName = $this->request->getFullActionName();
$isAjax = (bool) $this->request->isAjax();
if (!empty($scopes)) {
if (isset($scopes['*'])) {
return true;
} elseif ($isAjax && isset($scopes['ajax'])) {
return true;
} elseif (in_array($currentState, ['adminhtml', 'frontend']) && isset($scopes[$currentState])) {
return true;
} elseif (isset($scopes[$fullActionName])) {
return true;
}
}
return false;
}
/**
* @param $name
* @return SubSystemInterface
* @throws LocalizedException
*/
private function getSubsystem($name)
{
$object = $this->objectManager->get($name);
if (!($object instanceof SubSystemInterface)) {
throw new LocalizedException(
__('Class `%1` must implement `SubSystemInterface`', $name)
);
}
return $object;
}
/**
* @inheritdoc
*/
public function getAreaScopes()
{
return ['*'];
}
/**
* Shutdown the program
*/
public function __destruct()
{
HooksHelper::doAction('destruct');
}
}