xrstfComposer52AutoloadGenerator

The Yoast SEO xrstf Composer52 AutoloadGenerator class.

Defined (1)

The class is defined in the following location(s).

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