Пример #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;
     }
 }
Пример #2
0
 public function testMappingComplex()
 {
     $route = new Route();
     $route->setPattern("/{_module}/{_controller}/{_action}");
     $route->setMapping(array("_controller" => array("to" => 'Sandbox\\Controller\\{_module}\\{_controller}Controller', "transform" => "camelcase"), "_action" => array("to" => "{_action}Action", "transform" => "camelcase,lcfirst"), "_module" => array("transform" => "camelcase")));
     $expected = array("params" => array('_module' => 'admin', '_controller' => 'user_role', '_action' => 'test_list'), "mapped" => array('_module' => 'Admin', '_controller' => 'Sandbox\\Controller\\Admin\\UserRoleController', '_action' => 'testListAction'));
     $this->assertEquals($expected, $route->match('/admin/user_role/test_list'));
 }