📄 savant2.php
字号:
// find the template source. $file = $this->findFile('template', $tpl); if (! $file) { return $this->error( SAVANT2_ERROR_NOTEMPLATE, array('template' => $tpl) ); } // are we compiling source into a script? if (is_object($this->_compiler)) { // compile the template source and get the path to the // compiled script (will be returned instead of the // source path) $result = $this->_compiler->compile($file); } else { // no compiling requested, return the source path $result = $file; } // is there a script from the compiler? if (! $result || $this->isError($result)) { if ($setScript) { $this->_script = null; } // return an error, along with any error info // generated by the compiler. return $this->error( SAVANT2_ERROR_NOSCRIPT, array( 'template' => $tpl, 'compiler' => $result ) ); } else { if ($setScript) { $this->_script = $result; } return $result; } } /** * * This is a an alias to loadTemplate() that cannot set the script. * * @access public * * @param string $tpl The template source name to look for. * * @return string The full path to the compiled template script. * * @throws object An error object with a SAVANT2_ERROR_NOTEMPLATE code. * */ function findTemplate($tpl = null) { return $this->loadTemplate($tpl, false); } /** * * Executes a template script and returns the results as a string. * * @param string $_tpl The name of the template source file ... * automatically searches the template paths and compiles as needed. * * @return string The output of the the template script. * * @throws object An error object with a SAVANT2_ERROR_NOSCRIPT code. * */ function fetch($_tpl = null) { // clear prior output $this->_output = null; // load the template script $_result = $this->loadTemplate($_tpl, true); // is there a template script to be processed? if ($this->isError($_result)) { return $_result; } // unset so as not to introduce into template scope unset($_tpl); unset($_result); // never allow a 'this' property if (isset($this->this)) { unset($this->this); } // are we extracting variables into local scope? if ($this->_extract) { // extract references to this object's public properties. // this allows variables assigned by-reference to refer all // the way back to the model logic. variables assigned // by-copy only refer back to the property. foreach (array_keys(get_object_vars($this)) as $_prop) { if (substr($_prop, 0, 1) != '_') { // set a variable-variable to an object property // reference $$_prop =& $this->$_prop; } } // unset private loop vars unset($_prop); } // start capturing output into a buffer ob_start(); // include the requested template filename in the local scope // (this will execute the view logic). include $this->_script; // done with the requested template; get the buffer and // clear it. $this->_output = ob_get_contents(); ob_end_clean(); // done! return $this->applyFilters(); } /** * * Execute and display a template script. * * @param string $tpl The name of the template file to parse; * automatically searches through the template paths. * * @return void * * @throws object An error object with a SAVANT2_ERROR_NOSCRIPT code. * * @see fetch() * */ function display($tpl = null) { $result = $this->fetch($tpl); if ($this->isError($result)) { return $result; } else { echo $result; } } // ----------------------------------------------------------------- // // Plugins // // ----------------------------------------------------------------- /** * * Loads a plugin class and instantiates it within Savant2. * * @access public * * @param string $name The plugin name (not including Savant2_Plugin_ * prefix). * * @param array $conf An associative array of plugin configuration * options. * * @param bool $savantRef Default false. When true, sets the $Savant * property of the filter to a reference to this Savant object. * * @return void * * @throws object An error object with a SAVANT2_ERROR_NOPLUGIN code. * */ function loadPlugin($name, $conf = array(), $savantRef = null) { // if no $savantRef is provided, use the default. if (is_null($savantRef)) { $savantRef = $this->_reference; } // some basic information $class = "Savant2_Plugin_$name"; $file = "$class.php"; // is it loaded? if (! $this->_classExists($class)) { $result = $this->findFile('resource', $file); if (! $result) { return $this->error( SAVANT2_ERROR_NOPLUGIN, array('plugin' => $name) ); } else { include_once $result; } } // is it instantiated? if (! isset($this->_resource['plugin'][$name]) || ! is_object($this->_resource['plugin'][$name]) || ! is_a($this->_resource['plugin'][$name], $class)) { // instantiate it $this->_resource['plugin'][$name] =& new $class($conf); // add a Savant reference if requested if ($savantRef) { $this->_resource['plugin'][$name]->Savant =& $this; } } } /** * * Unloads one or more plugins from Savant2. * * @access public * * @param string|array $name The plugin name (not including Savant2_Plugin_ * prefix). If null, unloads all plugins; if a string, unloads that one * plugin; if an array, unloads all plugins named as values in the array. * * @return void * */ function unloadPlugin($name = null) { if (is_null($name)) { $this->_resource['plugin'] = array(); } else { settype($name, 'array'); foreach ($name as $key) { if (isset($this->_resource['plugin'][$key])) { unset($this->_resource['plugin'][$key]); } } } } /** * * Executes a plugin with arbitrary parameters and returns the * result. * * @access public * * @param string $name The plugin name (not including Savant2_Plugin_ * prefix). * * @return mixed The plugin results. * * @throws object An error object with a SAVANT2_ERROR_NOPLUGIN code. * * @see loadPlugin() * */ function splugin($name) { // attempt to load the plugin $result = $this->loadPlugin($name); if ($this->isError($result)) { return $result; } // call the plugin's "plugin()" method with arguments, // dropping the first argument (the plugin name) $args = func_get_args(); array_shift($args); return call_user_func_array( array(&$this->_resource['plugin'][$name], 'plugin'), $args ); } /** * * Executes a plugin with arbitrary parameters and displays the * result. * * @access public * * @param string $name The plugin name (not including Savant2_Plugin_ * prefix). * * @return void * * @throws object An error object with a SAVANT2_ERROR_NOPLUGIN code. * */ function plugin($name) { $args = func_get_args(); $result = call_user_func_array( array(&$this, 'splugin'), $args ); if ($this->isError($result)) { return $result; } else { echo $result; } } /** * * PHP5 ONLY: Magic method alias to plugin(). * * E.g., instead of $this->plugin('form', ...) you would use * $this->form(...). You can set this to use any other Savant2 method * by issuing, for example, setCall('splugin') to use splugin() ... which * is really the only other sensible choice. * * @access public * * @param string $func The plugin name. * * @param array $args Arguments passed to the plugin. * * @return void * * @throws object An error object with a SAVANT2_ERROR_NOPLUGIN code. * */ function __call($func, $args) { // add the plugin name to the args array_unshift($args, $func); // call the plugin() method return call_user_func_array( array(&$this, $this->_call), $args ); } // ----------------------------------------------------------------- // // Filters // // ----------------------------------------------------------------- /** * * Loads a filter class and instantiates it within Savant2. * * @access public * * @param string $name The filter name (not including Savant2_Filter_ * prefix). * * @param array $conf An associative array of filter configuration * options. * * @param bool $savantRef Default false. When true, sets the $Savant * property of the filter to a reference to this Savant object. * * @return void * * @throws object An error object with a SAVANT2_ERROR_NOFILTER code. * */ function loadFilter($name, $conf = array(), $savantRef = null) { // if no $savantRef is provided, use the default. if (is_null($savantRef)) { $savantRef = $this->_reference; } // some basic information $class = "Savant2_Filter_$name"; $file = "$class.php"; // is it loaded? if (! $this->_classExists($class)) { $result = $this->findFile('resource', $file); if (! $result) { return $this->error( SAVANT2_ERROR_NOFILTER, array('filter' => $name) ); } else { include_once $result; } } // is it instantiated? if (! isset($this->_resource['filter'][$name]) || ! is_object($this->_resource['filter'][$name]) || ! is_a($this->_resource['filter'][$name], $class)) { // instantiate it $this->_resource['filter'][$name] =& new $class($conf); // add a Savant reference if requested if ($savantRef) { $this->_resource['filter'][$name]->Savant =& $this; } } } /** * * Unloads one or more filters from Savant2. * * @access public * * @param string|array $name The filter name (not including Savant2_Filter_ * prefix). If null, unloads all filters; if a string, unloads that one * filter; if an array, unloads all filters named as values in the array. * * @return void * */ function unloadFilter($name = null) { if (is_null($name)) { $this->_resource['filter'] = array(); } else { settype($name, 'array'); foreach ($name as $key) { if (isset($this->_resource['filter'][$key])) { unset($this->_resource['filter'][$key]); } } } } /** * * Apply all loaded filters, in order, to text. * * @access public * * @param string $text The text to which filters should be applied. * If null, sets the text to $this->_output. * * @return string The text after being passed through all loded * filters. * */ function applyFilters($text = null) { // set to output text if no text specified if (is_null($text)) { $text = $this->_output; } // get the list of filter names... $filter = array_keys($this->_resource['filter']); // ... and apply them each in turn. foreach ($filter as $name) { $this->_resource['filter'][$name]->filter($text); } // done return $text; } // ----------------------------------------------------------------- // // Error handling // // ----------------------------------------------------------------- /** * * Returns an error object. * * @access public * * @param int $code A SAVANT2_ERROR_* constant. * * @param array $info An array of error-specific information. * * @return object An error object of the type specified by * $this->_error. * */ function &error($code, $info = array()) { // the error config array $conf = array( 'code' => $code, 'text' => 'Savant2: ', 'info' => (array) $info ); // set an error message from the globals if (isset($GLOBALS['_SAVANT2']['error'][$code])) { $conf['text'] .= $GLOBALS['_SAVANT2']['error'][$code]; } else { $conf['text'] .= '???'; } // set up the error class name if ($this->_error) { $class = 'Savant2_Error_' . $this->_error; } else { $class = 'Savant2_Error'; } // set up the error class file name $file = $class . '.php'; // is it loaded? if (! $this->_classExists($class)) { // find the error class $result = $this->findFile('resource', $file); if (! $result) { // could not find the custom error class, revert to // Savant_Error base class. $class = 'Savant2_Error'; $result = dirname(__FILE__) . '/Savant2/Error.php'; } // include the error class include_once $result; } // instantiate and return the error class $err =& new $class($conf); return $err; } /** * * Tests if an object is of the Savant2_Error class. * * @access public * * @param object &$obj The object to be tested. * * @return boolean True if $obj is an error object of the type * Savant2_Error, or is a subclass that Savant2_Error. False if not. * */ function isError(&$obj) { if (is_object($obj)) { if (is_a($obj, 'Savant2_Error') || is_subclass_of($obj, 'Savant2_Error')) { return true; } } return false; }}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -