Skip to content

Commit

Permalink
FileStorage & RobotLoader uses Nette\Finder
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Sep 15, 2010
1 parent f379461 commit 6505ec3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 68 deletions.
7 changes: 1 addition & 6 deletions Nette/Caching/FileStorage.php
Expand Up @@ -266,13 +266,8 @@ public function clean(array $conds)
// cleaning using file iterator
if ($all || $collector) {
$now = time();
$base = $this->dir . DIRECTORY_SEPARATOR . 'c';
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->dir), \RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $entry) {
foreach (Nette\Finder::find('/c*', '/c*/*')->from($this->dir)->limitDepth(1)->childFirst() as $entry) {
$path = (string) $entry;
if (strncmp($path, $base, strlen($base))) { // skip files out of cache
continue;
}
if ($entry->isDir()) { // collector: remove empty dirs
@rmdir($path); // @ - removing dirs is not necessary
continue;
Expand Down
83 changes: 21 additions & 62 deletions Nette/Loaders/RobotLoader.php
Expand Up @@ -44,15 +44,6 @@ class RobotLoader extends AutoLoader
/** @var bool */
private $rebuilt = FALSE;

/** @var string */
private $acceptMask;

/** @var string */
private $ignoreMask;

/** @var array */
private $disallow = array();



/**
Expand Down Expand Up @@ -143,8 +134,6 @@ public function rebuild()
*/
public function _rebuildCallback()
{
$this->acceptMask = self::wildcards2re($this->acceptFiles);
$this->ignoreMask = self::wildcards2re($this->ignoreDirs);
foreach ($this->list as $pair) {
if ($pair) $this->files[$pair[0]] = $pair[1];
}
Expand Down Expand Up @@ -216,46 +205,35 @@ private function addClass($class, $file, $time)
*/
private function scanDirectory($dir)
{
if (is_file($dir)) {
if (!isset($this->files[$dir]) || $this->files[$dir] !== filemtime($dir)) {
$this->scanScript($dir);
}
return;
}

$iterator = dir($dir);
if (!$iterator) return;

$base = $dir . DIRECTORY_SEPARATOR;
if (is_file($dir . '/netterobots.txt')) {
foreach (file($dir . '/netterobots.txt') as $s) {
if ($matches = String::match($s, '#^disallow\\s*:\\s*(\\S+)#i')) {
$this->disallow[$base . str_replace('/', DIRECTORY_SEPARATOR, trim($matches[1], '/'))] = TRUE;
}
}
if (isset($this->disallow[$base])) return;
}

while (FALSE !== ($entry = $iterator->read())) {
if ($entry == '.' || $entry == '..' || isset($this->disallow[$path = $base . $entry])) continue;

// process subdirectories
if (is_dir($path)) {
// check ignore mask
if (!String::match($entry, $this->ignoreMask)) {
$this->scanDirectory($path);
$disallow = array();
$iterator = new \AppendIterator;
$iterator->append(new \ArrayIterator(array(new \SplFileInfo($dir))));
if (is_dir($dir)) {
$iterator->append(Nette\Finder::find(String::split($this->acceptFiles, '#[,\s]+#'))
->from($dir)
->exclude(String::split($this->ignoreDirs, '#[,\s]+#'))
->filter(function($file) use (&$disallow){
return !isset($disallow[$file->getPathname()]);
})->getIterator()
);
};

foreach ($iterator as $entry) {
$path = (string) $entry;
if ($entry->isDir() && is_file("$path/netterobots.txt")) {
foreach (file("$path/netterobots.txt") as $s) {
if ($matches = String::match($s, '#^disallow\\s*:\\s*(\\S+)#i')) {
$disallow[$path . str_replace('/', DIRECTORY_SEPARATOR, '/' . trim($matches[1], '/'))] = TRUE;
}
}
continue;
}

if (is_file($path) && String::match($entry, $this->acceptMask)) {
if ($entry->isFile()) {
if (!isset($this->files[$path]) || $this->files[$path] !== filemtime($path)) {
$this->scanScript($path);
}
}
}

$iterator->close();
}


Expand Down Expand Up @@ -338,25 +316,6 @@ private function scanScript($file)



/**
* Converts comma separated wildcards to regular expression.
* @param string
* @return string
*/
private static function wildcards2re($wildcards)
{
$mask = array();
foreach (explode(',', $wildcards) as $wildcard) {
$wildcard = trim($wildcard);
$wildcard = addcslashes($wildcard, '.\\+[^]$(){}=!><|:#');
$wildcard = strtr($wildcard, array('*' => '.*', '?' => '.'));
$mask[] = $wildcard;
}
return '#^(' . implode('|', $mask) . ')$#i';
}



/********************* backend ****************d*g**/


Expand Down

0 comments on commit 6505ec3

Please sign in to comment.