xrstfComposer52AutoloadGenerator

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