PHPDaemon\FS\FileSystem::readdir PHP Method

readdir() public static method

Readdir()
public static readdir ( string $path, callable $cb = null, integer $flags = null, integer $pri = EIO_PRI_DEFAULT ) : resource | true
$path string Path
$cb callable = null Callback
$flags integer = null Flags
$pri integer = EIO_PRI_DEFAULT Priority
return resource | true
    public static function readdir($path, $cb = null, $flags = null, $pri = EIO_PRI_DEFAULT)
    {
        $cb = CallbackWrapper::forceWrap($cb);
        if (!FileSystem::$supported) {
            $r = glob($path);
            if ($cb) {
                $cb($path, $r);
            }
            return true;
        }
        return eio_readdir($path, $flags, $pri, $cb, $path);
    }

Usage Example

 /**
  * Constructor.
  * @return void
  */
 public function init()
 {
     if (!isset($this->attrs->server['FR_PATH'])) {
         $this->status(404);
         $this->finish();
         return;
     }
     $job = new \PHPDaemon\Core\ComplexJob(function ($job) {
         $this->wakeup();
     });
     $this->job = $job;
     $this->sleep(5, true);
     $this->attrs->server['FR_PATH'] = \PHPDaemon\FS\FileSystem::sanitizePath($this->attrs->server['FR_PATH']);
     $job('stat', function ($name, $job) {
         /** @var \PHPDaemon\Core\ComplexJob $job */
         \PHPDaemon\FS\FileSystem::stat($this->attrs->server['FR_PATH'], function ($path, $stat) use($job) {
             if ($stat === -1) {
                 $this->fileNotFound();
                 $job->setResult('stat', false);
                 return;
             }
             if ($stat['type'] === 'd') {
                 if (!\PHPDaemon\FS\FileSystem::$supported) {
                     $this->file(rtrim($path, '/') . '/index.html');
                 } else {
                     $job('readdir', function ($name, $job) use($path) {
                         /** @var \PHPDaemon\Core\ComplexJob $job */
                         \PHPDaemon\FS\FileSystem::readdir(rtrim($path, '/'), function ($path, $dir) use($job) {
                             $found = false;
                             if (is_array($dir)) {
                                 foreach ($dir['dents'] as $file) {
                                     if ($file['type'] === \EIO_DT_REG) {
                                         // is file
                                         if (in_array($file['name'], $this->appInstance->indexFiles)) {
                                             $this->file($path . '/' . $file['name']);
                                             $found = true;
                                             break;
                                         }
                                     }
                                 }
                             }
                             if (!$found) {
                                 if (isset($this->attrs->server['FR_AUTOINDEX']) && $this->attrs->server['FR_AUTOINDEX']) {
                                     $this->autoindex($path, $dir);
                                 } else {
                                     $this->fileNotFound();
                                 }
                             }
                             $job->setResult('readdir');
                         }, \EIO_READDIR_STAT_ORDER | \EIO_READDIR_DENTS);
                     });
                 }
             } elseif ($stat['type'] === 'f') {
                 $this->file($path);
             }
             $job->setResult('stat', $stat);
         });
     });
     $job();
 }