Skip to content

Commit

Permalink
forward-ported change from 3.X
Browse files Browse the repository at this point in the history
  • Loading branch information
Allan Paiste committed Aug 16, 2018
2 parents f8fb14f + 5267611 commit 06149e2
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 26 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,17 @@ the merging will be done in very late state based on the absolute path of the pa

## Basic Usage: comments in patch declaration

In case user wants to add extra comments to patch declaration file, a key that start with "_comment" can be
used.
In case user wants to add extra comments to patch declaration file, any key that start with "_" can be
used. Works on any level of the patch declaration.

```json
{
"_comment": "This patch file should hold patches that make world a better place",
"whole/world": {
"_excuse": "I really need this one",
"Fix: get closer to ending poverty": "patches/provide-affordable-education.patch"
},
"_comment0": "This is another comment"
"_note": "This is another comment"
}
```

Expand Down
14 changes: 13 additions & 1 deletion changelog.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"4.1.0": {
"feature": [
"allow comments on any level and with any keyword as long as it starts with underscore (_)"
]
},
"4.0.0": {
"breaking": [
"logic: installation/update/applying patches fails on first patch failure (used to be activated by COMPOSER_PATCHES_FATAL_FAIL); old default behaviour usable via COMPOSER_PATCHES_GRACEFUL or using --graceful flag",
Expand All @@ -13,10 +18,17 @@
"allow patch failures to be passed over gracefully with extra/patcher/graceful configuration in root package"
]
},
"3.32.0": {
"feature": [
"allow comments on any level and with any keyword as long as it starts with underscore (_)"
],
"branch": "release/3.X"
},
"3.31.0": {
"feature": [
"halt applying patches when encountering package with local changes to avoid developer from losing their work (allow override with env flag or command --force flag)"
]
],
"branch": "release/3.X"
},
"3.30.1": {
"fix": [
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
"tivie/php-os-detector": "^1.0",
"marcj/topsort": "^1.0"
},
"require-dev": {
"vaimo/composer-changelogs": "^0.6.2"
},
"support": {
"source": "https://github.com/vaimo/composer-patches",
"docs": "https://github.com/vaimo/composer-patches",
Expand Down
16 changes: 8 additions & 8 deletions src/Patch/Definition/ExploderComponents/ComplexItemComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ public function shouldProcess($label, $data)
if (!is_array($data)) {
return false;
}

$key = key($data);
$value = reset($data);

$versionKeySet = false;

if (is_array($value)) {
$versionKeySet = isset($value[PatchDefinition::VERSION])
$versionKeySet = isset($value[PatchDefinition::VERSION])
|| isset($value[PatchDefinition::DEPENDS]);
}
}

return !is_numeric($key) && is_array($value) && $versionKeySet;
}

public function explode($label, $data)
{
$items = array();

foreach ($data as $source => $subItem) {
$items[] = array(
$label,
Expand All @@ -40,7 +40,7 @@ public function explode($label, $data)
))
);
}

return $items;
}
}
35 changes: 21 additions & 14 deletions src/Patch/ListNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ class ListNormalizer
*/
private $definitionNormalizer;

/**
* @var \Vaimo\ComposerPatches\Utils\PatchListUtils
*/
private $patchListUtils;

/**
* @param \Vaimo\ComposerPatches\Patch\Definition\Exploder $definitionExploder
* @param \Vaimo\ComposerPatches\Patch\Definition\Normalizer $definitionNormalizer
Expand All @@ -27,38 +32,40 @@ public function __construct(
) {
$this->definitionExploder = $definitionExploder;
$this->definitionNormalizer = $definitionNormalizer;

$this->patchListUtils = new \Vaimo\ComposerPatches\Utils\PatchListUtils();
}

public function normalize(array $list, array $config)
{
$patchesPerPackage = array();
$result = array();

foreach ($list as $target => $packagePatches) {
foreach ($this->patchListUtils->getSanitizedList($list) as $target => $packagePatches) {
$patches = array();

if (strpos($target, '_comment') === 0) {
continue;
}

foreach ($packagePatches as $patchLabel => $patchConfig) {
if (strpos($target, '_comment') === 0) {
continue;
}

$definitionItems = $this->definitionExploder->process($patchLabel, $patchConfig);
$definitionItems = $this->definitionExploder->process(
$patchLabel,
$patchConfig
);

foreach ($definitionItems as $patchItem) {
list($label, $data) = $patchItem;

$patches[] = $this->definitionNormalizer->process($target, $label, $data, $config);
$patches[] = $this->definitionNormalizer->process(
$target,
$label,
$data,
$config
);
}
}

$patchesPerPackage[$target] = $patches;
$result[$target] = $patches;
}

return array_filter(
array_map('array_filter', $patchesPerPackage)
array_map('array_filter', $result)
);
}
}
39 changes: 39 additions & 0 deletions src/Utils/DataUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Copyright © Vaimo Group. All rights reserved.
* See LICENSE_VAIMO.txt for license details.
*/
namespace Vaimo\ComposerPatches\Utils;

class DataUtils
{
public function removeKeysByPrefix(array $data, $prefix)
{
return array_intersect_key(
$data,
array_flip(
array_filter(
array_keys($data),
function ($key) use ($prefix) {
return strpos($key, $prefix) !== 0;
}
)
)
);
}

public function walkArrayNodes(array $list, \Closure $callback)
{
$list = $callback($list);

foreach ($list as $key => $value) {
if (!is_array($value)) {
continue;
}

$list[$key] = $this->walkArrayNodes($value, $callback);
}

return $list;
}
}
22 changes: 22 additions & 0 deletions src/Utils/PatchListUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@

class PatchListUtils
{
/**
* @var \Vaimo\ComposerPatches\Utils\DataUtils
*/
private $dataUtils;

public function __construct()
{
$this->dataUtils = new \Vaimo\ComposerPatches\Utils\DataUtils();
}

public function createSimplifiedList(array $patches)
{
$groups = $this->createTargetsList($patches);
Expand Down Expand Up @@ -218,4 +228,16 @@ function ($items) {

return array_filter(array_map('array_filter', $items));
}

public function getSanitizedList(array $patches)
{
$dataUtils = $this->dataUtils;

return $this->dataUtils->walkArrayNodes(
$patches,
function (array $value) use ($dataUtils) {
return $dataUtils->removeKeysByPrefix($value, '_');
}
);
}
}

0 comments on commit 06149e2

Please sign in to comment.