From 5f4d46f7aee4eee752e2fd64014c5359999449ee Mon Sep 17 00:00:00 2001 From: everzet Date: Sat, 17 Sep 2011 14:39:37 +0300 Subject: [PATCH] repository factoring refactoring --- bin/composer | 37 +++++++---- src/Composer/ConfigurableComposer.php | 52 ++++----------- src/Composer/Package/Manager.php | 6 -- src/Composer/Repository/ArrayRepository.php | 10 +++ .../Repository/ComposerRepository.php | 12 +++- src/Composer/Repository/GitRepository.php | 12 +++- src/Composer/Repository/PearRepository.php | 10 +++ .../Repository/PlatformRepository.php | 7 ++- src/Composer/Repository/RepositoryFactory.php | 63 +++++++++++++++++++ .../Repository/RepositoryInterface.php | 3 + 10 files changed, 152 insertions(+), 60 deletions(-) create mode 100644 src/Composer/Repository/RepositoryFactory.php diff --git a/bin/composer b/bin/composer index 2c2376a19..35194b681 100755 --- a/bin/composer +++ b/bin/composer @@ -4,21 +4,34 @@ require __DIR__.'/../tests/bootstrap.php'; use Composer\Composer; -use Composer\Downloader\GitDownloader; -use Composer\Downloader\PearDownloader; -use Composer\Downloader\ZipDownloader; -use Composer\Installer\LibraryInstaller; -use Composer\Console\Application; +use Composer\Installer; +use Composer\Downloader; +use Composer\Repository; +use Composer\Package; +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 -$composer = new ConfigurableComposer('composer.json', 'composer.lock'); -$composer->addDownloader('git', new GitDownloader); -$composer->addDownloader('pear', new PearDownloader); -$composer->addDownloader('zip', new ZipDownloader); -$composer->addInstaller('library', new LibraryInstaller); +$composer = new Composer(); +$composer->setInstaller('library', $installer); +$composer->setRepository('Platform', new Repository\PlatformRepository()); +$composer->setRepository('Packagist', new Repository\ComposerRepository('http://packagist.org')); + +// initialize package +$loader = new Package\Loader\Json(); +$package = $loader->load('composer.json'); + +// initialize lock +$packageLock = new Package\PackageLock('composer.lock'); // run the command application -$application = new Application($composer); +$application = new ComposerApplication($composer, $package, $packageLock); $application->run(); diff --git a/src/Composer/ConfigurableComposer.php b/src/Composer/ConfigurableComposer.php index 1d0443c09..91704aa28 100644 --- a/src/Composer/ConfigurableComposer.php +++ b/src/Composer/ConfigurableComposer.php @@ -12,7 +12,7 @@ namespace Composer; -use Composer\Repository; +use Composer\Repository\RepositoryFactory; use Composer\Package; use Composer\Installer\LibraryInstaller; @@ -25,12 +25,16 @@ class ConfigurableComposer extends Composer private $lockFile; private $isLocked = false; 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->lockFile = $lockFile; - $this->setRepository('Platform', new Repository\PlatformRepository()); + $this->configFile = $configFile; + $this->lockFile = $lockFile; + $this->repositoryFactory = $repositoryFactory; + + $this->setRepository('Platform', $repositoryFactory->create('platform')); if (!file_exists($configFile)) { throw new \UnexpectedValueException('Can not find composer config file'); @@ -102,7 +106,9 @@ class ConfigurableComposer extends Composer $repositories = array(); foreach ($repositoriesList as $name => $spec) { 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) { $repositories[$name] = null; } else { @@ -120,7 +126,7 @@ class ConfigurableComposer extends Composer { $requirements = array(); foreach ($requirementsList as $name => $version) { - $name = $this->lowercase($name); + $name = strtolower($name); if ('latest' === $version) { $requirements[$name] = null; } else { @@ -165,36 +171,4 @@ class ConfigurableComposer extends Composer 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 - ); - } - } } diff --git a/src/Composer/Package/Manager.php b/src/Composer/Package/Manager.php index 03482602b..db0f73ebb 100644 --- a/src/Composer/Package/Manager.php +++ b/src/Composer/Package/Manager.php @@ -24,8 +24,6 @@ class Manager public function install(PackageInterface $package) { - $output->writeln('> Installing '.$package->getName()); - $installer = $this->composer->getInstaller($package->getType()); $downloader = $this->getDownloaderForPackage($package); $packageType = $this->getTypeForPackage($package); @@ -37,8 +35,6 @@ class Manager public function update(PackageInterface $package) { - $output->writeln('> Updating '.$package->getName()); - $installer = $this->composer->getInstaller($package->getType()); $downloader = $this->getDownloaderForPackage($package); $packageType = $this->getTypeForPackage($package); @@ -50,8 +46,6 @@ class Manager public function remove(PackageInterface $package) { - $output->writeln('> Removing '.$package->getName()); - $installer = $this->composer->getInstaller($package->getType()); $downloader = $this->getDownloaderForPackage($package); $packageType = $this->getTypeForPackage($package); diff --git a/src/Composer/Repository/ArrayRepository.php b/src/Composer/Repository/ArrayRepository.php index fb7025e9f..ad3dc7d3e 100644 --- a/src/Composer/Repository/ArrayRepository.php +++ b/src/Composer/Repository/ArrayRepository.php @@ -23,6 +23,16 @@ class ArrayRepository implements RepositoryInterface { 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 * diff --git a/src/Composer/Repository/ComposerRepository.php b/src/Composer/Repository/ComposerRepository.php index eddd2b3a3..548fe9d06 100644 --- a/src/Composer/Repository/ComposerRepository.php +++ b/src/Composer/Repository/ComposerRepository.php @@ -24,6 +24,16 @@ class ComposerRepository extends ArrayRepository { 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) { $this->url = $url; @@ -98,4 +108,4 @@ class ComposerRepository extends ArrayRepository } return $links; } -} \ No newline at end of file +} diff --git a/src/Composer/Repository/GitRepository.php b/src/Composer/Repository/GitRepository.php index 8eb0f95da..61d58cdff 100644 --- a/src/Composer/Repository/GitRepository.php +++ b/src/Composer/Repository/GitRepository.php @@ -26,6 +26,16 @@ class GitRepository extends ArrayRepository { 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) { $this->url = $url; @@ -98,4 +108,4 @@ class GitRepository extends ArrayRepository } return $links; } -} \ No newline at end of file +} diff --git a/src/Composer/Repository/PearRepository.php b/src/Composer/Repository/PearRepository.php index 6fc4e94f4..24b8a00e1 100644 --- a/src/Composer/Repository/PearRepository.php +++ b/src/Composer/Repository/PearRepository.php @@ -26,6 +26,16 @@ class PearRepository extends ArrayRepository private $name; 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 = '') { if (!filter_var($url, FILTER_VALIDATE_URL)) { diff --git a/src/Composer/Repository/PlatformRepository.php b/src/Composer/Repository/PlatformRepository.php index ebb9b81ed..acc685597 100644 --- a/src/Composer/Repository/PlatformRepository.php +++ b/src/Composer/Repository/PlatformRepository.php @@ -22,6 +22,11 @@ class PlatformRepository extends ArrayRepository { protected $packages; + static public function supports($type, $name = '', $url = '') + { + return 'platform' === strtolower($type); + } + protected function initialize() { parent::initialize(); @@ -51,4 +56,4 @@ class PlatformRepository extends ArrayRepository $this->addPackage($ext); } } -} \ No newline at end of file +} diff --git a/src/Composer/Repository/RepositoryFactory.php b/src/Composer/Repository/RepositoryFactory.php new file mode 100644 index 000000000..b18138f5b --- /dev/null +++ b/src/Composer/Repository/RepositoryFactory.php @@ -0,0 +1,63 @@ + + * Jordi Boggiano + * + * 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 + */ +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); + } +} diff --git a/src/Composer/Repository/RepositoryInterface.php b/src/Composer/Repository/RepositoryInterface.php index 49e16690e..ac702140d 100644 --- a/src/Composer/Repository/RepositoryInterface.php +++ b/src/Composer/Repository/RepositoryInterface.php @@ -17,5 +17,8 @@ namespace Composer\Repository; */ interface RepositoryInterface extends \Countable { + static function supports($type, $name = '', $url = ''); + static function create($type, $name = '', $url = ''); + function getPackages(); }