| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- <?php
- /*
- * This file is part of Composer.
- *
- * (c) Nils Adermann <naderman@naderman.de>
- * Jordi Boggiano <j.boggiano@seld.be>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Composer\Autoload;
- use Composer\Installer\InstallationManager;
- use Composer\Json\JsonFile;
- use Composer\Package\Loader\JsonLoader;
- use Composer\Package\PackageInterface;
- use Composer\Repository\RepositoryInterface;
- use Composer\Util\Filesystem;
- /**
- * @author Igor Wiedler <igor@wiedler.ch>
- * @author Jordi Boggiano <j.boggiano@seld.be>
- */
- class AutoloadGenerator
- {
- public function dump(RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir, $bcLinks = false)
- {
- $filesystem = new Filesystem();
- $filesystem->ensureDirectoryExists($installationManager->getVendorPath());
- $filesystem->ensureDirectoryExists($targetDir);
- $vendorPath = strtr(realpath($installationManager->getVendorPath()), '\\', '/');
- $relVendorPath = $filesystem->findShortestPath(getcwd(), $vendorPath, true);
- $vendorDirCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true);
- $appBaseDirCode = $filesystem->findShortestPathCode($vendorPath, getcwd(), true);
- $appBaseDirCode = str_replace('__DIR__', '$vendorDir', $appBaseDirCode);
- $namespacesFile = <<<EOF
- <?php
- // autoload_namespace.php generated by Composer
- \$vendorDir = $vendorDirCode;
- \$baseDir = $appBaseDirCode;
- return array(
- EOF;
- $packageMap = $this->buildPackageMap($installationManager, $mainPackage, $localRepo->getPackages());
- $autoloads = $this->parseAutoloads($packageMap);
- foreach ($autoloads['psr-0'] as $namespace => $paths) {
- $exportedPaths = array();
- foreach ($paths as $path) {
- $exportedPaths[] = $this->getPathCode($filesystem, $relVendorPath, $vendorPath, $path);
- }
- $exportedPrefix = var_export($namespace, true);
- $namespacesFile .= " $exportedPrefix => ";
- if (count($exportedPaths) > 1) {
- $namespacesFile .= "array(".implode(', ', $exportedPaths)."),\n";
- } else {
- $namespacesFile .= $exportedPaths[0].",\n";
- }
- }
- $namespacesFile .= ");\n";
- $classmapFile = <<<EOF
- <?php
- // autoload_classmap.php generated by Composer
- \$vendorDir = $vendorDirCode;
- \$baseDir = $appBaseDirCode;
- return array(
- EOF;
- // flatten array
- $autoloads['classmap'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['classmap']));
- foreach ($autoloads['classmap'] as $dir) {
- foreach (ClassMapGenerator::createMap($dir) as $class => $path) {
- $path = '/'.$filesystem->findShortestPath(getcwd(), $path, true);
- $classmapFile .= ' '.var_export($class, true).' => $baseDir . '.var_export($path, true).",\n";
- }
- }
- $classmapFile .= ");\n";
- file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile);
- file_put_contents($targetDir.'/autoload_classmap.php', $classmapFile);
- if ($includePathFile = $this->getIncludePathsFile($packageMap, $filesystem, $relVendorPath, $vendorPath, $vendorDirCode, $appBaseDirCode)) {
- file_put_contents($targetDir.'/include_paths.php', $includePathFile);
- }
- file_put_contents($targetDir.'/autoload.php', $this->getAutoloadFile(true, true, (Boolean) $includePathFile));
- copy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php');
- // TODO BC feature, add E_DEPRECATED in autoload.php on April 30th, remove after May 30th
- if ($bcLinks) {
- $filesystem->ensureDirectoryExists($targetDir.'/.composer');
- file_put_contents($targetDir.'/.composer/autoload_namespaces.php', "<?php\n// Deprecated file, use the one in root of vendor dir\nreturn include dirname(__DIR__).'/autoload_namespaces.php';\n");
- file_put_contents($targetDir.'/.composer/autoload_classmap.php', "<?php\n// Deprecated file, use the one in root of vendor dir\nreturn include dirname(__DIR__).'/autoload_classmap.php';\n");
- file_put_contents($targetDir.'/.composer/autoload.php', "<?php\n// Deprecated file, use the one in root of vendor dir\nreturn include dirname(__DIR__).'/autoload.php';\n");
- file_put_contents($targetDir.'/.composer/ClassLoader.php', "<?php\n// Deprecated file, use the one in root of vendor dir\nreturn include dirname(__DIR__).'/ClassLoader.php';\n");
- if ($includePathFile) {
- file_put_contents($targetDir.'/.composer/include_paths.php', "<?php\n// Deprecated file, use the one in root of vendor dir\nreturn include dirname(__DIR__).'/include_paths.php';\n");
- }
- }
- }
- public function buildPackageMap(InstallationManager $installationManager, PackageInterface $mainPackage, array $packages)
- {
- // build package => install path map
- $packageMap = array();
- // add main package
- $packageMap[] = array($mainPackage, '');
- foreach ($packages as $package) {
- $packageMap[] = array(
- $package,
- $installationManager->getInstallPath($package)
- );
- }
- return $packageMap;
- }
- /**
- * Compiles an ordered list of namespace => path mappings
- *
- * @param array $packageMap array of array(package, installDir-relative-to-composer.json)
- * @return array array('psr-0' => array('Ns\\Foo' => array('installDir')))
- */
- public function parseAutoloads(array $packageMap)
- {
- $autoloads = array('classmap' => array(), 'psr-0' => array());
- foreach ($packageMap as $item) {
- list($package, $installPath) = $item;
- if (null !== $package->getTargetDir()) {
- $installPath = substr($installPath, 0, -strlen('/'.$package->getTargetDir()));
- }
- foreach ($package->getAutoload() as $type => $mapping) {
- // skip misconfigured packages
- if (!is_array($mapping)) {
- continue;
- }
- foreach ($mapping as $namespace => $paths) {
- foreach ((array) $paths as $path) {
- $autoloads[$type][$namespace][] = empty($installPath) ? $path : $installPath.'/'.$path;
- }
- }
- }
- }
- foreach ($autoloads as $type => $maps) {
- krsort($autoloads[$type]);
- }
- return $autoloads;
- }
- /**
- * Registers an autoloader based on an autoload map returned by parseAutoloads
- *
- * @param array $autoloads see parseAutoloads return value
- * @return ClassLoader
- */
- public function createLoader(array $autoloads)
- {
- $loader = new ClassLoader();
- if (isset($autoloads['psr-0'])) {
- foreach ($autoloads['psr-0'] as $namespace => $path) {
- $loader->add($namespace, $path);
- }
- }
- return $loader;
- }
- protected function getIncludePathsFile(array $packageMap, Filesystem $filesystem, $relVendorPath, $vendorPath, $vendorDirCode, $appBaseDirCode)
- {
- $includePaths = array();
- foreach ($packageMap as $item) {
- list($package, $installPath) = $item;
- if (null !== $package->getTargetDir()) {
- $installPath = substr($installPath, 0, -strlen('/'.$package->getTargetDir()));
- }
- foreach ($package->getIncludePaths() as $includePath) {
- $includePath = trim($includePath, '/');
- $includePaths[] = empty($installPath) ? $includePath : $installPath.'/'.$includePath;
- }
- }
- if (!$includePaths) {
- return;
- }
- $includePathsFile = <<<EOF
- <?php
- // include_paths.php generated by Composer
- \$vendorDir = $vendorDirCode;
- \$baseDir = $appBaseDirCode;
- return array(
- EOF;
- foreach ($includePaths as $path) {
- $includePathsFile .= " " . $this->getPathCode($filesystem, $relVendorPath, $vendorPath, $path) . ",\n";
- }
- return $includePathsFile . ");\n";
- }
- protected function getPathCode(Filesystem $filesystem, $relVendorPath, $vendorPath, $path)
- {
- $path = strtr($path, '\\', '/');
- $baseDir = '';
- if (!$filesystem->isAbsolutePath($path)) {
- if (strpos($path, $relVendorPath) === 0) {
- // path starts with vendor dir
- $path = substr($path, strlen($relVendorPath));
- $baseDir = '$vendorDir . ';
- } else {
- $path = '/'.$path;
- $baseDir = '$baseDir . ';
- }
- } elseif (strpos($path, $vendorPath) === 0) {
- $path = substr($path, strlen($vendorPath));
- $baseDir = '$vendorDir . ';
- }
- return $baseDir.var_export($path, true);
- }
- protected function getAutoloadFile($usePSR0, $useClassMap, $useIncludePath)
- {
- $file = <<<'HEADER'
- <?php
- // autoload.php generated by Composer
- if (!class_exists('Composer\\Autoload\\ClassLoader', false)) {
- require __DIR__.'/ClassLoader.php';
- }
- return call_user_func(function() {
- $loader = new \Composer\Autoload\ClassLoader();
- HEADER;
- if ($useIncludePath) {
- $file .= <<<'INCLUDE_PATH'
- $includePaths = require __DIR__.'/include_paths.php';
- array_unshift($includePaths, get_include_path());
- set_include_path(join(PATH_SEPARATOR, $includePaths));
- INCLUDE_PATH;
- }
- if ($usePSR0) {
- $file .= <<<'PSR0'
- $map = require __DIR__.'/autoload_namespaces.php';
- foreach ($map as $namespace => $path) {
- $loader->add($namespace, $path);
- }
- PSR0;
- }
- if ($useClassMap) {
- $file .= <<<'CLASSMAP'
- $classMap = require __DIR__.'/autoload_classmap.php';
- if ($classMap) {
- $loader->addClassMap($classMap);
- }
- CLASSMAP;
- }
- return $file . <<<'FOOTER'
- $loader->register();
- return $loader;
- });
- FOOTER;
- }
- }
|