123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?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;
- /**
- * @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 = file_get_contents(__DIR__.'/ClassLoader.php');
- $autoloadFile .= <<<'EOF'
- // autoload.php generated by Composer
- function init() {
- $loader = new ClassLoader();
- $map = require __DIR__.'/autoload_namespaces.php';
- foreach ($map as $namespace => $path) {
- $loader->add($namespace, $path);
- }
- $loader->register();
- return $loader;
- }
- return init();
- EOF;
- $namespacesFile = <<<'EOF'
- <?php
- // autoload_namespace.php generated by Composer
- return array(
- EOF;
- // build package => install path map
- $packageMap = array();
- foreach ($localRepo->getPackages() as $installedPackage) {
- $packageMap[] = array(
- $installedPackage,
- $installationManager->getInstallPath($installedPackage)
- );
- }
- // add main package
- $packageMap[] = array($mainPackage, '');
- $autoloads = $this->parseAutoloads($packageMap);
- $vendorPath = $installationManager->getVendorPath();
- if (isset($autoloads['psr-0'])) {
- foreach ($autoloads['psr-0'] as $def) {
- if (!$this->isAbsolutePath($def['path'])) {
- $def['path'] = substr($def['path'], strlen($vendorPath));
- $baseDir = "dirname(__DIR__).";
- } else {
- $baseDir = '';
- }
- $exportedPrefix = var_export($def['namespace'], true);
- $exportedPath = var_export($def['path'], true);
- $namespacesFile .= " $exportedPrefix => {$baseDir}{$exportedPath},\n";
- }
- }
- $namespacesFile .= ");\n";
- file_put_contents($targetDir.'/autoload.php', $autoloadFile);
- file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile);
- }
- /**
- * 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(array('namespace' => 'Foo', 'path' => '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][] = array(
- 'namespace' => $namespace,
- 'path' => empty($installPath) ? $path : $installPath.'/'.$path,
- );
- }
- }
- }
- foreach ($autoloads as $type => $maps) {
- usort($autoloads[$type], function ($a, $b) {
- return strcmp($b['namespace'], $a['namespace']);
- });
- }
- 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 $def) {
- $loader->add($def['namespace'], $def['path']);
- }
- }
- return $loader;
- }
- protected function isAbsolutePath($path)
- {
- return substr($path, 0, 1) === '/' || substr($path, 1, 1) === ':';
- }
- }
|