Skip to content

#218 - Correctly resolve relative local paths pointing to patch files from dependencies - Follow up #247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
vendor/
tests/_data/test-package/src/
7 changes: 7 additions & 0 deletions src/Patch.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ class Patch implements \JsonSerializable
*/
public $depth;

/**
* The package that provides the patch.
*
* @var \Composer\Package\PackageInterface $provider
*/
public $provider;

/**
* Create a Patch from a serialized representation.
*
Expand Down
38 changes: 38 additions & 0 deletions src/Plugin/Patches.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,28 @@ public function checkPatches(Event $event)
}
}

/**
* Check whether a given path is relative.
*
* @param string $url
* The URL to check.
*
* @return bool
* True if URL is relative, false otherwise.
*/
protected function isRelativeUrl($url)
{
if (parse_url($url, PHP_URL_SCHEME) !== '') {
return false;
}

if (strpos($url, '/') === 0) {
return false;
}

return true;
}

/**
* @param PackageEvent $event
* @throws \Exception
Expand Down Expand Up @@ -322,6 +344,22 @@ public function postInstall(PackageEvent $event)
null,
new PatchEvent(PatchEvents::PRE_PATCH_APPLY, $package, $patch->url, $patch->description)
);

if ($this->isRelativeUrl($patch->url) && !empty($patch->provider)) {
if (!$manager->isPackageInstalled($localRepository, $patch->provider)) {
$this->io->write(
' - <info>Installing ' .
$patch->provider->getName() .
'</info> to ensure access to relative patches.'
);
$manager->getInstaller(
$patch->provider->getType()
)->install($localRepository, $patch->provider);
}
$providing_install_path = $manager->getInstallPath($patch->provider);
$patch->url = $providing_install_path.'/'.$patch->url;
}

$this->getAndApplyPatch($downloader, $install_path, $patch->url);
$this->eventDispatcher->dispatch(
null,
Expand Down
15 changes: 9 additions & 6 deletions src/Resolvers/DependencyPatches.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ public function resolve(PatchCollection $collection, PackageEvent $event)
$package = $this->getPackageFromOperation($operation);
/** @var PackageInterface $extra */
$extra = $package->getExtra();
if (isset($extra['patches'])) {
$patches = $this->findPatchesInJson($extra['patches']);
foreach ($patches as $package => $patch_list) {
foreach ($patch_list as $patch) {
$collection->addPatch($patch);
}
if (!isset($extra['patches'])) {
continue;
}

$patches = $this->findPatchesInJson($extra['patches']);
foreach ($patches as $patchList) {
foreach ($patchList as $patch) {
$patch->provider = $package;
$collection->addPatch($patch);
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions tests/_data/test-package/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "cweagans/test-package",
"description": "Project for use in cweagans/composer-patches acceptance tests.",
"type": "project",
"license": "BSD-2-Clause"
}
8 changes: 8 additions & 0 deletions tests/acceptance/ApplyPatchFromLocalDirectoryCept.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
$I = new AcceptanceTester($scenario);
$I->wantTo('Apply a patch from a local directory.');
$I->amInPath(realpath(__DIR__ . '/fixtures/apply-patch-from-local-directory'));
$I->runShellCommand('composer install');
$I->canSeeFileFound(
realpath(__DIR__ . '/fixtures/apply-patch-from-local-directory/vendor/cweagans/test-package/src/NewFile.php')
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "cweagans/composer-patches-test-project",
"description": "Project for use in cweagans/composer-patches acceptance tests.",
"type": "project",
"license": "BSD-2-Clause",
"repositories": [
{
"type": "path",
"url": "../composer-patches"
},
{
"type": "path",
"url": "../../../_data/test-package"
}
],
"require": {
"cweagans/composer-patches": "*@dev",
"cweagans/test-package": "*@dev"
},
"extra": {
"enable-patching": true,
"composer-exit-on-patch-failure": 1,
"patches": {
"cweagans/test-package": {
"test": "./resources/patches/newfile.patch"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
diff --git a/src/NewFile.php b/src/NewFile.php
new file mode 100644
index 0000000..e69de29