Ejemplo n.º 1
0
 /**
  * Routes the HTTP request into a Destination instance.
  * 
  * Greedily routes the request to {@see View::get()} if the
  * start of the request URI path matches the path given at
  * construction.
  * 
  * @see Destination
  * @param HTTPURIInterface $requestHTTPURI
  * @param HTTPURIInterface $baseHTTPURI
  * @return Destination|NULL An instance of Destination if the
  *         route matches the request, NULL otherwise.
  *
  */
 public function route(HTTPURIInterface $requestHTTPURI, HTTPURIInterface $baseHTTPURI)
 {
     $basePath = $baseHTTPURI->getPath();
     $requestPath = $requestHTTPURI->getPathWithoutBase($basePath);
     $guidePathEscaped = $this->escapePCREMetaCharacters($this->path);
     $pattern = "/^{$guidePathEscaped}((assets\\/)?(.+)){0,1}\$/uD";
     if (preg_match_all($pattern, $requestPath, $matches) == FALSE) {
         return;
     }
     $isStaticAssetRequest = empty($matches[2][0]) == FALSE;
     $segments = explode('/', $matches[3][0]);
     $lastSegment = end($segments);
     $segmentCount = count($segments);
     if ($isStaticAssetRequest) {
         if ($this->isValidStaticAssetRequest($lastSegment, $segmentCount) == FALSE) {
             return $this->get404Destination();
         }
         return new Destination(new Reference('Carrot\\Docs\\View'), 'getStaticAsset', array($lastSegment));
     }
     if ($this->isValidDocumentationRequest($lastSegment, $segments) == FALSE) {
         return $this->get404Destination();
     }
     unset($segments[$segmentCount - 1]);
     return new Destination(new Reference('Carrot\\Docs\\View'), 'getDocumentation', array($segments));
 }
Ejemplo n.º 2
0
 /**
  * Routes the HTTP request into a Destination instance.
  * 
  * The pattern is matched using
  * {@see HTTPURIInterface::pathMatches()}, while passing the base
  * path from the given base HTTP URI as an argument. This allows
  * the routing to be done in a host/location agnostic way.
  *
  * The process of routing the request is as follows:
  *
  * <ul>
  *     <li>
  *         Checks if the type of the request matches.
  *     </li>
  *     <li>
  *         Checks the structure of the path, see if it matches
  *         the structure provided by the pattern.
  *     </li>
  *     <li>
  *         Checks individual placeholder rules in the pattern,
  *         while also creating argument replacement array
  *         {@see $argsPlaceholderReplacements}.
  *     </li>
  *     <li>
  *         If everything matches, construct the Destination
  *         instance to be returned.
  *     </li>
  * </ul>
  *
  * @see Destination
  * @param HTTPURIInterface $requestHTTPURI
  * @param HTTPURIInterface $baseHTTPURI
  * @return Destination|NULL An instance of Destination if the
  *         route matches the request, NULL otherwise.
  *
  */
 public function route(HTTPURIInterface $requestHTTPURI, HTTPURIInterface $baseHTTPURI)
 {
     if ($this->doesRequestTypeMatch() == FALSE) {
         return;
     }
     $basePath = $baseHTTPURI->getPath();
     $path = $requestHTTPURI->getPathWithoutBase($basePath);
     if ($this->doesRequestPathMatchesStructureRegex($path) == FALSE) {
         return;
     }
     if ($this->doesIndividualPlaceholderRulesMatch($path) == FALSE) {
         return;
     }
     $args = $this->generateMethodArgs();
     return new Destination($this->config['reference'], $this->config['method'], $args);
 }