PHPDaemon\Applications\FileReaderRequest::init PHP Method

init() public method

Constructor.
public init ( ) : void
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();
    }