protected function loadRouter(ResourceMountHandler $mount)
 {
     $ref = new \ReflectionClass($mount->getTypeName());
     $cacheKey = md5($ref->name);
     $cacheFile = $this->cachePath . '/' . $cacheKey . '.dat';
     if ($this->cached) {
         $mtime = filemtime($ref->getFileName());
         // TODO: Cache check needs to consider class inheritance!
         if (is_file($cacheFile) && filemtime($cacheFile) > $mtime) {
             return new Router(@unserialize(file_get_contents($cacheFile)));
         }
     }
     $collector = new RouteCollector();
     foreach ($ref->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
         if ($method->isAbstract() || $method->isStatic()) {
             continue;
         }
         if (false === ($comment = $method->getDocComment())) {
             continue;
         }
         $match = NULL;
         if (!preg_match("'@route\\(\\s*([^\\)]*)\\s*\\)'iu", $comment, $match)) {
             continue;
         }
         $anno = $match[1];
         if (!preg_match("'(?:^|,)(?:\\s*value\\s*=\\s*)?\\s*\"(?P<pattern>[^\"]+)\"'iu", $anno, $match)) {
             throw new \RuntimeException(sprintf('Invalid route pattern in: %s', $anno));
         }
         $pattern = $match['pattern'];
         $name = NULL;
         if (preg_match("'(?:^|,)\\s*name\\s*=\\s*\"(?P<name>[^\"]*)\"'iu", $anno, $match)) {
             $name = trim($match['name']);
         }
         if ($name === NULL) {
             $name = ltrim(preg_replace_callback("'[A-Z]+'", function (array $match) {
                 return '-' . strtolower($match[0]);
             }, $method->name), '-');
         }
         $collector->addRoute($name, $pattern, $ref->name, $method->name);
     }
     if ($this->cached) {
         Filesystem::writeFile($cacheFile, serialize($collector));
     }
     return new Router($collector);
 }
Ejemplo n.º 2
0
 public function bootRouteCollector(RouteCollector $collector, ContainerInterface $container)
 {
     $container->eachMarked(function (Dispatchable $dispatchable, BindingInterface $binding) use($collector) {
         $collector->addMountHandler($dispatchable->name, $dispatchable->pattern, DispatchableMountHandler::class, [$binding->getTypeName(), $dispatchable->methodName]);
     });
     $container->eachMarked(function (RouteProvider $provider, BindingInterface $binding) use($collector) {
         $collector->addMount($provider->name, $provider->pattern, $binding->getTypeName());
     });
 }
 public function bootRestResources(RouteCollector $collector, ContainerInterface $container)
 {
     $container->eachMarked(function (RestResource $resource, BindingInterface $binding) use($collector) {
         $collector->addMountHandler($resource->name, $resource->pattern, ResourceMountHandler::class, [$binding->getTypeName()]);
     });
 }