From b71fc0682038bcd7213a3f9dda6080d9e5148e4a Mon Sep 17 00:00:00 2001 From: Christian Berkman Date: Mon, 10 Jun 2024 23:24:40 +0200 Subject: [PATCH 01/13] FileCollection add retainMultiplePatternsMethod Signed-off-by: Christian Berkman --- system/Files/FileCollection.php | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/system/Files/FileCollection.php b/system/Files/FileCollection.php index 2c252018dd6c..a01deae73b04 100644 --- a/system/Files/FileCollection.php +++ b/system/Files/FileCollection.php @@ -340,6 +340,44 @@ public function retainPattern(string $pattern, ?string $scope = null) return $this->removeFiles(array_diff($files, self::matchFiles($files, $pattern))); } + /** + * Keeps only the files from the list that match multiple patterns + * (within the optional scope). + * + * @param array $patterns Array of regex or pseudo-regex strings + * @param string|null $scope A directory to limit the scope + * + * @return $this + */ + public function retainMultiplePatterns(array $patterns, ?string $scope = null) + { + if (count($patterns) === 0) { + return $this; + } + + if (count($patterns) === 1 && $patterns[0] === '') { + return $this; + } + + // Start with all files or those in scope + $files = $scope === null ? $this->files : self::filterFiles($this->files, $scope); + + // Add files to retain to array + $filesToRetain = []; + + foreach ($patterns as $pattern) { + if ($pattern === '') { + continue; + } + + // Matches the pattern within the scoped files + $filesToRetain = array_merge($filesToRetain, self::matchFIles($files, $pattern)); + } + + // Remove the inverse of files to retain + return $this->removeFiles(array_diff($files, $filesToRetain)); + } + // -------------------------------------------------------------------- // Interface Methods // -------------------------------------------------------------------- From f4e89991a02f430c4fefd3dbefd46c1f9007a0d7 Mon Sep 17 00:00:00 2001 From: Christian Berkman Date: Mon, 10 Jun 2024 23:27:39 +0200 Subject: [PATCH 02/13] Add tests for FileCollection::retainMultiplePatterns Signed-off-by: Christian Berkman --- tests/system/Files/FileCollectionTest.php | 70 +++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/tests/system/Files/FileCollectionTest.php b/tests/system/Files/FileCollectionTest.php index 81005356fee3..ddde8667eb3f 100644 --- a/tests/system/Files/FileCollectionTest.php +++ b/tests/system/Files/FileCollectionTest.php @@ -557,6 +557,76 @@ public function testRetainPatternScope(): void $this->assertSame($expected, $collection->get()); } + public function testRetainMultiplePatternsEmptyArray(): void + { + $collection = new FileCollection(); + $collection->addDirectory(SUPPORTPATH . 'Files', true); + + $files = $collection->get(); + + $collection->retainMultiplePatterns([]); + + $this->assertSame($files, $collection->get()); + } + + public function testRetainMultiplePatternsEmptyString(): void + { + $collection = new FileCollection(); + $collection->addDirectory(SUPPORTPATH . 'Files', true); + + $files = $collection->get(); + + $collection->retainMultiplePatterns(['']); + + $this->assertSame($files, $collection->get()); + } + + public function testRetainMultiplePatternsRegex(): void + { + $collection = new FileCollection(); + $collection->addDirectory(SUPPORTPATH . 'Files', true); + + $expected = [ + SUPPORTPATH . 'Files/able/apple.php', + SUPPORTPATH . 'Files/baker/banana.php', + ]; + + $collection->retainMultiplePatterns(['#(apple).*#', '#(banana).*#']); + + $this->assertSame($expected, $collection->get()); + } + + public function testRetainMultiplePatternsPseudo(): void + { + $collection = new FileCollection(); + $collection->addDirectory(SUPPORTPATH . 'Files', true); + + $expected = [ + SUPPORTPATH . 'Files/able/apple.php', + SUPPORTPATH . 'Files/baker/banana.php', + ]; + + $collection->retainMultiplePatterns(['apple*', 'banana*']); + + $this->assertSame($expected, $collection->get()); + } + + public function testRetainMultiplePatternsScope(): void + { + $collection = new FileCollection(); + $collection->addDirectory(SUPPORTPATH . 'Files', true); + + $expected = [ + $this->directory . 'fig_3.php', + SUPPORTPATH . 'Files/baker/banana.php', + SUPPORTPATH . 'Files/baker/fig_3.php.txt', + ]; + + $collection->retainMultiplePatterns(['*_?.php'], $this->directory); + + $this->assertSame($expected, $collection->get()); + } + public function testCount(): void { $collection = new FileCollection(); From a57398d0c2509f1d199b27dc75c3d3786cfa076b Mon Sep 17 00:00:00 2001 From: Christian Berkman Date: Mon, 10 Jun 2024 23:31:49 +0200 Subject: [PATCH 03/13] Updated docs and changelog Signed-off-by: Christian Berkman --- user_guide_src/source/changelogs/v4.6.0.rst | 3 +++ user_guide_src/source/libraries/file_collections.rst | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/user_guide_src/source/changelogs/v4.6.0.rst b/user_guide_src/source/changelogs/v4.6.0.rst index 99d7476299f0..44726821d36f 100644 --- a/user_guide_src/source/changelogs/v4.6.0.rst +++ b/user_guide_src/source/changelogs/v4.6.0.rst @@ -66,6 +66,9 @@ Removed Deprecated Items Enhancements ************ +- Added `retainMultiplePatterns()` to `FileCollection` class. + + Exceptions ========== diff --git a/user_guide_src/source/libraries/file_collections.rst b/user_guide_src/source/libraries/file_collections.rst index 0e7b7b46a362..005fb14c6c5e 100644 --- a/user_guide_src/source/libraries/file_collections.rst +++ b/user_guide_src/source/libraries/file_collections.rst @@ -118,6 +118,15 @@ Examples: .. literalinclude:: files/015.php +retainMultiplePatterns(array $pattern, string $scope = null) +============================================================ + +Provides the same functionality as ``retainPattern()`` but accepts an array of patterns to retain files from all patterns. + +Example: + +.. literalinclude:: files/016.php + **************** Retrieving Files **************** From 61dc880edc560ead12a28a3713160043930f2747 Mon Sep 17 00:00:00 2001 From: Christian Berkman Date: Mon, 10 Jun 2024 23:43:45 +0200 Subject: [PATCH 04/13] Doc: add literal include file Signed-off-by: Christian Berkman --- user_guide_src/source/libraries/files/016.php | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 user_guide_src/source/libraries/files/016.php diff --git a/user_guide_src/source/libraries/files/016.php b/user_guide_src/source/libraries/files/016.php new file mode 100644 index 000000000000..caa78e3b4ca3 --- /dev/null +++ b/user_guide_src/source/libraries/files/016.php @@ -0,0 +1,3 @@ +retainMultiplePatterns(['*.css*', '*.js']); // Would keep only *.css and *.js files From 8056eb38819e2f4b31465dd7aa53c687b7155e7d Mon Sep 17 00:00:00 2001 From: Christian Berkman Date: Wed, 12 Jun 2024 06:54:40 +0200 Subject: [PATCH 05/13] fix: FIleCollection::retainMultiplePatterns phpstan errors Signed-off-by: Christian Berkman --- system/Files/FileCollection.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/system/Files/FileCollection.php b/system/Files/FileCollection.php index a01deae73b04..0f1666221f94 100644 --- a/system/Files/FileCollection.php +++ b/system/Files/FileCollection.php @@ -344,14 +344,14 @@ public function retainPattern(string $pattern, ?string $scope = null) * Keeps only the files from the list that match multiple patterns * (within the optional scope). * - * @param array $patterns Array of regex or pseudo-regex strings + * @param array $patterns Array of regex or pseudo-regex strings * @param string|null $scope A directory to limit the scope * * @return $this */ public function retainMultiplePatterns(array $patterns, ?string $scope = null) { - if (count($patterns) === 0) { + if ($patterns === []) { return $this; } From beb09a91d3b4d5039e244e49923625b66048d8d8 Mon Sep 17 00:00:00 2001 From: Christian Berkman Date: Wed, 12 Jun 2024 06:58:57 +0200 Subject: [PATCH 06/13] docs: move changelog entry to libraries section Signed-off-by: Christian Berkman --- user_guide_src/source/changelogs/v4.6.0.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/user_guide_src/source/changelogs/v4.6.0.rst b/user_guide_src/source/changelogs/v4.6.0.rst index 44726821d36f..d73917af42b9 100644 --- a/user_guide_src/source/changelogs/v4.6.0.rst +++ b/user_guide_src/source/changelogs/v4.6.0.rst @@ -66,9 +66,6 @@ Removed Deprecated Items Enhancements ************ -- Added `retainMultiplePatterns()` to `FileCollection` class. - - Exceptions ========== @@ -111,6 +108,8 @@ Model Libraries ========= +- Added `retainMultiplePatterns()` to `FileCollection` class. + Helpers and Functions ===================== From 0206e99a249887055489110af8207b6e5078778b Mon Sep 17 00:00:00 2001 From: christianberkman <39840601+christianberkman@users.noreply.github.com> Date: Wed, 12 Jun 2024 09:21:20 +0300 Subject: [PATCH 07/13] fix: system/Files/FileCollection.php PHPStan Co-authored-by: kenjis --- system/Files/FileCollection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Files/FileCollection.php b/system/Files/FileCollection.php index 0f1666221f94..5faa77fc194a 100644 --- a/system/Files/FileCollection.php +++ b/system/Files/FileCollection.php @@ -371,7 +371,7 @@ public function retainMultiplePatterns(array $patterns, ?string $scope = null) } // Matches the pattern within the scoped files - $filesToRetain = array_merge($filesToRetain, self::matchFIles($files, $pattern)); + $filesToRetain = array_merge($filesToRetain, self::matchFiles($files, $pattern)); } // Remove the inverse of files to retain From 8c6d7e525e6458becd047134077b040b4014db4a Mon Sep 17 00:00:00 2001 From: Christian Berkman Date: Wed, 12 Jun 2024 08:26:56 +0200 Subject: [PATCH 08/13] FileCollection::retainMultiplePatterns fix docblock param Signed-off-by: Christian Berkman --- system/Files/FileCollection.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/system/Files/FileCollection.php b/system/Files/FileCollection.php index 5faa77fc194a..5b8147480e3b 100644 --- a/system/Files/FileCollection.php +++ b/system/Files/FileCollection.php @@ -27,7 +27,7 @@ * filtering, and ordering them. * * @template-implements IteratorAggregate - * @see \CodeIgniter\Files\FileCollectionTest + * @see FileCollectionTest */ class FileCollection implements Countable, IteratorAggregate { @@ -344,8 +344,8 @@ public function retainPattern(string $pattern, ?string $scope = null) * Keeps only the files from the list that match multiple patterns * (within the optional scope). * - * @param array $patterns Array of regex or pseudo-regex strings - * @param string|null $scope A directory to limit the scope + * @param list $patterns Array of regex or pseudo-regex strings + * @param string|null $scope A directory to limit the scope * * @return $this */ From 13b8a546a10ab9432aa799e43b303161651aa826 Mon Sep 17 00:00:00 2001 From: Christian Berkman Date: Wed, 12 Jun 2024 09:25:57 +0200 Subject: [PATCH 09/13] fix: FileCollection.php AddSeeTestAnnotationRector Signed-off-by: Christian Berkman --- system/Files/FileCollection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Files/FileCollection.php b/system/Files/FileCollection.php index 5b8147480e3b..5ba9a925cc5f 100644 --- a/system/Files/FileCollection.php +++ b/system/Files/FileCollection.php @@ -27,7 +27,7 @@ * filtering, and ordering them. * * @template-implements IteratorAggregate - * @see FileCollectionTest + * @see \CodeIgniter\Files\FileCollectionTest */ class FileCollection implements Countable, IteratorAggregate { From 4fab1dad7585a28e9bccebb429b3e37490e617ae Mon Sep 17 00:00:00 2001 From: Christian Berkman Date: Wed, 12 Jun 2024 11:52:57 +0200 Subject: [PATCH 10/13] docs: add reference to new function Signed-off-by: Christian Berkman --- user_guide_src/source/changelogs/v4.6.0.rst | 2 +- user_guide_src/source/libraries/file_collections.rst | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/user_guide_src/source/changelogs/v4.6.0.rst b/user_guide_src/source/changelogs/v4.6.0.rst index d73917af42b9..ba9df64b69d1 100644 --- a/user_guide_src/source/changelogs/v4.6.0.rst +++ b/user_guide_src/source/changelogs/v4.6.0.rst @@ -108,7 +108,7 @@ Model Libraries ========= -- Added `retainMultiplePatterns()` to `FileCollection` class. +- Added `retainMultiplePatterns()` to `FileCollection` class. See :ref:`FileCollection::retainMultiplePatterns() ` Helpers and Functions ===================== diff --git a/user_guide_src/source/libraries/file_collections.rst b/user_guide_src/source/libraries/file_collections.rst index 005fb14c6c5e..7cf25a9cbd62 100644 --- a/user_guide_src/source/libraries/file_collections.rst +++ b/user_guide_src/source/libraries/file_collections.rst @@ -118,6 +118,9 @@ Examples: .. literalinclude:: files/015.php + +.. _file-collections-retain-multiple-patterns + retainMultiplePatterns(array $pattern, string $scope = null) ============================================================ From 549cc354b1646c59e269bb7f0a53a59c51ce1b61 Mon Sep 17 00:00:00 2001 From: christianberkman <39840601+christianberkman@users.noreply.github.com> Date: Wed, 12 Jun 2024 13:04:56 +0300 Subject: [PATCH 11/13] docs: fix reference Co-authored-by: kenjis --- user_guide_src/source/changelogs/v4.6.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/changelogs/v4.6.0.rst b/user_guide_src/source/changelogs/v4.6.0.rst index ba9df64b69d1..ed4b591aa334 100644 --- a/user_guide_src/source/changelogs/v4.6.0.rst +++ b/user_guide_src/source/changelogs/v4.6.0.rst @@ -108,7 +108,7 @@ Model Libraries ========= -- Added `retainMultiplePatterns()` to `FileCollection` class. See :ref:`FileCollection::retainMultiplePatterns() ` +- Added `retainMultiplePatterns()` to `FileCollection` class. See :ref:`FileCollection::retainMultiplePatterns() `. Helpers and Functions ===================== From fcd33801d413f73f63d167c651eb0d893b969e98 Mon Sep 17 00:00:00 2001 From: christianberkman <39840601+christianberkman@users.noreply.github.com> Date: Wed, 12 Jun 2024 13:05:40 +0300 Subject: [PATCH 12/13] docs: fix section label Co-authored-by: kenjis --- user_guide_src/source/libraries/file_collections.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/libraries/file_collections.rst b/user_guide_src/source/libraries/file_collections.rst index 7cf25a9cbd62..77c046005f62 100644 --- a/user_guide_src/source/libraries/file_collections.rst +++ b/user_guide_src/source/libraries/file_collections.rst @@ -119,7 +119,7 @@ Examples: .. literalinclude:: files/015.php -.. _file-collections-retain-multiple-patterns +.. _file-collections-retain-multiple-patterns: retainMultiplePatterns(array $pattern, string $scope = null) ============================================================ From bb1494a42b7b70eba9079e7369126edf6f0992c5 Mon Sep 17 00:00:00 2001 From: christianberkman <39840601+christianberkman@users.noreply.github.com> Date: Thu, 13 Jun 2024 06:15:40 +0300 Subject: [PATCH 13/13] docs: fix typo Co-authored-by: Michal Sniatala --- user_guide_src/source/libraries/files/016.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/libraries/files/016.php b/user_guide_src/source/libraries/files/016.php index caa78e3b4ca3..b40498cf5786 100644 --- a/user_guide_src/source/libraries/files/016.php +++ b/user_guide_src/source/libraries/files/016.php @@ -1,3 +1,3 @@ retainMultiplePatterns(['*.css*', '*.js']); // Would keep only *.css and *.js files +$files->retainMultiplePatterns(['*.css', '*.js']); // Would keep only *.css and *.js files