Skip to content

Commit

Permalink
Remove persistence of alias data, make abstract branch aliasing and k…
Browse files Browse the repository at this point in the history
…eep it in composer only
  • Loading branch information
Seldaek committed Feb 25, 2012
1 parent 1bdab5c commit 0b7ee19
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/Composer/Factory.php
Expand Up @@ -130,7 +130,7 @@ protected function createRepositoryManager(IOInterface $io)

protected function addLocalRepository(RepositoryManager $rm, $vendorDir)
{
$rm->setLocalRepository(new Repository\FilesystemRepository(new JsonFile($vendorDir.'/.composer/installed.json')));
$rm->setLocalRepository(new Repository\InstalledFilesystemRepository(new JsonFile($vendorDir.'/.composer/installed.json')));
}

protected function addPackagistRepository(RepositoryManager $rm)
Expand Down
2 changes: 2 additions & 0 deletions src/Composer/Installer/InstallationManager.php
Expand Up @@ -127,6 +127,7 @@ public function install(InstallOperation $operation)
$package = $operation->getPackage();
if ($package instanceof AliasPackage) {
$package = $package->getAliasOf();
$package->setInstalledAsAlias(true);
}
$installer = $this->getInstaller($package->getType());
$installer->install($package);
Expand All @@ -146,6 +147,7 @@ public function update(UpdateOperation $operation)
$target = $operation->getTargetPackage();
if ($target instanceof AliasPackage) {
$target = $target->getAliasOf();
$target->setInstalledAsAlias(true);
}

$initialType = $initial->getType();
Expand Down
5 changes: 0 additions & 5 deletions src/Composer/Package/Dumper/ArrayDumper.php
Expand Up @@ -43,11 +43,6 @@ public function dump(PackageInterface $package)
$data['version'] = $package->getPrettyVersion();
$data['version_normalized'] = $package->getVersion();

if ($package->getAlias()) {
$data['alias'] = $package->getPrettyAlias();
$data['alias_normalized'] = $package->getAlias();
}

if ($package->getTargetDir()) {
$data['target-dir'] = $package->getTargetDir();
}
Expand Down
26 changes: 22 additions & 4 deletions src/Composer/Package/Loader/ArrayLoader.php
Expand Up @@ -134,10 +134,28 @@ public function load($config)
$package->setDistSha1Checksum(isset($config['dist']['shasum']) ? $config['dist']['shasum'] : null);
}

// load alias for named dev packages
if (!empty($config['alias']) && 'dev-' === substr($package->getPrettyVersion(), 0, 4)) {
$package->setAlias($config['alias_normalized']);
$package->setPrettyAlias($config['alias']);
// check for a branch alias (dev-master => 1.0.x-dev for example) if this is a named branch
if ('dev-' === substr($package->getPrettyVersion(), 0, 4) && isset($config['extra']['branch-alias']) && is_array($config['extra']['branch-alias'])) {
foreach ($config['extra']['branch-alias'] as $sourceBranch => $targetBranch) {
// ensure it is an alias to a -dev package
if ('-dev' !== substr($targetBranch, -4)) {
continue;
}
// normalize without -dev and ensure it's a numeric branch that is parseable
$validatedTargetBranch = $this->versionParser->normalizeBranch(substr($targetBranch, 0, -4));
if ('-dev' !== substr($validatedTargetBranch, -4)) {
continue;
}

// ensure that it is the current branch aliasing itself
if (strtolower($package->getPrettyVersion()) !== strtolower($sourceBranch)) {
continue;
}

$package->setAlias($validatedTargetBranch);
$package->setPrettyAlias($targetBranch);
break;
}
}

foreach (Package\BasePackage::$supportedLinkTypes as $type => $description) {
Expand Down
3 changes: 1 addition & 2 deletions src/Composer/Package/Locker.php
Expand Up @@ -139,8 +139,7 @@ public function setLockData(array $packages, array $aliases)
if ($package->isDev()) {
$spec['source-reference'] = $package->getSourceReference();
}
// TODO discriminate between really installed as alias and installed as real package
if ($package->getAlias()) {
if ($package->getAlias() && $package->isInstalledAsAlias()) {
$spec['alias'] = $package->getAlias();
}

Expand Down
19 changes: 19 additions & 0 deletions src/Composer/Package/MemoryPackage.php
Expand Up @@ -44,6 +44,7 @@ class MemoryPackage extends BasePackage
protected $aliases = array();
protected $alias;
protected $prettyAlias;
protected $installedAsAlias;
protected $dev;

protected $requires = array();
Expand Down Expand Up @@ -207,6 +208,24 @@ public function getPrettyAlias()
return $this->prettyAlias;
}

/**
* Enabled if the package is installed from its alias package
*
* @param string $installedAsAlias
*/
public function setInstalledAsAlias($installedAsAlias)
{
$this->installedAsAlias = $installedAsAlias;
}

/**
* @return string
*/
public function isInstalledAsAlias()
{
return $this->installedAsAlias;
}

/**
* {@inheritDoc}
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Composer/Repository/ArrayRepository.php
Expand Up @@ -95,6 +95,11 @@ public function addPackage(PackageInterface $package)
}
$package->setRepository($this);
$this->packages[] = $package;

// create alias package on the fly if needed (installed repos manage aliases themselves)
if ($package->getAlias() && !$this instanceof InstalledRepositoryInterface) {
$this->addPackage($this->createAliasPackage($package));
}
}

protected function createAliasPackage(PackageInterface $package)
Expand Down
17 changes: 12 additions & 5 deletions src/Composer/Repository/FilesystemRepository.php
Expand Up @@ -54,12 +54,15 @@ protected function initialize()
}

$loader = new ArrayLoader();
foreach ($packages as $package) {
$package = $loader->load($package);
// TODO discriminate between really installed as alias and installed as real package
if ($package->getAlias()) {
foreach ($packages as $packageData) {
$package = $loader->load($packageData);

// package was installed as alias, so we only add the alias
if ($this instanceof InstalledRepositoryInterface && !empty($packageData['installed-as-alias'])) {
$package->setInstalledAsAlias(true);
$this->addPackage($this->createAliasPackage($package));
} else {
// only add regular package - if it's not an installed repo the alias will be created on the fly
$this->addPackage($package);
}
}
Expand All @@ -73,7 +76,11 @@ public function write()
$packages = array();
$dumper = new ArrayDumper();
foreach ($this->getPackages() as $package) {
$packages[] = $dumper->dump($package);
$data = $dumper->dump($package);
if ($this instanceof InstalledRepositoryInterface && $package->isInstalledAsAlias()) {
$data['installed-as-alias'] = true;
}
$packages[] = $data;
}

$this->file->write($packages);
Expand Down
27 changes: 27 additions & 0 deletions src/Composer/Repository/InstalledFilesystemRepository.php
@@ -0,0 +1,27 @@
<?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\Repository;

use Composer\Json\JsonFile;
use Composer\Package\PackageInterface;
use Composer\Package\Loader\ArrayLoader;
use Composer\Package\Dumper\ArrayDumper;

/**
* Installed filesystem repository.
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class InstalledFilesystemRepository extends FilesystemRepository implements InstalledRepositoryInterface
{
}
26 changes: 26 additions & 0 deletions src/Composer/Repository/InstalledRepositoryInterface.php
@@ -0,0 +1,26 @@
<?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\Repository;

use Composer\Package\PackageInterface;

/**
* Installable repository interface.
*
* Just used to tag installed repositories so the base classes can act differently on Alias packages
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
interface InstalledRepositoryInterface
{
}
27 changes: 0 additions & 27 deletions src/Composer/Repository/VcsRepository.php
Expand Up @@ -185,36 +185,9 @@ private function preProcess(VcsDriverInterface $driver, array $data, $identifier
$data['source'] = $driver->getSource($identifier);
}

// check for a branch alias (dev-master => 1.0.x-dev for example) if this is a named branch
if ('dev-' === substr($data['version'], 0, 4) && isset($data['extra']['branch-alias']) && is_array($data['extra']['branch-alias'])) {
foreach ($data['extra']['branch-alias'] as $sourceBranch => $targetBranch) {
// ensure it is an alias to a numeric branch that is parseable
if (!($validatedTargetBranch = $this->validateBranch($targetBranch)) || '-dev' !== substr($validatedTargetBranch, -4)) {
continue;
}
// ensure that it is the current branch aliasing itself
if ($data['version'] !== $sourceBranch && substr($data['version'], 4) !== $sourceBranch) {
continue;
}

$data['alias'] = $targetBranch.'-dev';
$data['alias_normalized'] = $validatedTargetBranch;
break;
}
}

return $data;
}

public function addPackage(PackageInterface $package)
{
parent::addPackage($package);

if ($package->getAlias()) {
$this->addPackage($this->createAliasPackage($package));
}
}

private function validateBranch($branch)
{
try {
Expand Down

0 comments on commit 0b7ee19

Please sign in to comment.