/** * HTTP method routing logic condition generator. * * @param string $method HTPP method name * @param string $conditionFunction PHP Code generator condition function name */ protected function buildRoutesByMethod($method, $conditionFunction = 'defIfCondition') { $this->generator->{$conditionFunction}('$method === "' . $method . '"'); // Perform routing logic generation $this->innerGenerate2($this->logic->find($method)); // Add return found route $this->generator->newLine('return null;'); }
/** * 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 $httpMethods = array(); foreach ($routes as $route) { if (!isset($httpMethods[$route->method])) { $this->logic->add($route->method); $httpMethods[$route->method] = ''; } } /** @var Route $route */ 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->fullPath = $matchedRoute->pattern; } } } } // Sort branches in correct order following routing logic rules $this->logic->sort(); }