Example #1
0
 /**
  * Structure constructor.
  *
  * @param RouteCollection $routes Collection of routes for routing logic creation
  * @param Generator $generator Code generation
  */
 public function __construct(RouteCollection $routes, Generator $generator)
 {
     $this->generator = $generator;
     // Add root branch object
     $this->logic = new Branch('');
     // Collect all HTTP method that this routes collection has
     $this->httpMethods = array();
     foreach ($routes as $route) {
         if (!isset($this->httpMethods[$route->method])) {
             $this->logic->add($route->method);
             $this->httpMethods[$route->method] = $route->method;
         }
     }
     /** @var Route $route Build routing logic branches */
     $this->routeBuild($routes);
     $this->optimizeBranchesWithRoutes($this->logic);
     // Optimize each top level branch(method branch)
     foreach ($this->httpMethods as $method) {
         foreach ($this->logic->branches[$method]->branches as $branch) {
             $this->optimizeBranches($branch);
         }
     }
     // Sort branches in correct order following routing logic rules
     $this->logic->sort();
 }
Example #2
0
 /**
  * Structure constructor.
  *
  * @param RouteCollection $routes Collection of routes for routing logic creation
  * @param Generator $generator Code generation
  */
 public function __construct(RouteCollection $routes, Generator $generator)
 {
     $this->generator = $generator;
     // Add root branch object
     $this->logic = new Branch("");
     // Collect all HTTP method that this routes collection has
     $this->httpMethods = array();
     foreach ($routes as $route) {
         if (!isset($this->httpMethods[$route->method])) {
             $this->logic->add($route->method);
             $this->httpMethods[$route->method] = $route->method;
         }
     }
     /** @var Route $route Build routing logic branches */
     foreach ($routes as $route) {
         // Set branch pointer to root HTTP method branch
         $currentBranch = $this->logic->find($route->method);
         // We should count "/" route here
         $routeParts = $route->pattern == '/' ? array('/') : array_values(array_filter(explode(Route::DELIMITER, $route->pattern)));
         // Split route pattern into parts by its delimiter
         for ($i = 0, $size = sizeof($routeParts); $i < $size; $i++) {
             $routePart = $routeParts[$i];
             // Try to find matching branch by its part
             $tempBranch = $currentBranch->find($routePart);
             // Define if this is last part so this branch should match route
             $matchedRoute = $i == $size - 1 ? $route : null;
             // We have not found this branch
             if (null === $tempBranch) {
                 // Create new inner branch and store pointer to it
                 $currentBranch = $currentBranch->add($routePart, $matchedRoute);
             } else {
                 // Store pointer to found branch
                 $currentBranch = $tempBranch;
                 // TODO: this should be improved
                 // If we have created this branch before but now we got route for it
                 if (isset($matchedRoute)) {
                     // Store route identifier
                     $currentBranch->identifier = $matchedRoute->identifier;
                     $currentBranch->setCallback($matchedRoute->callback);
                 }
             }
         }
     }
     // Optimize each top level branch(method branch)
     foreach ($this->httpMethods as $method) {
         foreach ($this->logic->branches[$method]->branches as $branch) {
             //$this->optimizeBranches($branch);
         }
     }
     $this->optimizeBranchesWithRoutes($this->logic);
     // Sort branches in correct order following routing logic rules
     $this->logic->sort();
 }