From caf0fce98de84a9498063536f3aa5debb18d704a Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 9 Jul 2020 13:37:16 +0700 Subject: [PATCH 1/5] Fixes #3254 add ability to override existing translation validation language in App This reverts commit eeaa9221aec4ad63bf63236bc037190fd4a36495. --- app/Language/en/Validation.php | 4 ++++ system/Language/Language.php | 11 ++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 app/Language/en/Validation.php diff --git a/app/Language/en/Validation.php b/app/Language/en/Validation.php new file mode 100644 index 0000000000..54d1e7a4a2 --- /dev/null +++ b/app/Language/en/Validation.php @@ -0,0 +1,4 @@ +search($path); + $files = Services::locator()->search($path); + $files = array_merge( + array_filter($files, function ($value) { + return strpos($value, APPPATH) === false; + }), + array_filter($files, function ($value) { + return strpos($value, APPPATH) === 0; + }) + ); + $strings = []; foreach ($files as $file) From 0bdef6bff6fe628e1d7786476705bbb302e43803 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 9 Jul 2020 17:46:21 +0700 Subject: [PATCH 2/5] apply 3rd parameter $prioritizeApp = true to FileLocator::search() --- system/Autoloader/FileLocator.php | 23 +++++++++++++++++---- system/Language/Language.php | 11 +--------- tests/system/Autoloader/FileLocatorTest.php | 16 ++++++++++++++ 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/system/Autoloader/FileLocator.php b/system/Autoloader/FileLocator.php index 5aa217e11c..a4c06efde9 100644 --- a/system/Autoloader/FileLocator.php +++ b/system/Autoloader/FileLocator.php @@ -226,28 +226,43 @@ class FileLocator * 'app/Modules/bar/Config/Routes.php', * ] * - * @param string $path - * @param string $ext + * @param string $path + * @param string $ext + * @param boolean $prioritizeApp * * @return array */ - public function search(string $path, string $ext = 'php'): array + public function search(string $path, string $ext = 'php', bool $prioritizeApp = true): array { $path = $this->ensureExt($path, $ext); $foundPaths = []; + $appPaths = []; foreach ($this->getNamespaces() as $namespace) { if (isset($namespace['path']) && is_file($namespace['path'] . $path)) { - $foundPaths[] = $namespace['path'] . $path; + $fullPath = $namespace['path'] . $path; + if ($prioritizeApp || strpos($fullPath, APPPATH) !== 0) + { + $foundPaths[] = $fullPath; + } + else + { + $appPaths[] = $fullPath; + } } } // Remove any duplicates $foundPaths = array_unique($foundPaths); + if (! $prioritizeApp && ! empty($appPaths)) + { + $foundPaths = array_merge($foundPaths, array_unique($appPaths)); + } + return $foundPaths; } diff --git a/system/Language/Language.php b/system/Language/Language.php index 061e04c468..3ee0c508fc 100644 --- a/system/Language/Language.php +++ b/system/Language/Language.php @@ -338,16 +338,7 @@ class Language */ protected function requireFile(string $path): array { - $files = Services::locator()->search($path); - $files = array_merge( - array_filter($files, function ($value) { - return strpos($value, APPPATH) === false; - }), - array_filter($files, function ($value) { - return strpos($value, APPPATH) === 0; - }) - ); - + $files = Services::locator()->search($path, 'php', false); $strings = []; foreach ($files as $file) diff --git a/tests/system/Autoloader/FileLocatorTest.php b/tests/system/Autoloader/FileLocatorTest.php index f40471e416..eb8088d611 100644 --- a/tests/system/Autoloader/FileLocatorTest.php +++ b/tests/system/Autoloader/FileLocatorTest.php @@ -191,6 +191,22 @@ class FileLocatorTest extends \CodeIgniter\Test\CIUnitTestCase $this->assertArrayNotHasKey(0, $foundFiles); } + //-------------------------------------------------------------------- + + public function testSearchPrioritizeSystemOverApp() + { + $foundFiles = $this->locator->search('Language/en/Validation.php', 'php', false); + + $this->assertEquals([ + SYSTEMPATH . 'Language/en/Validation.php', + APPPATH . 'Language/en/Validation.php', + ], + $foundFiles + ); + } + + //-------------------------------------------------------------------- + public function testListNamespaceFilesEmptyPrefixAndPath() { $this->assertEmpty($this->locator->listNamespaceFiles('', '')); From d9a793bb43c9fb0f23873fd98425f55521aec017 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 9 Jul 2020 18:11:32 +0700 Subject: [PATCH 3/5] ensure add array entry to $appPaths when strpos() found --- system/Autoloader/FileLocator.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/system/Autoloader/FileLocator.php b/system/Autoloader/FileLocator.php index a4c06efde9..7a573e304f 100644 --- a/system/Autoloader/FileLocator.php +++ b/system/Autoloader/FileLocator.php @@ -250,7 +250,10 @@ class FileLocator } else { - $appPaths[] = $fullPath; + if (! in_array($fullPath, $appPaths, true) && strpos($fullPath, APPPATH) === 0) + { + $appPaths[] = $fullPath; + } } } } @@ -260,7 +263,7 @@ class FileLocator if (! $prioritizeApp && ! empty($appPaths)) { - $foundPaths = array_merge($foundPaths, array_unique($appPaths)); + $foundPaths = array_merge($foundPaths, $appPaths); } return $foundPaths; From b8bff1b442c8987b963750e155cc2253d91507bd Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 9 Jul 2020 18:22:37 +0700 Subject: [PATCH 4/5] more verbose check for check prioritizeApp --- system/Autoloader/FileLocator.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/system/Autoloader/FileLocator.php b/system/Autoloader/FileLocator.php index 7a573e304f..b68026a450 100644 --- a/system/Autoloader/FileLocator.php +++ b/system/Autoloader/FileLocator.php @@ -244,16 +244,20 @@ class FileLocator if (isset($namespace['path']) && is_file($namespace['path'] . $path)) { $fullPath = $namespace['path'] . $path; - if ($prioritizeApp || strpos($fullPath, APPPATH) !== 0) + if ($prioritizeApp) { $foundPaths[] = $fullPath; } else { - if (! in_array($fullPath, $appPaths, true) && strpos($fullPath, APPPATH) === 0) + if (strpos($fullPath, APPPATH) === 0) { $appPaths[] = $fullPath; } + else + { + $foundPaths[] = $fullPath; + } } } } @@ -263,7 +267,7 @@ class FileLocator if (! $prioritizeApp && ! empty($appPaths)) { - $foundPaths = array_merge($foundPaths, $appPaths); + $foundPaths = array_merge($foundPaths, array_unique($appPaths)); } return $foundPaths; From 41a8f04014441eeb3dcf48ff221660606e82aaf6 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 10 Jul 2020 12:59:38 +0700 Subject: [PATCH 5/5] make run array_unique once after merge --- system/Autoloader/FileLocator.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/system/Autoloader/FileLocator.php b/system/Autoloader/FileLocator.php index b68026a450..4557cb1685 100644 --- a/system/Autoloader/FileLocator.php +++ b/system/Autoloader/FileLocator.php @@ -262,14 +262,14 @@ class FileLocator } } - // Remove any duplicates - $foundPaths = array_unique($foundPaths); - if (! $prioritizeApp && ! empty($appPaths)) { - $foundPaths = array_merge($foundPaths, array_unique($appPaths)); + $foundPaths = array_merge($foundPaths, $appPaths); } + // Remove any duplicates + $foundPaths = array_unique($foundPaths); + return $foundPaths; }