/qeephp/library/web/view_render_php.php
PHP | 478 lines | 221 code | 47 blank | 210 comment | 15 complexity | b7b37590981f0fa871f714d3c506870e MD5 | raw file
Possible License(s): BSD-3-Clause
- <?php
- // $Id: view_render_php.php 2445 2009-04-27 08:12:19Z dualface $
- /**
- * ?? QView_Render_PHP ?
- *
- * @link http://qeephp.com/
- * @copyright Copyright (c) 2006-2009 Qeeyuan Inc. {@link http://www.qeeyuan.com}
- * @license New BSD License {@link http://qeephp.com/license/}
- * @version $Id: view_render_php.php 2445 2009-04-27 08:12:19Z dualface $
- * @package mvc
- */
- /**
- * QView_Render_PHP ???????????
- *
- * @author YuLei Liao <liaoyulei@qeeyuan.com>
- * @version $Id: view_render_php.php 2445 2009-04-27 08:12:19Z dualface $
- * @package mvc
- */
- class QView_Render_PHP
- {
- /**
- * ????????
- *
- * @var string
- */
- public $view_dir;
- /**
- * ???????
- *
- * @var array
- */
- public $headers;
- /**
- * ????????
- *
- * @var string
- */
- public $file_extname = 'php';
- /**
- * ????
- *
- * @var array
- */
- protected $_vars;
- /**
- * ??
- *
- * @var string
- */
- protected $_viewname;
- /**
- * ????????
- *
- * @var string
- */
- protected $_view_layouts;
- /**
- * ????????
- *
- * @var QView_Render_PHP_Parser
- */
- protected $_parser;
- /**
- * ????
- *
- * @param array $config
- */
- function __construct(array $config = null)
- {
- if (is_array($config))
- {
- foreach ($config as $key => $value)
- {
- $this->{$key} = $value;
- }
- }
- $this->cleanVars();
- }
- /**
- * ??????
- *
- * @param string $viewname
- *
- * @return QView_Render_PHP
- */
- function setViewname($viewname)
- {
- $this->_viewname = $viewname;
- return $this;
- }
- /**
- * ??????
- *
- * @param string|array $key
- * @param mixed $data
- *
- * @return QView_Render_PHP
- */
- function assign($key, $data = null)
- {
- if (is_array($key))
- {
- $this->_vars = array_merge($this->_vars, $key);
- }
- else
- {
- $this->_vars[$key] = $data;
- }
- return $this;
- }
- /**
- * ????????
- *
- * @return QView_Render_PHP
- */
- function cleanVars()
- {
- $context = QContext::instance();
- $this->_vars = array(
- '_ctx' => $context,
- '_BASE_DIR' => $context->baseDir(),
- '_BASE_URI' => $context->baseUri(),
- '_REQUEST_URI' => $context->requestUri(),
- );
- return $this;
- }
- /**
- * ????
- *
- * @param string $viewname
- * @param array $vars
- * @param array $config
- */
- function display($viewname = null, array $vars = null, array $config = null)
- {
- if (empty($viewname))
- {
- $viewname = $this->_viewname;
- }
- if (Q::ini('runtime_response_header'))
- {
- header('Content-Type: text/html; charset=' . Q::ini('i18n_response_charset'));
- }
- echo $this->fetch($viewname, $vars, $config);
- }
- /**
- * ??
- */
- function execute()
- {
- $this->display($this->_viewname);
- }
- /**
- * ???????????
- *
- * @param string $viewname
- * @param array $vars
- * @param array $config
- *
- * @return string
- */
- function fetch($viewname, array $vars = null, array $config = null)
- {
- $this->_before_render();
- $view_dir = isset($config['view_dir']) ? $config['view_dir'] : $this->view_dir;
- $extname = isset($config['file_extname']) ? $config['file_extname'] : $this->file_extname;
- $filename = "{$view_dir}/{$viewname}.{$extname}";
- if (file_exists($filename))
- {
- if (!is_array($vars))
- {
- $vars = $this->_vars;
- }
- if (is_null($this->_parser))
- {
- $this->_parser = new QView_Render_PHP_Parser($view_dir);
- }
-
- $output = $this->_parser->assign($vars)->parse($filename);
- }
- else
- {
- $output = '';
- }
- $this->_after_render($output);
- return $output;
- }
- /**
- * ??????
- *
- * ???????????
- */
- protected function _before_render()
- {
- }
- /**
- * ??????
- *
- * ???????????
- *
- * @param string $output
- */
- protected function _after_render(& $output)
- {
- }
- }
- /**
- * QView_Render_PHP_Parser ?????????
- *
- * @author YuLei Liao <liaoyulei@qeeyuan.com>
- * @version $Id: view_render_php.php 2445 2009-04-27 08:12:19Z dualface $
- * @package mvc
- */
- class QView_Render_PHP_Parser
- {
- /**
- * ???????
- *
- * @var string
- */
- private $_extname;
- /**
- * ????
- *
- * @var array
- */
- private $_stacks = array();
- /**
- * ???????
- *
- * @var int
- */
- private $_current;
- /**
- * ????
- *
- * @var array
- */
- private $_vars;
- /**
- * ????????
- *
- * @var string
- */
- private $_view_dir;
- /**
- * ????
- */
- function __construct($view_dir)
- {
- $this->_view_dir = $view_dir;
- }
- /**
- * ????????????
- *
- * @param array $vars
- *
- * @return QView_Render_PHP_Parser
- */
- function assign(array $vars)
- {
- $this->_vars = $vars;
- return $this;
- }
- /**
- * ????????????????
- *
- * @return string
- */
- function extname()
- {
- return $this->_extname;
- }
- /**
- * ?????????????
- *
- * @param string $filename
- * @param string $view_id
- * @param array $inherited_stack
- *
- * @return string
- */
- function parse($filename, $view_id = null, array $inherited_stack = null)
- {
- if (!$view_id) $view_id = mt_rand();
- $stack = array(
- 'id' => $view_id,
- 'contents' => '',
- 'extends' => '',
- 'blocks_stacks' => array(),
- 'blocks' => array(),
- 'blocks_config' => array(),
- 'nested_blocks' => array(),
- );
- array_push($this->_stacks, $stack);
- $this->_current = count($this->_stacks) - 1;
- unset($stack);
- ob_start();
- $this->_include($filename);
- $stack = $this->_stacks[$this->_current];
- $stack['contents'] = ob_get_clean(); //????????extends)??????contents?????????????contents??????
- // ???????????????????????????????
- if (is_array($inherited_stack))
- {
- foreach ($inherited_stack['blocks'] as $block_name => $contents)
- {
- if (isset($stack['blocks_config'][$block_name]))
- {
- switch (strtolower($stack['blocks_config'][$block_name]))
- {
- case 'append':
- $stack['blocks'][$block_name] .= $contents;
- break;
- case 'replace':
- default:
- $stack['blocks'][$block_name] = $contents;
- }
- }
- else
- {
- $stack['blocks'][$block_name] = $contents;
- }
- }
- }
- // ????? block??????
- while (list($child, $parent) = array_pop($stack['nested_blocks']))
- {
- $stack['blocks'][$parent] = str_replace("%block_contents_placeholder_{$child}_{$view_id}%",
- $stack['blocks'][$child], $stack['blocks'][$parent]);
- unset($stack['blocks'][$child]);
- }
- // ????????????
- $this->_stacks[$this->_current] = $stack;
- if ($stack['extends'])
- {
- // ????????????????????????
- $filename = "{$this->_view_dir}/{$stack['extends']}.{$this->_extname}";
- return $this->parse($filename, $view_id, $this->_stacks[$this->_current]);
- }
- else
- {
- // ??????????? extends ?
- $last = array_pop($this->_stacks);
- foreach ($last['blocks'] as $block_name => $contents)
- {
- $last['contents'] = str_replace("%block_contents_placeholder_{$block_name}_{$last['id']}%",
- $contents, $last['contents']);
- }
- $this->_stacks = array();
- return $last['contents'];
- }
- }
- /**
- * ?????
- *
- * @param string $tplname
- *
- * @access public
- */
- protected function _extends($tplname)
- {
- $this->_stacks[$this->_current]['extends'] = $tplname;
- }
- /**
- * ????????
- *
- * @param string $block_name
- * @param mixed $config
- *
- * @access public
- */
- protected function _block($block_name, $config = null)
- {
- $stack =& $this->_stacks[$this->_current];
- if (!empty($stack['blocks_stacks']))
- {
- // ??????? block????????????
- $last = $stack['blocks_stacks'][count($stack['blocks_stacks']) - 1];
- $stack['nested_blocks'][] = array($block_name, $last);
- }
- $this->_stacks[$this->_current]['blocks_config'][$block_name] = $config;
- array_push($stack['blocks_stacks'], $block_name);
- ob_start();
- }
- /**
- * ??????
- *
- * @access public
- */
- protected function _endblock()
- {
- $block_name = array_pop($this->_stacks[$this->_current]['blocks_stacks']);
- $this->_stacks[$this->_current]['blocks'][$block_name] = ob_get_clean();
- echo "%block_contents_placeholder_{$block_name}_{$this->_stacks[$this->_current]['id']}%";
- }
- /**
- * ??????
- *
- * @param string $control_type
- * @param string $id
- * @param array $args
- *
- * @access public
- */
- protected function _control($control_type, $id = null, array $args = array())
- {
- Q::control($control_type, $id, $args)->display($this);
- }
- /**
- * ????????
- *
- * @param string $element_name
- * @param array $vars
- *
- * @access public
- */
- protected function _element($element_name, array $vars = null)
- {
- $filename = "{$this->_view_dir}/_elements/{$element_name}_element.{$this->_extname}";
- $this->_include($filename, $vars);
- }
- /**
- * ??????
- */
- private function _include($___filename, array $___vars = null)
- {
- $this->_extname = pathinfo($___filename, PATHINFO_EXTENSION);
- extract($this->_vars);
- if (is_array($___vars)) extract($___vars);
- include $___filename;
- }
- }