From abf282488bbcd92507303ef9ff10bf6e7ca2b12e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20H=C5=AFla?= Date: Tue, 2 Dec 2014 17:24:30 +0100 Subject: [PATCH] Allowed loading from phar:// paths [Closes #3] --- src/RobotLoader/RobotLoader.php | 8 +++-- tests/Loaders/RobotLoader.phar.phpt | 49 +++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 tests/Loaders/RobotLoader.phar.phpt diff --git a/src/RobotLoader/RobotLoader.php b/src/RobotLoader/RobotLoader.php index ae1306e..72392ee 100644 --- a/src/RobotLoader/RobotLoader.php +++ b/src/RobotLoader/RobotLoader.php @@ -118,11 +118,13 @@ public function tryLoad($type) public function addDirectory($path) { foreach ((array) $path as $val) { - $real = realpath($val); - if ($real === FALSE) { + if (strncasecmp($val, 'phar://', 7) === 0 && file_exists($val)) { + $this->scanDirs[] = $val; + } elseif (($real = realpath($val)) === FALSE) { throw new Nette\DirectoryNotFoundException("Directory '$val' not found."); + } else { + $this->scanDirs[] = $real; } - $this->scanDirs[] = $real; } return $this; } diff --git a/tests/Loaders/RobotLoader.phar.phpt b/tests/Loaders/RobotLoader.phar.phpt new file mode 100644 index 0000000..172b918 --- /dev/null +++ b/tests/Loaders/RobotLoader.phar.phpt @@ -0,0 +1,49 @@ +setStub('setCacheStorage(new DevNullStorage); +$loader->addDirectory("phar://$pharFile/sub"); +$loader->addDirectory("PHAR://$pharFile/class.B.php"); +Phar::loadPhar($pharFile, 'test.phar'); +$loader->addDirectory("phar://test.phar/class.C.php"); +$loader->register(); + +Assert::false( class_exists('A') ); +Assert::true( class_exists('B') ); +Assert::true( class_exists('C') ); +Assert::true( class_exists('D') ); + + +Assert::exception(function() use ($loader, $pharFile) { + $loader->addDirectory("phar://$pharFile/non-dir"); +}, 'Nette\DirectoryNotFoundException', "Directory 'phar://$pharFile/non-dir' not found."); + +Assert::exception(function() use ($loader, $pharFile) { + $loader->addDirectory("phar://$pharFile/non-file.php"); +}, 'Nette\DirectoryNotFoundException', "Directory 'phar://$pharFile/non-file.php' not found.");