コード例 #1
0
ファイル: Router.php プロジェクト: shaggy8871/frame
 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();
                 }
             }
         }
     }
 }
コード例 #2
0
ファイル: Foundation.php プロジェクト: shaggy8871/frame
 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;
 }