123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470 |
- <?php namespace Maze\Template\Parse;
- use Maze\Template\Parse;
- use Maze\Template\Compile;
- use Maze\Http\Output;
- use Sunra\PhpSimple\HtmlDomParser;
- use Maze\Debug\Process as Debug;
- use Maze\Config\Lang;
- class Dom implements Parse
- {
- /**
- * current
- *
- * @var object
- */
- protected $current;
- /**
- * data
- *
- * @var string
- */
- protected $data;
- /**
- * expression
- *
- * @var string
- */
- protected $expression;
- /**
- * attr
- *
- * @var string
- */
- protected $attr = 'outertext';
- /**
- * compile
- *
- * @var \Maze\Template\Compile
- */
- protected $compile;
- /**
- * dom
- *
- * @var \Sunra\PhpSimple\HtmlDomParser
- */
- protected $dom;
- /**
- * temp dom
- *
- * @var \Sunra\PhpSimple\HtmlDomParser
- */
- protected $temp;
- /**
- * __construct
- *
- * @return mixed
- */
- public function __construct(Compile $compile)
- {
- $this->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></$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);
- }
- }
- }
|