abstract.php

来自「Bug tracker, and reporter.」· PHP 代码 · 共 1,043 行 · 第 1/2 页

PHP
1,043
字号
<?php/** * Zend Framework * * LICENSE * * This source file is subject to the new BSD license that is bundled * with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: * http://framework.zend.com/license/new-bsd * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to license@zend.com so we can send you a copy immediately. * * @category   Zend * @package    Zend_View * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com) * @license    http://framework.zend.com/license/new-bsd     New BSD License *//** Zend_Loader */require_once 'Zend/Loader.php';/** Zend_View_Interface */require_once 'Zend/View/Interface.php';/** * Abstract class for Zend_View to help enforce private constructs. * * @category   Zend * @package    Zend_View * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com) * @license    http://framework.zend.com/license/new-bsd     New BSD License */abstract class Zend_View_Abstract implements Zend_View_Interface{    /**     * Path stack for script, helper, and filter directories.     *     * @var array     */    private $_path = array(        'script' => array(),        'helper' => array(),        'filter' => array(),    );    /**     * Script file name to execute     *     * @var string     */    private $_file = null;    /**     * Instances of helper objects.     *     * @var array     */    private $_helper = array();    /**     * Map of helper => class pairs to help in determining helper class from     * name     * @var array     */    private $_helperLoaded = array();    /**     * Map of helper => classfile pairs to aid in determining helper classfile     * @var array     */    private $_helperLoadedDir = array();    /**     * Stack of Zend_View_Filter names to apply as filters.     * @var array     */    private $_filter = array();    /**     * Stack of Zend_View_Filter objects that have been loaded     * @var array     */    private $_filterClass = array();    /**     * Map of filter => class pairs to help in determining filter class from     * name     * @var array     */    private $_filterLoaded = array();    /**     * Map of filter => classfile pairs to aid in determining filter classfile     * @var array     */    private $_filterLoadedDir = array();    /**     * Callback for escaping.     *     * @var string     */    private $_escape = 'htmlspecialchars';    /**     * Encoding to use in escaping mechanisms; defaults to latin1 (ISO-8859-1)     * @var string     */    private $_encoding = 'ISO-8859-1';    /**     * Strict variables flag; when on, undefined variables accessed in the view     * scripts will trigger notices     * @var boolean     */    private $_strictVars = false;    /**     * Constructor.     *     * @param array $config Configuration key-value pairs.     */    public function __construct($config = array())    {        // set inital paths and properties        $this->setScriptPath(null);        $this->setHelperPath(null);        $this->setFilterPath(null);        // user-defined escaping callback        if (array_key_exists('escape', $config)) {            $this->setEscape($config['escape']);        }        // encoding        if (array_key_exists('encoding', $config)) {            $this->setEncoding($config['encoding']);        }        // base path        if (array_key_exists('basePath', $config)) {            $prefix = 'Zend_View';            if (array_key_exists('basePathPrefix', $config)) {                $prefix = $config['basePathPrefix'];            }            $this->setBasePath($config['basePath'], $prefix);        }        // user-defined view script path        if (array_key_exists('scriptPath', $config)) {            $this->addScriptPath($config['scriptPath']);        }        // user-defined helper path        if (array_key_exists('helperPath', $config)) {            $prefix = 'Zend_View_Helper';            if (array_key_exists('helperPathPrefix', $config)) {                $prefix = $config['helperPathPrefix'];            }            $this->addHelperPath($config['helperPath'], $prefix);        }        // user-defined filter path        if (array_key_exists('filterPath', $config)) {            $prefix = 'Zend_View_Filter';            if (array_key_exists('filterPathPrefix', $config)) {                $prefix = $config['filterPathPrefix'];            }            $this->addFilterPath($config['filterPath'], $prefix);        }        // user-defined filters        if (array_key_exists('filter', $config)) {            $this->addFilter($config['filter']);        }        // strict vars        if (array_key_exists('strictVars', $config)) {            $this->strictVars($config['strictVars']);        }        $this->init();    }    /**     * Return the template engine object     *     * Returns the object instance, as it is its own template engine     *     * @return Zend_View_Abstract     */    public function getEngine()    {        return $this;    }    /**     * Allow custom object initialization when extending Zend_View_Abstract or     * Zend_View     *     * Triggered by {@link __construct() the constructor} as its final action.     *     * @return void     */    public function init()    {    }    /**     * Prevent E_NOTICE for nonexistent values     *     * If {@link strictVars()} is on, raises a notice.     *     * @param  string $key     * @return null     */    public function __get($key)    {        if ($this->_strictVars) {            trigger_error('Key "' . $key . '" does not exist', E_USER_NOTICE);        }        return null;    }    /**     * Allows testing with empty() and isset() to work inside     * templates.     *     * @param  string $key     * @return boolean     */    public function __isset($key)    {        if ('_' != substr($key, 0, 1)) {            return isset($this->$key);        }        return false;    }    /**     * Directly assigns a variable to the view script.     *     * Checks first to ensure that the caller is not attempting to set a     * protected or private member (by checking for a prefixed underscore); if     * not, the public member is set; otherwise, an exception is raised.     *     * @param string $key The variable name.     * @param mixed $val The variable value.     * @return void     * @throws Zend_View_Exception if an attempt to set a private or protected     * member is detected     */    public function __set($key, $val)    {        if ('_' != substr($key, 0, 1)) {            $this->$key = $val;            return;        }        require_once 'Zend/View/Exception.php';        throw new Zend_View_Exception('Setting private or protected class members is not allowed', $this);    }    /**     * Allows unset() on object properties to work     *     * @param string $key     * @return void     */    public function __unset($key)    {        if ('_' != substr($key, 0, 1) && isset($this->$key)) {            unset($this->$key);        }    }    /**     * Accesses a helper object from within a script.     *     * If the helper class has a 'view' property, sets it with the current view     * object.     *     * @param string $name The helper name.     * @param array $args The parameters for the helper.     * @return string The result of the helper output.     */    public function __call($name, $args)    {        // is the helper already loaded?        $helper = $this->getHelper($name);        // call the helper method        return call_user_func_array(            array($helper, $name),            $args        );    }    /**     * Given a base path, sets the script, helper, and filter paths relative to it     *     * Assumes a directory structure of:     * <code>     * basePath/     *     scripts/     *     helpers/     *     filters/     * </code>     *     * @param  string $path     * @param  string $prefix Prefix to use for helper and filter paths     * @return Zend_View_Abstract     */    public function setBasePath($path, $classPrefix = 'Zend_View')    {        $path        = rtrim($path, '/');        $path        = rtrim($path, '\\');        $path       .= DIRECTORY_SEPARATOR;        $classPrefix = rtrim($classPrefix, '_') . '_';        $this->setScriptPath($path . 'scripts');        $this->setHelperPath($path . 'helpers', $classPrefix . 'Helper');        $this->setFilterPath($path . 'filters', $classPrefix . 'Filter');        return $this;    }    /**     * Given a base path, add script, helper, and filter paths relative to it     *     * Assumes a directory structure of:     * <code>     * basePath/     *     scripts/     *     helpers/     *     filters/     * </code>     *     * @param  string $path     * @param  string $prefix Prefix to use for helper and filter paths     * @return Zend_View_Abstract     */    public function addBasePath($path, $classPrefix = 'Zend_View')    {        $path        = rtrim($path, '/');        $path        = rtrim($path, '\\');        $path       .= DIRECTORY_SEPARATOR;        $classPrefix = rtrim($classPrefix, '_') . '_';        $this->addScriptPath($path . 'scripts');        $this->addHelperPath($path . 'helpers', $classPrefix . 'Helper');        $this->addFilterPath($path . 'filters', $classPrefix . 'Filter');        return $this;    }    /**     * Adds to the stack of view script paths in LIFO order.     *     * @param string|array The directory (-ies) to add.     * @return Zend_View_Abstract     */    public function addScriptPath($path)    {        $this->_addPath('script', $path);        return $this;    }    /**     * Resets the stack of view script paths.     *     * To clear all paths, use Zend_View::setScriptPath(null).     *     * @param string|array The directory (-ies) to set as the path.     * @return Zend_View_Abstract     */    public function setScriptPath($path)    {        $this->_path['script'] = array();        $this->_addPath('script', $path);        return $this;    }    /**     * Return full path to a view script specified by $name     *     * @param  string $name     * @return false|string False if script not found     * @throws Zend_View_Exception if no script directory set     */    public function getScriptPath($name)    {        try {            $path = $this->_script($name);            return $path;        } catch (Zend_View_Exception $e) {            if (strstr($e->getMessage(), 'no view script directory set')) {                throw $e;            }            return false;        }    }    /**     * Returns an array of all currently set script paths     *     * @return array     */    public function getScriptPaths()    {        return $this->_getPaths('script');    }    /**     * Adds to the stack of helper paths in LIFO order.     *     * @param string|array The directory (-ies) to add.     * @param string $classPrefix Class prefix to use with classes in this     * directory; defaults to Zend_View_Helper     * @return Zend_View_Abstract     */    public function addHelperPath($path, $classPrefix = 'Zend_View_Helper_')    {        if (!empty($classPrefix) && ('_' != substr($classPrefix, -1))) {            $classPrefix .= '_';        }        $this->_addPath('helper', $path, $classPrefix);        return $this;    }    /**     * Resets the stack of helper paths.     *     * To clear all paths, use Zend_View::setHelperPath(null).     *     * @param string|array $path The directory (-ies) to set as the path.     * @param string $classPrefix The class prefix to apply to all elements in     * $path; defaults to Zend_View_Helper     * @return Zend_View_Abstract     */    public function setHelperPath($path, $classPrefix = 'Zend_View_Helper_')    {        if ($classPrefix == null) {            throw new Zend_View_Exception('The classPrefix cannot be empty.');        }                if (!empty($classPrefix) && ('_' != substr($classPrefix, -1))) {            $classPrefix .= '_';        }        $this->_setPath('helper', $path, $classPrefix);        return $this;    }    /**     * Get full path to a helper class file specified by $name     *     * @param  string $name     * @return string|false False on failure, path on success     */    public function getHelperPath($name)    {        if (isset($this->_helperLoadedDir[$name])) {            return $this->_helperLoadedDir[$name];        }        try {            $this->_loadClass('helper', $name);            return $this->_helperLoadedDir[$name];        } catch (Zend_View_Exception $e) {            return false;        }    }    /**     * Returns an array of all currently set helper paths     *     * @return array     */    public function getHelperPaths()    {        return $this->_getPaths('helper');    }    /**     * Get a helper by name     *     * @param  string $name     * @return object     */    public function getHelper($name)    {        $name = ucfirst($name);        if (!isset($this->_helper[$name])) {            $class = $this->_loadClass('helper', $name);            $this->_helper[$name] = new $class();            if (method_exists($this->_helper[$name], 'setView')) {                $this->_helper[$name]->setView($this);            }        }        return $this->_helper[$name];    }    /**     * Get array of all active helpers     *     * Only returns those that have already been instantiated.     *     * @return array     */    public function getHelpers()    {        return $this->_helper;    }    /**     * Adds to the stack of filter paths in LIFO order.     *     * @param string|array The directory (-ies) to add.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?