Esempio n. 1
0
 public function compile($id, $config)
 {
     $engine = \lasa\view\Engine::currentEngine();
     $loader = $engine->getLoader();
     $template = [];
     $template[] = '<?php $' . $this->componentId . "= new " . VIEWHOLDER_CLASS . '($' . $this->_holderName . '->getArray("' . $id . '")); ?>';
     foreach ($config as $id => $conf) {
         if (is_string($conf)) {
             $conf = [$conf];
         }
         $component = array_shift($conf);
         $builder = $loader->getBuilder($component);
         if (!$builder) {
             continue;
         }
         if (is_numeric($id)) {
             $id = "_v" . $id;
         }
         $builder->setHolderName($id);
         $res = $builder->compile($engine);
         $res = $this->cleanupCode($id, $res, $conf);
         $template[] = $res;
     }
     $template = implode("", $template);
     $obj = new \lasa\view\component\HTMLViewComponent($template);
     $result = $obj->compile();
     return $result;
 }
 public function _createClosureWithConfig(HTMLViewComponent $viewComponent, $config)
 {
     return function (HTMLView $view) use($viewComponent, $config) {
         if (isset($config["@extends"])) {
             $loader = \lasa\view\Engine::currentEngine()->getLoader();
             $builder = $loader->getBuilder($config["@extends"]);
             if (!$builder || !$builder instanceof self) {
                 continue;
             }
             //先に読み込んで拡張を行う
             $func = $this->_createClosureWithConfig($viewComponent, $builder->conf);
             $func($viewComponent);
         }
         //まずはプロパティの処理を行う
         foreach ($config as $key => $value) {
             if (empty($key)) {
                 continue;
             }
             //制御命令系
             if ($key[0] != "@") {
                 continue;
             }
             //レイアウト変数
             if ($key == "@layout") {
                 if (is_string($value)) {
                     $viewComponent->layout($value);
                 } else {
                     if (is_array($value)) {
                         if (isset($value[0])) {
                             $layout_name = array_shift($value);
                             $viewComponent->layout($layout_name);
                         }
                         $viewComponent->layoutParams($value);
                     }
                 }
             }
         }
         /* アノテーションの処理 */
         foreach ($config as $key => $value) {
             //制御命令系
             if ($key[0] == "@") {
                 continue;
             }
             if ($value instanceof \Closure) {
                 $view->apply($value);
                 continue;
             }
             if (!is_array($value)) {
                 $component = $value;
                 $value = [];
             } else {
                 $component = array_shift($value);
             }
             //View系
             if (in_array($component, ["label", "link", "image", "raw", "json"])) {
                 $funcName = "add" . ucfirst($component);
                 $defValue = "";
                 if (isset($value[0]) && (is_string($value[0]) || is_numeric($value[0]))) {
                     $defValue = \array_shift($value);
                 }
                 $opt = $value;
                 $view->{$funcName}($key, $defValue, $opt);
                 continue;
             }
             //Form系
             if (in_array($component, ["input", "check", "select", "textarea"])) {
                 if ($component == "textarea") {
                     $funcName = "addTextArea";
                 } else {
                     $funcName = "add" . ucfirst($component);
                 }
                 $name = array_shift($value);
                 $opt = $value;
                 $view->{$funcName}($key, $name, "", $opt);
                 continue;
             }
             //特殊(view)
             if ($component == "view") {
                 $viewName = array_shift($value);
                 $opt = $value;
                 $view->addView($key, $viewName, $opt);
                 continue;
             }
             //特殊(list)
             if ($component == "list") {
                 if (isset($value[0]) && is_array($value[0])) {
                     $conf = array_shift($value);
                 } else {
                     $conf = $value;
                 }
                 $view->addList($key, $this->_createClosureWithConfig($view, $conf));
                 continue;
             }
             //特殊(Condition)
             if ($component == "if" || $component == "condition") {
                 $defValue = $value ? array_shift($value) : null;
                 $view->addCondition($key, $defValue);
                 continue;
             }
             //特殊(Form)
             if ($component == "form") {
                 $component = new \lasa\view\component\HTMLFormComponent($value);
                 $defValue = $value ? array_shift($value) : null;
                 if ($defValue) {
                     $component->setDefault($defValue);
                 }
                 $view->addComponent($key, $component);
                 continue;
             }
             //特殊(composer)
             if ($component == "composer") {
                 $component = new \lasa\view\component\HTMLViewComposer($value);
                 $view->addComponent($key, $component);
                 continue;
             }
             throw new \Exception("[" . __CLASS__ . "]unknown component:" . $component);
         }
     };
 }
Esempio n. 3
0
 function getContent($id, $tag, $inner, $attributes)
 {
     if (!$this->view) {
         return $this->getDynamicViewContent($id, $tag, $inner, $attributes);
     }
     $holderName = "view";
     $engine = \lasa\view\Engine::currentEngine();
     $loader = $engine->getLoader();
     $builder = $loader->getBuilder($this->view);
     if (!$builder) {
         // 見つからなかった時はそのままinnerを返す
         return $inner;
     }
     $builder->setHolderName($holderName);
     $result = $builder->compile($engine);
     $tokens = token_get_all($result);
     $php_tag_open = 0;
     foreach ($tokens as $token) {
         if (!is_array($token)) {
             continue;
         }
         $type = $token[0];
         if ($type == T_OPEN_TAG) {
             $php_tag_open++;
             continue;
         }
         if ($type == T_CLOSE_TAG) {
             $php_tag_open--;
             continue;
         }
     }
     $suffix = $php_tag_open > 0 ? "" : "<?php ";
     $prefix = '$_ = $' . $holderName . ";";
     $result = '<?php call_user_func(function($' . $holderName . '){ ' . $prefix . '?>' . $result . $suffix . '}, new ' . VIEWHOLDER_CLASS . '($' . $this->_holderName . '->getArray("' . $id . '")));' . '?>';
     if ($this->_outerEndNewLine) {
         $result .= "\n";
     }
     return $result;
 }