Skip to content

Commit

Permalink
Added preserve duplicates to PathConvert (#969)
Browse files Browse the repository at this point in the history
  • Loading branch information
siad007 authored and mrook committed Oct 12, 2018
1 parent fdca258 commit 317ac90
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 5 deletions.
20 changes: 19 additions & 1 deletion classes/phing/tasks/system/PathConvert.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class PathConvert extends Task
public $from = null;
public $to = null;
private $mapper;
private $preserveDuplicates = false;

/**
* constructor
Expand Down Expand Up @@ -246,7 +247,7 @@ public function main()
$rslt = '';

// Get the list of path components in canonical form
$elems = $this->path->listPaths();
$elems = $this->path->listPaths($this->isPreserveDuplicates());

$mapperImpl = $this->mapper === null ? new IdentityMapper() : $this->mapper->getImplementation();
foreach ($elems as &$elem) {
Expand Down Expand Up @@ -391,4 +392,21 @@ private function noChildrenAllowed()
return new BuildException("You must not specify nested <path> "
. "elements when using the refid attribute.");
}

/**
* Get the preserveDuplicates.
* @return boolean
*/
public function isPreserveDuplicates(): bool
{
return $this->preserveDuplicates;
}

/**
* @param bool $preserveDuplicates
*/
public function setPreserveDuplicates(bool $preserveDuplicates): void
{
$this->preserveDuplicates = $preserveDuplicates;
}
}
9 changes: 5 additions & 4 deletions classes/phing/types/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,12 @@ public function addExisting(Path $source)
/**
* Returns all path elements defined by this and nested path objects.
*
* @throws BuildException
*
* @param bool $preserveDuplicates
* @return array List of path elements.
* @throws IOException
* @throws NullPointerException
*/
public function listPaths()
public function listPaths($preserveDuplicates = false)
{
if (!$this->checked) {
// make sure we don't have a circular reference here
Expand Down Expand Up @@ -353,7 +354,7 @@ public function listPaths()
}
}

return array_unique($result);
return $preserveDuplicates ? $result : array_unique($result);
}


Expand Down
7 changes: 7 additions & 0 deletions docs/guide/en/source/appendixes/coretasks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2849,6 +2849,13 @@ verbose="true" failonerror="false" /></programlisting>
<entry>true</entry>
<entry>No</entry>
</row>
<row>
<entry><literal>preserveduplicates</literal></entry>
<entry><literal role="type">Boolean</literal></entry>
<entry>Whether to preserve duplicate resources.</entry>
<entry>false</entry>
<entry>No</entry>
</row>
</tbody>
</tgroup>
</table>
Expand Down
5 changes: 5 additions & 0 deletions etc/phing-grammar.rng
Original file line number Diff line number Diff line change
Expand Up @@ -2004,6 +2004,11 @@
<optional>
<attribute name="setonempty"/>
</optional>
<optional>
<attribute name="preserveduplicates" a:defaultValue="false">
<data type="boolean"/>
</attribute>
</optional>
</interleave>
<interleave>
<optional>
Expand Down
16 changes: 16 additions & 0 deletions test/classes/phing/tasks/system/PathConvertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ public function testMapper()
$this->assertTarget('testmapper');
}

public function testUnique()
{
$p = new Path($this->project, '/a:/a');
$p->setPath("\\a;/a");
$l = $p->listPaths();
$this->assertCount(1, $l, "1 after setPath");
$p->append(new Path($this->project, "/a;\\a:\\a"));
$l = $p->listPaths();
$this->assertCount(1, $l, "1 after append");
$p->createPath()->setPath("\\a:/a");
$l = $p->listPaths();
$this->assertCount(1, $l, "1 after append");
$l = $p->listPaths(true);
$this->assertCount(6, $l, "6 after preserved duplicates");
}

private function assertTarget(string $target)
{
$this->executeTarget($target);
Expand Down

0 comments on commit 317ac90

Please sign in to comment.