/core/classes/framework/view/view.php
PHP | 448 lines | 305 code | 53 blank | 90 comment | 27 complexity | 61eb271ea4d02224d9f413d024c8318d MD5 | raw file
- <?php
- class View {
- private $_templates = array();
-
- private $_templateStack = array();
-
- private $_vars = array();
-
- /**
- *
- * Indicates that block contents will be appended to the contents of the
- * block with the same name. By default contents is replaced
- *
- */
- const BLOCK_APPEND = 1;
-
- /**
- *
- * Indicates that block contents will be prepended to the contents of the
- * block with the same name. By default contents is replaced
- *
- */
- const BLOCK_PREPEND = 2;
-
- /**
- *
- * If block is enclosed by another block, then its contents may be or may
- * not be included into the parent block contents. By default blocks are
- * private and their contents is closed. Otherwise you should use
- * BLOCK_NOT_PRIVATE flag
- *
- */
- const BLOCK_NOT_PRIVATE = 4;
-
- private $_blocks = array();
-
- private $_blockKeys = array();
-
- /**
- *
- * Contains list of environments
- *
- * @var array environment filters list
- */
- private $_environments = array();
-
- public static function registerFunction($key, array $callArgs) {
-
- }
-
- public static function callRegistered($key, $params = array()) {
-
- }
- /**
- *
- * Outputs variable specified by key, that was used to set this variable
- *
- * <pre><code>
- * <?php
- * $this->out('foo|abs'); //if foo = -5, outputs '5'
- * ?>
- * </code></pre>
- *
- * @param string $keyVar variable key, specify the list of filters to use before
- * outputting the value after the vbar "|", separate
- * them with the comma
- */
- public function out($keyVar)
- {
- @list($keyVar, $filters) = explode('|', strval($keyVar));
-
- if (isset($this->_vars[$keyVar]))
- {
- return $this->outValue($this->_vars[$keyVar], $filters);
- }
- else
- {
- return null;
- }
- }
-
- /**
- *
- * @param mixed $value
- * @param array $filters
- */
- public function outValue($value, $filterString = "")
- {
- if ($filterString)
- {
- $filterArray = array_values(array_unique(
- array_merge(
- (array)end($this->_environments),
- array_filter(array_map('trim', explode(',', $filterString)))
- )
- ));
- }
- else
- {
- $filterArray = (array)end($this->_environments);
- }
- //find exluding filters
- if (strpos($filterString, '^') !== false){
- $count = count($filterArray);
- $excluding = array();
-
- for($i=0; $i < $count; $i++)
- {
- if ($filterArray[$i][0] == '^')
- $excluding[] = str_replace('^', '', $filterArray[$i]);
- }
- $filterArray = array_diff($filterArray, $excluding);
- }
- echo $this->applyFilters($value, $filterArray);
- }
-
- /**
- *
- * @param mixed $value
- * @param array $filters
- */
- public function applyFilters($value, $filters = array())
- {
- foreach($filters as $filter)
- {
- if (is_string($filter))
- {
- $value = Filters::$filter($value);
- }
- }
-
- return $value;
- }
-
- /**
- *
- * Creates environment for output. Filters will be applied to all
- * output values
- *
- * @param string $filters string of comma-separated filters
- */
- public function openEnv($filters)
- {
- $envOptions = array_map('trim', explode(',', $filters));
-
- array_push($this->_environments, $envOptions);
- }
-
- /**
- *
- * Closes current opened environment
- *
- * @return array set of filters from the closed environment
- */
- public function closeEnv()
- {
- return array_pop($this->_environments);
- }
-
- /**
- *
- * Starts buffering of the block with the defined key-name
- *
- * @param mixed $key
- */
- public function startBlock($key)
- {
- $key = $this->_modifyBlockKey($key);
-
- if (!in_array($key, $this->_blockKeys))
- {
- array_push($this->_blockKeys, $key);
- ob_start();
- }
- else
- {
- trigger_error("This block has been already started", E_USER_WARNING);
- }
- }
-
- /**
- *
- * @param mixed $key
- * @param int $flags BLOCK_APPEND, BLOCK_PREPEND, BLOCK_NOT_PRIVATE flags combination
- *
- */
- public function endBlock($key, $flags = 0)
- {
- $key = $this->_modifyBlockKey($key);
- $keyToClose = array_pop($this->_blockKeys);
-
- if ($keyToClose && ob_get_status())
- {
- if (0 === strcmp($key, $keyToClose))
- {
- if (!($flags & View::BLOCK_NOT_PRIVATE))
- {
- $buffer = ob_get_clean();
- }
- else
- {
- $buffer = ob_get_flush();
- }
-
- if (isset($this->_blocks[$key]))
- {
- if ($flags & View::BLOCK_APPEND)
- {
- $this->_blocks[$key] = $this->_blocks[$key] . $buffer;
- }
- elseif($flags & View::BLOCK_PREPEND)
- {
- $this->_blocks[$key] = $buffer . $this->_blocks[$key];
- }
- else
- {
- $this->_blocks[$key] = $buffer;
- }
- }
- else
- {
- $this->_blocks[$key] = $buffer;
- }
- }
- else
- {
- array_push($this->_blockKeys, $keyToClose);
- trigger_error("Currently opened block is \"$keyToClose\", but you tried to close \"$key\"", E_USER_WARNING);
- }
- }
- else
- {
- trigger_error("There's no currently opened blocks to close", E_USER_WARNING);
- }
- }
-
- /**
- *
- * Returns data associated with the block
- *
- * @param string $key
- * @return string block data or null if no block present
- */
- public function getBlock($key)
- {
- return isset($this->_blocks[$key]) ? $this->_blocks[$key] : null;
- }
-
- /**
- *
- * Outputs block data
- *
- * @param string $key
- */
- public function printBlock($key)
- {
- echo $this->getBlock($key);
- }
-
- private function _modifyBlockKey($key)
- {
- return strtolower(strval($key));
- }
-
- public function __get($name) {
- return isset($this->_vars[$name]) ? $this->_vars[$name] : null;
- }
-
- public function __set($name, $value) {
- $this->_vars[$name] = $value;
- }
-
- /**
- *
- * Registers template for view
- *
- * @param string $filename
- * @param mixed $key
- */
- public function setTemplate($filename, $key = "default")
- {
- if (isset($this->_templates[$key]))
- {
- trigger_error("Template key duplication for the key \"$key\"! Overriding previous value.", E_USER_WARNING);
- }
-
- $this->_templates[$key] = $filename;
- }
-
- /**
- *
- * Registers multiple templates for route
- *
- * @param array $templates
- */
- public function setTemplates(array $templates = array())
- {
- foreach($templates as $tplKey => $tplValue)
- {
- $this->setTemplate($tplValue, $tplKey);
- }
- }
-
- /**
- *
- * Returns the template from template collection
- *
- * @param string $key
- * @return string
- */
- public function getTemplate($key = "default")
- {
- if (!isset($this->_templates[$key]))
- {
- trigger_error("Template with key \"$key\" doesn't exist!", E_USER_WARNING);
-
- return null;
- }
- else
- {
- return $this->_templates[$key];
- }
- }
- /**
- *
- * Render template content
- *
- * @param array 'key' key to define template,
- * 'file' to define template file,
- * 'forceSystemUse' to force using system templates instead of custom,
- * 'packName' to search templates in specified pack,
- * 'componentName' to search templates in specified component
- * @return render response
- */
- public function renderTpl($options = array())
- {
- $templateFile = $this->getTemplateFileName($options);
-
- if ($templateFile)
- {
- if (!in_array($templateFile, $this->_templateStack))
- {
- //eliminates current active environment influense
- $this->openEnv("");
- array_push($this->_templateStack, $templateFile);
- ob_start();
- $obLevel = ob_get_level();
- include $templateFile;
- //clear unclosed buffers
- while ($obLevel !== ob_get_level())
- ob_get_clean();
- //restores environment state
- $this->closeEnv();
- array_pop($this->_templateStack);
- return ob_get_clean();
- }
- else
- {
- array_push($this->_templateStack, $templateFile);
- trigger_error("Template loop detected! Templates: " . var_export($this->_templateStack, true), E_USER_WARNING);
- }
- }
- }
-
- public function getTemplateFileName($options = array())
- {
- $key = "";
- $file = "";
- $forceUseSystem = false;
- extract($options, EXTR_OVERWRITE | EXTR_IF_EXISTS);
-
- $template = "";
-
- if ($key)
- {
- $template = $this->getTemplate($key);
- }
- elseif($file)
- {
- $template = $file;
- }
- if ($template)
- {
- if (!is_readable($template))
- {
- $templates = array(
- Loader::getComponentTplFilePath($template, $options),
- Loader::getPackTplFilePath($template, $options),
- Loader::getSystemTplFilePath($template)
- );
- }
- else
- {
- $templates = array(
- $template
- );
- }
- if ($forceUseSystem)
- {
- $templates = array_reverse($templates);
- }
- $templateFile = "";
-
- foreach($templates as $tmpTemplate)
- {
- if (is_file($tmpTemplate) && is_readable($tmpTemplate))
- {
- $templateFile = $tmpTemplate;
- break;
- }
- }
- if ($templateFile)
- {
- return $templateFile;
- }
- else
- {
- trigger_error("Template file not found!", E_USER_WARNING);
- }
- }
- else
- {
- trigger_error("There's no template!", E_USER_WARNING);
- }
- }
- }