示例#1
0
 public function getExpr(Compiler $cmp, \Closure $callback)
 {
     switch ($this->type) {
         case self::CONSTANT:
             $expr = '$parts[' . $this->index . '] === ' . var_export($this->raw, true);
             break;
         case self::MIXED:
             $regex = array();
             $filters = array();
             $i = 0;
             foreach ($this->parts as $id => $part) {
                 if ($part[0] == self::CONSTANT) {
                     $regex[] = preg_quote($part[1]);
                 } else {
                     $regex[] = "(.+)";
                     $f = $cmp->getFilterExpr($part[1]);
                     if (!empty($f)) {
                         $i++;
                         $index = (int) $this->index;
                         $filters[] = $callback($f['filter'], '$req', $f['name'], "\$matches_{$index}[{$i}]");
                     }
                 }
             }
             $regex = var_export("/^" . implode("", $regex) . "/", true);
             $index = (int) $this->index;
             $expr = "preg_match({$regex}, \$parts[{$this->index}], \$matches_{$index}) > 0";
             if (count($filters)) {
                 $expr .= ' && ' . implode(' && ', $filters);
             }
             break;
         case self::VARIABLE:
             $f = $cmp->getFilterExpr($this->parts[0][1]);
             if (empty($f)) {
                 return "";
             }
             $f['filter'] = $callback($f['filter'], '$req', $f['name'], '$parts[' . $this->index . ']');
             $name = "\$filter_" . substr(sha1($f['name']), 0, 8) . "_" . intval($this->index);
             if ($this->isLoop) {
                 $expr = "({$name}={$f['filter']})";
             } else {
                 $expr = "(!empty({$name}) || ({$name}={$f['filter']}))";
             }
             break;
         case self::LOOP:
             $this->type = $this->stype;
             $expr = $this->getExpr($cmp, $callback);
             $this->type = self::LOOP;
     }
     return $expr;
 }
示例#2
0
 public function generate()
 {
     $output = $this->getOutput();
     $cache = new Watch($output . '.cache');
     $dirs = array_unique($this->dirs);
     $files = array_unique($this->files);
     $files[] = $output;
     $dirs[] = __DIR__ . '/Filters';
     $cache->watchFiles($files);
     $cache->watchDirs($dirs);
     if (!$cache->hasChanged()) {
         return;
     }
     $annotations = new \Notoj\Filesystem(array_unique(array_merge($dirs, $files)));
     $compiler = new Compiler($this, $annotations);
     File::write($output, $compiler->getOutput());
     $cache->watchFiles($files)->watchDirs($dirs);
     $cache->watch();
     return $compiler->getOutput();
 }
示例#3
0
 public function generate()
 {
     $output = $this->getOutput();
     if (empty($output)) {
         throw new \RuntimeException("You need to set an output file");
     }
     $cache = new Watch($output . '.cache');
     $dirs = $this->dirs;
     foreach ($this->files as $file) {
         $dirs[] = dirname($file);
     }
     $cache->watchFiles($this->files);
     $cache->watchDirs($dirs);
     if (!$cache->hasChanged()) {
         return;
     }
     $annotations = new Annotations();
     $isCached = $output && file_exists($output);
     $files = array();
     $dirs = array();
     foreach (array_unique($this->files) as $file) {
         $ann = new FileParser($file);
         $ann->getAnnotations($annotations);
         $files[] = $file;
         $dirs[] = dirname($file);
     }
     foreach (array_unique($this->dirs) as $dir) {
         $ann = new DirParser($dir);
         $ann->getAnnotations($annotations);
         $dirs[] = $dir;
         $files = array_merge($files, $ann->getFiles());
     }
     foreach ($files as $file) {
         $dirs[] = dirname($file);
     }
     $cache->watchFiles($files)->watchDirs($dirs);
     $cache->watch();
     foreach ($files as $file) {
         $dirs[] = dirname($file);
     }
     $compiler = new Compiler($this, $annotations);
     if (!empty($output)) {
         file_put_contents($output, $compiler->getOutput());
     }
     return $compiler->getOutput();
 }
示例#4
0
文件: Url.php 项目: crodas/dispatcher
 public function getApplications()
 {
     return Compiler::getApplications($this->def);
 }