diff --git a/system/Debug/Exceptions.php b/system/Debug/Exceptions.php index ef7c45a3ed6e..7f7ca2cb2628 100644 --- a/system/Debug/Exceptions.php +++ b/system/Debug/Exceptions.php @@ -372,17 +372,20 @@ protected function determineCodes(Throwable $exception): array */ public static function cleanPath(string $file): string { - if (strpos($file, APPPATH) === 0) + switch (true) { - $file = 'APPPATH/' . substr($file, strlen(APPPATH)); - } - elseif (strpos($file, SYSTEMPATH) === 0) - { - $file = 'SYSTEMPATH/' . substr($file, strlen(SYSTEMPATH)); - } - elseif (strpos($file, FCPATH) === 0) - { - $file = 'FCPATH/' . substr($file, strlen(FCPATH)); + case strpos($file, APPPATH) === 0: + $file = 'APPPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(APPPATH)); + break; + case strpos($file, SYSTEMPATH) === 0: + $file = 'SYSTEMPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(SYSTEMPATH)); + break; + case strpos($file, FCPATH) === 0: + $file = 'FCPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(FCPATH)); + break; + case defined('VENDORPATH') && strpos($file, VENDORPATH) === 0; + $file = 'VENDORPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(VENDORPATH)); + break; } return $file; diff --git a/system/Test/bootstrap.php b/system/Test/bootstrap.php index 4a217fef12e7..83585f374171 100644 --- a/system/Test/bootstrap.php +++ b/system/Test/bootstrap.php @@ -22,6 +22,7 @@ defined('TESTPATH') || define('TESTPATH', realpath(HOMEPATH . 'tests/') . DIRECTORY_SEPARATOR); defined('SUPPORTPATH') || define('SUPPORTPATH', realpath(TESTPATH . '_support/') . DIRECTORY_SEPARATOR); defined('COMPOSER_PATH') || define('COMPOSER_PATH', realpath(HOMEPATH . 'vendor/autoload.php')); +defined('VENDORPATH') || define('VENDORPATH', realpath(HOMEPATH . 'vendor') . DIRECTORY_SEPARATOR); // Load Common.php from App then System if (file_exists(APPPATH . 'Common.php')) diff --git a/system/bootstrap.php b/system/bootstrap.php index 33ab45598d07..e014ad05e144 100644 --- a/system/bootstrap.php +++ b/system/bootstrap.php @@ -139,6 +139,16 @@ class_alias('Config\Services', 'CodeIgniter\Services'); // Now load Composer's if it's available if (is_file(COMPOSER_PATH)) { + /** + * The path to the vendor directory. + * + * We do not want to enforce this, so set the constant if Composer was used. + */ + if (! defined('VENDORPATH')) + { + define('VENDORPATH', realpath(ROOTPATH . 'vendor') . DIRECTORY_SEPARATOR); + } + require_once COMPOSER_PATH; } diff --git a/tests/system/Debug/ExceptionsTest.php b/tests/system/Debug/ExceptionsTest.php index d9f1dae8b8cc..25fdf8890cb3 100644 --- a/tests/system/Debug/ExceptionsTest.php +++ b/tests/system/Debug/ExceptionsTest.php @@ -7,4 +7,36 @@ public function testNew() $actual = new Exceptions(new \Config\Exceptions(), \Config\Services::request(), \Config\Services::response()); $this->assertInstanceOf(Exceptions::class, $actual); } + + /** + * @dataProvider dirtyPathsProvider + */ + public function testCleanPaths($file, $expected) + { + $this->assertEquals($expected, Exceptions::cleanPath($file)); + } + + public function dirtyPathsProvider() + { + $ds = DIRECTORY_SEPARATOR; + + return [ + [ + APPPATH . 'Config' . $ds . 'App.php', + 'APPPATH' . $ds . 'Config' . $ds . 'App.php', + ], + [ + SYSTEMPATH . 'CodeIgniter.php', + 'SYSTEMPATH' . $ds . 'CodeIgniter.php', + ], + [ + VENDORPATH . 'autoload.php', + 'VENDORPATH' . $ds . 'autoload.php', + ], + [ + FCPATH . 'index.php', + 'FCPATH' . $ds . 'index.php', + ], + ]; + } }