Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
VirtualTypeMapper.php
Go to the documentation of this file.
1 <?php
9 
10 use DOMDocument;
12 
14 {
18  private $map = [];
19 
23  private static $mainScope = 'global';
24 
29  public function __construct(array $map = [])
30  {
31  $this->map = $map;
32  }
33 
39  public function getType($name, $scope = null)
40  {
41  if (empty($this->map)) {
42  $this->loadMap();
43  }
44 
45  $scopes = [];
46  if ($scope !== null && $scope !== self::$mainScope) {
47  array_unshift($scopes, $scope);
48  }
49  $scopes[] = self::$mainScope;
50  foreach ($scopes as $scp) {
51  if (isset($this->map[$scp][$name])) {
52  return $this->map[$scp][$name];
53  }
54  }
55 
56  return $name;
57  }
58 
63  public function getScopeFromFile($file)
64  {
65  $basename = basename(pathinfo($file, PATHINFO_DIRNAME));
66  return $basename === 'etc' ? 'global' : $basename;
67  }
68 
73  private function loadDiConfigs()
74  {
75  return Files::init()->getDiConfigs();
76  }
77 
83  public function loadMap(array $diFiles = [])
84  {
85  if (empty($diFiles)) {
86  $diFiles = $this->loadDiConfigs();
87  }
88 
89  foreach ($diFiles as $file) {
90  $scope = $this->getScopeFromFile($file);
91  $doc = new DOMDocument();
92  $doc->loadXML(file_get_contents($file));
93  $nodes = $doc->getElementsByTagName('virtualType');
95  foreach ($nodes as $node) {
96  $name = $node->getAttribute('name');
97  $type = $node->getAttribute('type');
98  if (empty($type)) {
99  continue;
100  }
101  $this->map[$scope][$name] = $type;
102  }
103  }
104 
105  return $this->map;
106  }
107 }
$type
Definition: item.phtml:13
if(!isset($_GET['name'])) $name
Definition: log.php:14