Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Converter.php
Go to the documentation of this file.
1 <?php
7 declare(strict_types=1);
8 
10 
15 {
19  const KEY_SERVICES = 'services';
20  const KEY_METHOD = 'method';
21  const KEY_METHODS = 'methods';
22  const KEY_SYNCHRONOUS_INVOCATION_ONLY = 'synchronousInvocationOnly';
23  const KEY_ROUTES = 'routes';
26  private $allowedRouteMethods = [
32  ];
33 
39  public function convert($source)
40  {
41  $result = [self::KEY_SERVICES => []];
43  $services = $source->getElementsByTagName('service');
45  foreach ($services as $service) {
46  if (!$this->canConvertXmlNode($service)) {
47  continue;
48  }
49  $serviceClass = $this->getServiceClass($service);
50  $serviceMethod = $this->getServiceMethod($service);
51 
52  // Define the service method's key if this hasn't yet been defined
53  $this->initServiceMethodsKey($result, $serviceClass, $serviceMethod);
54  $this->mergeSynchronousInvocationMethodsData($service, $result, $serviceClass, $serviceMethod);
55  }
56  $result[self::KEY_ROUTES] = $this->convertRouteCustomizations($source);
57 
58  return $result;
59  }
60 
69  private function mergeSynchronousInvocationMethodsData(
70  \DOMElement $service,
71  array &$result,
72  $serviceClass,
73  $serviceMethod
74  ) {
75  $result[self::KEY_SERVICES][$serviceClass][self::KEY_METHODS][$serviceMethod] = array_merge(
76  $result[self::KEY_SERVICES][$serviceClass][self::KEY_METHODS][$serviceMethod],
77  [
78  self::KEY_SYNCHRONOUS_INVOCATION_ONLY => $this->isSynchronousMethodInvocationOnly($service)
79  ]
80  );
81  }
82 
87  private function canConvertXmlNode(\DOMElement $node)
88  {
89  if ($node->nodeType !== XML_ELEMENT_NODE) {
90  return false;
91  }
92 
93  if ($this->getServiceClass($node) === null) {
94  return false;
95  }
96 
97  if ($this->getServiceMethod($node) === null) {
98  return false;
99  }
100 
101  return true;
102  }
103 
111  private function initServiceMethodsKey(array &$result, $serviceClass, $serviceMethod)
112  {
113  if (!isset($result[self::KEY_SERVICES][$serviceClass])) {
114  $result[self::KEY_SERVICES][$serviceClass] = [self::KEY_METHODS => []];
115  }
116 
117  if (!isset($result[self::KEY_SERVICES][$serviceClass][self::KEY_METHODS][$serviceMethod])) {
118  $result[self::KEY_SERVICES][$serviceClass][self::KEY_METHODS][$serviceMethod] = [];
119  }
120  }
121 
126  private function getServiceClass(\DOMElement $service)
127  {
128  $serviceClass = $service->attributes->getNamedItem('class')->nodeValue;
129 
130  return mb_strlen((string) $serviceClass) === 0 ? null : $serviceClass;
131  }
132 
137  private function getServiceMethod(\DOMElement $service)
138  {
139  $serviceMethod = $service->attributes->getNamedItem('method')->nodeValue;
140 
141  return mb_strlen((string) $serviceMethod) === 0 ? null : $serviceMethod;
142  }
143 
148  private function isSynchronousMethodInvocationOnly(\DOMElement $serviceNode)
149  {
150  $synchronousInvocationOnlyNodes = $serviceNode->getElementsByTagName('synchronousInvocationOnly');
151 
152  return $this->isSynchronousInvocationOnlyTrue($synchronousInvocationOnlyNodes->item(0));
153  }
154 
159  private function isSynchronousInvocationOnlyTrue(\DOMElement $synchronousInvocationOnlyNode = null)
160  {
161  if ($synchronousInvocationOnlyNode === null) {
162  return false;
163  }
164 
165  if (mb_strlen((string) $synchronousInvocationOnlyNode->nodeValue) === 0) {
166  return true;
167  }
168 
169  return filter_var($synchronousInvocationOnlyNode->nodeValue, FILTER_VALIDATE_BOOLEAN);
170  }
171 
177  private function convertRouteCustomizations($source)
178  {
179  $customRoutes = [];
180  $routes = $source->getElementsByTagName('route');
182  foreach ($routes as $route) {
183  $routeUrl = $this->getRouteUrl($route);
184  $routeMethod = $this->getRouteMethod($route);
185  $routeAlias = $this->getRouteAlias($route);
186  if ($routeUrl && $routeMethod && $routeAlias) {
187  if (!isset($customRoutes[$routeAlias])) {
188  $customRoutes[$routeAlias] = [];
189  }
190  $customRoutes[$routeAlias][$routeMethod] = $routeUrl;
191  }
192  }
193  return $customRoutes;
194  }
195 
200  private function getRouteUrl($route)
201  {
202  $url = $route->attributes->getNamedItem('url')->nodeValue;
203  return mb_strlen((string) $url) === 0 ? null : $url;
204  }
205 
210  private function getRouteAlias($route)
211  {
212  $alias = $route->attributes->getNamedItem('alias')->nodeValue;
213  return mb_strlen((string) $alias) === 0 ? null : ltrim($alias, '/');
214  }
215 
220  private function getRouteMethod($route)
221  {
222  $method = $route->attributes->getNamedItem('method')->nodeValue;
223  $method = mb_strlen((string) $method) === 0 ? null : $method;
224  return ($this->validateRouteMethod($method)) ? $method : null;
225  }
226 
231  private function validateRouteMethod($method)
232  {
233  return in_array($method, $this->allowedRouteMethods);
234  }
235 }
$source
Definition: source.php:23
$method
Definition: info.phtml:13
if(!trim($html)) $alias
Definition: details.phtml:20