Skip to content

Commit

Permalink
Merge pull request #2847 from paulbalandan/normalize-exceptions-clean…
Browse files Browse the repository at this point in the history
…path

Normalize dir separator of Exceptions::cleanPath and added more paths to clean
  • Loading branch information
lonnieezell authored Apr 20, 2020
2 parents 39a4867 + 4cb62d1 commit 3e6b851
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
23 changes: 13 additions & 10 deletions system/Debug/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions system/Test/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
10 changes: 10 additions & 0 deletions system/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
32 changes: 32 additions & 0 deletions tests/system/Debug/ExceptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
];
}
}

0 comments on commit 3e6b851

Please sign in to comment.