2011-10-16 18:04:29 +00:00
|
|
|
<?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>
|
2011-10-23 19:23:37 +00:00
|
|
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
2011-10-16 18:04:29 +00:00
|
|
|
*/
|
|
|
|
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($targetFilename)
|
|
|
|
{
|
|
|
|
$autoloads = $this->parseAutoloads();
|
|
|
|
|
2011-10-29 05:24:30 +00:00
|
|
|
$file = file_get_contents(__DIR__.'/ClassLoader.php');
|
2011-10-16 18:04:29 +00:00
|
|
|
|
2011-10-29 05:24:30 +00:00
|
|
|
$file .= <<<'EOF'
|
|
|
|
|
|
|
|
// autoload.php generated by composer
|
2011-10-16 18:04:29 +00:00
|
|
|
|
2011-10-27 20:08:46 +00:00
|
|
|
$loader = new ClassLoader();
|
2011-10-16 18:04:29 +00:00
|
|
|
|
|
|
|
EOF;
|
|
|
|
|
2011-10-24 10:03:11 +00:00
|
|
|
if (isset($autoloads['psr-0'])) {
|
|
|
|
foreach ($autoloads['psr-0'] as $def) {
|
2011-10-16 18:04:29 +00:00
|
|
|
foreach ($def['mapping'] as $prefix => $path) {
|
|
|
|
$exportedPrefix = var_export($prefix, true);
|
|
|
|
$exportedPath = var_export(($def['path'] ? '/'.$def['path'] : '').'/'.$path, true);
|
|
|
|
$file .= <<<EOF
|
2011-10-30 08:51:16 +00:00
|
|
|
\$loader->add($exportedPrefix, dirname(dirname(__DIR__)).$exportedPath);
|
2011-10-16 18:04:29 +00:00
|
|
|
|
|
|
|
EOF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$file .= <<<'EOF'
|
|
|
|
$loader->register();
|
|
|
|
|
|
|
|
EOF;
|
|
|
|
|
|
|
|
file_put_contents($targetFilename, $file);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function parseAutoloads()
|
|
|
|
{
|
|
|
|
$installPaths = array();
|
|
|
|
foreach ($this->localRepo->getPackages() as $package) {
|
|
|
|
$installPaths[] = array(
|
2011-10-16 18:10:50 +00:00
|
|
|
$package,
|
2011-10-16 18:04:29 +00:00
|
|
|
$this->installationManager->getInstallPath($package)
|
|
|
|
);
|
|
|
|
}
|
2011-10-22 17:49:54 +00:00
|
|
|
$installPaths[] = array($this->package, '');
|
2011-10-16 18:04:29 +00:00
|
|
|
|
|
|
|
$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) {
|
|
|
|
$autoloads[$type][] = array(
|
|
|
|
'mapping' => $mapping,
|
|
|
|
'path' => $installPath,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $autoloads;
|
|
|
|
}
|
|
|
|
}
|