Esempio 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;
     }
 }
Esempio n. 2
0
 public function testDumpWithQuotes()
 {
     $yaml = new Yaml();
     $yaml->settingDumpForceQuotes = true;
     foreach ($this->files_to_test as $file) {
         $cont = $yaml->load($file);
         $dump = $yaml->dump($cont);
         $yaml_after_dump = $yaml->loadString($dump);
         $this->assertEquals($cont, $yaml_after_dump);
     }
 }
Esempio n. 3
0
 public function parse()
 {
     $yaml = new Yaml();
     $data = (array) $yaml->load($this->filepath);
     //echo "<pre>"; print_r($data); die;
     if (!is_array($data)) {
         throw new \Exception("Invalid UI definition");
     }
     $keys = array_keys($data);
     if (count($keys) !== 1) {
         throw new \Exception("Invalid UI definition, zero or more at one ui component was defined.");
     }
     $this->attributes['type'] = $keys[0];
     $data = $data[$keys[0]];
     //var_dump($data); //die;
     if (isset($data['attributes']) && is_array($data['attributes'])) {
         $this->attributes = array_merge($this->attributes, $data['attributes']);
     } else {
         foreach ($data as $key => $value) {
             $key = self::toCamelCase($key);
             if (!is_numeric($key) && is_string($key)) {
                 $this->attributes[$key] = $value;
             }
             if (is_array($this->attributes[$key]) && !empty($this->attributes[$key][0]) && is_array($this->attributes[$key][0])) {
                 $skeys = array_keys($this->attributes[$key][0]);
                 if (!empty($skeys) && is_array($this->attributes[$key][0][$skeys[0]])) {
                     foreach ($this->attributes[$key] as $i => $sitem) {
                         if (!empty($sitem)) {
                             $sxtype = array_keys($sitem);
                             $this->attributes[$key][$i] = $sitem[$sxtype[0]];
                             $this->attributes[$key][$i]["xtype"] = $sxtype[0];
                         }
                     }
                 }
             }
         }
     }
     $elementClass = 'Alchemy\\Component\\UI\\Element\\' . ucfirst($keys[0]);
     if (!class_exists($elementClass)) {
         throw new \RuntimeException(sprintf("Runtime Error: Undefined UI Element Class '%s'.", ucfirst($elementClass)));
     }
     $this->element = new $elementClass($this->attributes);
 }
Esempio n. 4
0
function roundTrip($a)
{
    $yaml = new Yaml();
    return $yaml->loadString($yaml->dump(array('x' => $a)));
}