diff --git a/.gitignore b/.gitignore index dfbe013f5..5992f93fc 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ /.project /.buildpath /composer.phar -/vendor \ No newline at end of file +/vendor +/nbproject diff --git a/README.md b/README.md index 79cfb19c9..9dae242e9 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,10 @@ Installation / Usage 1. Download the [`composer.phar`](http://getcomposer.org/composer.phar) executable or use the installer. - $ curl http://getcomposer.org/installer | php + ``` sh + $ curl -s http://getcomposer.org/installer | php + ``` + 2. Create a composer.json defining your dependencies. Note that this example is a short version for applications that are not meant to be published as packages @@ -46,10 +49,11 @@ Since composer works with the current working directory it is possible to instal in a system wide way. 1. Change into a directory in your path like `cd /usr/local/bin` -2. Get composer `curl http://getcomposer.org/installer | php` +2. Get composer `curl -s http://getcomposer.org/installer | php` 3. Make the phar executeable `chmod a+x composer.phar` -3. Change into a project directory `cd /path/to/my/project` -4. Use composer as you normally would `composer.phar install` +4. Change into a project directory `cd /path/to/my/project` +5. Use composer as you normally would `composer.phar install` +6. Optionally you can rename the composer.phar to composer to make it easier Global installation of composer (via homebrew) ---------------------------------------------- @@ -99,7 +103,7 @@ Community --------- The developer mailing list is on [google groups](http://groups.google.com/group/composer-dev) -IRC channels are available for discussion as well, on irc.freenode.org [#composer](irc://irc.freenode.org/composer) +IRC channels are available for discussion as well, on irc.freenode.org [#composer](irc://irc.freenode.org/composer) for users and [#composer-dev](irc://irc.freenode.org/composer-dev) for development. Requirements diff --git a/doc/DefaultPolicy.md b/doc/DefaultPolicy.md index 4254e20bf..328ceec8f 100644 --- a/doc/DefaultPolicy.md +++ b/doc/DefaultPolicy.md @@ -27,7 +27,7 @@ Request: install A Packages Repo1.Av1, Repo2.Av1 * priority(Repo1) >= priority(Repo2) => (Repo1.Av1, Repo2.Av1) -* priority(Repo2) < priority(Repo2) => (Repo2.Av1, Repo1.Av1) +* priority(Repo1) < priority(Repo2) => (Repo2.Av1, Repo1.Av1) ### Virtual Packages (provides) @@ -39,7 +39,7 @@ Packages Av1, Bv1 Request: install X * priority(Av1.repo) >= priority(Bv1.repo) => (Av1, Bv1) -* priority(Av1.repo) < priority(Bv1.repo) => (Bv1, Av1) +* priority(Av1.repo) < priority(Bv1.repo) => (Bv1, Av1) ### Package replacements @@ -49,8 +49,7 @@ Packages: Av1, Bv2 Request: install A -* priority(Av1.repo) > priority(Bv2.repo) => (Av1, Bv2) -* priority(Av1.repo) = priority(Bv2.repo) => (Av1, Bv2) -* priority(Av1.repo) < priority(Bv2.repo) => (Bv2, Av1) +* priority(Av1.repo) >= priority(Bv2.repo) => (Av1, Bv2) +* priority(Av1.repo) < priority(Bv2.repo) => (Bv2, Av1) -Bv2.version is ignored, only the replacement version for A matters. +Bv2 version is ignored, only the replacement version for A matters. diff --git a/doc/faqs/packagist-update-schedule.md b/doc/faqs/packagist-update-schedule.md new file mode 100644 index 000000000..865e48a44 --- /dev/null +++ b/doc/faqs/packagist-update-schedule.md @@ -0,0 +1,22 @@ +# Packagist Update Schedule + +## How often does Packagist crawl newly added packages? + +New packages will be crawled **within ten minutes**. + + +## How often does Packagist crawl existing packages? + +Existing packages will be crawled **every hour**. + + +## How often does the Packagist search index update? + +The search index is updated **every five minutes**. It will index (or reindex) +any package that has been crawled since the last time the search +indexer ran. + + +## Can Packagist be triggered to recrawl a package (on commit or by other means)? + +Not yet. :) See [#84](https://github.com/composer/packagist/issues/84). \ No newline at end of file diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 37b305640..8b60e50f1 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -17,7 +17,7 @@ use Composer\Json\JsonFile; use Composer\Package\Loader\JsonLoader; use Composer\Package\PackageInterface; use Composer\Repository\RepositoryInterface; -use Composer\Downloader\Util\Filesystem; +use Composer\Util\Filesystem; /** * @author Igor Wiedler diff --git a/src/Composer/Autoload/ClassLoader.php b/src/Composer/Autoload/ClassLoader.php index aa4669e34..8d38c9900 100644 --- a/src/Composer/Autoload/ClassLoader.php +++ b/src/Composer/Autoload/ClassLoader.php @@ -26,6 +26,9 @@ namespace Composer\Autoload; * // activate the autoloader * $loader->register(); * + * // to enable searching the include path (eg. for PEAR packages) + * $loader->setUseIncludePath(true); + * * In this example, if you try to use a class in the Symfony\Component * namespace or one of its children (Symfony\Component\Console for instance), * the autoloader will first look for the class under the component/ @@ -41,6 +44,7 @@ class ClassLoader { private $prefixes = array(); private $fallbackDirs = array(); + private $useIncludePath = false; public function getPrefixes() { @@ -61,7 +65,9 @@ class ClassLoader public function add($prefix, $paths) { if (!$prefix) { - $this->fallbackDirs = (array) $paths; + foreach ((array) $paths as $path) { + $this->fallbackDirs[] = $path; + } return; } if (isset($this->prefixes[$prefix])) { @@ -74,6 +80,27 @@ class ClassLoader } } + /** + * Turns on searching the include for class files. + * + * @param Boolean $useIncludePath + */ + public function setUseIncludePath($useIncludePath) + { + $this->useIncludePath = $useIncludePath; + } + + /** + * Can be used to check if the autoloader uses the include path to check + * for classes. + * + * @return Boolean + */ + public function getUseIncludePath() + { + return $this->useIncludePath; + } + /** * Registers this instance as an autoloader. * @@ -121,7 +148,7 @@ class ClassLoader if (false !== $pos = strrpos($class, '\\')) { // namespaced class name - $classPath = DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos)); + $classPath = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos)) . DIRECTORY_SEPARATOR; $className = substr($class, $pos + 1); } else { // PEAR-like class name @@ -129,22 +156,26 @@ class ClassLoader $className = $class; } - $classPath .= DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; + $classPath .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; foreach ($this->prefixes as $prefix => $dirs) { foreach ($dirs as $dir) { if (0 === strpos($class, $prefix)) { - if (file_exists($dir . $classPath)) { - return $dir . $classPath; + if (file_exists($dir . DIRECTORY_SEPARATOR . $classPath)) { + return $dir . DIRECTORY_SEPARATOR . $classPath; } } } } foreach ($this->fallbackDirs as $dir) { - if (file_exists($dir . $classPath)) { - return $dir . $classPath; + if (file_exists($dir . DIRECTORY_SEPARATOR . $classPath)) { + return $dir . DIRECTORY_SEPARATOR . $classPath; } } + + if ($this->useIncludePath && $file = stream_resolve_include_path($classPath)) { + return $file; + } } } diff --git a/src/Composer/Command/Command.php b/src/Composer/Command/Command.php index c5f83998d..8744a0da1 100644 --- a/src/Composer/Command/Command.php +++ b/src/Composer/Command/Command.php @@ -25,8 +25,8 @@ abstract class Command extends BaseCommand /** * @return \Composer\Composer */ - protected function getComposer() + protected function getComposer($required = true) { - return $this->getApplication()->getComposer(); + return $this->getApplication()->getComposer($required); } } diff --git a/src/Composer/Command/DebugPackagesCommand.php b/src/Composer/Command/DebugPackagesCommand.php deleted file mode 100644 index e3d196466..000000000 --- a/src/Composer/Command/DebugPackagesCommand.php +++ /dev/null @@ -1,90 +0,0 @@ - - * Jordi Boggiano - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Composer\Command; - -use Composer\Autoload\AutoloadGenerator; -use Composer\DependencyResolver; -use Composer\DependencyResolver\Pool; -use Composer\DependencyResolver\Request; -use Composer\DependencyResolver\Operation; -use Composer\Package\LinkConstraint\VersionConstraint; -use Composer\Repository\CompositeRepository; -use Composer\Repository\PlatformRepository; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Output\OutputInterface; - -/** - * @author Jordi Boggiano - */ -class DebugPackagesCommand extends Command -{ - protected function configure() - { - $this - ->setName('debug:packages') - ->setDescription('Lists all existing packages and their version') - ->setDefinition(array( - new InputOption('local', null, InputOption::VALUE_NONE, 'list locally installed packages only'), - new InputOption('platform', null, InputOption::VALUE_NONE, 'list platform packages only'), - )) - ->setHelp(<<php composer.phar debug:packages - -EOT - ) - ; - } - - protected function execute(InputInterface $input, OutputInterface $output) - { - $composer = $this->getComposer(); - - // create local repo, this contains all packages that are installed in the local project - $localRepo = $composer->getRepositoryManager()->getLocalRepository(); - // create installed repo, this contains all local packages + platform packages (php & extensions) - $installedRepo = new CompositeRepository(array($localRepo, new PlatformRepository())); - - if ($input->getOption('local')) { - foreach ($localRepo->getPackages() as $package) { - $output->writeln('local: ' . $package->getPrettyName() . ' ' . $package->getPrettyVersion() . ' (' . $package->getVersion() . ')'); - } - - return; - } - - if ($input->getOption('platform')) { - $repos = array_diff($installedRepo->getPackages(), $localRepo->getPackages()); - foreach ($repos as $package) { - $output->writeln('plattform: ' . $package->getPrettyName() . ' ' . $package->getPrettyVersion() . ' (' . $package->getVersion() . ')'); - } - - return; - } - - foreach ($installedRepo->getPackages() as $package) { - if ($localRepo->hasPackage($package)) { - $output->writeln('installed: ' . $package->getPrettyName() . ' ' . $package->getPrettyVersion() . ' (' . $package->getVersion() . ')'); - } else { - $output->writeln('platform: ' . $package->getPrettyName() . ' ' . $package->getPrettyVersion() . ' (' . $package->getName() . ' ' . $package->getVersion() . ')'); - } - } - - foreach ($composer->getRepositoryManager()->getRepositories() as $repository) { - foreach ($repository->getPackages() as $package) { - $output->writeln('available: ' . $package->getPrettyName() . ' ' . $package->getPrettyVersion() . ' (' . $package->getName() . ' ' . $package->getVersion() . ')'); - } - } - } - -} \ No newline at end of file diff --git a/src/Composer/Command/InstallCommand.php b/src/Composer/Command/InstallCommand.php index 26e1a5ba2..d656ad5d1 100644 --- a/src/Composer/Command/InstallCommand.php +++ b/src/Composer/Command/InstallCommand.php @@ -46,7 +46,7 @@ class InstallCommand extends Command ->setName('install') ->setDescription('Parses the composer.json file and downloads the needed dependencies.') ->setDefinition(array( - new InputOption('dev', null, InputOption::VALUE_NONE, 'Forces installation from package sources when possible, including VCS information.'), + new InputOption('prefer-source', null, InputOption::VALUE_NONE, 'Forces installation from package sources when possible, including VCS information.'), new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Outputs the operations but will not execute anything (implicitly enables --verbose).'), new InputOption('no-install-recommends', null, InputOption::VALUE_NONE, 'Do not install recommended packages (ignored when installing from an existing lock file).'), new InputOption('install-suggests', null, InputOption::VALUE_NONE, 'Also install suggested packages (ignored when installing from an existing lock file).'), @@ -73,7 +73,7 @@ EOT $io, $composer, $eventDispatcher, - (Boolean)$input->getOption('dev'), + (Boolean)$input->getOption('prefer-source'), (Boolean)$input->getOption('dry-run'), (Boolean)$input->getOption('verbose'), (Boolean)$input->getOption('no-install-recommends'), diff --git a/src/Composer/Command/SelfUpdateCommand.php b/src/Composer/Command/SelfUpdateCommand.php index 50ad96c6d..febc78295 100644 --- a/src/Composer/Command/SelfUpdateCommand.php +++ b/src/Composer/Command/SelfUpdateCommand.php @@ -13,6 +13,7 @@ namespace Composer\Command; use Composer\Composer; +use Composer\Util\StreamContextFactory; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -39,7 +40,9 @@ EOT protected function execute(InputInterface $input, OutputInterface $output) { - $latest = trim(file_get_contents('http://getcomposer.org/version')); + $ctx = StreamContextFactory::getContext(); + + $latest = trim(file_get_contents('http://getcomposer.org/version'), false, $ctx); if (Composer::VERSION !== $latest) { $output->writeln(sprintf("Updating to version %s.", $latest)); @@ -47,7 +50,7 @@ EOT $remoteFilename = 'http://getcomposer.org/composer.phar'; $localFilename = $_SERVER['argv'][0]; - file_put_contents($localFilename, file_get_contents($remoteFilename)); + copy($remoteFilename, $localFilename, $ctx); } else { $output->writeln("You are using the latest composer version."); } diff --git a/src/Composer/Command/ShowCommand.php b/src/Composer/Command/ShowCommand.php index 9de3ed2de..4450966b3 100644 --- a/src/Composer/Command/ShowCommand.php +++ b/src/Composer/Command/ShowCommand.php @@ -16,7 +16,12 @@ use Composer\Composer; use Composer\Package\PackageInterface; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Composer\Repository\CompositeRepository; +use Composer\Repository\PlatformRepository; +use Composer\Repository\ComposerRepository; +use Composer\Repository\RepositoryInterface; /** * @author Robert Schönthal @@ -28,14 +33,16 @@ class ShowCommand extends Command { $this ->setName('show') - ->setDescription('Show package details') + ->setDescription('Show information about packages') ->setDefinition(array( - new InputArgument('package', InputArgument::REQUIRED, 'the package to inspect'), - new InputArgument('version', InputArgument::OPTIONAL, 'the version'), + new InputArgument('package', InputArgument::OPTIONAL, 'Package to inspect'), + new InputArgument('version', InputArgument::OPTIONAL, 'Version to inspect'), + new InputOption('installed', null, InputOption::VALUE_NONE, 'List installed packages only'), + new InputOption('platform', null, InputOption::VALUE_NONE, 'List platform packages only'), )) ->setHelp(<<php composer.phar show composer/composer master-dev +The show command displays detailed information about a package, or +lists all packages available. EOT ) @@ -44,16 +51,48 @@ EOT protected function execute(InputInterface $input, OutputInterface $output) { - $composer = $this->getComposer(); - $package = $this->getPackage($input, $output, $composer); - if (!$package) { - throw new \InvalidArgumentException('no package found'); + // init repos + $platformRepo = new PlatformRepository; + if ($input->getOption('platform')) { + $repos = $installedRepo = $platformRepo; + } elseif ($input->getOption('installed')) { + $composer = $this->getComposer(); + $repos = $installedRepo = $composer->getRepositoryManager()->getLocalRepository(); + } elseif ($composer = $this->getComposer(false)) { + $localRepo = $composer->getRepositoryManager()->getLocalRepository(); + $installedRepo = new CompositeRepository(array($localRepo, $platformRepo)); + $repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories())); + } else { + $output->writeln('No composer.json found in the current directory, showing packages from packagist.org'); + $installedRepo = $platformRepo; + $repos = new CompositeRepository(array($installedRepo, new ComposerRepository(array('url' => 'http://packagist.org')))); } - $this->printMeta($input, $output, $package, $composer); - $this->printLinks($input, $output, $package, 'requires'); - $this->printLinks($input, $output, $package, 'recommends'); - $this->printLinks($input, $output, $package, 'replaces'); + // show single package or single version + if ($input->getArgument('package')) { + $package = $this->getPackage($input, $output, $installedRepo, $repos); + if (!$package) { + throw new \InvalidArgumentException('Package '.$input->getArgument('package').' not found'); + } + + $this->printMeta($input, $output, $package, $installedRepo, $repos); + $this->printLinks($input, $output, $package, 'requires'); + $this->printLinks($input, $output, $package, 'recommends'); + $this->printLinks($input, $output, $package, 'replaces'); + return; + } + + // list packages + foreach ($repos->getPackages() as $package) { + if ($platformRepo->hasPackage($package)) { + $type = 'platform: '; + } elseif ($installedRepo->hasPackage($package)) { + $type = 'installed: '; + } else { + $type = 'available: '; + } + $output->writeln($type . ' ' . $package->getPrettyName() . ' ' . $package->getPrettyVersion() . ' (' . $package->getVersion() . ')'); + } } /** @@ -63,16 +102,15 @@ EOT * @return PackageInterface * @throws \InvalidArgumentException */ - protected function getPackage(InputInterface $input, OutputInterface $output, Composer $composer) + protected function getPackage(InputInterface $input, OutputInterface $output, RepositoryInterface $installedRepo, RepositoryInterface $repos) { // we have a name and a version so we can use ::findPackage if ($input->getArgument('version')) { - return $composer->getRepositoryManager()->findPackage($input->getArgument('package'), $input->getArgument('version')); + return $repos->findPackage($input->getArgument('package'), $input->getArgument('version')); } // check if we have a local installation so we can grab the right package/version - $localRepo = $composer->getRepositoryManager()->getLocalRepository(); - foreach ($localRepo->getPackages() as $package) { + foreach ($installedRepo->getPackages() as $package) { if ($package->getName() === $input->getArgument('package')) { return $package; } @@ -80,15 +118,9 @@ EOT // we only have a name, so search for the highest version of the given package $highestVersion = null; - $repos = array_merge( - array($composer->getRepositoryManager()->getLocalRepository()), - $composer->getRepositoryManager()->getRepositories() - ); - foreach ($repos as $repository) { - foreach ($repository->findPackagesByName($input->getArgument('package')) as $package) { - if (null === $highestVersion || version_compare($package->getVersion(), $highestVersion->getVersion(), '>=')) { - $highestVersion = $package; - } + foreach ($repos->findPackagesByName($input->getArgument('package')) as $package) { + if (null === $highestVersion || version_compare($package->getVersion(), $highestVersion->getVersion(), '>=')) { + $highestVersion = $package; } } @@ -98,15 +130,15 @@ EOT /** * prints package meta data */ - protected function printMeta(InputInterface $input, OutputInterface $output, PackageInterface $package, Composer $composer) + protected function printMeta(InputInterface $input, OutputInterface $output, PackageInterface $package, RepositoryInterface $installedRepo, RepositoryInterface $repos) { $output->writeln('name : ' . $package->getPrettyName()); - $this->printVersions($input, $output, $package, $composer); + $this->printVersions($input, $output, $package, $installedRepo, $repos); $output->writeln('type : ' . $package->getType()); $output->writeln('names : ' . join(', ', $package->getNames())); $output->writeln('source : ' . sprintf('[%s] %s %s', $package->getSourceType(), $package->getSourceUrl(), $package->getSourceReference())); $output->writeln('dist : ' . sprintf('[%s] %s %s', $package->getDistType(), $package->getDistUrl(), $package->getDistReference())); - $output->writeln('licence : ' . join(', ', $package->getLicense())); + $output->writeln('license : ' . join(', ', $package->getLicense())); if ($package->getAutoload()) { $output->writeln("\nautoload"); @@ -123,7 +155,7 @@ EOT /** * prints all available versions of this package and highlights the installed one if any */ - protected function printVersions(InputInterface $input, OutputInterface $output, PackageInterface $package, Composer $composer) + protected function printVersions(InputInterface $input, OutputInterface $output, PackageInterface $package, RepositoryInterface $installedRepo, RepositoryInterface $repos) { if ($input->getArgument('version')) { $output->writeln('version : ' . $package->getPrettyVersion()); @@ -132,16 +164,14 @@ EOT $versions = array(); - foreach ($composer->getRepositoryManager()->getRepositories() as $repository) { - foreach ($repository->findPackagesByName($package->getName()) as $version) { - $versions[] = $version->getPrettyVersion(); - } + foreach ($repos->findPackagesByName($package->getName()) as $version) { + $versions[] = $version->getPrettyVersion(); } $versions = join(', ', $versions); // highlight installed version - if ($composer->getRepositoryManager()->getLocalRepository()->hasPackage($package)) { + if ($installedRepo->hasPackage($package)) { $versions = str_replace($package->getPrettyVersion(), '* ' . $package->getPrettyVersion() . '', $versions); } diff --git a/src/Composer/Command/UpdateCommand.php b/src/Composer/Command/UpdateCommand.php index 3c2b394c0..7e1e8e0c1 100644 --- a/src/Composer/Command/UpdateCommand.php +++ b/src/Composer/Command/UpdateCommand.php @@ -35,7 +35,7 @@ class UpdateCommand extends Command ->setName('update') ->setDescription('Updates your dependencies to the latest version, and updates the composer.lock file.') ->setDefinition(array( - new InputOption('dev', null, InputOption::VALUE_NONE, 'Forces installation from package sources when possible, including VCS information.'), + new InputOption('prefer-source', null, InputOption::VALUE_NONE, 'Forces installation from package sources when possible, including VCS information.'), new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Outputs the operations but will not execute anything (implicitly enables --verbose).'), new InputOption('no-install-recommends', null, InputOption::VALUE_NONE, 'Do not install recommended packages.'), new InputOption('install-suggests', null, InputOption::VALUE_NONE, 'Also install suggested packages.'), @@ -63,7 +63,7 @@ EOT $io, $composer, $eventDispatcher, - (Boolean)$input->getOption('dev'), + (Boolean)$input->getOption('prefer-source'), (Boolean)$input->getOption('dry-run'), (Boolean)$input->getOption('verbose'), (Boolean)$input->getOption('no-install-recommends'), diff --git a/src/Composer/Console/Application.php b/src/Composer/Console/Application.php index 745d5510d..6d1d6ba0b 100644 --- a/src/Composer/Console/Application.php +++ b/src/Composer/Console/Application.php @@ -71,14 +71,18 @@ class Application extends BaseApplication /** * @return Composer */ - public function getComposer() + public function getComposer($required = true) { if (null === $this->composer) { try { $this->composer = Factory::create($this->io); } catch (\InvalidArgumentException $e) { - $this->io->write($e->getMessage()); - exit(1); + if ($required) { + $this->io->write($e->getMessage()); + exit(1); + } + + return; } } @@ -102,7 +106,6 @@ class Application extends BaseApplication $this->add(new Command\DependsCommand()); $this->add(new Command\InstallCommand()); $this->add(new Command\UpdateCommand()); - $this->add(new Command\DebugPackagesCommand()); $this->add(new Command\SearchCommand()); $this->add(new Command\ValidateCommand()); $this->add(new Command\ShowCommand()); diff --git a/src/Composer/Downloader/DownloadManager.php b/src/Composer/Downloader/DownloadManager.php index a279e6e23..e466bbb09 100644 --- a/src/Composer/Downloader/DownloadManager.php +++ b/src/Composer/Downloader/DownloadManager.php @@ -14,6 +14,7 @@ namespace Composer\Downloader; use Composer\Package\PackageInterface; use Composer\Downloader\DownloaderInterface; +use Composer\Util\Filesystem; /** * Downloaders manager. @@ -134,9 +135,9 @@ class DownloadManager ); } - $fs = new Util\Filesystem(); + $fs = new Filesystem(); $fs->ensureDirectoryExists($targetDir); - + $downloader = $this->getDownloaderForInstalledPackage($package); $downloader->download($package, $targetDir); } diff --git a/src/Composer/Downloader/FileDownloader.php b/src/Composer/Downloader/FileDownloader.php index e3ccc1773..168705b5a 100644 --- a/src/Composer/Downloader/FileDownloader.php +++ b/src/Composer/Downloader/FileDownloader.php @@ -13,6 +13,8 @@ namespace Composer\Downloader; use Composer\IO\IOInterface; use Composer\Package\PackageInterface; +use Composer\Util\Filesystem; +use Composer\Util\StreamContextFactory; /** * Base downloader for file packages @@ -77,31 +79,14 @@ abstract class FileDownloader implements DownloaderInterface } } - // Handle system proxy - $params = array('http' => array()); - - if (isset($_SERVER['HTTP_PROXY'])) { - // http(s):// is not supported in proxy - $proxy = str_replace(array('http://', 'https://'), array('tcp://', 'ssl://'), $_SERVER['HTTP_PROXY']); - - if (0 === strpos($proxy, 'ssl:') && !extension_loaded('openssl')) { - throw new \RuntimeException('You must enable the openssl extension to use a proxy over https'); - } - - $params['http'] = array( - 'proxy' => $proxy, - 'request_fulluri' => true, - ); - } - + $options = array(); if ($this->io->hasAuthorization($package->getSourceUrl())) { $auth = $this->io->getAuthorization($package->getSourceUrl()); $authStr = base64_encode($auth['username'] . ':' . $auth['password']); - $params['http'] = array_merge($params['http'], array('header' => "Authorization: Basic $authStr\r\n")); + $options['http']['header'] = "Authorization: Basic $authStr\r\n"; } - $ctx = stream_context_create($params); - stream_context_set_params($ctx, array("notification" => array($this, 'callbackGet'))); + $ctx = StreamContextFactory::getContext($options, array('notification' => array($this, 'callbackGet'))); $this->io->overwrite(" Downloading: connection...", false); @copy($url, $fileName, $ctx); @@ -142,7 +127,7 @@ abstract class FileDownloader implements DownloaderInterface */ public function update(PackageInterface $initial, PackageInterface $target, $path) { - $fs = new Util\Filesystem(); + $fs = new Filesystem(); $fs->removeDirectory($path); $this->download($target, $path); } @@ -152,7 +137,7 @@ abstract class FileDownloader implements DownloaderInterface */ public function remove(PackageInterface $package, $path) { - $fs = new Util\Filesystem(); + $fs = new Filesystem(); $fs->removeDirectory($path); } diff --git a/src/Composer/Downloader/VcsDownloader.php b/src/Composer/Downloader/VcsDownloader.php index 8af2ce48e..4bcc79d0c 100644 --- a/src/Composer/Downloader/VcsDownloader.php +++ b/src/Composer/Downloader/VcsDownloader.php @@ -15,6 +15,7 @@ namespace Composer\Downloader; use Composer\Package\PackageInterface; use Composer\Util\ProcessExecutor; use Composer\IO\IOInterface; +use Composer\Util\Filesystem; /** * @author Jordi Boggiano @@ -73,7 +74,7 @@ abstract class VcsDownloader implements DownloaderInterface public function remove(PackageInterface $package, $path) { $this->enforceCleanDirectory($path); - $fs = new Util\Filesystem(); + $fs = new Filesystem(); $fs->removeDirectory($path); } diff --git a/src/Composer/Installer/LibraryInstaller.php b/src/Composer/Installer/LibraryInstaller.php index b1bdfa60d..0873ed63c 100644 --- a/src/Composer/Installer/LibraryInstaller.php +++ b/src/Composer/Installer/LibraryInstaller.php @@ -17,7 +17,7 @@ use Composer\Downloader\DownloadManager; use Composer\Repository\WritableRepositoryInterface; use Composer\DependencyResolver\Operation\OperationInterface; use Composer\Package\PackageInterface; -use Composer\Downloader\Util\Filesystem; +use Composer\Util\Filesystem; /** * Package installation manager. @@ -147,6 +147,12 @@ class LibraryInstaller implements InstallerInterface foreach ($package->getBinaries() as $bin) { $link = $this->binDir.'/'.basename($bin); if (file_exists($link)) { + if (is_link($link)) { + // likely leftover from a previous install, make sure + // that the target is still executable in case this + // is a fresh install of the vendor. + chmod($link, 0755); + } $this->io->write('Skipped installation of '.$bin.' for package '.$package->getName().', name conflicts with an existing file'); continue; } @@ -156,14 +162,14 @@ class LibraryInstaller implements InstallerInterface // add unixy support for cygwin and similar environments if ('.bat' !== substr($bin, -4)) { file_put_contents($link, $this->generateUnixyProxyCode($bin, $link)); - chmod($link, 0777); + chmod($link, 0755); $link .= '.bat'; } file_put_contents($link, $this->generateWindowsProxyCode($bin, $link)); } else { symlink($bin, $link); } - chmod($link, 0777); + chmod($link, 0755); } } diff --git a/src/Composer/Json/JsonFile.php b/src/Composer/Json/JsonFile.php index 67a60c1ea..7d4ab8cb6 100644 --- a/src/Composer/Json/JsonFile.php +++ b/src/Composer/Json/JsonFile.php @@ -14,6 +14,7 @@ namespace Composer\Json; use Composer\Repository\RepositoryManager; use Composer\Composer; +use Composer\Util\StreamContextFactory; /** * Reads/writes json files. @@ -59,11 +60,12 @@ class JsonFile */ public function read() { - $context = stream_context_create(array( - 'http' => array('header' => 'User-Agent: Composer/'.Composer::VERSION."\r\n") - )); + $ctx = StreamContextFactory::getContext(array( + 'http' => array( + 'header' => 'User-Agent: Composer/'.Composer::VERSION."\r\n" + ))); - $json = file_get_contents($this->path, false, $context); + $json = file_get_contents($this->path, false, $ctx); if (!$json) { throw new \RuntimeException('Could not read '.$this->path.', you are probably offline'); } diff --git a/src/Composer/Package/Dumper/ArrayDumper.php b/src/Composer/Package/Dumper/ArrayDumper.php index ee2a0b376..f372929b1 100644 --- a/src/Composer/Package/Dumper/ArrayDumper.php +++ b/src/Composer/Package/Dumper/ArrayDumper.php @@ -24,11 +24,16 @@ class ArrayDumper { $keys = array( 'binaries' => 'bin', + 'scripts', 'type', 'names', 'extra', 'installationSource' => 'installation-source', 'license', + 'authors', + 'description', + 'homepage', + 'keywords', 'autoload', 'repositories', ); @@ -41,6 +46,10 @@ class ArrayDumper $data['target-dir'] = $package->getTargetDir(); } + if ($package->getReleaseDate()) { + $data['time'] = $package->getReleaseDate()->format('Y-m-d H:i:s'); + } + if ($package->getSourceType()) { $data['source']['type'] = $package->getSourceType(); $data['source']['url'] = $package->getSourceUrl(); diff --git a/src/Composer/Package/MemoryPackage.php b/src/Composer/Package/MemoryPackage.php index 02222e43b..d1e63e102 100644 --- a/src/Composer/Package/MemoryPackage.php +++ b/src/Composer/Package/MemoryPackage.php @@ -438,7 +438,7 @@ class MemoryPackage extends BasePackage * * @param DateTime $releaseDate */ - public function setReleasedate(\DateTime $releaseDate) + public function setReleaseDate(\DateTime $releaseDate) { $this->releaseDate = $releaseDate; } diff --git a/src/Composer/Repository/PearRepository.php b/src/Composer/Repository/PearRepository.php index 51c1722b4..edf5b9e3b 100644 --- a/src/Composer/Repository/PearRepository.php +++ b/src/Composer/Repository/PearRepository.php @@ -13,6 +13,7 @@ namespace Composer\Repository; use Composer\Package\Loader\ArrayLoader; +use Composer\Util\StreamContextFactory; /** * @author Benjamin Eberlei @@ -20,7 +21,8 @@ use Composer\Package\Loader\ArrayLoader; */ class PearRepository extends ArrayRepository { - protected $url; + private $url; + private $streamContext; public function __construct(array $config) { @@ -31,7 +33,7 @@ class PearRepository extends ArrayRepository throw new \UnexpectedValueException('Invalid url given for PEAR repository: '.$config['url']); } - $this->url = $config['url']; + $this->url = rtrim($config['url'], '/'); } protected function initialize() @@ -41,6 +43,7 @@ class PearRepository extends ArrayRepository set_error_handler(function($severity, $message, $file, $line) { throw new \ErrorException($message, $severity, $severity, $file, $line); }); + $this->streamContext = StreamContextFactory::getContext(); $this->fetchFromServer(); restore_error_handler(); } @@ -51,24 +54,62 @@ class PearRepository extends ArrayRepository $categories = $categoryXML->getElementsByTagName("c"); foreach ($categories as $category) { - $categoryLink = $category->getAttribute("xlink:href"); - $categoryLink = str_replace("info.xml", "packages.xml", $categoryLink); - if ('/' !== substr($categoryLink, 0, 1)) { - $categoryLink = '/' . $categoryLink; + $link = '/' . ltrim($category->getAttribute("xlink:href"), '/'); + try { + $packagesLink = str_replace("info.xml", "packagesinfo.xml", $link); + $this->fetchPear2Packages($this->url . $packagesLink); + } catch (\ErrorException $e) { + if (false === strpos($e->getMessage(), '404')) { + throw $e; + } + $categoryLink = str_replace("info.xml", "packages.xml", $link); + $this->fetchPearPackages($this->url . $categoryLink); } - $packagesXML = $this->requestXml($this->url . $categoryLink); - $packages = $packagesXML->getElementsByTagName('p'); - $loader = new ArrayLoader(); - foreach ($packages as $package) { - $packageName = $package->nodeValue; + } + } - $packageLink = $package->getAttribute('xlink:href'); - $releaseLink = $this->url . str_replace("/rest/p/", "/rest/r/", $packageLink); - $allReleasesLink = $releaseLink . "/allreleases2.xml"; + /** + * @param string $categoryLink + * @throws ErrorException + * @throws InvalidArgumentException + */ + private function fetchPearPackages($categoryLink) + { + $packagesXML = $this->requestXml($categoryLink); + $packages = $packagesXML->getElementsByTagName('p'); + $loader = new ArrayLoader(); + foreach ($packages as $package) { + $packageName = $package->nodeValue; + + $packageLink = $package->getAttribute('xlink:href'); + $releaseLink = $this->url . str_replace("/rest/p/", "/rest/r/", $packageLink); + $allReleasesLink = $releaseLink . "/allreleases2.xml"; + + try { + $releasesXML = $this->requestXml($allReleasesLink); + } catch (\ErrorException $e) { + if (strpos($e->getMessage(), '404')) { + continue; + } + throw $e; + } + + $releases = $releasesXML->getElementsByTagName('r'); + + foreach ($releases as $release) { + /* @var $release \DOMElement */ + $pearVersion = $release->getElementsByTagName('v')->item(0)->nodeValue; + + $packageData = array( + 'name' => $packageName, + 'type' => 'library', + 'dist' => array('type' => 'pear', 'url' => $this->url.'/get/'.$packageName.'-'.$pearVersion.".tgz"), + 'version' => $pearVersion, + ); try { - $releasesXML = $this->requestXml($allReleasesLink); + $deps = file_get_contents($releaseLink . "/deps.".$pearVersion.".txt", false, $this->streamContext); } catch (\ErrorException $e) { if (strpos($e->getMessage(), '404')) { continue; @@ -76,54 +117,155 @@ class PearRepository extends ArrayRepository throw $e; } - $releases = $releasesXML->getElementsByTagName('r'); + $packageData += $this->parseDependencies($deps); - foreach ($releases as $release) { - /* @var $release DOMElement */ - $pearVersion = $release->getElementsByTagName('v')->item(0)->nodeValue; + try { + $this->addPackage($loader->load($packageData)); + } catch (\UnexpectedValueException $e) { + continue; + } + } + } + } - $packageData = array( - 'name' => $packageName, - 'type' => 'library', - 'dist' => array('type' => 'pear', 'url' => $this->url.'/get/'.$packageName.'-'.$pearVersion.".tgz"), - 'version' => $pearVersion, + /** + * @param array $data + * @return string + */ + private function parseVersion(array $data) + { + if (!isset($data['min']) && !isset($data['max'])) { + return '*'; + } + $versions = array(); + if (isset($data['min'])) { + $versions[] = '>=' . $data['min']; + } + if (isset($data['max'])) { + $versions[] = '<=' . $data['max']; + } + return implode(',', $versions); + } + + /** + * @todo Improve dependencies resolution of pear packages. + * @param array $options + * @return array + */ + private function parseDependenciesOptions(array $depsOptions) + { + $data = array(); + foreach ($depsOptions as $name => $options) { + // make sure single deps are wrapped in an array + if (isset($options['name'])) { + $options = array($options); + } + if ('php' == $name) { + $data[$name] = $this->parseVersion($options); + } elseif ('package' == $name) { + foreach ($options as $key => $value) { + if (is_array($value)) { + $dataKey = $value['name']; + $data[$dataKey] = $this->parseVersion($value); + } + } + } elseif ('extension' == $name) { + foreach ($options as $key => $value) { + $dataKey = 'ext-' . $value['name']; + $data[$dataKey] = $this->parseVersion($value); + } + } + } + return $data; + } + + /** + * @param string $deps + * @return array + * @throws InvalidArgumentException + */ + private function parseDependencies($deps) + { + if (preg_match('((O:([0-9])+:"([^"]+)"))', $deps, $matches)) { + if (strlen($matches[3]) == $matches[2]) { + throw new \InvalidArgumentException("Invalid dependency data, it contains serialized objects."); + } + } + $deps = (array) @unserialize($deps); + unset($deps['required']['pearinstaller']); + + $depsData = array(); + if (!empty($deps['required'])) { + $depsData['require'] = $this->parseDependenciesOptions($deps['required']); + } + + if (!empty($deps['optional'])) { + $depsData['suggest'] = $this->parseDependenciesOptions($deps['optional']); + } + + return $depsData; + } + + /** + * @param string $packagesLink + * @return void + * @throws InvalidArgumentException + */ + private function fetchPear2Packages($packagesLink) + { + $loader = new ArrayLoader(); + $packagesXml = $this->requestXml($packagesLink); + $informations = $packagesXml->getElementsByTagName('pi'); + foreach ($informations as $information) { + $package = $information->getElementsByTagName('p')->item(0); + + $packageName = $package->getElementsByTagName('n')->item(0)->nodeValue; + $packageData = array( + 'name' => $packageName, + 'type' => 'library' + ); + $packageKeys = array('l' => 'license', 'd' => 'description'); + foreach ($packageKeys as $pear => $composer) { + if ($package->getElementsByTagName($pear)->length > 0 + && ($pear = $package->getElementsByTagName($pear)->item(0)->nodeValue)) { + $packageData[$composer] = $pear; + } + } + + $depsData = array(); + foreach ($information->getElementsByTagName('deps') as $depElement) { + $depsVersion = $depElement->getElementsByTagName('v')->item(0)->nodeValue; + $depsData[$depsVersion] = $this->parseDependencies( + $depElement->getElementsByTagName('d')->item(0)->nodeValue + ); + } + + $releases = $information->getElementsByTagName('a')->item(0); + if (!$releases) { + continue; + } + + $releases = $releases->getElementsByTagName('r'); + $packageUrl = $this->url . '/get/' . $packageName; + foreach ($releases as $release) { + $version = $release->getElementsByTagName('v')->item(0)->nodeValue; + $releaseData = array( + 'dist' => array( + 'type' => 'pear', + 'url' => $packageUrl . '-' . $version . '.tgz' + ), + 'version' => $version + ); + if (isset($depsData[$version])) { + $releaseData += $depsData[$version]; + } + + try { + $this->addPackage( + $loader->load($packageData + $releaseData) ); - - try { - $deps = file_get_contents($releaseLink . "/deps.".$pearVersion.".txt"); - } catch (\ErrorException $e) { - if (strpos($e->getMessage(), '404')) { - continue; - } - throw $e; - } - - if (preg_match('((O:([0-9])+:"([^"]+)"))', $deps, $matches)) { - if (strlen($matches[3]) == $matches[2]) { - throw new \InvalidArgumentException("Invalid dependency data, it contains serialized objects."); - } - } - $deps = unserialize($deps); - if (isset($deps['required']['package'])) { - - if (isset($deps['required']['package']['name'])) { - $deps['required']['package'] = array($deps['required']['package']); - } - - foreach ($deps['required']['package'] as $dependency) { - if (isset($dependency['min'])) { - $packageData['require'][$dependency['name']] = '>='.$dependency['min']; - } else { - $packageData['require'][$dependency['name']] = '>=0.0.0'; - } - } - } - - try { - $this->addPackage($loader->load($packageData)); - } catch (\UnexpectedValueException $e) { - continue; - } + } catch (\UnexpectedValueException $e) { + continue; } } } @@ -135,7 +277,7 @@ class PearRepository extends ArrayRepository */ private function requestXml($url) { - $content = file_get_contents($url); + $content = file_get_contents($url, false, $this->streamContext); if (!$content) { throw new \UnexpectedValueException('The PEAR channel at '.$url.' did not respond.'); } diff --git a/src/Composer/Downloader/Util/Filesystem.php b/src/Composer/Util/Filesystem.php similarity index 98% rename from src/Composer/Downloader/Util/Filesystem.php rename to src/Composer/Util/Filesystem.php index 6d044e40e..4f6e324ac 100644 --- a/src/Composer/Downloader/Util/Filesystem.php +++ b/src/Composer/Util/Filesystem.php @@ -10,9 +10,7 @@ * file that was distributed with this source code. */ -namespace Composer\Downloader\Util; - -use Composer\Util\ProcessExecutor; +namespace Composer\Util; /** * @author Jordi Boggiano diff --git a/src/Composer/Util/StreamContextFactory.php b/src/Composer/Util/StreamContextFactory.php new file mode 100644 index 000000000..4ea31da4d --- /dev/null +++ b/src/Composer/Util/StreamContextFactory.php @@ -0,0 +1,56 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Util; + +/** + * Allows the creation of a basic context supporting http proxy + * + * @author Jordan Alliot + */ +final class StreamContextFactory +{ + /** + * Creates a context supporting HTTP proxies + * + * @param array $defaultOptions Options to merge with the default + * @param array $defaultParams Parameters to specify on the context + * @return resource Default context + * @throws \RuntimeException if https proxy required and OpenSSL uninstalled + */ + static public function getContext(array $defaultOptions = array(), array $defaultParams = array()) + { + $options = array('http' => array()); + + // Handle system proxy + if (isset($_SERVER['HTTP_PROXY']) || isset($_SERVER['http_proxy'])) { + // Some systems seem to rely on a lowercased version instead... + $proxy = isset($_SERVER['HTTP_PROXY']) ? $_SERVER['HTTP_PROXY'] : $_SERVER['http_proxy']; + + // http(s):// is not supported in proxy + $proxy = str_replace(array('http://', 'https://'), array('tcp://', 'ssl://'), $proxy); + + if (0 === strpos($proxy, 'ssl:') && !extension_loaded('openssl')) { + throw new \RuntimeException('You must enable the openssl extension to use a proxy over https'); + } + + $options['http'] = array( + 'proxy' => $proxy, + 'request_fulluri' => true, + ); + } + + $options = array_merge_recursive($options, $defaultOptions); + + return stream_context_create($options, $defaultParams); + } +} diff --git a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php index f7eee0d69..a4e8b9449 100644 --- a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php +++ b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php @@ -13,7 +13,7 @@ namespace Composer\Test\Installer; use Composer\Autoload\AutoloadGenerator; -use Composer\Downloader\Util\Filesystem; +use Composer\Util\Filesystem; use Composer\Package\MemoryPackage; class AutoloadGeneratorTest extends \PHPUnit_Framework_TestCase diff --git a/tests/Composer/Test/Downloader/Util/FilesystemTest.php b/tests/Composer/Test/Downloader/Util/FilesystemTest.php index 243dfa7aa..9605a111c 100644 --- a/tests/Composer/Test/Downloader/Util/FilesystemTest.php +++ b/tests/Composer/Test/Downloader/Util/FilesystemTest.php @@ -12,7 +12,7 @@ namespace Composer\Test\Repository; -use Composer\Downloader\Util\Filesystem; +use Composer\Util\Filesystem; use Composer\Test\TestCase; class FilesystemTest extends TestCase diff --git a/tests/Composer/Test/Installer/LibraryInstallerTest.php b/tests/Composer/Test/Installer/LibraryInstallerTest.php index ed655f983..561143db2 100644 --- a/tests/Composer/Test/Installer/LibraryInstallerTest.php +++ b/tests/Composer/Test/Installer/LibraryInstallerTest.php @@ -14,7 +14,7 @@ namespace Composer\Test\Installer; use Composer\Installer\LibraryInstaller; use Composer\DependencyResolver\Operation; -use Composer\Downloader\Util\Filesystem; +use Composer\Util\Filesystem; class LibraryInstallerTest extends \PHPUnit_Framework_TestCase { diff --git a/tests/Composer/Test/Package/Dumper/ArrayDumperTest.php b/tests/Composer/Test/Package/Dumper/ArrayDumperTest.php new file mode 100644 index 000000000..eab3450bf --- /dev/null +++ b/tests/Composer/Test/Package/Dumper/ArrayDumperTest.php @@ -0,0 +1,66 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Test\Package\Dumper; + +use Composer\Package\Dumper\ArrayDumper; +use Composer\Package\MemoryPackage; + +class ArrayDumperTest extends \PHPUnit_Framework_TestCase +{ + public function setUp() + { + $this->dumper = new ArrayDumper(); + } + + public function testRequiredInformations() + { + $package = new MemoryPackage('foo', '1.0.0.0', '1.0'); + + $config = $this->dumper->dump($package); + $this->assertEquals(array('name', 'version', 'version_normalized', 'type', 'names'), array_keys($config)); + } + + /** + * @dataProvider getKeys + */ + public function testKeys($key, $value, $expectedValue = null, $method = null) + { + $package = new MemoryPackage('foo', '1.0.0.0', '1.0'); + + $setter = 'set'.ucfirst($method ?: $key); + $package->$setter($value); + + $config = $this->dumper->dump($package); + $this->assertArrayHasKey($key, $config); + + $expectedValue = $expectedValue ?: $value; + $this->assertSame($expectedValue, $config[$key]); + } + + public function getKeys() + { + return array( + array('time', new \DateTime('2012-02-01'), '2012-02-01 00:00:00', 'ReleaseDate'), + array('authors', array('Nils Adermann ', 'Jordi Boggiano ')), + array('homepage', 'http://getcomposer.org'), + array('description', 'Package Manager'), + array('keywords', array('package', 'dependency', 'autoload')), + array('bin', array('bin/composer'), null, 'binaries'), + array('license', array('MIT')), + array('autoload', array('psr-0' => array('Composer' => 'src/'))), + array('repositories', array('packagist' => false)), + array('scripts', array('post-update-cmd' => 'MyVendor\\MyClass::postUpdate')), + array('extra', array('class' => 'MyVendor\\Installer')), + ); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 3c891e9d8..02bd0e169 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -16,6 +16,4 @@ if ((!$loader = @include __DIR__.'/../../../.composer/autoload.php') && (!$loade 'php composer.phar install'.PHP_EOL); } -$loader = require __DIR__.'/../vendor/.composer/autoload.php'; $loader->add('Composer\Test', __DIR__); -$loader->register();