/** * Set Up */ public function setUp() { // Defining the Request URI & Request Method to satisfy Unit Testing. These variables are // not used during the build process in a unit test like they would be on a production application, // so their values are trivial. $_SERVER['REQUEST_URI'] = 'satisfyUnitTest'; $_SERVER['REQUEST_METHOD'] = 'GET'; // Ob Start if ($this->bCleanOutput) { ob_start(); } // Get Rust instance $this->oInstance = Rust::getRouter(); // Define Rust configuration $this->oInstance->config(array('dev' => true, 'unit_test' => true, 'build_all' => true)); // Clean routes & param casts after every unit test $this->oInstance->cleanRoutes(); $this->oInstance->cleanCastParams(); // Reset $aTestCase var after every unit test self::$aTestCase = array(); }
/** * Build Route. * * @param $sRoute * @param $mRouteFunctionOrResponse * @return bool */ public function route($sRoute, $mRouteFunctionOrResponse) { self::$sRoute = trim($sRoute); // If this route is being defined within a Namespace, then // prepend the route with that Namespace. if (self::$sNamespace !== null) { self::$sRoute = self::$sNamespace . '/' . $this->trimSlashes(self::$sRoute); } /* | Case 1: Route is the root Route (that is, a forward slash '/') | Case 2: Route is not the root. */ if (self::$sRoute === '/') { self::$aRoute = array(); self::$aRoute[] = '/'; } else { self::$sRoute = $this->trimSlashes(self::$sRoute); self::$aRoute = explode('/', self::$sRoute); } // Define Parent Route as first index of aRoute. self::$sParentRoute = self::$aRoute[0]; /* | Case 1: Parent URI matches the Parent Route, or is an "Otherwise" or "All" route, | so it is considered to be a Relevant Route. | Case 2: Case 1 is false, so the route is not considered to be a Relevant Route. */ if ($this->getUriParent() === self::$sParentRoute || self::$sParentRoute === ':otherwise' || self::$sParentRoute === ':all') { $bRelevantRoute = true; } else { $bRelevantRoute = false; } /* | Case 1: We are in DevMode, so build all routes in existence. | Case 2: We aren't in DevMode and the Route is considered to be | relevant, so build that Route. Routes deemed as not | relevant will not be built to save memory and process | time. */ if ($this->bBuildAll || !$this->bBuildAll && $bRelevantRoute) { // Parent Route has not been added to aRoutes, so add it. if (!array_key_exists(self::$sParentRoute, $this->aRoutes)) { $this->aRoutes[self::$sParentRoute] = array(); } // The route contains more than one argument, therefore is considered // a Child Route. Add it to the childRoutes array. if (count(self::$aRoute) > 1) { $aChildRoute = array(); foreach (self::$aRoute as $iPosition => $sArgument) { $aChildRoute['args'][$iPosition] = array(); $aChildRoute['args'][$iPosition]['arg'] = $sArgument; /* | Case 1: Argument contains a colon, so we consider the | argument to be dynamic. | Case 2: Argument doesn't contain a colon, so we consider | it to be static. */ if (strpos($sArgument, ':') !== false) { $aChildRoute['args'][$iPosition]['type'] = 'dynamic'; } else { $aChildRoute['args'][$iPosition]['type'] = 'static'; } } // Insert aChildRoute array into the childRoute Array on // an Index equal to the full Route name. $this->aRoutes[self::$sParentRoute]['childRoutes'][self::$sRoute] = $aChildRoute; } // Set bRouteScope to true so __call knows it's being fired // within the Route Scope. self::$bRouteScope = true; /* | Case 1: $mRouteFunctionOrResponse is an instance of Closure. | Case 2: $mRouteFunctionOrResponse is a string, so build a | response method using the string. | Case 3: $mRouteFunctionOrResponse is not of an acceptable type, | so pass operations to throwError. */ if ($mRouteFunctionOrResponse instanceof \Closure) { $mRouteFunctionOrResponse = $mRouteFunctionOrResponse->bindTo(self::$oInstance); $mRouteFunctionOrResponse(); } elseif (gettype($mRouteFunctionOrResponse) === 'string') { self::$oInstance->response($mRouteFunctionOrResponse); } else { $this->throwError($this->getError(2)); } // Reset bRouteScope to false so any subsequent calls to __call // outside the Route Scope will not be rendered. self::$bRouteScope = false; return true; } else { return false; } }