From 323537ad0fc59e0abb91cfffb0e3795657eba283 Mon Sep 17 00:00:00 2001 From: Dennis Birkholz Date: Tue, 15 Sep 2015 16:41:07 +0200 Subject: [PATCH 01/11] Allow wildcards in repository path names. --- src/Composer/Repository/PathRepository.php | 49 ++++++++++--------- .../Fixtures/path/with-version/composer.json | 2 +- .../path/without-version/composer.json | 2 +- .../Test/Repository/PathRepositoryTest.php | 26 +++++++++- 4 files changed, 52 insertions(+), 27 deletions(-) diff --git a/src/Composer/Repository/PathRepository.php b/src/Composer/Repository/PathRepository.php index 8e0b2edca..f2753241a 100644 --- a/src/Composer/Repository/PathRepository.php +++ b/src/Composer/Repository/PathRepository.php @@ -97,36 +97,39 @@ class PathRepository extends ArrayRepository { parent::initialize(); - $path = $this->getPath(); - $composerFilePath = $path.'composer.json'; - if (!file_exists($composerFilePath)) { - throw new \RuntimeException(sprintf('No `composer.json` file found in path repository "%s"', $path)); - } + foreach ($this->getPaths() as $path) { + $composerFilePath = $path.'composer.json'; + if (!file_exists($composerFilePath)) { + continue; + } - $json = file_get_contents($composerFilePath); - $package = JsonFile::parseJson($json, $composerFilePath); - $package['dist'] = array( - 'type' => 'path', - 'url' => $this->url, - 'reference' => '', - ); + $json = file_get_contents($composerFilePath); + $package = JsonFile::parseJson($json, $composerFilePath); + $package['dist'] = array( + 'type' => 'path', + 'url' => $this->url, + 'reference' => '', + ); - if (!isset($package['version'])) { - $package['version'] = $this->versionGuesser->guessVersion($package, $path) ?: 'dev-master'; - } - if (is_dir($path.'/.git') && 0 === $this->process->execute('git log -n1 --pretty=%H', $output, $path)) { - $package['dist']['reference'] = trim($output); - } + if (!isset($package['version'])) { + $package['version'] = $this->versionGuesser->guessVersion($package, $path) ?: 'dev-master'; + } + if (is_dir($path.'/.git') && 0 === $this->process->execute('git log -n1 --pretty=%H', $output, $path)) { + $package['dist']['reference'] = trim($output); + } - $package = $this->loader->load($package); - $this->addPackage($package); + $package = $this->loader->load($package); + $this->addPackage($package); + } } /** - * @return string + * Get a list of all absolute path names matching the supplied urls + * + * @return string[] */ - private function getPath() + private function getPaths() { - return realpath(rtrim($this->url, '/')) . '/'; + return glob($this->url, GLOB_MARK|GLOB_ONLYDIR); } } diff --git a/tests/Composer/Test/Repository/Fixtures/path/with-version/composer.json b/tests/Composer/Test/Repository/Fixtures/path/with-version/composer.json index fa853d690..985b89947 100644 --- a/tests/Composer/Test/Repository/Fixtures/path/with-version/composer.json +++ b/tests/Composer/Test/Repository/Fixtures/path/with-version/composer.json @@ -1,4 +1,4 @@ { - "name": "test/path", + "name": "test/path-versioned", "version": "0.0.2" } \ No newline at end of file diff --git a/tests/Composer/Test/Repository/Fixtures/path/without-version/composer.json b/tests/Composer/Test/Repository/Fixtures/path/without-version/composer.json index 2fcbad12a..cddefceb2 100644 --- a/tests/Composer/Test/Repository/Fixtures/path/without-version/composer.json +++ b/tests/Composer/Test/Repository/Fixtures/path/without-version/composer.json @@ -1,3 +1,3 @@ { - "name": "test/path" + "name": "test/path-unversioned" } \ No newline at end of file diff --git a/tests/Composer/Test/Repository/PathRepositoryTest.php b/tests/Composer/Test/Repository/PathRepositoryTest.php index fb64e458b..03ad46fea 100644 --- a/tests/Composer/Test/Repository/PathRepositoryTest.php +++ b/tests/Composer/Test/Repository/PathRepositoryTest.php @@ -35,7 +35,7 @@ class PathRepositoryTest extends TestCase $repository->getPackages(); $this->assertEquals(1, $repository->count()); - $this->assertTrue($repository->hasPackage($this->getPackage('test/path', '0.0.2'))); + $this->assertTrue($repository->hasPackage($this->getPackage('test/path-versioned', '0.0.2'))); } public function testLoadPackageFromFileSystemWithoutVersion() @@ -54,9 +54,31 @@ class PathRepositoryTest extends TestCase $this->assertEquals(1, $repository->count()); $package = $packages[0]; - $this->assertEquals('test/path', $package->getName()); + $this->assertEquals('test/path-unversioned', $package->getName()); $packageVersion = $package->getVersion(); $this->assertTrue(!empty($packageVersion)); } + + public function testLoadPackageFromFileSystemWithWildcard() + { + $ioInterface = $this->getMockBuilder('Composer\IO\IOInterface') + ->getMock(); + + $config = new \Composer\Config(); + $loader = new ArrayLoader(new VersionParser()); + $versionGuesser = null; + + $repositoryUrl = implode(DIRECTORY_SEPARATOR, array(__DIR__, 'Fixtures', 'path', '*')); + $repository = new PathRepository(array('url' => $repositoryUrl), $ioInterface, $config, $loader); + $packages = $repository->getPackages(); + + $this->assertEquals(2, $repository->count()); + + $package = $packages[0]; + $this->assertEquals('test/path-versioned', $package->getName()); + + $package = $packages[1]; + $this->assertEquals('test/path-unversioned', $package->getName()); + } } From 4209fd2e4aaf7951712d3749effd27d8f9f45216 Mon Sep 17 00:00:00 2001 From: Dennis Birkholz Date: Tue, 15 Sep 2015 17:27:36 +0200 Subject: [PATCH 02/11] Store correct url for package, not original url containing wildcards. --- src/Composer/Repository/PathRepository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Composer/Repository/PathRepository.php b/src/Composer/Repository/PathRepository.php index f2753241a..81de87866 100644 --- a/src/Composer/Repository/PathRepository.php +++ b/src/Composer/Repository/PathRepository.php @@ -107,7 +107,7 @@ class PathRepository extends ArrayRepository $package = JsonFile::parseJson($json, $composerFilePath); $package['dist'] = array( 'type' => 'path', - 'url' => $this->url, + 'url' => $path, 'reference' => '', ); From e4435790a4055c58f60200c34b527f649455b311 Mon Sep 17 00:00:00 2001 From: Dennis Birkholz Date: Tue, 15 Sep 2015 17:39:55 +0200 Subject: [PATCH 03/11] Documentation --- doc/05-repositories.md | 7 +++++-- src/Composer/Repository/PathRepository.php | 4 ++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/doc/05-repositories.md b/doc/05-repositories.md index 5dae47295..8e0442bd8 100644 --- a/doc/05-repositories.md +++ b/doc/05-repositories.md @@ -608,8 +608,8 @@ update to the latest version. ### Path -In addition to the artifact repository, you can use the path one, which allow -you to depends on a relative directory. This can be especially useful when dealing +In addition to the artifact repository, you can use the path one, which allows +you to depend on a relative directory. This can be especially useful when dealing with monolith repositories. For instance, if you have the following directory structure in your repository: @@ -639,6 +639,9 @@ file, you can use the following configuration: } ``` +Repository paths can also contain wildcards like ``*`` and ``?``. +For details, see the [PHP glob function](http://php.net/glob). + ## Disabling Packagist You can disable the default Packagist repository by adding this to your diff --git a/src/Composer/Repository/PathRepository.php b/src/Composer/Repository/PathRepository.php index 81de87866..94df63104 100644 --- a/src/Composer/Repository/PathRepository.php +++ b/src/Composer/Repository/PathRepository.php @@ -38,6 +38,10 @@ use Composer\Util\ProcessExecutor; * { * "type": "path", * "url": "/absolute/path/to/package/" + * }, + * { + * "type": "path", + * "url": "/absolute/path/to/several/packages/*" * } * ] * @endcode From c06edd61e4f6e2e93273809a7da32d12922b3b71 Mon Sep 17 00:00:00 2001 From: Dennis Birkholz Date: Tue, 15 Sep 2015 18:28:17 +0200 Subject: [PATCH 04/11] Fail if no repository was found --- src/Composer/Repository/PathRepository.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Composer/Repository/PathRepository.php b/src/Composer/Repository/PathRepository.php index 94df63104..1abca00b6 100644 --- a/src/Composer/Repository/PathRepository.php +++ b/src/Composer/Repository/PathRepository.php @@ -101,12 +101,15 @@ class PathRepository extends ArrayRepository { parent::initialize(); + $foundPackage = false; + foreach ($this->getPaths() as $path) { $composerFilePath = $path.'composer.json'; if (!file_exists($composerFilePath)) { continue; } + $foundPackage = true; $json = file_get_contents($composerFilePath); $package = JsonFile::parseJson($json, $composerFilePath); $package['dist'] = array( @@ -125,6 +128,10 @@ class PathRepository extends ArrayRepository $package = $this->loader->load($package); $this->addPackage($package); } + + if (!$foundPackage) { + throw new \RuntimeException(sprintf('No `composer.json` file found in any path repository in "%s"', $this->url)); + } } /** From 17636d1bd243f50a6820ec7c3f32a1ae59239b57 Mon Sep 17 00:00:00 2001 From: Dennis Birkholz Date: Fri, 18 Sep 2015 14:23:54 +0200 Subject: [PATCH 05/11] Use 'note' box to inform about wildcards --- doc/05-repositories.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/05-repositories.md b/doc/05-repositories.md index 8e0442bd8..9308e1279 100644 --- a/doc/05-repositories.md +++ b/doc/05-repositories.md @@ -639,8 +639,8 @@ file, you can use the following configuration: } ``` -Repository paths can also contain wildcards like ``*`` and ``?``. -For details, see the [PHP glob function](http://php.net/glob). +> **Note:** Repository paths can also contain wildcards like ``*`` and ``?``. +> For details, see the [PHP glob function](http://php.net/glob). ## Disabling Packagist From 19146d1cecaeee46700eb272000a55e47a23975c Mon Sep 17 00:00:00 2001 From: Dennis Birkholz Date: Fri, 18 Sep 2015 14:26:01 +0200 Subject: [PATCH 06/11] Docblock fix as suggested by @alcohol --- src/Composer/Repository/PathRepository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Composer/Repository/PathRepository.php b/src/Composer/Repository/PathRepository.php index 1abca00b6..5f9fc0a53 100644 --- a/src/Composer/Repository/PathRepository.php +++ b/src/Composer/Repository/PathRepository.php @@ -135,7 +135,7 @@ class PathRepository extends ArrayRepository } /** - * Get a list of all absolute path names matching the supplied urls + * Get a list of all absolute path names matching given url (supports globbing). * * @return string[] */ From 973491b65df45a5f82c125aaae0427da1b0dda30 Mon Sep 17 00:00:00 2001 From: Dennis Birkholz Date: Fri, 18 Sep 2015 14:29:42 +0200 Subject: [PATCH 07/11] Fix docblock, path names returned by glob() are not necessary absolute --- src/Composer/Repository/PathRepository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Composer/Repository/PathRepository.php b/src/Composer/Repository/PathRepository.php index 5f9fc0a53..665e4037b 100644 --- a/src/Composer/Repository/PathRepository.php +++ b/src/Composer/Repository/PathRepository.php @@ -135,7 +135,7 @@ class PathRepository extends ArrayRepository } /** - * Get a list of all absolute path names matching given url (supports globbing). + * Get a list of all path names matching given url (supports globbing). * * @return string[] */ From 3ef222c9201e990aa380930802678467c558401f Mon Sep 17 00:00:00 2001 From: Dennis Birkholz Date: Fri, 18 Sep 2015 15:01:06 +0200 Subject: [PATCH 08/11] Make repository path absolute (again) --- src/Composer/Repository/PathRepository.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Composer/Repository/PathRepository.php b/src/Composer/Repository/PathRepository.php index 665e4037b..891320118 100644 --- a/src/Composer/Repository/PathRepository.php +++ b/src/Composer/Repository/PathRepository.php @@ -104,6 +104,8 @@ class PathRepository extends ArrayRepository $foundPackage = false; foreach ($this->getPaths() as $path) { + $path = realpath($path); + $composerFilePath = $path.'composer.json'; if (!file_exists($composerFilePath)) { continue; From 2fb7dd881a9f692379b493280515497bef82c482 Mon Sep 17 00:00:00 2001 From: Dennis Birkholz Date: Fri, 18 Sep 2015 15:03:15 +0200 Subject: [PATCH 09/11] Fix missing trailing / in repository paths --- src/Composer/Repository/PathRepository.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Composer/Repository/PathRepository.php b/src/Composer/Repository/PathRepository.php index 891320118..5e4b1d931 100644 --- a/src/Composer/Repository/PathRepository.php +++ b/src/Composer/Repository/PathRepository.php @@ -104,8 +104,8 @@ class PathRepository extends ArrayRepository $foundPackage = false; foreach ($this->getPaths() as $path) { - $path = realpath($path); - + $path = realpath($path) . '/'; + $composerFilePath = $path.'composer.json'; if (!file_exists($composerFilePath)) { continue; From 6b1c9882dd113f630c2dcf69115e02887f5e2a27 Mon Sep 17 00:00:00 2001 From: Dennis Birkholz Date: Fri, 18 Sep 2015 17:26:31 +0200 Subject: [PATCH 10/11] Replace tracking variable with direct package count check --- src/Composer/Repository/PathRepository.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Composer/Repository/PathRepository.php b/src/Composer/Repository/PathRepository.php index 5e4b1d931..b4b08b152 100644 --- a/src/Composer/Repository/PathRepository.php +++ b/src/Composer/Repository/PathRepository.php @@ -101,8 +101,6 @@ class PathRepository extends ArrayRepository { parent::initialize(); - $foundPackage = false; - foreach ($this->getPaths() as $path) { $path = realpath($path) . '/'; @@ -111,7 +109,6 @@ class PathRepository extends ArrayRepository continue; } - $foundPackage = true; $json = file_get_contents($composerFilePath); $package = JsonFile::parseJson($json, $composerFilePath); $package['dist'] = array( @@ -131,7 +128,7 @@ class PathRepository extends ArrayRepository $this->addPackage($package); } - if (!$foundPackage) { + if (count($this) == 0) { throw new \RuntimeException(sprintf('No `composer.json` file found in any path repository in "%s"', $this->url)); } } From 681e543793d402f712d033a2cc4530a064ccad70 Mon Sep 17 00:00:00 2001 From: Dennis Birkholz Date: Fri, 18 Sep 2015 17:55:07 +0200 Subject: [PATCH 11/11] Make check more obvious --- src/Composer/Repository/PathRepository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Composer/Repository/PathRepository.php b/src/Composer/Repository/PathRepository.php index b4b08b152..a76a2e052 100644 --- a/src/Composer/Repository/PathRepository.php +++ b/src/Composer/Repository/PathRepository.php @@ -128,7 +128,7 @@ class PathRepository extends ArrayRepository $this->addPackage($package); } - if (count($this) == 0) { + if (count($this->getPackages()) == 0) { throw new \RuntimeException(sprintf('No `composer.json` file found in any path repository in "%s"', $this->url)); } }