public function __construct(Context $context) { parent::__construct($context); $this->type = 'RouteParams'; if (isset($context->getCaller()->annotations['canonical'])) { $this->routeParams = Url::extract($context->getCaller()->annotations['canonical'], $context->getUrl()->requestUri); } }
public static function createFromRequest(Request $request) { $self = new static($request->context); if (isset($request->context->getCaller()->annotations['canonical'])) { $self->setRouteParams(Url::extract($request->context->getCaller()->annotations['canonical'], $request->context->getUrl()->requestUri)); } else { throw new Exception\MissingDocBlockException('RouteParams Request class requires @canonical DocBlock on calling method.'); } return $self; }
private function scanForMethodMatches($controller) { $controllerClassReflection = new \ReflectionClass($controller); $methods = $controllerClassReflection->getMethods(); foreach ($methods as $method) { if ($method->getDocComment()) { $annotation = Utils\Annotations::parseDocBlock($method->getDocComment()); if (isset($annotation['canonical'])) { $canonical = Utils\Url::templateToRegex($annotation['canonical'], $keys); // Return as soon as a match is found if (preg_match($canonical, $this->url->getRequestUri())) { return $method->getName(); } } } } }
public function urlFor($callback, array $params = null) { try { // Standard array-based callable [$object, $methodName] if (is_array($callback)) { $reflection = new \ReflectionMethod($callback[0], $callback[1]); } else { // Static callable - class::methodName if (is_callable($callback)) { $reflection = new \ReflectionMethod($callback); } else { // Fallback 1 - try to make it callable by adding a namespace if (strpos($callback, '::') !== false) { $reflection = new \ReflectionMethod($this->context->getProject()->ns . '\\Controllers\\' . $callback); } else { // Fallback 2 - if partial string, assume it's a method name in the current controller class if ($this->context->getCaller()->controller) { $reflection = new \ReflectionMethod($this->context->getCaller()->controller, $callback); } else { throw new ReverseRouteLookupException("Parameter passed to the urlFor method is not callable"); } } } } } catch (\ReflectionException $e) { throw new ReverseRouteLookupException("Parameter passed to the urlFor method is not callable"); } $doc = $reflection->getDocComment(); if ($doc) { $annotations = Annotations::parseDocBlock($doc); if (isset($annotations['canonical'])) { $canonical = $annotations['canonical']; } } // If it can't be determined from the DocBlock, try to guess it if (!isset($canonical)) { $className = $reflection->getDeclaringClass()->getShortName(); if ($className == 'Index') { $className = ''; } $methodName = ltrim($reflection->getName(), 'route'); if ($methodName == 'Default') { $methodName = ''; } $canonical = str_replace('_', '-', strtolower(($className ? '/' . $className : '') . ($methodName ? '/' . $methodName : '/'))); } // Replace in parameters $canonical = $this->context->getUrl()->rootUri . Url::replaceIntoTemplate($canonical, $params); return $canonical; }