PageRenderTime 42ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wordpress-seo/vendor/xrstf/composer-php52/lib/xrstf/Composer52/AutoloadGenerator.php

https://gitlab.com/ngochuynh1991/cuacuon
PHP | 346 lines | 213 code | 86 blank | 47 comment | 24 complexity | 9911cda6268d7962e4b1795d73ac637e MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright (c) 2013, Christoph Mewes, http://www.xrstf.de
  4. *
  5. * This file is released under the terms of the MIT license. You can find the
  6. * complete text in the attached LICENSE file or online at:
  7. *
  8. * http://www.opensource.org/licenses/mit-license.php
  9. *
  10. * --------------------------------------------------------------------------
  11. *
  12. * 99% of this is copied as-is from the original Composer source code and is
  13. * released under MIT license as well. Copyright goes to:
  14. *
  15. * - Igor Wiedler <igor@wiedler.ch>
  16. * - Jordi Boggiano <j.boggiano@seld.be>
  17. */
  18. namespace xrstf\Composer52;
  19. use Composer\Autoload\AutoloadGenerator as BaseGenerator;
  20. use Composer\Autoload\ClassMapGenerator;
  21. use Composer\Config;
  22. use Composer\Installer\InstallationManager;
  23. use Composer\Package\AliasPackage;
  24. use Composer\Package\PackageInterface;
  25. use Composer\Repository\InstalledRepositoryInterface;
  26. use Composer\Util\Filesystem;
  27. class AutoloadGenerator extends BaseGenerator {
  28. /**
  29. * @var bool
  30. */
  31. private $classMapAuthoritative = false;
  32. public function __construct() {
  33. // do nothing (but keep this constructor so we can build an instance without the need for an event dispatcher)
  34. }
  35. /**
  36. * Whether or not generated autoloader considers the class map
  37. * authoritative.
  38. *
  39. * @param bool $classMapAuthoritative
  40. */
  41. public function setClassMapAuthoritative($classMapAuthoritative)
  42. {
  43. $this->classMapAuthoritative = (boolean) $classMapAuthoritative;
  44. }
  45. public function dump(Config $config, InstalledRepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir, $scanPsr0Packages = false, $suffix = '') {
  46. if ($this->classMapAuthoritative) {
  47. // Force scanPsr0Packages when classmap is authoritative
  48. $scanPsr0Packages = true;
  49. }
  50. $filesystem = new Filesystem();
  51. $filesystem->ensureDirectoryExists($config->get('vendor-dir'));
  52. $cwd = getcwd();
  53. $basePath = $filesystem->normalizePath($cwd);
  54. $vendorPath = $filesystem->normalizePath(realpath($config->get('vendor-dir')));
  55. $targetDir = $vendorPath.'/'.$targetDir;
  56. $filesystem->ensureDirectoryExists($targetDir);
  57. $useGlobalIncludePath = (bool) $config->get('use-include-path');
  58. $prependAutoloader = $config->get('prepend-autoloader') === false ? 'false' : 'true';
  59. $classMapAuthoritative = $config->get('classmap-authoritative');
  60. $vendorPathCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true);
  61. $vendorPathToTargetDirCode = $filesystem->findShortestPathCode($vendorPath, realpath($targetDir), true);
  62. $appBaseDirCode = $filesystem->findShortestPathCode($vendorPath, $basePath, true);
  63. $appBaseDirCode = str_replace('__DIR__', '$vendorDir', $appBaseDirCode);
  64. // add 5.2 compat
  65. $vendorPathCode = str_replace('__DIR__', 'dirname(__FILE__)', $vendorPathCode);
  66. $vendorPathToTargetDirCode = str_replace('__DIR__', 'dirname(__FILE__)', $vendorPathToTargetDirCode);
  67. $packageMap = $this->buildPackageMap($installationManager, $mainPackage, $localRepo->getCanonicalPackages());
  68. $autoloads = $this->parseAutoloads($packageMap, $mainPackage);
  69. // add custom psr-0 autoloading if the root package has a target dir
  70. $targetDirLoader = null;
  71. $mainAutoload = $mainPackage->getAutoload();
  72. if ($mainPackage->getTargetDir() && !empty($mainAutoload['psr-0'])) {
  73. $levels = count(explode('/', $filesystem->normalizePath($mainPackage->getTargetDir())));
  74. $prefixes = implode(', ', array_map(function ($prefix) {
  75. return var_export($prefix, true);
  76. }, array_keys($mainAutoload['psr-0'])));
  77. $baseDirFromTargetDirCode = $filesystem->findShortestPathCode($targetDir, $basePath, true);
  78. $targetDirLoader = <<<EOF
  79. public static function autoload(\$class) {
  80. \$dir = $baseDirFromTargetDirCode.'/';
  81. \$prefixes = array($prefixes);
  82. foreach (\$prefixes as \$prefix) {
  83. if (0 !== strpos(\$class, \$prefix)) {
  84. continue;
  85. }
  86. \$path = explode(DIRECTORY_SEPARATOR, self::getClassPath(\$class));
  87. \$path = \$dir.implode('/', array_slice(\$path, $levels));
  88. if (!\$path = self::resolveIncludePath(\$path)) {
  89. return false;
  90. }
  91. require \$path;
  92. return true;
  93. }
  94. }
  95. EOF;
  96. }
  97. $filesCode = "";
  98. $autoloads['files'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['files']));
  99. foreach ($autoloads['files'] as $functionFile) {
  100. // don't include file if it is using PHP 5.3+ syntax
  101. // https://bitbucket.org/xrstf/composer-php52/issue/4
  102. if ($this->isPHP53($functionFile)) {
  103. $filesCode .= '// require '.$this->getPathCode($filesystem, $basePath, $vendorPath, $functionFile)."; // disabled because of PHP 5.3 syntax\n";
  104. }
  105. else {
  106. $filesCode .= ' require '.$this->getPathCode($filesystem, $basePath, $vendorPath, $functionFile).";\n";
  107. }
  108. }
  109. if (!$suffix) {
  110. $suffix = md5(uniqid('', true));
  111. }
  112. $includePathFile = $this->getIncludePathsFile($packageMap, $filesystem, $basePath, $vendorPath, $vendorPathCode, $appBaseDirCode);
  113. file_put_contents($vendorPath.'/autoload_52.php', $this->getAutoloadFile($vendorPathToTargetDirCode, $suffix));
  114. file_put_contents($targetDir.'/autoload_real_52.php', $this->getAutoloadRealFile(true, (bool) $includePathFile, $targetDirLoader, $filesCode, $vendorPathCode, $appBaseDirCode, $suffix, $useGlobalIncludePath, $prependAutoloader));
  115. // use stream_copy_to_stream instead of copy
  116. // to work around https://bugs.php.net/bug.php?id=64634
  117. $sourceLoader = fopen(__DIR__.'/ClassLoader.php', 'r');
  118. $targetLoader = fopen($targetDir.'/ClassLoader52.php', 'w+');
  119. stream_copy_to_stream($sourceLoader, $targetLoader);
  120. fclose($sourceLoader);
  121. fclose($targetLoader);
  122. unset($sourceLoader, $targetLoader);
  123. }
  124. protected function isPHP53($file) {
  125. $tokens = token_get_all(file_get_contents($file));
  126. $php53 = array(T_DIR, T_GOTO, T_NAMESPACE, T_NS_C, T_NS_SEPARATOR, T_USE);
  127. // PHP 5.4+
  128. if (defined('T_TRAIT')) {
  129. $php53[] = T_TRAIT;
  130. $php53[] = T_TRAIT_C;
  131. $php53[] = T_TRAIT_C;
  132. }
  133. // PHP 5.5+
  134. if (defined('T_FINALLY')) {
  135. $php53[] = T_FINALLY;
  136. $php53[] = T_YIELD;
  137. }
  138. foreach ($tokens as $token) {
  139. if (is_array($token) && in_array($token[0], $php53)) {
  140. return true;
  141. }
  142. }
  143. return false;
  144. }
  145. protected function getIncludePathsFile(array $packageMap, Filesystem $filesystem, $basePath, $vendorPath, $vendorPathCode, $appBaseDirCode) {
  146. $includePaths = array();
  147. foreach ($packageMap as $item) {
  148. list($package, $installPath) = $item;
  149. if (null !== $package->getTargetDir() && strlen($package->getTargetDir()) > 0) {
  150. $installPath = substr($installPath, 0, -strlen('/'.$package->getTargetDir()));
  151. }
  152. foreach ($package->getIncludePaths() as $includePath) {
  153. $includePath = trim($includePath, '/');
  154. $includePaths[] = empty($installPath) ? $includePath : $installPath.'/'.$includePath;
  155. }
  156. }
  157. if (!$includePaths) {
  158. return;
  159. }
  160. $includePathsFile = <<<EOF
  161. <?php
  162. // include_paths_52.php generated by xrstf/composer-php52
  163. \$vendorDir = $vendorPathCode;
  164. \$baseDir = $appBaseDirCode;
  165. return array(
  166. EOF;
  167. foreach ($includePaths as $path) {
  168. $includePathsFile .= "\t" . $this->getPathCode($filesystem, $basePath, $vendorPath, $path) . ",\n";
  169. }
  170. return $includePathsFile . ");\n";
  171. }
  172. protected function getAutoloadFile($vendorPathToTargetDirCode, $suffix) {
  173. return <<<AUTOLOAD
  174. <?php
  175. // autoload_52.php generated by xrstf/composer-php52
  176. require_once $vendorPathToTargetDirCode.'/autoload_real_52.php';
  177. return ComposerAutoloaderInit$suffix::getLoader();
  178. AUTOLOAD;
  179. }
  180. protected function getAutoloadRealFile($useClassMap, $useIncludePath, $targetDirLoader, $filesCode, $vendorPathCode, $appBaseDirCode, $suffix, $useGlobalIncludePath, $prependAutoloader) {
  181. // TODO the class ComposerAutoloaderInit should be revert to a closure
  182. // when APC has been fixed:
  183. // - https://github.com/composer/composer/issues/959
  184. // - https://bugs.php.net/bug.php?id=52144
  185. // - https://bugs.php.net/bug.php?id=61576
  186. // - https://bugs.php.net/bug.php?id=59298
  187. if ($filesCode) {
  188. $filesCode = "\n\n".rtrim($filesCode);
  189. }
  190. $file = <<<HEADER
  191. <?php
  192. // autoload_real_52.php generated by xrstf/composer-php52
  193. class ComposerAutoloaderInit$suffix {
  194. private static \$loader;
  195. public static function loadClassLoader(\$class) {
  196. if ('xrstf_Composer52_ClassLoader' === \$class) {
  197. require dirname(__FILE__).'/ClassLoader52.php';
  198. }
  199. }
  200. /**
  201. * @return xrstf_Composer52_ClassLoader
  202. */
  203. public static function getLoader() {
  204. if (null !== self::\$loader) {
  205. return self::\$loader;
  206. }
  207. spl_autoload_register(array('ComposerAutoloaderInit$suffix', 'loadClassLoader'), true /*, true */);
  208. self::\$loader = \$loader = new xrstf_Composer52_ClassLoader();
  209. spl_autoload_unregister(array('ComposerAutoloaderInit$suffix', 'loadClassLoader'));
  210. \$vendorDir = $vendorPathCode;
  211. \$baseDir = $appBaseDirCode;
  212. \$dir = dirname(__FILE__);
  213. HEADER;
  214. if ($useIncludePath) {
  215. $file .= <<<'INCLUDE_PATH'
  216. $includePaths = require $dir.'/include_paths.php';
  217. array_push($includePaths, get_include_path());
  218. set_include_path(implode(PATH_SEPARATOR, $includePaths));
  219. INCLUDE_PATH;
  220. }
  221. $file .= <<<'PSR0'
  222. $map = require $dir.'/autoload_namespaces.php';
  223. foreach ($map as $namespace => $path) {
  224. $loader->add($namespace, $path);
  225. }
  226. PSR0;
  227. if ($useClassMap) {
  228. $file .= <<<'CLASSMAP'
  229. $classMap = require $dir.'/autoload_classmap.php';
  230. if ($classMap) {
  231. $loader->addClassMap($classMap);
  232. }
  233. CLASSMAP;
  234. }
  235. if ($this->classMapAuthoritative) {
  236. $file .= <<<'CLASSMAPAUTHORITATIVE'
  237. $loader->setClassMapAuthoritative(true);
  238. CLASSMAPAUTHORITATIVE;
  239. }
  240. if ($useGlobalIncludePath) {
  241. $file .= <<<'INCLUDEPATH'
  242. $loader->setUseIncludePath(true);
  243. INCLUDEPATH;
  244. }
  245. if ($targetDirLoader) {
  246. $file .= <<<REGISTER_AUTOLOAD
  247. spl_autoload_register(array('ComposerAutoloaderInit$suffix', 'autoload'), true);
  248. REGISTER_AUTOLOAD;
  249. }
  250. $file .= <<<METHOD_FOOTER
  251. \$loader->register($prependAutoloader);{$filesCode}
  252. return \$loader;
  253. }
  254. METHOD_FOOTER;
  255. $file .= $targetDirLoader;
  256. return $file . <<<FOOTER
  257. }
  258. FOOTER;
  259. }
  260. }