Example #1
0
 public function testGetVariables()
 {
     $context = new Context();
     $this->assertEquals([], $context->getRegisteredVariables());
     $context->registerVariable('test1', 'hello');
     $context->registerVariable('test2', 12.34);
     $this->assertEquals(['test1' => 'hello', 'test2' => 12.34], $context->getRegisteredVariables());
 }
Example #2
0
 /**
  * Dispatches a Route, executing its action.
  *
  * @param Route $route The route to be executed.
  * @param Request $request The request information, if available. Used for mapping route variables.
  * @return mixed Route target function return value, if any.
  */
 public function dispatch(Route $route, Request $request = null)
 {
     $context = $this->context;
     if (empty($this->context) && !empty($request)) {
         // If we have no context, but do have a request, prepare a context to store path variables in.
         // Otherwise routing path variables would be lost for no good reason.
         $context = new Context();
         $context->registerInstance($request);
     }
     if (!empty($context)) {
         // If we have a context, ensure that the route is made available in it.
         $context->registerInstance($route);
         if (!empty($request)) {
             // If we have a request, map the path variables and pass them to the context as primitive types by name.
             // This will allow us to inject info from a route e.g. "/view/$userId" to a $userId variable.
             $pathVariables = VariableUrl::extractUrlVariables($request->getRequestUri(), $route->getPattern());
             foreach ($pathVariables as $name => $value) {
                 $context->registerVariable($name, $value);
             }
         }
     }
     return $route->action($context);
 }