Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
NamespaceResolver.php
Go to the documentation of this file.
1 <?php
8 
13 {
17  const NS_SEPARATOR = '\\';
18 
22  private $scalarTypesProvider;
23 
27  private $namespaces = [];
28 
33  public function __construct(ScalarTypesProvider $scalarTypesProvider = null)
34  {
35  $this->scalarTypesProvider = $scalarTypesProvider ?: new ScalarTypesProvider();
36  }
37 
45  public function resolveNamespace($type, array $availableNamespaces)
46  {
47  if (substr($type, 0, 1) !== self::NS_SEPARATOR
48  && !in_array($type, $this->scalarTypesProvider->getTypes())
49  && !empty($type)
50  ) {
51  $name = explode(self::NS_SEPARATOR, $type);
52  $unqualifiedName = $name[0];
53  $isQualifiedName = count($name) > 1 ? true : false;
54  if (isset($availableNamespaces[$unqualifiedName])) {
55  $namespace = $availableNamespaces[$unqualifiedName];
56  if ($isQualifiedName) {
57  array_shift($name);
58  return $namespace . self::NS_SEPARATOR . implode(self::NS_SEPARATOR, $name);
59  }
60  return $namespace;
61  } else {
62  return self::NS_SEPARATOR . $availableNamespaces[0] . self::NS_SEPARATOR . $type;
63  }
64  }
65  return $type;
66  }
67 
75  public function getImportedNamespaces(array $fileContent)
76  {
77  $fileContent = implode('', $fileContent);
78 
79  $cacheKey = sha1($fileContent);
80 
81  if (isset($this->namespaces[$cacheKey])) {
82  return $this->namespaces[$cacheKey];
83  }
84 
85  $fileContent = token_get_all($fileContent);
86  $classStart = array_search('{', $fileContent);
87  $fileContent = array_slice($fileContent, 0, $classStart);
88  $output = [];
89  foreach ($fileContent as $position => $token) {
90  if (is_array($token) && $token[0] === T_USE) {
91  $import = array_slice($fileContent, $position);
92  $importEnd = array_search(';', $import);
93  $import = array_slice($import, 0, $importEnd);
94  $imports = [];
95  $importsCount = 0;
96  foreach ($import as $item) {
97  if ($item === ',') {
98  $importsCount++;
99  continue;
100  }
101  $imports[$importsCount][] = $item;
102  }
103  foreach ($imports as $import) {
104  $import = array_filter($import, function ($token) {
105  $whitelist = [T_NS_SEPARATOR, T_STRING, T_AS];
106  if (isset($token[0]) && in_array($token[0], $whitelist)) {
107  return true;
108  }
109  return false;
110  });
111  $import = array_map(function ($element) {
112  return $element[1];
113  }, $import);
114  $import = array_values($import);
115  if ($import[0] === self::NS_SEPARATOR) {
116  array_shift($import);
117  }
118  $importName = null;
119  if (in_array('as', $import)) {
120  $importName = array_splice($import, -1)[0];
121  array_pop($import);
122  }
123  $useStatement = implode('', $import);
124  if ($importName) {
125  $output[$importName] = self::NS_SEPARATOR . $useStatement;
126  } else {
127  $key = explode(self::NS_SEPARATOR, $useStatement);
128  $key = end($key);
129  $output[$key] = self::NS_SEPARATOR . $useStatement;
130  }
131  }
132  }
133  }
134  $this->namespaces[$cacheKey] = $output;
135  return $this->namespaces[$cacheKey];
136  }
137 }
resolveNamespace($type, array $availableNamespaces)
__construct(ScalarTypesProvider $scalarTypesProvider=null)
$type
Definition: item.phtml:13
if(!isset($_GET['name'])) $name
Definition: log.php:14
$element
Definition: element.phtml:12