123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?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\Downloader\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)
- {
- $autoloadFile = <<<'EOF'
- <?php
- // autoload.php generated by Composer
- if (!class_exists('Composer\\Autoload\\ClassLoader', false)) {
- require __DIR__.'/ClassLoader.php';
- }
- $__composer_autoload_init = function() {
- $loader = new \Composer\Autoload\ClassLoader();
- $map = require __DIR__.'/autoload_namespaces.php';
- foreach ($map as $namespace => $path) {
- $loader->add($namespace, $path);
- }
- $loader->register();
- return $loader;
- };
- return $__composer_autoload_init();
- EOF;
- $filesystem = new Filesystem();
- $vendorPath = strtr(realpath($installationManager->getVendorPath()), '\\', '/');
- $relVendorPath = ltrim(substr($vendorPath, strlen(getcwd())), '/');
- $vendorDirCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true);
- $namespacesFile = <<<EOF
- <?php
- // autoload_namespace.php generated by Composer
- \$vendorDir = $vendorDirCode;
- return array(
- EOF;
- // build package => install path map
- $packageMap = array();
- // add main package
- $packageMap[] = array($mainPackage, '');
- foreach ($localRepo->getPackages() as $installedPackage) {
- $packageMap[] = array(
- $installedPackage,
- $installationManager->getInstallPath($installedPackage)
- );
- }
- $autoloads = $this->parseAutoloads($packageMap);
- $appBaseDir = $filesystem->findShortestPathCode($vendorPath, getcwd(), true);
- $appBaseDir = str_replace('__DIR__', '$vendorDir', $appBaseDir);
- if (isset($autoloads['psr-0'])) {
- foreach ($autoloads['psr-0'] as $namespace => $paths) {
- $exportedPaths = array();
- foreach ($paths as $path) {
- $path = strtr($path, '\\', '/');
- $baseDir = '';
- if (!$filesystem->isAbsolutePath($path)) {
- if (strpos($path, $relVendorPath) === 0) {
- $path = substr($path, strlen($relVendorPath));
- $baseDir = '$vendorDir . ';
- } else {
- $path = '/'.$path;
- $baseDir = $appBaseDir . ' . ';
- }
- } elseif (strpos($path, $vendorPath) === 0) {
- $path = substr($path, strlen($vendorPath));
- $baseDir = '$vendorDir . ';
- }
- $exportedPaths[] = $baseDir.var_export($path, true);
- }
- $exportedPrefix = var_export($namespace, true);
- $namespacesFile .= " $exportedPrefix => ";
- if (count($exportedPaths) > 1) {
- $namespacesFile .= "array(".implode(', ',$exportedPaths)."),\n";
- } else {
- $namespacesFile .= $exportedPaths[0].",\n";
- }
- }
- }
- $namespacesFile .= ");\n";
- file_put_contents($targetDir.'/autoload.php', $autoloadFile);
- file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile);
- copy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php');
- }
- /**
- * 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();
- 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) {
- foreach ($mapping as $namespace => $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;
- }
- }
|