1
0
Fork 0

repository factoring refactoring

pull/19/head
everzet 2011-09-17 14:39:37 +03:00
parent 1e1ecb80b7
commit 5f4d46f7ae
10 changed files with 152 additions and 60 deletions

View File

@ -4,21 +4,34 @@
require __DIR__.'/../tests/bootstrap.php'; require __DIR__.'/../tests/bootstrap.php';
use Composer\Composer; use Composer\Composer;
use Composer\Downloader\GitDownloader; use Composer\Installer;
use Composer\Downloader\PearDownloader; use Composer\Downloader;
use Composer\Downloader\ZipDownloader; use Composer\Repository;
use Composer\Installer\LibraryInstaller; use Composer\Package;
use Composer\Console\Application; use Composer\Console\Application as ComposerApplication;
setlocale(LC_ALL, 'C'); $composerFolder = '.composer';
$composerCache = $composerFolder.'/packages';
// initialize installer
$installer = new Installer\LibraryInstaller('vendor', $preferSource = true);
$installer->setDownloader('git', new Downloader\GitDownloader());
$installer->setDownloader('pear', new Downloader\PearDownloader());
$installer->setDownloader('zip', new Downloader\ZipDownloader());
// initialize composer // initialize composer
$composer = new ConfigurableComposer('composer.json', 'composer.lock'); $composer = new Composer();
$composer->addDownloader('git', new GitDownloader); $composer->setInstaller('library', $installer);
$composer->addDownloader('pear', new PearDownloader); $composer->setRepository('Platform', new Repository\PlatformRepository());
$composer->addDownloader('zip', new ZipDownloader); $composer->setRepository('Packagist', new Repository\ComposerRepository('http://packagist.org'));
$composer->addInstaller('library', new LibraryInstaller);
// initialize package
$loader = new Package\Loader\Json();
$package = $loader->load('composer.json');
// initialize lock
$packageLock = new Package\PackageLock('composer.lock');
// run the command application // run the command application
$application = new Application($composer); $application = new ComposerApplication($composer, $package, $packageLock);
$application->run(); $application->run();

View File

@ -12,7 +12,7 @@
namespace Composer; namespace Composer;
use Composer\Repository; use Composer\Repository\RepositoryFactory;
use Composer\Package; use Composer\Package;
use Composer\Installer\LibraryInstaller; use Composer\Installer\LibraryInstaller;
@ -25,12 +25,16 @@ class ConfigurableComposer extends Composer
private $lockFile; private $lockFile;
private $isLocked = false; private $isLocked = false;
private $lockedPackages = array(); private $lockedPackages = array();
private $repositoryFactory;
public function __construct($configFile = 'composer.json', $lockFile = 'composer.lock') public function __construct($configFile = 'composer.json', $lockFile = 'composer.lock',
RepositoryFactory $repositoryFactory)
{ {
$this->configFile = $configFile; $this->configFile = $configFile;
$this->lockFile = $lockFile; $this->lockFile = $lockFile;
$this->setRepository('Platform', new Repository\PlatformRepository()); $this->repositoryFactory = $repositoryFactory;
$this->setRepository('Platform', $repositoryFactory->create('platform'));
if (!file_exists($configFile)) { if (!file_exists($configFile)) {
throw new \UnexpectedValueException('Can not find composer config file'); throw new \UnexpectedValueException('Can not find composer config file');
@ -102,7 +106,9 @@ class ConfigurableComposer extends Composer
$repositories = array(); $repositories = array();
foreach ($repositoriesList as $name => $spec) { foreach ($repositoriesList as $name => $spec) {
if (is_array($spec) && count($spec) === 1) { if (is_array($spec) && count($spec) === 1) {
$repositories[$name] = $this->createRepository($name, key($spec), current($spec)); $repositories[$name] = $this->repositoryFactory->create(
key($spec), $name, current($spec)
);
} elseif (null === $spec) { } elseif (null === $spec) {
$repositories[$name] = null; $repositories[$name] = null;
} else { } else {
@ -120,7 +126,7 @@ class ConfigurableComposer extends Composer
{ {
$requirements = array(); $requirements = array();
foreach ($requirementsList as $name => $version) { foreach ($requirementsList as $name => $version) {
$name = $this->lowercase($name); $name = strtolower($name);
if ('latest' === $version) { if ('latest' === $version) {
$requirements[$name] = null; $requirements[$name] = null;
} else { } else {
@ -165,36 +171,4 @@ class ConfigurableComposer extends Composer
return $config; return $config;
} }
private function lowercase($str)
{
if (function_exists('mb_strtolower')) {
return mb_strtolower($str, 'UTF-8');
}
return strtolower($str, 'UTF-8');
}
private function createRepository($name, $type, $spec)
{
if (is_string($spec)) {
$spec = array('url' => $spec);
}
$spec['url'] = rtrim($spec['url'], '/');
switch ($type) {
case 'git-bare':
case 'git-multi':
throw new \Exception($type.' repositories not supported yet');
case 'git':
return new Repository\GitRepository($spec['url']);
case 'composer':
return new Repository\ComposerRepository($spec['url']);
case 'pear':
return new Repository\PearRepository($spec['url'], $name);
default:
throw new \UnexpectedValueException(
'Unknown repository type: '.$type.', could not create repository '.$name
);
}
}
} }

View File

@ -24,8 +24,6 @@ class Manager
public function install(PackageInterface $package) public function install(PackageInterface $package)
{ {
$output->writeln('> Installing '.$package->getName());
$installer = $this->composer->getInstaller($package->getType()); $installer = $this->composer->getInstaller($package->getType());
$downloader = $this->getDownloaderForPackage($package); $downloader = $this->getDownloaderForPackage($package);
$packageType = $this->getTypeForPackage($package); $packageType = $this->getTypeForPackage($package);
@ -37,8 +35,6 @@ class Manager
public function update(PackageInterface $package) public function update(PackageInterface $package)
{ {
$output->writeln('> Updating '.$package->getName());
$installer = $this->composer->getInstaller($package->getType()); $installer = $this->composer->getInstaller($package->getType());
$downloader = $this->getDownloaderForPackage($package); $downloader = $this->getDownloaderForPackage($package);
$packageType = $this->getTypeForPackage($package); $packageType = $this->getTypeForPackage($package);
@ -50,8 +46,6 @@ class Manager
public function remove(PackageInterface $package) public function remove(PackageInterface $package)
{ {
$output->writeln('> Removing '.$package->getName());
$installer = $this->composer->getInstaller($package->getType()); $installer = $this->composer->getInstaller($package->getType());
$downloader = $this->getDownloaderForPackage($package); $downloader = $this->getDownloaderForPackage($package);
$packageType = $this->getTypeForPackage($package); $packageType = $this->getTypeForPackage($package);

View File

@ -23,6 +23,16 @@ class ArrayRepository implements RepositoryInterface
{ {
protected $packages; protected $packages;
static public function supports($type, $name = '', $url = '')
{
return 'array' === strtolower($type);
}
static public function create($type, $name = '', $url = '')
{
return new static();
}
/** /**
* Adds a new package to the repository * Adds a new package to the repository
* *

View File

@ -24,6 +24,16 @@ class ComposerRepository extends ArrayRepository
{ {
protected $packages; protected $packages;
static public function supports($type, $name = '', $url = '')
{
return 'composer' === strtolower($type) && '' !== $url;
}
static public function create($type, $name = '', $url = '')
{
return new static($url);
}
public function __construct($url) public function __construct($url)
{ {
$this->url = $url; $this->url = $url;
@ -98,4 +108,4 @@ class ComposerRepository extends ArrayRepository
} }
return $links; return $links;
} }
} }

View File

@ -26,6 +26,16 @@ class GitRepository extends ArrayRepository
{ {
protected $packages; protected $packages;
static public function supports($type, $name = '', $url = '')
{
return 'git' === strtolower($type) && '' !== $url;
}
static public function create($type, $name = '', $url = '')
{
return new static($url);
}
public function __construct($url) public function __construct($url)
{ {
$this->url = $url; $this->url = $url;
@ -98,4 +108,4 @@ class GitRepository extends ArrayRepository
} }
return $links; return $links;
} }
} }

View File

@ -26,6 +26,16 @@ class PearRepository extends ArrayRepository
private $name; private $name;
private $url; private $url;
static public function supports($type, $name = '', $url = '')
{
return 'pear' === strtolower($type) && '' !== $url;
}
static public function create($type, $name = '', $url = '')
{
return new static($url, $name);
}
public function __construct($url, $name = '') public function __construct($url, $name = '')
{ {
if (!filter_var($url, FILTER_VALIDATE_URL)) { if (!filter_var($url, FILTER_VALIDATE_URL)) {

View File

@ -22,6 +22,11 @@ class PlatformRepository extends ArrayRepository
{ {
protected $packages; protected $packages;
static public function supports($type, $name = '', $url = '')
{
return 'platform' === strtolower($type);
}
protected function initialize() protected function initialize()
{ {
parent::initialize(); parent::initialize();
@ -51,4 +56,4 @@ class PlatformRepository extends ArrayRepository
$this->addPackage($ext); $this->addPackage($ext);
} }
} }
} }

View File

@ -0,0 +1,63 @@
<?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;
/**
* @author Konstantin Kudryashiv <ever.zet@gmail.com>
*/
class RepositoryFactory
{
private $classes = array();
public function __construct(array $classes)
{
foreach ($classes as $class) {
$this->registerRepositoryClass($class);
}
}
public function registerRepositoryClass($class)
{
$reflection = new \ReflectionClass($class);
if (!$reflection->implementsInterface('Composer\Repository\RepositoryInterface')) {
throw new \InvalidArgumentException(
'Repository class should implement "RepositoryInterface", but "'.$class.'"'.
'given'
);
}
$this->classes[] = $class;
}
public function classWhichSupports($type, $name = '', $url = '')
{
foreach ($this->classes as $class) {
if ($class::supports($type, $name, $url)) {
return $class;
}
}
throw new \UnexpectedValueException(sprintf(
"Can not find repository class, which supports:\n%s",
json_encode(array($name => array($type => $url)))
));
}
public function create($type, $name = '', $url = '')
{
$class = $this->classWhichSupports($type, $name, $url);
return $class::create($type, $name, $url);
}
}

View File

@ -17,5 +17,8 @@ namespace Composer\Repository;
*/ */
interface RepositoryInterface extends \Countable interface RepositoryInterface extends \Countable
{ {
static function supports($type, $name = '', $url = '');
static function create($type, $name = '', $url = '');
function getPackages(); function getPackages();
} }