* send->mySentence = "Hello world"; * echo $t->process( "calc_sentence_length.ezt" ); * * $number = $t->receive->length; * ?> * * * The template code: * * {use $mySentence = ""} * * {var $length = str_len( $mySentence )} * {return $length} * * * @package Template * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved. * @license http://ez.no/licenses/new_bsd New BSD License * @version 1.0beta1 */ class ezcTemplate { /** * An array containing the properties of this object: * * @var array(string=>mixed) */ private $properties = array( 'configuration' => null, 'send' => null, 'receive' => null, 'compiledTemplatePath' => null, 'tstTree' => false, 'astTree' => false, 'output' => "", ); /** * Returns the value of the property $name. * * The properties that can be retrieved are: * * - ezcTemplateVariableCollection send : Contains the variables that are send to the template. * - ezcTemplateVariableCollection receive : Contains the variables that are returned by the template. * - ezcTemplateConfiguration configuration : Contains the template configuration. * - string output : The output of the processed template. * - string compiledTemplatePath : The path of the comiled template. * - tstTree : The generated tstTree (debug). * - astTree : The generated astTree (debug). * * @throws ezcBasePropertyNotFoundException if the property does not exist. * @param string $name * @return mixed */ public function __get( $name ) { switch( $name ) { case 'send': case 'receive': case 'tstTree': case 'astTree': case 'compiledTemplatePath': case 'output': return $this->properties[$name]; case 'configuration': if ( $this->properties[$name] === null ) { $this->properties[$name] = ezcTemplateConfiguration::getInstance(); if ( get_class( $this->properties[$name] ) != 'ezcTemplateConfiguration' ) { throw new Exception( "Static method ezcTemplateConfiguration::getInstance() did not return an object of class ezcTemplateConfiguration" ); //$this->properties[$name] = new ezcTemplateConfiguration(); } } return $this->properties[$name]; default: throw new ezcBasePropertyNotFoundException( $name ); } } /** * Returns true if the property $name is set, otherwise false. */ public function __isset( $name ) { switch( $name ) { case 'configuration': case 'compiledTemplatePath': case 'send': case 'receive': case 'tstTree': case 'astTree': case 'output': return true; default: return false; } } /** * Sets the property $name to $value. * * The properties that can be set are: * * - ezcTemplateVariableCollection send : Contains the variables that are send to the template. * - ezcTemplateConfiguration configuration : Contains the template configuration. * * @throws ezcBasePropertyNotFoundException if the property does not exist. * @param string $name * @param mixed $value * @return void */ public function __set( $name, $value ) { switch( $name ) { case 'send': if( !$value instanceof ezcTemplateVariableCollection ) { throw new ezcBaseValueException( $name, $value, 'ezcTemplateVariableCollection' ); } $this->properties[$name] = $value; break; case 'configuration': if ( $value !== null and !( $value instanceof ezcTemplateConfiguration ) ) { throw new ezcBaseValueException( $name, $value, 'ezcTemplateConfiguration' ); } $this->properties[$name] = $value; break; case 'tstTree': case 'astTree': case 'compiledTemplatePath': case 'output': case 'receive': throw new ezcBasePropertyPermissionException( $name, ezcBasePropertyPermissionException::READ ); default: throw new ezcBasePropertyNotFoundException( $name ); } } /** * Intializes the ezcTemplate with the default settings. */ public function __construct() { $this->properties["send"] = new ezcTemplateVariableCollection(); $this->properties["receive"] = new ezcTemplateVariableCollection(); } /** * Processes the specified template source and returns the output string. * * @note The first time a template is accessed it needs to be compiled so the * execution time will be higher than subsequent calls. * * @return string * * @throw Exception, ezcTemplateSourceToTstParserException, ezcTemplateParserException if the * template couldn't be compiled. */ public function process( $location, ezcTemplateConfiguration $config = null ) { if( $config === null ) { $config = $this->configuration; } $this->properties["tstTree"] = false; $this->properties["astTree"] = false; $stream = false; $stream = $location; if( $stream[0] != "/" ) // Is it a relative path? { $stream = $config->templatePath ."/". $stream; } $source = new ezcTemplateSourceCode( $stream, $stream ); // lookup compiled code here $compiled = ezcTemplateCompiledCode::findCompiled( $source->stream, $config->context, $this ); $this->properties["compiledTemplatePath"] = $compiled->path; if( !file_exists( $compiled->path ) || ( $config->checkModifiedTemplates && filemtime($source->stream) >= filemtime($compiled->path) ) ) { $this->createDirectory( dirname( $compiled->path ) ); // get the compiled path. // use parser here $source->load(); $parser = new ezcTemplateParser( $source, $this ); $this->properties["tstTree"] = $parser->parseIntoNodeTree(); $tstToAst = new ezcTemplateTstToAstTransformer( $parser ); $this->properties["tstTree"]->accept( $tstToAst ); $this->properties["astTree"] = $tstToAst->programNode; $astToAst = new ezcTemplateAstToAstContextAppender( $config->context ); $tstToAst->programNode->accept( $astToAst ); // Extra optimization. $astToAst = new ezcTemplateAstToAstAssignmentOptimizer(); $tstToAst->programNode->accept( $astToAst ); $g = new ezcTemplateAstToPhpGenerator( $compiled->path ); // Write to the file. $tstToAst->programNode->accept($g); } // execute compiled code here $this->properties["output"] = $compiled->execute(); return $this->properties["output"]; } private function createDirectory( $path ) { if( !is_dir( $path ) ) { return mkdir( $path, 0700, true ); } return true; } /** * Generates a unique hash from the current options. */ public function generateOptionHash() { return base_convert( crc32( 'ezcTemplate::options(' . false /*(bool)$this->outputDebugEnabled*/ . '-' . false /*(bool)$this->compiledDebugEnabled*/ . ')' ), 10, 36 ); } // /** // * Locates the source template file named $source and returns an // * ezcTemplateSource object which can be queried. // * // * @param string $source The source name of the template source to find. // * @return ezcTemplateSource // */ // public function findSource( $source ) // { // $location = ezcTemplateResourceLocator::parseLocationString( $source ); // if ( $location->locator ) // { // if ( !isset( $this->resourceLocators[$location->locator] ) ) // throw new ezcTemplateLocatorNotFound( $location ); // $locator = $this->resourceLocators[$location->locator]; // } // else // { // $locator = $this->defaultLocator; // // create the default if it does not exist // if ( $locator === null ) // $locator = new ezcTemplateDirectResourceLocator(); // } // // return $locator->findSource( $location->stream ); // } } ?>