コード例 #1
0
 public function index()
 {
     /**
      * because we can run withou caching, this flag is needed
      */
     if ($this->built === true) {
         return $this;
     }
     /**
      * intentionally first line - built is done even if failature occures
      */
     $this->built = true;
     if ($this->cache->load('index') === true) {
         return $this;
     }
     $exclude = '~(' . implode(')|(', $this->excludeList) . ')~';
     $this->storage->truncate($schema = $this->createScannerFile()->schema());
     $this->storage->createSchema($schema);
     $this->storage->startTransaction(self::class);
     try {
         foreach ($this->pathList as $directory) {
             foreach ($this->createIterator($directory) as $path => $item) {
                 /**
                  * nice line, isn't it ;)?
                  */
                 if (!empty($this->excludeList) && StringUtils::match(($tmp = realpath($path)) ? $path = $tmp : $path, $exclude)) {
                     continue;
                 }
                 $finfo = new \finfo(FILEINFO_MIME);
                 $scannerFile = $this->createScannerFile();
                 $scannerFile->restore(['file' => FileUtils::normalize($item->getRealPath()), 'extension' => ($extension = $item->getExtension()) ? $extension : null, 'mime' => $finfo->file($path), 'size' => (int) $item->getSize(), 'stamp' => DateTimeUtils::create(), 'changed' => DateTimeUtils::from($item->getMTime())]);
                 $this->storage->save($scannerFile);
             }
         }
         $this->storage->commitTransaction(self::class);
         $this->cache->save('index', true);
     } catch (\Exception $e) {
         $this->storage->rollbackTransaction(self::class);
         throw $e;
     }
     return $this;
 }