/** * Creates a new controller object and sets all the request variables as class variables. * * @throws ezcMvcControllerException if the action method is empty * @param string $action * @param ezcMvcRequest $request */ public function __construct($action, ezcMvcRequest $request) { if (ezcBase::inDevMode() && (!is_string($action) || strlen($action) == 0)) { throw new ezcMvcControllerException("The '" . get_class($this) . "' controller requires an action."); } $this->action = $action; $this->setRequestVariables($request); }
/** * Returns routing information, including a controller classname from the set of routes. * * This method is run by the dispatcher to obtain a controller. It uses the * user implemented createRoutes() method from the inherited class to fetch the * routes. It then loops over these routes in order - the first one that * matches the request returns the routing information. The loop stops as * soon as a route has matched. In case none of the routes matched * with the request data an exception is thrown. * * @throws ezcMvcNoRoutesException when there are no routes defined. * @throws ezcBaseValueException when one of the returned routes was not * actually an object implementing the ezcMvcRoute interface. * @throws ezcMvcRouteNotFoundException when no routes matched the request URI. * @return ezcMvcRoutingInformation */ public function getRoutingInformation() { $routes = $this->createRoutes(); if (ezcBase::inDevMode() && (!is_array($routes) || !count($routes))) { throw new ezcMvcNoRoutesException(); } foreach ($routes as $route) { if (ezcBase::inDevMode() && !$route instanceof ezcMvcRoute) { throw new ezcBaseValueException('route', $route, 'instance of ezcMvcRoute'); } $routingInformation = $route->matches($this->request); if ($routingInformation !== null) { return $routingInformation; } } throw new ezcMvcRouteNotFoundException($this->request); }
/** * Returns routing information, including a controller classname from the set of routes. * * This method is run by the dispatcher to obtain a controller. It uses the * user implemented createRoutes() method from the inherited class to fetch the * routes. It then loops over these routes in order - the first one that * matches the request returns the routing information. The loop stops as * soon as a route has matched. In case none of the routes matched * with the request data an exception is thrown. * * @throws ezcMvcNoRoutesException when there are no routes defined. * @throws ezcBaseValueException when one of the returned routes was not * actually an object implementing the ezcMvcRoute interface. * @throws ezcMvcRouteNotFoundException when no routes matched the request URI. * @return ezcMvcRoutingInformation */ public function getRoutingInformation() { $routes = $this->createRoutes(); if (ezcBase::inDevMode() && (!is_array($routes) || !count($routes))) { throw new ezcMvcNoRoutesException(); } foreach ($routes as $route) { if (ezcBase::inDevMode() && !$route instanceof ezcMvcRoute) { throw new ezcBaseValueException('route', $route, 'instance of ezcMvcRoute'); } $routingInformation = $route->matches($this->request); if ($routingInformation !== null) { // Add the router to the routing information struct, so that // can be passed to the controllers for reversed route // generation. $routingInformation->router = $this; return $routingInformation; } } throw new ezcMvcRouteNotFoundException($this->request); }
public function testSetGetRunMode() { self::assertEquals(ezcBase::MODE_DEVELOPMENT, ezcBase::getRunMode()); self::assertEquals(true, ezcBase::inDevMode()); ezcBase::setRunMode(ezcBase::MODE_PRODUCTION); self::assertEquals(ezcBase::MODE_PRODUCTION, ezcBase::getRunMode()); self::assertEquals(false, ezcBase::inDevMode()); ezcBase::setRunMode(ezcBase::MODE_DEVELOPMENT); self::assertEquals(ezcBase::MODE_DEVELOPMENT, ezcBase::getRunMode()); self::assertEquals(true, ezcBase::inDevMode()); }
function runResponseFilters(ezcMvcRoutingInformation $routeInfo, ezcMvcRequest $request, ezcMvcResult $result, ezcMvcResponse $response) { if (!ezcBase::inDevMode()) { if (in_array('gzip', $request->accept->encodings)) { $filter = new ezcMvcGzipResponseFilter(); $filter->filterResponse($response); } else { if (in_array('deflate', $request->accept->encodings)) { $filter = new ezcMvcGzDeflateResponseFilter(); $filter->filterResponse($response); } } } }
/** * Uses the configuration to fetch the response writer * * @throws ezcMvcInvalidConfiguration when the returned object is of the wrong class * * @param ezcMvcRoutingInformation $routingInformation * @param ezcMvcRequest $request * @param ezcMvcResult $result * @param ezcMvcResponse $response * @return ezcMvcResponseWriter */ protected function getResponseWriter(ezcMvcRoutingInformation $routingInformation, ezcMvcRequest $request, ezcMvcResult $result, ezcMvcResponse $response) { $responseWriter = $this->configuration->createResponseWriter($routingInformation, $request, $result, $response); if (ezcBase::inDevMode() && !$responseWriter instanceof ezcMvcResponseWriter) { throw new ezcMvcInvalidConfiguration('responseWriter', $responseWriter, 'instance of ezcMvcResponseWriter'); } return $responseWriter; }
/** * Creates a controller from the set of routes. * * This method is run by the createResponse() method to obtain a rendered * result from data from the controller. It uses the user implemented * createZones() method from the inherited class to fetch the different * zones of the view, and then loops over these zones in order. Each zone's * results are made available to subsequent zones. Each zone will be * processed by a view handler of the ezcMvcViewHandler class. * * @throws ezcMvcNoZonesException when there are no zones defined. * @throws ezcBaseValueException when one of the returned zones was not * actually an object implementing the ezcMvcViewHandler interface. * @return mixed */ protected function createResponseBody() { $processed = array(); $zones = $this->createZones(true); if (ezcBase::inDevMode() && (!is_array($zones) || !count($zones))) { throw new ezcMvcNoZonesException(); } // get the last zone $lastZone = array_pop($zones); array_push($zones, $lastZone); foreach ($zones as $zone) { if (ezcBase::inDevMode() && !$zone instanceof ezcMvcViewHandler) { throw new ezcBaseValueException('zone', $zone, 'instance of ezcMvcViewHandler'); } // Get the variables returned by the controller for the view foreach ($this->result->variables as $propertyName => $propertyValue) { // Send it verbatim to the template processor $zone->send($propertyName, $propertyValue); } // Zones are additional templates that the final view should be built // with. The main page layout is the last zone returned from // createZones() method. foreach ($processed as $processedZone) { $zone->send($processedZone->getName(), $processedZone->getResult()); } $zone->process($zone === $lastZone); $processed[] = $zone; } return $zone->getResult(); }