1
0
Fork 0

[autoload] autoloading prototype

pull/44/head
Igor Wiedler 2011-10-09 16:43:16 +02:00
parent 536c1e0f88
commit 7a32860871
10 changed files with 201 additions and 4 deletions

View File

@ -13,6 +13,10 @@
"type": "string", "type": "string",
"optional": true "optional": true
}, },
"installAs": {
"description": "Override install location of package",
"type": "string"
},
"description": { "description": {
"type": "string", "type": "string",
"description": "Package description", "description": "Package description",
@ -102,6 +106,10 @@
"type": ["object", "array"], "type": ["object", "array"],
"additionalProperties": true, "additionalProperties": true,
"optional": true "optional": true
},
"autoload": {
"type": "object",
"additionalProperties": true
} }
} }
} }

View File

@ -108,6 +108,104 @@ EOT
} }
$localRepo->write(); $localRepo->write();
$output->writeln('> Generating autoload.php');
$this->generateAutoload($composer, $installationManager);
$output->writeln('> Done'); $output->writeln('> Done');
} }
private function generateAutoload(\Composer\Composer $composer,
\Composer\Installer\InstallationManager $installationManager)
{
$localRepo = new \Composer\Repository\FilesystemRepository(
new \Composer\Json\JsonFile('.composer/installed.json'));
$installPaths = array();
foreach ($localRepo->getPackages() as $package) {
$installPaths[] = array(
$this->getFullPackage($package, $installationManager),
$installationManager->getInstallPath($package)
);
}
$installPaths[] = array($composer->getPackage(), '');
$autoloads = array();
foreach ($installPaths as $item) {
list($package, $installPath) = $item;
if (null !== $package->getInstallAs()) {
$installPath = substr($installPath, 0, -strlen('/'.$package->getInstallAs()));
}
foreach ($package->getAutoload() as $type => $mapping) {
$autoloads[$type] = isset($autoloads[$type]) ? $autoloads[$type] : array();
$autoloads[$type][] = array(
'mapping' => $mapping,
'path' => $installPath,
);
}
}
$this->dumpAutoload($autoloads);
}
private function dumpAutoload(array $autoloads)
{
$file = <<<'EOF'
<?php
// autoload.php generated by composer
require_once __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/ClassLoader/UniversalClassLoader.php';
use Symfony\Component\ClassLoader\UniversalClassLoader;
$loader = new UniversalClassLoader();
EOF;
if (isset($autoloads['psr0'])) {
foreach ($autoloads['psr0'] as $def) {
foreach ($def['mapping'] as $namespace => $path) {
$exportedNamespace = var_export($namespace, true);
$exportedPath = var_export(($def['path'] ? '/'.$def['path'] : '').'/'.$path, true);
$file .= <<<EOF
\$loader->registerNamespace($exportedNamespace, dirname(__DIR__).$exportedPath);
EOF;
}
}
}
if (isset($autoloads['pear'])) {
foreach ($autoloads['pear'] as $def) {
foreach ($def['mapping'] as $prefix => $path) {
$exportedPrefix = var_export($prefix, true);
$exportedPath = var_export(($def['path'] ? '/'.$def['path'] : '').'/'.$path, true);
$file .= <<<EOF
\$loader->registerPrefix($exportedPrefix, dirname(__DIR__).$exportedPath);
EOF;
}
}
}
$file .= <<<'EOF'
$loader->register();
EOF;
file_put_contents('.composer/autoload.php', $file);
}
private function getFullPackage(\Composer\Package\PackageInterface $package,
\Composer\Installer\InstallationManager $installationManager)
{
$path = $installationManager->getInstallPath($package);
$loader = new \Composer\Package\Loader\JsonLoader();
$fullPackage = $loader->load(new \Composer\Json\JsonFile($path.'/composer.json'));
return $fullPackage;
}
} }

View File

@ -128,4 +128,10 @@ class InstallationManager
$installer = $this->getInstaller($operation->getPackage()->getType()); $installer = $this->getInstaller($operation->getPackage()->getType());
$installer->uninstall($operation->getPackage()); $installer->uninstall($operation->getPackage());
} }
public function getInstallPath(PackageInterface $package)
{
$installer = $this->getInstaller($package->getType());
return $installer->getInstallPath($package);
}
} }

View File

@ -54,4 +54,6 @@ interface InstallerInterface
* @param PackageInterface $package package instance * @param PackageInterface $package package instance
*/ */
function uninstall(PackageInterface $package); function uninstall(PackageInterface $package);
function getInstallPath(PackageInterface $package);
} }

View File

@ -78,7 +78,7 @@ class LibraryInstaller implements InstallerInterface
*/ */
public function install(PackageInterface $package) public function install(PackageInterface $package)
{ {
$downloadPath = $this->directory.DIRECTORY_SEPARATOR.$package->getName(); $downloadPath = $this->getInstallPath($package);
$this->downloadManager->download($package, $downloadPath); $this->downloadManager->download($package, $downloadPath);
$this->repository->addPackage(clone $package); $this->repository->addPackage(clone $package);
@ -98,7 +98,7 @@ class LibraryInstaller implements InstallerInterface
throw new \InvalidArgumentException('Package is not installed: '.$initial); throw new \InvalidArgumentException('Package is not installed: '.$initial);
} }
$downloadPath = $this->directory.DIRECTORY_SEPARATOR.$initial->getName(); $downloadPath = $this->getInstallPath($initial);
$this->downloadManager->update($initial, $target, $downloadPath); $this->downloadManager->update($initial, $target, $downloadPath);
$this->repository->removePackage($initial); $this->repository->removePackage($initial);
@ -118,9 +118,18 @@ class LibraryInstaller implements InstallerInterface
throw new \InvalidArgumentException('Package is not installed: '.$package); throw new \InvalidArgumentException('Package is not installed: '.$package);
} }
$downloadPath = $this->directory.DIRECTORY_SEPARATOR.$package->getName(); $downloadPath = $this->getInstallPath($package);
$this->downloadManager->remove($package, $downloadPath); $this->downloadManager->remove($package, $downloadPath);
$this->repository->removePackage($package); $this->repository->removePackage($package);
} }
public function getInstallPath(PackageInterface $package)
{
if (null === $package->getInstallAs()) {
return ($this->directory ? $this->directory.'/' : '').$package->getName();
} else {
return ($this->directory ? $this->directory.'/' : '').$package->getInstallAs();
}
}
} }

View File

@ -38,7 +38,9 @@ class ArrayDumper
'provides', 'provides',
'replaces', 'replaces',
'recommends', 'recommends',
'suggests' 'suggests',
'autoload',
'installAs',
); );
$data = array(); $data = array();

View File

@ -46,6 +46,10 @@ class ArrayLoader
$package->setType(isset($config['type']) ? $config['type'] : 'library'); $package->setType(isset($config['type']) ? $config['type'] : 'library');
if (isset($config['installAs'])) {
$package->setInstallAs($config['installAs']);
}
if (isset($config['extra'])) { if (isset($config['extra'])) {
$package->setExtra($config['extra']); $package->setExtra($config['extra']);
} }
@ -91,6 +95,10 @@ class ArrayLoader
} }
} }
if (isset($config['autoload'])) {
$package->setAutoload($config['autoload']);
}
return $package; return $package;
} }

View File

@ -20,6 +20,7 @@ namespace Composer\Package;
class MemoryPackage extends BasePackage class MemoryPackage extends BasePackage
{ {
protected $type; protected $type;
protected $installAs;
protected $installationSource; protected $installationSource;
protected $sourceType; protected $sourceType;
protected $sourceUrl; protected $sourceUrl;
@ -39,6 +40,7 @@ class MemoryPackage extends BasePackage
protected $replaces = array(); protected $replaces = array();
protected $recommends = array(); protected $recommends = array();
protected $suggests = array(); protected $suggests = array();
protected $autoload = array();
/** /**
* Creates a new in memory package. * Creates a new in memory package.
@ -71,6 +73,22 @@ class MemoryPackage extends BasePackage
return $this->type ?: 'library'; return $this->type ?: 'library';
} }
/**
* @param string $installAs
*/
public function setInstallAs($installAs)
{
$this->installAs = $installAs;
}
/**
* {@inheritDoc}
*/
public function getInstallAs()
{
return $this->installAs;
}
/** /**
* @param array $extra * @param array $extra
*/ */
@ -376,4 +394,22 @@ class MemoryPackage extends BasePackage
{ {
return $this->suggests; return $this->suggests;
} }
/**
* Set the autoload mapping
*
* @param array $autoload Mapping of autoloading rules
*/
public function setAutoload(array $autoload)
{
$this->autoload = $autoload;
}
/**
* {@inheritDoc}
*/
public function getAutoload()
{
return $this->autoload;
}
} }

View File

@ -75,6 +75,13 @@ interface PackageInterface
*/ */
function getType(); function getType();
/**
* Returns the package installAs property
*
* @return string The package installAs
*/
function getInstallAs();
/** /**
* Returns the package extra data * Returns the package extra data
* *
@ -209,6 +216,18 @@ interface PackageInterface
*/ */
function getSuggests(); function getSuggests();
/**
* Returns an associative array of autoloading rules
*
* {"<type>": {"<namespace": "<directory>"}}
*
* Type is either "psr0" or "pear". Namespaces are mapped to directories
* for autoloading using the type specified.
*
* @return array Mapping of autoloading rules
*/
function getAutoload();
/** /**
* Stores a reference to the repository that owns the package * Stores a reference to the repository that owns the package
* *

View File

@ -67,6 +67,10 @@ class GitRepository extends ArrayRepository
$package->setSourceType('git'); $package->setSourceType('git');
$package->setSourceUrl($this->url); $package->setSourceUrl($this->url);
if (isset($data['installAs'])) {
$package->setInstallAs($data['installAs']);
}
if (isset($data['license'])) { if (isset($data['license'])) {
$package->setLicense($data['license']); $package->setLicense($data['license']);
} }
@ -85,6 +89,11 @@ class GitRepository extends ArrayRepository
$package->{$method}($this->createLinks($data['name'], $link.'s', $data[$link])); $package->{$method}($this->createLinks($data['name'], $link.'s', $data[$link]));
} }
} }
if (isset($data['autoload'])) {
$package->setAutoload($data['autoload']);
}
$this->addPackage($package); $this->addPackage($package);
} }