126 lines
3.3 KiB
PHP
126 lines
3.3 KiB
PHP
<?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
|
|
{
|
|
private $localRepo;
|
|
private $package;
|
|
private $installationManager;
|
|
|
|
public function __construct(RepositoryInterface $localRepo, PackageInterface $package, InstallationManager $installationManager)
|
|
{
|
|
$this->localRepo = $localRepo;
|
|
$this->package = $package;
|
|
$this->installationManager = $installationManager;
|
|
}
|
|
|
|
public function dump($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;
|
|
|
|
$autoloads = $this->parseAutoloads();
|
|
|
|
if (isset($autoloads['psr-0'])) {
|
|
foreach ($autoloads['psr-0'] as $def) {
|
|
$exportedPrefix = var_export($def['namespace'], true);
|
|
$exportedPath = var_export($def['path'], true);
|
|
$namespacesFile .= " $exportedPrefix => dirname(dirname(__DIR__)).$exportedPath,\n";
|
|
}
|
|
}
|
|
|
|
$namespacesFile .= ");\n";
|
|
|
|
file_put_contents($targetDir.'/autoload.php', $autoloadFile);
|
|
file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile);
|
|
}
|
|
|
|
private function parseAutoloads()
|
|
{
|
|
$installPaths = array();
|
|
foreach ($this->localRepo->getPackages() as $package) {
|
|
$installPaths[] = array(
|
|
$package,
|
|
$this->installationManager->getInstallPath($package)
|
|
);
|
|
}
|
|
$installPaths[] = array($this->package, '');
|
|
|
|
$autoloads = array();
|
|
foreach ($installPaths 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' => ($installPath ? '/'.$installPath : '').'/'.$path,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach ($autoloads as $type => $maps) {
|
|
usort($autoloads[$type], function ($a, $b) {
|
|
return strcmp($b['namespace'], $a['namespace']);
|
|
});
|
|
}
|
|
|
|
return $autoloads;
|
|
}
|
|
}
|