Skip to content

Commit d47058c

Browse files
committed
Merge pull request #5 from dg/pull-realpath
removed usage of slow realpath()
2 parents 836de32 + a904dd5 commit d47058c

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

src/RobotLoader/RobotLoader.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,12 @@ public function tryLoad($type)
111111

112112
/**
113113
* Add directory (or directories) to list.
114-
* @param string|array
114+
* @param string|array absolute path
115115
* @return self
116-
* @throws Nette\DirectoryNotFoundException if path is not found
117116
*/
118117
public function addDirectory($path)
119118
{
120-
foreach ((array) $path as $val) {
121-
$real = realpath($val);
122-
if ($real === FALSE) {
123-
throw new Nette\DirectoryNotFoundException("Directory '$val' not found.");
124-
}
125-
$this->scanDirs[] = $real;
126-
}
119+
$this->scanDirs = array_merge($this->scanDirs, (array) $path);
127120
return $this;
128121
}
129122

@@ -170,14 +163,15 @@ public function rebuildCallback()
170163
}
171164

172165
$this->classes = array();
173-
foreach (array_unique($this->scanDirs) as $dir) {
174-
foreach ($this->createFileIterator($dir) as $file) {
166+
foreach ($this->scanDirs as $path) {
167+
foreach (is_file($path) ? array(new SplFileInfo($path)) : $this->createFileIterator($path) as $file) {
175168
$file = $file->getPathname();
176169
if (isset($files[$file]) && $files[$file]['time'] == filemtime($file)) {
177170
$classes = $files[$file]['classes'];
178171
} else {
179172
$classes = $this->scanPhp(file_get_contents($file));
180173
}
174+
$files[$file] = array('classes' => array(), 'time' => filemtime($file));
181175

182176
foreach ($classes as $class) {
183177
$info = & $this->classes[strtolower($class)];
@@ -196,11 +190,12 @@ public function rebuildCallback()
196190
/**
197191
* Creates an iterator scaning directory for PHP files, subdirectories and 'netterobots.txt' files.
198192
* @return \Iterator
193+
* @throws Nette\IOException if path is not found
199194
*/
200195
private function createFileIterator($dir)
201196
{
202197
if (!is_dir($dir)) {
203-
return new \ArrayIterator(array(new \SplFileInfo($dir)));
198+
throw new Nette\IOException("File or directory '$dir' not found.");
204199
}
205200

206201
$ignoreDirs = is_array($this->ignoreDirs) ? $this->ignoreDirs : preg_split('#[,\s]+#', $this->ignoreDirs);

tests/Loaders/RobotLoader.renamed.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ $loader = new RobotLoader;
1616
$loader->setCacheStorage(new DevNullStorage);
1717
$loader->addDirectory(TEMP_DIR);
1818

19-
$dir = realpath(TEMP_DIR) . DIRECTORY_SEPARATOR;
19+
$dir = TEMP_DIR . DIRECTORY_SEPARATOR;
2020
file_put_contents($dir . 'file1.php', '<?php class A {}');
2121
file_put_contents($dir . 'file2.php', '<?php class B {}');
2222

0 commit comments

Comments
 (0)