|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2015 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Framework\Code\Reader; |
| 7 | + |
| 8 | +class SourceArgumentsReader |
| 9 | +{ |
| 10 | + /** |
| 11 | + * Namespace separator |
| 12 | + */ |
| 13 | + const NS_SEPARATOR = '\\'; |
| 14 | + |
| 15 | + /** |
| 16 | + * Read constructor argument types from source code and perform namespace resolution if required. |
| 17 | + * |
| 18 | + * @param \ReflectionClass $class |
| 19 | + * @param bool $inherited |
| 20 | + * @return array List of constructor argument types. |
| 21 | + */ |
| 22 | + public function getConstructorArgumentTypes(\ReflectionClass $class, $inherited = false) |
| 23 | + { |
| 24 | + $output = [null]; |
| 25 | + if (!$class->getFileName() || false == $class->hasMethod( |
| 26 | + '__construct' |
| 27 | + ) || !$inherited && $class->getConstructor()->class !== $class->getName() |
| 28 | + ) { |
| 29 | + return $output; |
| 30 | + } |
| 31 | + $reflectionConstructor = $class->getConstructor(); |
| 32 | + $fileContent = file($class->getFileName()); |
| 33 | + $availableNamespaces = $this->getImportedNamespaces($fileContent); |
| 34 | + $availableNamespaces[0] = $class->getNamespaceName(); |
| 35 | + $constructorStartLine = $reflectionConstructor->getStartLine() - 1; |
| 36 | + $constructorEndLine = $reflectionConstructor->getEndLine(); |
| 37 | + $fileContent = array_slice($fileContent, $constructorStartLine, $constructorEndLine - $constructorStartLine); |
| 38 | + $source = '<?php ' . trim(implode('', $fileContent)); |
| 39 | + $methodTokenized = token_get_all($source); |
| 40 | + $argumentsStart = array_search('(', $methodTokenized) + 1; |
| 41 | + $argumentsEnd = array_search(')', $methodTokenized); |
| 42 | + $arguments = array_slice($methodTokenized, $argumentsStart, $argumentsEnd - $argumentsStart); |
| 43 | + foreach ($arguments as &$argument) { |
| 44 | + is_array($argument) ?: $argument = [1 => $argument]; |
| 45 | + } |
| 46 | + unset($argument); |
| 47 | + $arguments = array_filter($arguments, function ($token) { |
| 48 | + $blacklist = [T_VARIABLE, T_WHITESPACE]; |
| 49 | + if (isset($token[0]) && in_array($token[0], $blacklist)) { |
| 50 | + return false; |
| 51 | + } |
| 52 | + return true; |
| 53 | + }); |
| 54 | + $arguments = array_map(function ($element) { |
| 55 | + return $element[1]; |
| 56 | + }, $arguments); |
| 57 | + $arguments = array_values($arguments); |
| 58 | + $arguments = implode('', $arguments); |
| 59 | + if (empty($arguments)) { |
| 60 | + return $output; |
| 61 | + } |
| 62 | + $arguments = explode(',', $arguments); |
| 63 | + foreach ($arguments as $key => &$argument) { |
| 64 | + $argument = $this->removeDefaultValue($argument); |
| 65 | + $argument = $this->resolveNamespaces($argument, $availableNamespaces); |
| 66 | + } |
| 67 | + unset($argument); |
| 68 | + return $arguments; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Perform namespace resolution if required and return fully qualified name. |
| 73 | + * |
| 74 | + * @param string $argument |
| 75 | + * @param array $availableNamespaces |
| 76 | + * @return string |
| 77 | + */ |
| 78 | + protected function resolveNamespaces($argument, $availableNamespaces) |
| 79 | + { |
| 80 | + if (substr($argument, 0, 1) !== self::NS_SEPARATOR && $argument !== 'array' && !empty($argument)) { |
| 81 | + $name = explode(self::NS_SEPARATOR, $argument); |
| 82 | + $unqualifiedName = $name[0]; |
| 83 | + $isQualifiedName = count($name) > 1 ? true : false; |
| 84 | + if (isset($availableNamespaces[$unqualifiedName])) { |
| 85 | + $namespace = $availableNamespaces[$unqualifiedName]; |
| 86 | + if ($isQualifiedName) { |
| 87 | + array_shift($name); |
| 88 | + return $namespace . self::NS_SEPARATOR . implode(self::NS_SEPARATOR, $name); |
| 89 | + } |
| 90 | + return $namespace; |
| 91 | + } else { |
| 92 | + return self::NS_SEPARATOR . $availableNamespaces[0] . self::NS_SEPARATOR . $argument; |
| 93 | + } |
| 94 | + } |
| 95 | + return $argument; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Remove default value from argument. |
| 100 | + * |
| 101 | + * @param string $argument |
| 102 | + * @return string |
| 103 | + */ |
| 104 | + protected function removeDefaultValue($argument) |
| 105 | + { |
| 106 | + $position = strpos($argument, '='); |
| 107 | + if (is_numeric($position)) { |
| 108 | + return substr($argument, 0, $position); |
| 109 | + } |
| 110 | + return $argument; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Get all imported namespaces. |
| 115 | + * |
| 116 | + * @param array $file |
| 117 | + * @return array |
| 118 | + */ |
| 119 | + protected function getImportedNamespaces(array $file) |
| 120 | + { |
| 121 | + $file = implode('', $file); |
| 122 | + $file = token_get_all($file); |
| 123 | + $classStart = array_search('{', $file); |
| 124 | + $file = array_slice($file, 0, $classStart); |
| 125 | + $output = []; |
| 126 | + foreach ($file as $position => $token) { |
| 127 | + if (is_array($token) && $token[0] === T_USE) { |
| 128 | + $import = array_slice($file, $position); |
| 129 | + $importEnd = array_search(';', $import); |
| 130 | + $import = array_slice($import, 0, $importEnd); |
| 131 | + $imports = []; |
| 132 | + $importsCount = 0; |
| 133 | + foreach ($import as $item) { |
| 134 | + if ($item === ',') { |
| 135 | + $importsCount++; |
| 136 | + continue; |
| 137 | + } |
| 138 | + $imports[$importsCount][] = $item; |
| 139 | + } |
| 140 | + foreach ($imports as $import) { |
| 141 | + $import = array_filter($import, function ($token) { |
| 142 | + $whitelist = [T_NS_SEPARATOR, T_STRING, T_AS]; |
| 143 | + if (isset($token[0]) && in_array($token[0], $whitelist)) { |
| 144 | + return true; |
| 145 | + } |
| 146 | + return false; |
| 147 | + }); |
| 148 | + $import = array_map(function ($element) { |
| 149 | + return $element[1]; |
| 150 | + }, $import); |
| 151 | + $import = array_values($import); |
| 152 | + if ($import[0] === self::NS_SEPARATOR) { |
| 153 | + array_shift($import); |
| 154 | + } |
| 155 | + $importName = null; |
| 156 | + if (in_array('as', $import)) { |
| 157 | + $importName = array_splice($import, -1)[0]; |
| 158 | + array_pop($import); |
| 159 | + } |
| 160 | + $useStatement = implode('', $import); |
| 161 | + if ($importName) { |
| 162 | + $output[$importName] = self::NS_SEPARATOR . $useStatement; |
| 163 | + } else { |
| 164 | + $key = explode(self::NS_SEPARATOR, $useStatement); |
| 165 | + $key = end($key); |
| 166 | + $output[$key] = self::NS_SEPARATOR . $useStatement; |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + return $output; |
| 172 | + } |
| 173 | +} |
0 commit comments