* // accessing templates in /usr/share and compile them in /var/cache * $conf->templatePath = "/usr/share/eztemplate"; * $conf->compilePath = "/var/cache/eztemplate"; * * * Accessing templates from the applications directory is done with a single dot * (.), these are also the default values. * * // uses current directory for accessing templates and compiling them * $conf->templatePath = "."; * $conf->compilePath = "."; * * * The $context property is by default assigned to an ezcTemplateXhtmlContext object. * * @package Template * @version 1.1beta1 */ class ezcTemplateConfiguration { /** * List of global instances, looked up using the identifier string. */ static private $instanceList = array(); /** * An array containing the properties of this object. * * templatePath: The base path for all the source templates. e.g. 'design' or 'templates'. * compilepath : The base path for all the compiled templates. e.g. 'var/template/compiled'. * * @var array(string=>mixed) */ private $properties = array( 'context' => false, 'templatePath' => ".", 'compilePath' => ".", 'cachePath' => null, 'cacheSystem' => null, 'checkModifiedTemplates' => true, 'customBlocks' => array(), 'customFunctions' => array(), ); /** * Returns the value of the property $name. * * The properties that can be retrieved are: * * - ezcTemplateOutputCollection context : Contains the template context. * - string templatePath : Base path where the source templates are stored. * - string compilePath : Base path where the compiled templates are stored. * - bool checkModifiedTemplates : Set to true, to recompile outdated compiled templates. * * @throws ezcBasePropertyNotFoundException if the property does not exist. * @return mixed */ public function __get( $name ) { switch ( $name ) { case 'context': case 'templatePath': case 'compilePath': case 'cachePath': case 'cacheSystem': case 'checkModifiedTemplates': return $this->properties[$name]; case 'customBlocks': case 'customFunctions': return (array) $this->properties[$name]; default: throw new ezcBasePropertyNotFoundException( $name ); } } /** * Sets the property $name to the value $value * * The properties that can be set are: * * - ezcTemplateOutputCollection context : Contains the template context. * - string templatePath : Base path where the source templates are stored. * - string compilePath : Base path where the compiled templates are stored. * - bool checkModifiedTemplates : Set to true, to recompile outdated compiled templates. * * @throws ezcBasePropertyNotFoundException if the property does not exist. * @return mixed */ public function __set( $name, $value ) { switch ( $name ) { case 'context': if ( !$value instanceof ezcTemplateOutputContext ) { throw new ezcBaseValueException( $name, $value, 'ezcTemplateContext' ); } $this->properties[$name] = $value; break; case 'templatePath': case 'compilePath': case 'cachePath': case 'cacheSystem': case 'checkModifiedTemplates': case 'customBlocks': case 'customFunctions': $this->properties[$name] = $value; break; default: throw new ezcBasePropertyNotFoundException( $name ); } } /** * Returns true if the property $name is set, otherwise false. * * @return bool */ public function __isset( $name ) { switch ( $name ) { case 'context': return true; case 'templatePath': case 'compilePath': case 'cachePath': case 'cacheSystem': case 'checkModifiedTemplates': return isset( $this->properties[$name] ); default: return false; } } /** * Initialises the configuration with default template, compiled path, and context. * * All requested templates are search from the defined $templatePath. * Use an empty string to fetch templates from the root of the filesystem. * * All compiled templates are placed in subfolders under the compiled templates. * Use an empty string to compile templates at the root of the filesystem. */ public function __construct( $templatePath = ".", $compilePath = ".", $context = null ) { $this->properties["templatePath"] = $templatePath; $this->properties["compilePath"] = $compilePath; $this->properties["cachePath"] = "." . DIRECTORY_SEPARATOR . "cache"; $this->properties["cacheSystem"] = new ezcTemplateCacheFilesystem( $this ); $this->properties['context'] = ( $context == null ? new ezcTemplateXhtmlContext() : $context ); } /** * Returns the unique configuration instance named $name. * * @return ezcTemplateConfiguration */ public static function getInstance( $name = "default" ) { if ( !isset( self::$instanceList[$name] ) ) { self::$instanceList[$name] = new ezcTemplateConfiguration(); } return self::$instanceList[$name]; } /** * Adds custom tags or function to the customBlock or customFunction property and * indirectly add the custom extension to the template language. * * The parameter $customBlockClass expects a class that implements either * the interface ezcTemplateCustomBlock, ezcTemplateCustomFunction, or both. * * @param string $customClass * @return void */ public function addExtension( $customClass ) { if( !is_string( $customClass ) ) { throw new ezcTemplateCustomBlockException( "Could not add the extension $customClass, because the given value is not a string." ); } $implements = class_implements( $customClass ); $added = false; if( in_array( "ezcTemplateCustomBlock", $implements ) ) { $this->properties["customBlocks"][$customClass] = $customClass; $added = true; } if( in_array( "ezcTemplateCustomFunction", $implements ) ) { $this->properties["customFunctions"][$customClass] = $customClass; $added = true; } if( !$added) { throw new ezcTemplateCustomBlockException( "Could not add the extension $customClass. Does it implement ezcTemplateCustomBlock or ezcTemplateCustomFunction?" ); } } } ?>