Skip to content

Commit ef93f16

Browse files
vlastaveselydg
authored andcommitted
Added RobotLoader::excludeDirectory() (#9)
1 parent 26ae601 commit ef93f16

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ When RobotLoader encounters duplicate class name during indexing, it throws an e
4545
The `$loader->setAutoRefresh(TRUE or FALSE)` determines whether RobotLoader should reindex the scripts if asked for nonexistent class.
4646
This feature should be disabled on production server.
4747

48-
If you want RobotLoader to skip some directory, create a file there called `netterobots.txt`:
48+
If you want RobotLoader to skip some directory, use `$loader->excludeDirectory('temp')` or create a file there called `netterobots.txt`:
4949

5050
```
5151
Disallow: /Zend

src/RobotLoader/RobotLoader.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313

1414
/**
1515
* Nette auto loader is responsible for loading classes and interfaces.
16+
*
17+
* <code>
18+
* $loader = new Nette\Loaders\RobotLoader;
19+
* $loader->addDirectory('app');
20+
* $loader->excludeDirectory('app/exclude');
21+
* $loader->setTempDirectory('temp');
22+
* $loader->register();
23+
* </code>
1624
*/
1725
class RobotLoader
1826
{
@@ -32,6 +40,9 @@ class RobotLoader
3240
/** @var array */
3341
private $scanPaths = [];
3442

43+
/** @var array */
44+
private $excludeDirs = [];
45+
3546
/** @var array of class => [file, time] */
3647
private $classes = [];
3748

@@ -116,6 +127,18 @@ public function addDirectory($path)
116127
}
117128

118129

130+
/**
131+
* Excludes path or paths from list.
132+
* @param string|string[] absolute path
133+
* @return static
134+
*/
135+
public function excludeDirectory($path)
136+
{
137+
$this->excludeDirs = array_merge($this->excludeDirs, (array) $path);
138+
return $this;
139+
}
140+
141+
119142
/**
120143
* @return array of class => filename
121144
*/
@@ -192,7 +215,7 @@ private function createFileIterator($dir)
192215

193216
$ignoreDirs = is_array($this->ignoreDirs) ? $this->ignoreDirs : preg_split('#[,\s]+#', $this->ignoreDirs);
194217
$disallow = [];
195-
foreach ($ignoreDirs as $item) {
218+
foreach (array_merge($ignoreDirs, $this->excludeDirs) as $item) {
196219
if ($item = realpath($item)) {
197220
$disallow[str_replace('\\', '/', $item)] = TRUE;
198221
}
@@ -415,7 +438,7 @@ private function getCacheFile()
415438
*/
416439
protected function getCacheKey()
417440
{
418-
return [$this->ignoreDirs, $this->acceptFiles, $this->scanPaths];
441+
return [$this->ignoreDirs, $this->acceptFiles, $this->scanPaths, $this->excludeDirs];
419442
}
420443

421444
}

tests/Loaders/RobotLoader.phpt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ $loader->addDirectory(__DIR__ . '/file/interface.php'); // as file
1919
$loader->addDirectory(__DIR__ . '/file/class.const.php');
2020
$loader->addDirectory(__DIR__ . '/file/trait.php');
2121
$loader->addDirectory(__DIR__ . '/files.robots');
22+
$loader->excludeDirectory(__DIR__ . '/files/exclude');
2223
$loader->register();
2324

2425
Assert::false(class_exists('ConditionalClass')); // files/conditional.class.php
@@ -38,3 +39,5 @@ Assert::false(class_exists('Disallowed4')); // files.robots\subdir\disallowed4
3839
Assert::false(class_exists('Disallowed5')); // files.robots\subdir\subdir2\disallowed5\class.php
3940
Assert::false(class_exists('Disallowed6')); // files.robots\subdir\subdir2\class.php
4041
Assert::true(class_exists('Allowed2')); // files.robots\subdir\subdir2\allowed.php
42+
43+
Assert::false(class_exists('ExcludedClass')); // files/exclude/excluded.php
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
class ExcludedClass
4+
{}

0 commit comments

Comments
 (0)