Skip to content

Commit cfaa8f8

Browse files
authored
Merge pull request #8960 from christianberkman/feat-retain-multiple-patterns
feat: [FileCollection] add function to reatain multiple patterns
2 parents 27f11f5 + bb1494a commit cfaa8f8

File tree

5 files changed

+125
-0
lines changed

5 files changed

+125
-0
lines changed

system/Files/FileCollection.php

+38
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,44 @@ public function retainPattern(string $pattern, ?string $scope = null)
340340
return $this->removeFiles(array_diff($files, self::matchFiles($files, $pattern)));
341341
}
342342

343+
/**
344+
* Keeps only the files from the list that match multiple patterns
345+
* (within the optional scope).
346+
*
347+
* @param list<string> $patterns Array of regex or pseudo-regex strings
348+
* @param string|null $scope A directory to limit the scope
349+
*
350+
* @return $this
351+
*/
352+
public function retainMultiplePatterns(array $patterns, ?string $scope = null)
353+
{
354+
if ($patterns === []) {
355+
return $this;
356+
}
357+
358+
if (count($patterns) === 1 && $patterns[0] === '') {
359+
return $this;
360+
}
361+
362+
// Start with all files or those in scope
363+
$files = $scope === null ? $this->files : self::filterFiles($this->files, $scope);
364+
365+
// Add files to retain to array
366+
$filesToRetain = [];
367+
368+
foreach ($patterns as $pattern) {
369+
if ($pattern === '') {
370+
continue;
371+
}
372+
373+
// Matches the pattern within the scoped files
374+
$filesToRetain = array_merge($filesToRetain, self::matchFiles($files, $pattern));
375+
}
376+
377+
// Remove the inverse of files to retain
378+
return $this->removeFiles(array_diff($files, $filesToRetain));
379+
}
380+
343381
// --------------------------------------------------------------------
344382
// Interface Methods
345383
// --------------------------------------------------------------------

tests/system/Files/FileCollectionTest.php

+70
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,76 @@ public function testRetainPatternScope(): void
557557
$this->assertSame($expected, $collection->get());
558558
}
559559

560+
public function testRetainMultiplePatternsEmptyArray(): void
561+
{
562+
$collection = new FileCollection();
563+
$collection->addDirectory(SUPPORTPATH . 'Files', true);
564+
565+
$files = $collection->get();
566+
567+
$collection->retainMultiplePatterns([]);
568+
569+
$this->assertSame($files, $collection->get());
570+
}
571+
572+
public function testRetainMultiplePatternsEmptyString(): void
573+
{
574+
$collection = new FileCollection();
575+
$collection->addDirectory(SUPPORTPATH . 'Files', true);
576+
577+
$files = $collection->get();
578+
579+
$collection->retainMultiplePatterns(['']);
580+
581+
$this->assertSame($files, $collection->get());
582+
}
583+
584+
public function testRetainMultiplePatternsRegex(): void
585+
{
586+
$collection = new FileCollection();
587+
$collection->addDirectory(SUPPORTPATH . 'Files', true);
588+
589+
$expected = [
590+
SUPPORTPATH . 'Files/able/apple.php',
591+
SUPPORTPATH . 'Files/baker/banana.php',
592+
];
593+
594+
$collection->retainMultiplePatterns(['#(apple).*#', '#(banana).*#']);
595+
596+
$this->assertSame($expected, $collection->get());
597+
}
598+
599+
public function testRetainMultiplePatternsPseudo(): void
600+
{
601+
$collection = new FileCollection();
602+
$collection->addDirectory(SUPPORTPATH . 'Files', true);
603+
604+
$expected = [
605+
SUPPORTPATH . 'Files/able/apple.php',
606+
SUPPORTPATH . 'Files/baker/banana.php',
607+
];
608+
609+
$collection->retainMultiplePatterns(['apple*', 'banana*']);
610+
611+
$this->assertSame($expected, $collection->get());
612+
}
613+
614+
public function testRetainMultiplePatternsScope(): void
615+
{
616+
$collection = new FileCollection();
617+
$collection->addDirectory(SUPPORTPATH . 'Files', true);
618+
619+
$expected = [
620+
$this->directory . 'fig_3.php',
621+
SUPPORTPATH . 'Files/baker/banana.php',
622+
SUPPORTPATH . 'Files/baker/fig_3.php.txt',
623+
];
624+
625+
$collection->retainMultiplePatterns(['*_?.php'], $this->directory);
626+
627+
$this->assertSame($expected, $collection->get());
628+
}
629+
560630
public function testCount(): void
561631
{
562632
$collection = new FileCollection();

user_guide_src/source/changelogs/v4.6.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ Model
108108
Libraries
109109
=========
110110

111+
- Added `retainMultiplePatterns()` to `FileCollection` class. See :ref:`FileCollection::retainMultiplePatterns() <file-collections-retain-multiple-patterns>`.
112+
111113
Helpers and Functions
112114
=====================
113115

user_guide_src/source/libraries/file_collections.rst

+12
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,18 @@ Examples:
118118

119119
.. literalinclude:: files/015.php
120120

121+
122+
.. _file-collections-retain-multiple-patterns:
123+
124+
retainMultiplePatterns(array $pattern, string $scope = null)
125+
============================================================
126+
127+
Provides the same functionality as ``retainPattern()`` but accepts an array of patterns to retain files from all patterns.
128+
129+
Example:
130+
131+
.. literalinclude:: files/016.php
132+
121133
****************
122134
Retrieving Files
123135
****************
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
$files->retainMultiplePatterns(['*.css', '*.js']); // Would keep only *.css and *.js files

0 commit comments

Comments
 (0)