compile = $compile; $this->load($this->compile->template()); } /** * load file or string * @param string $file * * @return mixed */ public function load($value) { if(is_file($value)) { $this->dom = HtmlDomParser::file_get_html($value); } else { $this->dom = HtmlDomParser::str_get_html($value); } $this->filter(); $this->import(); } /** * make * @param array $param * * @return mixed */ public function make($param) { $this->current($param[0]); $this->data = $param[1]; if($this->current) $this->expression = $this->current->innertext; if(isset($param[2]) && is_array($param[2])) { $this->child($param[2]); } elseif(isset($param[2]) && $param[2] == 'none' && $this->current && is_string($this->data)) { $value = $this->data; if(strpos($this->data, 'Maze::') === false) { $value = '<{Maze::load("'.$this->data.'")}>'; } $value = str_replace('<{', '<{if(!', $value); $value = str_replace('}>', '):}>', $value); $this->current->style = $this->compile->content($value . 'display:none;<{endif;}>'); } $this->handle(); } /** * get * * @return string */ public function get() { return $this->dom->save(); } /** * child * @param array $child * * @return array */ public function child($child) { $this->attr = 'outertext'; if($this->current && $child) { $this->expression = ''; foreach($child as $k => $v) { if($k == 'self') { $this->attribute($v, $this->current); } elseif($k == 'parent') { $parent = $this->current->parent(); if(isset($v['number']) && $v['number'] >= 2) { for($i = 2; $i <= $v['number']; $i++) { $parent = $parent->parent(); } } $this->attribute($v, $parent); } else { if(strpos($k, '|') !== false) { list($k, $index) = explode('|', $k); } else { $index = 0; if(isset($v['key'])) { $index = $v['key'];unset($v['key']); } } $this->attribute($v, $this->current->find($k, $index)); } } } } /** * filter * * @return mixed */ private function filter() { $dom = $this->dom->find('filter'); foreach($dom as $k => $v) { $dom[$k]->outertext = ''; } } /** * import * * @return mixed */ private function import() { $dom = $this->dom->find('.include'); foreach($dom as $k => $v) { if(isset($v->path)) $v->file = $v->path . $v->file; $v->outertext = $this->compile->load($v->file, $v->system); } } /** * attribute * @param array|string $value * @param object $dom * @param array $data * * @return mixed */ private function attribute($value, $dom, $data = false) { if(is_array($value)) { foreach($value as $k => $v) { if($k == 'html') { $data = $v; } else { $index = 0; if(strpos($k, '|') !== false) { list($k, $index) = explode('|', $k); } $this->plugin($dom, $k, $v, $index); } } if($this->temp) $this->temp = null; } else { $data = $value; } //if($data) $dom->outertext = $this->compile->handle($data, $dom->outertext); if($data) $dom->innertext = $this->compile->content($data); } /** * plugin * @param object $dom * @param string $attribute * @param string $value * @param int $index * * @return mixed */ private function plugin($dom, $attribute, $value, $index = 0) { if(is_array($value)) { $key = '{data}'; $child = $dom->find($attribute, $index); if(!$child) { if(!$this->temp) { # 这里因为dom类对append支持的不好,所以只能重新读取一次 if($index > 0) { # 当有一个节点时,后续的所有节点均使用第一个节点为模板 $this->temp = HtmlDomParser::str_get_html($dom->innertext); } else { # 清空原有父节点的全部内容 $dom->innertext = ''; # 当没有节点时,直接创建 $this->temp = HtmlDomParser::str_get_html("<$attribute>"); } } $child = $this->temp->find($attribute, 0); } /* foreach($value as $k => $v) { if($k != $key) { $this->plugin($child, $k, $v); } } */ $this->attribute($value, $child); if(isset($value[$key])) { $dom->innertext = $this->compile->logic($value[$key], $child->outertext); } if($this->temp) { $dom->innertext .= $this->temp->innertext; } return; } if($attribute == 'html') { $attribute = 'innertext'; } # modal elseif($attribute == 'modal') { $dom->{'data-am-modal'} = '{target: \'#maze_modal\', closeViaDimmer: 0}'; $dom->{'href'} = '#maze_modal'; $dom->{'data-toggle'} = 'modal'; $attribute = 'onclick'; if(strpos($value, '|') !== false) { $temp = explode('|', $value); $dom->{'data-modal-title'} = $temp[0]; $dom->{'data-modal-content'} = $temp[1]; } else { $dom->{'data-modal-title'} = '提醒您'; $dom->{'data-modal-content'} = $value; } $value = '$(\'#maze_modal_title\').html($(this).attr(\'data-modal-title\'));$(\'#maze_modal_body\').html($(this).attr(\'data-modal-content\'))'; } $value = $this->compile->content($value, $this->data); if(strpos($attribute, '++') !== false) { $attribute = str_replace('++', '', $attribute); if(!strstr($dom->$attribute, $value)) { $dom->$attribute = $dom->$attribute . $value; } } elseif(strpos($attribute, '--') !== false) { $attribute = str_replace('--', '', $attribute); if(strpos($dom->$attribute, $value) !== false) { $dom->$attribute = str_replace($value, '', $dom->$attribute); } } else { $dom->$attribute = $value; } } /** * parse * @param string $parse * * @return mixed */ private function parse(& $parse) { if(strpos($parse, '@') !== false) { $temp = explode('@', $parse); $parse = $temp[0]; $array = array ( 'html' => 'innertext', ); $this->attr = isset($array[$temp[1]]) ? $array[$temp[1]] : $temp[1]; } } /** * current * @param array $parse * * @return mixed */ private function current($parse) { //$this->attr = 'outertext'; $this->attr = 'innertext'; if(is_array($parse)) { $this->parse($parse[0]); $this->current = $this->dom->find($parse[0], $parse[1]); } else { $this->parse($parse); $dom = $this->dom->find($parse); if($dom) { foreach($dom as $k => $v) { if($k == 0) { $this->current = $v; } else { $dom[$k]->outertext = ''; } } } else { //Output::abert('dom_exists', $parse); Debug::log(Lang::get('dom_exists', $parse)); } } if(!$this->current) { //Output::abert('dom_exists', $parse); Debug::log(Lang::get('dom_exists', $parse)); } return $this; } /** * handle * * @return array */ private function handle() { if($this->current) { //echo $this->current->outertext;die; $this->current->{$this->attr} = $this->compile->handle($this->data, $this->current->{$this->attr}, $this->expression); } } }