Skip to content

Commit abf2824

Browse files
committed
Allowed loading from phar:// paths [Closes #3]
1 parent 1daa13a commit abf2824

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

src/RobotLoader/RobotLoader.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,13 @@ public function tryLoad($type)
118118
public function addDirectory($path)
119119
{
120120
foreach ((array) $path as $val) {
121-
$real = realpath($val);
122-
if ($real === FALSE) {
121+
if (strncasecmp($val, 'phar://', 7) === 0 && file_exists($val)) {
122+
$this->scanDirs[] = $val;
123+
} elseif (($real = realpath($val)) === FALSE) {
123124
throw new Nette\DirectoryNotFoundException("Directory '$val' not found.");
125+
} else {
126+
$this->scanDirs[] = $real;
124127
}
125-
$this->scanDirs[] = $real;
126128
}
127129
return $this;
128130
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Loaders\RobotLoader loading from PHAR.
5+
*
6+
* @phpIni phar.readonly=0
7+
*/
8+
9+
use Nette\Loaders\RobotLoader,
10+
Nette\Caching\Storages\DevNullStorage,
11+
Tester\Assert;
12+
13+
14+
require __DIR__ . '/../bootstrap.php';
15+
16+
$pharFile = TEMP_DIR . '/test.phar';
17+
18+
$phar = new Phar($pharFile);
19+
$phar['class.A.php'] = '<?php class A {}';
20+
$phar['class.B.php'] = '<?php class B {}';
21+
$phar['class.C.php'] = '<?php class C {}';
22+
$phar['sub/class.D.php'] = '<?php class D {}';
23+
$phar->setStub('<?php __HALT_COMPILER();');
24+
unset($phar);
25+
26+
Assert::true( is_file($pharFile) );
27+
28+
29+
$loader = new RobotLoader;
30+
$loader->setCacheStorage(new DevNullStorage);
31+
$loader->addDirectory("phar://$pharFile/sub");
32+
$loader->addDirectory("PHAR://$pharFile/class.B.php");
33+
Phar::loadPhar($pharFile, 'test.phar');
34+
$loader->addDirectory("phar://test.phar/class.C.php");
35+
$loader->register();
36+
37+
Assert::false( class_exists('A') );
38+
Assert::true( class_exists('B') );
39+
Assert::true( class_exists('C') );
40+
Assert::true( class_exists('D') );
41+
42+
43+
Assert::exception(function() use ($loader, $pharFile) {
44+
$loader->addDirectory("phar://$pharFile/non-dir");
45+
}, 'Nette\DirectoryNotFoundException', "Directory 'phar://$pharFile/non-dir' not found.");
46+
47+
Assert::exception(function() use ($loader, $pharFile) {
48+
$loader->addDirectory("phar://$pharFile/non-file.php");
49+
}, 'Nette\DirectoryNotFoundException', "Directory 'phar://$pharFile/non-file.php' not found.");

0 commit comments

Comments
 (0)