readfile() public static method

Reads whole file
public static readfile ( string $path, callable $cb, integer $pri = EIO_PRI_DEFAULT ) : resource | true
$path string Path
$cb callable Callback (Path, Contents)
$pri integer Priority
return resource | true
 public function init()
 {
     $req = $this;
     $this->sleep(1, true);
     \PHPDaemon\FS\FileSystem::readfile('/etc/filesystems', function ($file, $data) use($req) {
         $req->fileData = $data;
         $req->wakeup();
     });
 }
Beispiel #2
0
 /**
  * Called when worker is going to update configuration
  * @return void
  */
 public function onConfigUpdated()
 {
     parent::onConfigUpdated();
     if (Daemon::$process instanceof \PHPDaemon\Thread\Worker) {
         $pool = $this;
         FileSystem::readfile($this->config->file->value, function ($file, $data) use($pool) {
             $pool->policyData = $data;
             $pool->enable();
         });
     }
 }
Beispiel #3
0
 /**
  * Applies config
  * @return void
  */
 public function applyConfig()
 {
     parent::applyConfig();
     $pool = $this;
     if (!isset($this->preloading)) {
         $this->preloading = new ComplexJob();
     }
     $job = $this->preloading;
     $job->addJob('resolvfile', function ($jobname, $job) use($pool) {
         FileSystem::readfile($pool->config->resolvfile->value, function ($file, $data) use($pool, $job, $jobname) {
             if ($file) {
                 preg_match_all('~nameserver ([^\\r\\n;]+)~', $data, $m);
                 foreach ($m[1] as $s) {
                     $pool->addServer('udp://' . $s);
                     //$pool->addServer('tcp://' . $s);
                 }
             }
             $job->setResult($jobname);
         });
     });
     $job->addJob('hostsfile', function ($jobname, $job) use($pool) {
         FileSystem::readfile($pool->config->hostsfile->value, function ($file, $data) use($pool, $job, $jobname) {
             if ($file) {
                 preg_match_all('~^(\\S+)\\s+([^\\r\\n]+)\\s*~m', $data, $m, PREG_SET_ORDER);
                 $pool->hosts = [];
                 foreach ($m as $h) {
                     $hosts = preg_split('~\\s+~', $h[2]);
                     $ip = $h[1];
                     foreach ($hosts as $host) {
                         $host = rtrim($host, '.') . '.';
                         $pool->hosts[$host] = $ip;
                     }
                 }
             }
             $job->setResult($jobname);
         });
     });
     $job();
 }
Beispiel #4
0
 /**
  * Applies config
  * @return void
  */
 public function applyConfig()
 {
     parent::applyConfig();
     $pool = $this;
     if (!isset($this->preloading)) {
         $this->preloading = new ComplexJob();
     }
     $job = $this->preloading;
     $job->addJob('resolvfile', function ($jobname, $job) use($pool) {
         FileSystem::readfile($pool->config->resolvfile->value, function ($file, $data) use($pool, $job, $jobname) {
             if ($file) {
                 foreach (explode("\n", $data) as $line) {
                     $line = trim($line);
                     if ($line !== '' && $line[0] !== '#' && preg_match('~nameserver ([^\\r\\n;]+)~i', $line, $m)) {
                         $pool->nameServers[] = $m[1];
                     }
                 }
             }
             $job->setResult($jobname);
         });
     });
     $job->addJob('hostsfile', function ($jobname, $job) use($pool) {
         FileSystem::readfile($pool->config->hostsfile->value, function ($file, $data) use($pool, $job, $jobname) {
             if ($file) {
                 preg_match_all('~^(\\S+)\\s+([^\\r\\n]+)\\s*~m', $data, $m, PREG_SET_ORDER);
                 $pool->hosts = [];
                 foreach ($m as $h) {
                     $hosts = preg_split('~\\s+~', $h[2]);
                     $ip = $h[1];
                     foreach ($hosts as $host) {
                         $host = rtrim($host, '.') . '.';
                         $pool->hosts[$host][] = $ip;
                     }
                 }
             }
             $job->setResult($jobname);
         });
     });
     $job();
 }