Ejemplo n.º 1
0
 public function loadFrom($file)
 {
     $extension = pathinfo($file, PATHINFO_EXTENSION);
     switch ($extension) {
         case "yaml":
         case "yml":
             $filename = basename($file);
             if (!empty($this->cacheDir) && $this->isCached($filename)) {
                 // try load from cache
                 $data = $this->getCached();
             } else {
                 if (!is_object($this->yaml)) {
                     throw new \Exception("Yaml Parser library is not loaded");
                 }
                 $data = $this->yaml->load($file);
                 if (!empty($this->cacheDir)) {
                     $this->saveInCache($file, $data);
                 }
             }
             $routesList = $data["routes"];
             if (isset($data["mapping"])) {
                 $this->mapping = $data["mapping"];
             }
             foreach ($routesList as $routeData) {
                 // validate route data
                 if (!isset($routeData["pattern"])) {
                     throw new \Exception("Invalid route definition, param: \"pattern\" is required.");
                 }
                 if (!is_string($routeData["pattern"])) {
                     throw new \Exception("Invalid route definition, param: \"pattern\" must be a string.");
                 }
                 if (isset($routeData["defaults"]) && !is_array($routeData["defaults"])) {
                     throw new \Exception("Invalid route definition, param: \"defaults\" must be an array.");
                 }
                 if (isset($routeData["requirements"]) && !is_array($routeData["requirements"])) {
                     throw new \Exception("Invalid route definition, param: \"requirements\" must be an array.");
                 }
                 $route = new Route();
                 $route->setPattern($routeData["pattern"]);
                 if (array_key_exists("defaults", $routeData)) {
                     $route->setDefaults($routeData["defaults"]);
                 }
                 if (array_key_exists("requirements", $routeData)) {
                     $route->setRequirements($routeData["requirements"]);
                 }
                 if (array_key_exists("mapping", $routeData)) {
                     $route->setMapping($routeData["mapping"]);
                 } elseif (!empty($this->mapping)) {
                     $route->setMapping($this->mapping);
                 }
                 $this->connect($routeData["pattern"], $route);
             }
             break;
     }
 }
Ejemplo n.º 2
0
 public function testSetters()
 {
     $route = new Route();
     $route->setPattern('/my_controller/{action}/{year}');
     $route->setDefaults(array('controller' => 'my_controller'));
     $route->setRequirements(array('year' => '\\d+'));
     $expected = array('controller' => 'my_controller', 'action' => 'my_action', 'year' => 2012);
     $this->assertEquals($expected, $route->match('/my_controller/my_action/2012'));
 }