summaryrefslogtreecommitdiff
path: root/framework
diff options
context:
space:
mode:
authorxue <>2005-11-20 02:59:17 +0000
committerxue <>2005-11-20 02:59:17 +0000
commite0c69d2e0ca0e3a82ac04ede422af8996c0d7ff9 (patch)
treea3ee4ba8976a72e986b02dda3f65103c091755ba /framework
parent8b157421c027e455a3b52c28f08d328236a7d05f (diff)
Diffstat (limited to 'framework')
-rw-r--r--framework/Exceptions/TErrorHandler.php6
-rw-r--r--framework/Exceptions/TException.php35
-rw-r--r--framework/Exceptions/messages.txt12
-rw-r--r--framework/Web/UI/TTemplate.php546
-rw-r--r--framework/Web/UI/TTemplateManager.php569
-rw-r--r--framework/Web/UI/TThemeManager.php134
6 files changed, 661 insertions, 641 deletions
diff --git a/framework/Exceptions/TErrorHandler.php b/framework/Exceptions/TErrorHandler.php
index d053619f..69166561 100644
--- a/framework/Exceptions/TErrorHandler.php
+++ b/framework/Exceptions/TErrorHandler.php
@@ -236,6 +236,7 @@ class TErrorHandler extends TComponent implements IModule
$errorLine=$exception->getLine();
$beginLine=$errorLine-9>=0?$errorLine-9:0;
$endLine=$errorLine+8<=count($lines)?$errorLine+8:count($lines);
+
$source='';
for($i=$beginLine;$i<$endLine;++$i)
{
@@ -247,6 +248,7 @@ class TErrorHandler extends TComponent implements IModule
else
$source.=highlight_string(sprintf("Line %4d: %s",$i+1,$lines[$i]),true);
}
+
$fields=array(
'%%ErrorType%%',
'%%ErrorMessage%%',
@@ -266,9 +268,9 @@ class TErrorHandler extends TComponent implements IModule
strftime('%Y-%m-%d %H:%m',time())
);
$lang=Prado::getPreferredLanguage();
- $exceptionFile=$this->_templatePath.'/'.self::EXCEPTION_FILE_NAME.'-'.$lang.'.html';
+ $exceptionFile=dirname(__FILE__).'/'.self::EXCEPTION_FILE_NAME.'-'.$lang.'.html';
if(!is_file($exceptionFile))
- $exceptionFile=$this->_templatePath.'/'.self::EXCEPTION_FILE_NAME.'.html';
+ $exceptionFile=dirname(__FILE__).'/'.self::EXCEPTION_FILE_NAME.'.html';
if(($content=@file_get_contents($exceptionFile))===false)
die("Unable to open exception template file '$exceptionFile'.");
echo str_replace($fields,$values,$content);
diff --git a/framework/Exceptions/TException.php b/framework/Exceptions/TException.php
index 84ff1b89..49519f9f 100644
--- a/framework/Exceptions/TException.php
+++ b/framework/Exceptions/TException.php
@@ -16,15 +16,10 @@
* TException is the base class for all PRADO exceptions.
* TException
* TSystemException
- * TNullReferenceException
- * TIndexOutOfRangeException
- * TArithmeticException
- * TInvalidValueException
- * TInvalidTypeException
- * TInvalidFormatException
+ * TInvalidDataValueException
+ * TInvalidDataTypeException
* TInvalidOperationException
* TConfigurationException
- * TSecurityException
* TIOException
* TDBException
* THttpException
@@ -88,51 +83,47 @@ class TApplicationException extends TException
{
}
-class TNullReferenceException extends TSystemException
-{
-}
-
-class TIndexOutOfRangeException extends TSystemException
+class TInvalidOperationException extends TSystemException
{
}
-class TArithmeticException extends TSystemException
+class TInvalidDataTypeException extends TSystemException
{
}
-class TInvalidOperationException extends TSystemException
+class TInvalidDataValueException extends TSystemException
{
}
-class TInvalidDataTypeException extends TSystemException
+class TInvalidDataFormatException extends TSystemException
{
}
-class TInvalidDataValueException extends TSystemException
+class TConfigurationException extends TSystemException
{
}
-class TInvalidDataFormatException extends TSystemException
+class TIOException extends TSystemException
{
}
-class TConfigurationException extends TSystemException
+class TDBException extends TSystemException
{
}
-class TIOException extends TException
+class TSecurityException extends TSystemException
{
}
-class TDBException extends TException
+class TNotSupportedException extends TSystemException
{
}
-class TSecurityException extends TException
+class TTemplateParsingException extends TSystemException
{
}
-class TNotSupportedException extends TException
+class TTemplateRuntimeException extends TSystemException
{
}
diff --git a/framework/Exceptions/messages.txt b/framework/Exceptions/messages.txt
index 17e1e627..8df4dde1 100644
--- a/framework/Exceptions/messages.txt
+++ b/framework/Exceptions/messages.txt
@@ -60,6 +60,18 @@ errorhandler_errortemplatepath_invalid = TErrorHandler.ErrorTemplatePath '%s' is
pageservice_page_unknown = Page '%s' Not Found
+template_templatefile_invalid = Template file '%s' cannot open.
+template_closingtag_unexpected = %s: Unexpected closing tag '%s' is found.
+template_closingtag_expected = %s: Closing tag '%s' is expected.
+template_directive_nonunique = %s: Directive '<%@ ... %>' must appear at most once in a template.
+template_comments_forbidden = %s: Template comments are not allowed within property tags.
+template_component_unknown = Component '%s' configured in template is not defined.
+template_event_invalid = Event '%s.%s' configured in template is not defined.
+template_property_readonly = Property '%s.%s' configured in template is read only.
+template_attribute_unbindable = Attribute '%s.%s' configured in template cannot be bound to an expression.
+template_property_undefined = Property '%s.%s' configured in template is not defined.
+template_property_unbindable = Property '%s.%s' configured in template cannot be bound to an expression. Only properties of controls can be bound.
+template_component_required = '%s' is not a component. Only components can be configured in template.
body_contents_not_allowed = %s: body contents are not allowed.
control_id_not_unique = Control ID '%s' is not unique for control type '%s'.
diff --git a/framework/Web/UI/TTemplate.php b/framework/Web/UI/TTemplate.php
deleted file mode 100644
index 5d361119..00000000
--- a/framework/Web/UI/TTemplate.php
+++ /dev/null
@@ -1,546 +0,0 @@
-<?php
-/**
- * TTemplate class file.
- *
- * @author Qiang Xue <qiang.xue@gmail.com>
- * @link http://www.pradosoft.com/
- * @copyright Copyright &copy; 2005 PradoSoft
- * @license http://www.pradosoft.com/license/
- * @version $Revision: $ $Date: $
- * @package System.Web.UI
- */
-
-/**
- * TTemplate implements PRADO template parsing logic.
- * A TTemplate object represents a parsed PRADO control template.
- * It can instantiate the template as child controls of a specified control.
- * The template format is like HTML, with the following special tags introduced,
- * - component tags: a component tag represents the configuration of a component.
- * The tag name is in the format of com:ComponentType, where ComponentType is the component
- * class name. Component tags must be well-formed. Attributes of the component tag
- * are treated as either property initial values, event handler attachment, or regular
- * tag attributes.
- * - property tags: property tags are used to set large block of attribute values.
- * The property tag name is in the format of prop:AttributeName, where AttributeName
- * can be a property name, an event name or a regular tag attribute name.
- * - directive: directive specifies the property values for the template owner.
- * It is in the format of &lt;% properyt name-value pairs %&gt;
- * - expressions: expressions are shorthand of {@link TExpression} and {@link TStatements}
- * controls. They are in the formate of &lt;= PHP expression &gt; and &lt; PHP statements &gt;
- * - comments: There are two kinds of comments, regular HTML comments and special template comments.
- * The former is in the format of &lt;!-- comments --&gt;, which will be treated as text strings.
- * The latter is in the format of &lt;%* comments %&gt;, which will be stripped out.
- *
- * Tags are not required to be well-formed.
- *
- * @author Qiang Xue <qiang.xue@gmail.com>
- * @version $Revision: $ $Date: $
- * @package System.Web.UI
- * @since 3.0
- */
-class TTemplate extends TComponent implements ITemplate
-{
- /**
- * '<!.*?!>' - template comments
- * '<!--.*?-->' - HTML comments
- * '<\/?com:([\w\.]+)((?:\s*[\w\.]+=\'.*?\'|\s*[\w\.]+=".*?"|\s*[\w\.]+=<%.*?%>)*)\s*\/?>' - component tags
- * '<\/?prop:([\w\.]+)\s*>' - property tags
- * '<%@\s*(\w+)((?:\s*[\w\.]+=\'.*?\'|\s*[\w\.]+=".*?")*)\s*%>' - directives
- * '<%=?(.*?)%>' - expressions
- */
- const REGEX_RULES='/<!.*?!>|<!--.*?-->|<\/?com:([\w\.]+)((?:\s*[\w\.]+=\'.*?\'|\s*[\w\.]+=".*?"|\s*[\w\.]+=<%.*?%>)*)\s*\/?>|<\/?prop:([\w\.]+)\s*>|<%@\s*((?:\s*[\w\.]+=\'.*?\'|\s*[\w\.]+=".*?")*)\s*%>|<%=?(.*?)%>/msS';
-
- /**
- * @var array list of component tags and strings
- */
- private $_tpl=array();
- /**
- * @var array list of directive settings
- */
- private $_directive=array();
- /**
- * @var string context path
- */
- private $_contextPath;
-
- /**
- * Constructor.
- * The template will be parsed after construction.
- * @param string the template string
- */
- public function __construct($template,$contextPath)
- {
- $this->_contextPath=$contextPath;
- $this->parse($template);
- }
-
- /**
- * @return array name-value pairs declared in the directive
- */
- public function getDirective()
- {
- return $this->_directive;
- }
-
- /**
- * Instantiates the template.
- * Content in the template will be instantiated as components and text strings
- * and passed to the specified parent control.
- * @param TControl the parent control
- * @throws TTemplateRuntimeException if an error is encountered during the instantiation.
- */
- public function instantiateIn($tplControl)
- {
- $page=$tplControl->getPage();
- $assetManager=$page->getService()->getAssetManager();
- $controls=array();
- foreach($this->_tpl as $key=>$object)
- {
- if(isset($object[2])) // component
- {
- if(strpos($object[1],'.')===false)
- {
- if(class_exists($object[1],false))
- $component=new $object[1];
- else
- {
- include_once($object[1].Prado::CLASS_FILE_EXT);
- if(class_exists($object[1],false))
- $component=new $object[1];
- else
- throw new TTemplateRuntimeException('template_component_unknown',$object[1]);
- }
- }
- else
- $component=Prado::createComponent($object[1]);
- if($component instanceof TControl)
- {
- $controls[$key]=$component;
- $component->setTemplateControl($tplControl);
- if(isset($object[2]['id']))
- $tplControl->registerObject($object[2]['id'],$component);
- if(isset($object[2]['skinid']))
- {
- $component->setSkinID($object[2]['skinid']);
- unset($object[2]['skinid']);
- }
- $component->applyStyleSheetSkin($page);
- // apply attributes
- foreach($object[2] as $name=>$value)
- {
- if($component->hasEvent($name)) // is an event
- {
- if(is_string($value))
- {
- if(strpos($value,'.')===false)
- $component->attachEventHandler($name,array($component,'TemplateControl.'.$value));
- else
- $component->attachEventHandler($name,array($component,$value));
- }
- else
- throw new TTemplateRuntimeException('template_event_invalid',$name);
- }
- else if(strpos($name,'.')===false) // is simple property or custom attribute
- {
- if($component->hasProperty($name))
- {
- if($component->canSetProperty($name))
- {
- $setter='set'.$name;
- if(is_string($value))
- $component->$setter($value);
- else if($value[0]===0)
- $component->bindProperty($name,$value[1]);
- else if($value[0]===1)
- $component->$setter($component->evaluateExpression($value[1]));
- else // url
- {
- $url=$assetManager->publishFilePath($this->_contextPath.'/'.$value[1]);
- $component->$setter($url);
- }
- }
- else
- throw new TTemplateRuntimeException('property_read_only',get_class($component).'.'.$name);
- }
- else if($component->getAllowCustomAttributes())
- {
- if(is_array($value))
- {
- if($value[0]===1)
- $value=$component->evaluateExpression($value[1]);
- else if($value[0]===2)
- $value=$assetManager->publishFilePath($this->_contextPath.'/'.$value[1]);
- else
- throw new TTemplateRuntimeException('template_attribute_unbindable',$name);
- }
- $component->getAttributes()->add($name,$value);
- }
- else
- throw new TTemplateRuntimeException('property_not_defined',get_class($component).'.'.$name);
- }
- else // complex property
- {
- if(is_string($value))
- $component->setSubProperty($name,$value);
- else if($value[0]===0)
- $component->bindProperty($name,$value[1]);
- else if($value[0]===1)
- $component->setSubProperty($component->evaluateExpression($value[1]));
- else
- {
- $url=$assetManager->publishFilePath($this->_contextPath.'/'.$value[1]);
- $component->$setter($url);
- }
- }
- }
- $parent=isset($controls[$object[0]])?$controls[$object[0]]:$tplControl;
- $component->createdOnTemplate($parent);
- }
- else if($component instanceof TComponent)
- {
- if(isset($object[2]['id']))
- {
- $tplControl->registerObject($object[2]['id'],$component);
- if(!$component->hasProperty('id'))
- unset($object[2]['id']);
- }
- foreach($object[2] as $name=>$value)
- {
- if($component->hasProperty($name))
- {
- if($component->canSetProperty($name))
- {
- $setter='set'.$name;
- if(is_string($value))
- $component->$setter($value);
- else if($value[0]===1)
- $component->$setter($component->evaluateExpression($value[1]));
- else if($value[0]===2)
- {
- $url=$assetManager->publishFilePath($this->_contextPath.'/'.$value[1]);
- $component->$setter($url);
- }
- else
- throw new TTemplateRuntimeException('template_component_property_unbindable',get_class($component),$name);
- }
- else
- throw new TTemplateRuntimeException('property_read_only',get_class($component).'.'.$name);
- }
- else
- throw new TTemplateRuntimeException('property_not_defined',get_class($component).'.'.$name);
- }
- $parent=isset($controls[$object[0]])?$controls[$object[0]]:$tplControl;
- $parent->addParsedObject($component);
- }
- else
- throw new TTemplateRuntimeException('must_be_component',$object[1]);
- }
- else // string
- {
- if(isset($controls[$object[0]]))
- $controls[$object[0]]->addParsedObject($object[1]);
- else
- $tplControl->addParsedObject($object[1]);
- }
- }
- }
-
- /**
- * NOTE, this method is currently not used!!!
- * Processes an attribute set in a component tag.
- * The attribute will be checked to see if it represents a property or an event.
- * If so, the value will be set to the property, or the value will be treated
- * as an event handler and attached to the event.
- * Otherwise, it will be added as regualr attribute if the control allows so.
- * @param TComponent the component represented by the tag
- * @param string attribute name
- * @param string attribute value
- * @throws TTemplateRuntimeException
- */
- public static function applyAttribute($component,$name,$value)
- {
- $target=$component;
- if(strpos($name,'.')===false)
- $property=$name;
- else
- {
- $names=explode('.',$name);
- $property=array_pop($names);
- foreach($names as $p)
- {
- if(($target instanceof TComponent) && $target->canGetProperty($p))
- {
- $getter='get'.$p;
- $target=$target->$getter();
- }
- else
- throw new TTemplateRuntimeException('invalid_subproperty',$name);
- }
- }
- if($target instanceof TControl)
- {
- if($target->hasProperty($property))
- {
- $setter='set'.$property;
- if(is_string($value))
- $target->$setter($value);
- else if($value[0]===0)
- $target->bindProperty($property,$value[1]);
- else
- {
- $target->$setter($target->evaluateExpression($value[1]));
- }
- }
- else if($target->hasEvent($property))
- {
- if(strpos($value,'.')===false)
- $target->attachEventHandler($property,'TemplateControl.'.$value);
- else
- $target->attachEventHandler($property,$value);
- }
- else if($target->getAllowCustomAttributes())
- $target->getAttributes()->add($property,$value);
- else
- throw new TTemplateRuntimeException('property_not_defined',get_class($target).'.'.$property);
- }
- else if($target instanceof TComponent)
- {
- $setter='set'.$property;
- $target->$setter($value);
- }
- else
- throw new TTemplateRuntimeException('must_extend_TComponent',get_class($target));
- }
-
- /**
- * Parses a template string.
- *
- * This template parser recognizes five types of data:
- * regular string, well-formed component tags, well-formed property tags, directives, and expressions.
- *
- * The parsing result is returned as an array. Each array element can be of three types:
- * - a string, 0: container index; 1: string content;
- * - a component tag, 0: container index; 1: component type; 2: attributes (name=>value pairs)
- * If a directive is found in the template, it will be parsed and can be
- * retrieved via {@link getDirective}, which returns an array consisting of
- * name-value pairs in the directive.
- *
- * Note, attribute names are treated as case-insensitive and will be turned into lower cases.
- * Component and directive types are case-sensitive.
- * Container index is the index to the array element that stores the container object.
- * If an object has no container, its container index is -1.
- *
- * @param string the template string
- * @return array the parsed result
- * @throws TTemplateParsingException if a parsing error is encountered
- */
- protected function &parse($input)
- {
- $tpl=&$this->_tpl;
- $n=preg_match_all(self::REGEX_RULES,$input,$matches,PREG_SET_ORDER|PREG_OFFSET_CAPTURE);
- $expectPropEnd=false;
- $textStart=0;
- $stack=array();
- $container=-1;
- $c=0;
- for($i=0;$i<$n;++$i)
- {
- $match=&$matches[$i];
- $str=$match[0][0];
- $matchStart=$match[0][1];
- $matchEnd=$matchStart+strlen($str)-1;
- if(strpos($str,'<com:')===0) // opening component tag
- {
- if($expectPropEnd)
- continue;
- if($matchStart>$textStart)
- $tpl[$c++]=array($container,substr($input,$textStart,$matchStart-$textStart));
- $textStart=$matchEnd+1;
- $type=$match[1][0];
- $attributes=$this->parseAttributes($match[2][0]);
- $tpl[$c++]=array($container,$type,$attributes);
- if($str[strlen($str)-2]!=='/') // open tag
- {
- array_push($stack,$type);
- $container=$c-1;
- }
- }
- else if(strpos($str,'</com:')===0) // closing component tag
- {
- if($expectPropEnd)
- continue;
- if($matchStart>$textStart)
- $tpl[$c++]=array($container,substr($input,$textStart,$matchStart-$textStart));
- $textStart=$matchEnd+1;
- $type=$match[1][0];
-
- if(empty($stack))
- {
- $line=count(explode("\n",substr($input,0,$matchEnd+1)));
- throw new TTemplateParsingException('unexpected_closing_tag',$line,"</com:$type>");
- }
-
- $name=array_pop($stack);
- if($name!==$type)
- {
- if($name[0]==='@')
- $tag='</prop:'.substr($name,1).'>';
- else
- $tag='</com:'.$name.'>';
- $line=count(explode("\n",substr($input,0,$matchEnd+1)));
- throw new TTemplateParsingException('expecting_closing_tag',$line,$tag);
- }
- $container=$tpl[$container][0];
- }
- else if(strpos($str,'<%@')===0) // directive
- {
- if($expectPropEnd)
- continue;
- if($matchStart>$textStart)
- $tpl[$c++]=array($container,substr($input,$textStart,$matchStart-$textStart));
- $textStart=$matchEnd+1;
- if(isset($tpl[0]))
- {
- $line=count(explode("\n",substr($input,0,$matchEnd+1)));
- throw new TTemplateParsingException('nonunique_template_directive',$line);
- }
- $this->_directive=$this->parseAttributes($match[4][0]);
- }
- else if(strpos($str,'<%')===0) // expression
- {
- if($expectPropEnd)
- continue;
- if($matchStart>$textStart)
- $tpl[$c++]=array($container,substr($input,$textStart,$matchStart-$textStart));
- $textStart=$matchEnd+1;
- if($str[2]==='=')
- $tpl[$c++]=array($container,'TExpression',array('Expression'=>$match[5][0]));
- else
- $tpl[$c++]=array($container,'TStatements',array('Statements'=>$match[5][0]));
- }
- else if(strpos($str,'<prop:')===0) // opening property
- {
- $prop=strtolower($match[3][0]);
- array_push($stack,'@'.$prop);
- if(!$expectPropEnd)
- {
- if($matchStart>$textStart)
- $tpl[$c++]=array($container,substr($input,$textStart,$matchStart-$textStart));
- $textStart=$matchEnd+1;
- $expectPropEnd=true;
- }
- }
- else if(strpos($str,'</prop:')===0) // closing property
- {
- $prop=strtolower($match[3][0]);
- if(empty($stack))
- {
- $line=count(explode("\n",substr($input,0,$matchEnd+1)));
- throw new TTemplateParsingException('unexpected_closing_tag',$line,"</prop:$prop>");
- }
- $name=array_pop($stack);
- if($name!=='@'.$prop)
- {
- if($name[0]==='@')
- $tag='</prop:'.substr($name,1).'>';
- else
- $tag='</com:'.$name.'>';
- $line=count(explode("\n",substr($input,0,$matchEnd+1)));
- throw new TTemplateParsingException('expecting_closing_tag',$line,$tag);
- }
- if(($last=count($stack))<1 || $stack[$last-1][0]!=='@')
- {
- if($matchStart>$textStart && $container>=0)
- {
- $value=substr($input,$textStart,$matchStart-$textStart);
- if(preg_match('/^<%.*?%>$/msS',$value))
- {
- if($value[2]==='#') // databind
- $tpl[$container][2][$prop]=array(0,substr($value,3,strlen($value)-5));
- else if($value[2]==='=') // a dynamic initialization
- $tpl[$container][2][$prop]=array(1,substr($value,3,strlen($value)-5));
- else
- $tpl[$container][2][$prop]=$value;
- }
- else
- $tpl[$container][2][$prop]=$value;
- $textStart=$matchEnd+1;
- }
- $expectPropEnd=false;
- }
- }
- else if(strpos($str,'<!--')===0) // HTML comments
- {
- $state=0;
- }
- else if(strpos($str,'<!')===0) // template comments
- {
- if($expectPropEnd)
- {
- $line=count(explode("\n",substr($input,0,$matchEnd+1)));
- throw new TTemplateParsingException('no_comments_in_property',$line);
- }
- if($matchStart>$textStart)
- $tpl[$c++]=array($container,substr($input,$textStart,$matchStart-$textStart));
- $textStart=$matchEnd+1;
- }
- else
- throw new TTemplateParsingException('unexpected_matching',$match);
- }
- if(!empty($stack))
- {
- $name=array_pop($stack);
- if($name[0]==='@')
- $tag='</prop:'.substr($name,1).'>';
- else
- $tag='</com:'.$name.'>';
- $line=count(explode("\n",substr($input,0,$matchEnd+1)));
- throw new TTemplateParsingException('expecting_closing_tag',$line,$tag);
- }
- if($textStart<strlen($input))
- $tpl[$c++]=array($container,substr($input,$textStart));
- return $tpl;
- }
-
- /**
- * Parses the attributes of a tag from a string.
- * @param string the string to be parsed.
- * @return array attribute values indexed by names.
- */
- protected function parseAttributes($str)
- {
- if($str==='')
- return array();
- $pattern='/([\w\.]+)=(\'.*?\'|".*?"|<%.*?%>)/msS';
- $attributes=array();
- $n=preg_match_all($pattern,$str,$matches,PREG_SET_ORDER);
- for($i=0;$i<$n;++$i)
- {
- $name=strtolower($matches[$i][1]);
- $value=$matches[$i][2];
- if($value[0]==='\'' || $value[0]==='"')
- {
- $value=substr($value,1,strlen($value)-2);
- if(!preg_match('/(<%#.*?%>|<%=.*?%>|<%~.*?%>)/msS',$value))
- {
- $attributes[$name]=$value;
- continue;
- }
- }
- if($value[0]==='<')
- {
- if($value[2]==='#') // databind
- $attributes[$name]=array(0,substr($value,3,strlen($value)-5));
- else if($value[2]==='=') // a dynamic initialization
- $attributes[$name]=array(1,substr($value,3,strlen($value)-5));
- else if($value[2]==='~') // a URL
- $attributes[$name]=array(2,trim(substr($value,3,strlen($value)-5)));
- else
- $attributes[$name]=substr($value,2,strlen($value)-4);
- }
- }
- return $attributes;
- }
-}
-
-?> \ No newline at end of file
diff --git a/framework/Web/UI/TTemplateManager.php b/framework/Web/UI/TTemplateManager.php
index d9d26c0a..c9a4b9da 100644
--- a/framework/Web/UI/TTemplateManager.php
+++ b/framework/Web/UI/TTemplateManager.php
@@ -1,20 +1,65 @@
<?php
+/**
+ * TTemplateManager and TTemplate class file
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @link http://www.pradosoft.com/
+ * @copyright Copyright &copy; 2005 PradoSoft
+ * @license http://www.pradosoft.com/license/
+ * @version $Revision: $ $Date: $
+ * @package System.Web.UI
+ */
/**
- * include TTemplate class file
+ * TTemplateManager class
+ *
+ * TTemplateManager manages the loading and parsing of control templates.
+ *
+ * By default, TTemplateManager is registered with {@link TPageService} as the
+ * template manager module that can be accessed via {@link TPageService::getTemplateManager()}.
+ *
+ * Given a class name, TTemplateManager tries to locate the corresponding template
+ * file under the directory containing the class file. The name of the template file
+ * is the class name with the extension '.tpl'.
+ * where {@link getCacheExpire CacheExpire}, {@link getCacheControl CacheControl}
+ * and {@link getBufferOutput BufferOutput} are configurable properties of THttpResponse.
+ *
+ * If cache is enabled for the application, TTemplateManager will try to make
+ * of the cache to save the parsing time.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @version $Revision: $ $Date: $
+ * @package System.Web.UI
+ * @since 3.0
*/
-Prado::using('System.Web.UI.TTemplate');
+
class TTemplateManager extends TComponent implements IModule
{
+ /**
+ * Template file extension
+ */
const TEMPLATE_FILE_EXT='.tpl';
+ /**
+ * Prefix of the cache variable name for storing parsed templates
+ */
const TEMPLATE_CACHE_PREFIX='prado:template:';
+ /**
+ * @var TApplication application instance
+ */
private $_application;
/**
* @var string module ID
*/
private $_id;
+ /**
+ * Initializes the module.
+ * This method is required by IModule and is invoked by application.
+ * It starts output buffer if it is enabled.
+ * @param TApplication application
+ * @param TXmlElement module configuration
+ */
public function init($application,$config)
{
$this->_application=$application;
@@ -37,6 +82,10 @@ class TTemplateManager extends TComponent implements IModule
$this->_id=$value;
}
+ /**
+ * Loads the template corresponding to the specified class name.
+ * @return ITemplate template for the class name, null if template doesn't exist.
+ */
public function getTemplateByClassName($className)
{
$class=new ReflectionClass($className);
@@ -44,12 +93,16 @@ class TTemplateManager extends TComponent implements IModule
return $this->getTemplateByFileName($tplFile);
}
+ /**
+ * Loads the template from the specified file.
+ * @return ITemplate template parsed from the specified file, null if the file doesn't exist.
+ */
public function getTemplateByFileName($fileName)
{
if(($fileName=realpath($fileName))!==false && is_file($fileName))
{
if(($cache=$this->_application->getCache())===null)
- return new TTemplate(file_get_contents($fileName),dirname($fileName));
+ return new TTemplate(file_get_contents($fileName),dirname($fileName),$fileName);
else
{
$array=$cache->get(self::TEMPLATE_CACHE_PREFIX.$fileName);
@@ -59,7 +112,7 @@ class TTemplateManager extends TComponent implements IModule
if(filemtime($fileName)<$timestamp)
return $template;
}
- $template=new TTemplate(file_get_contents($fileName),dirname($fileName));
+ $template=new TTemplate(file_get_contents($fileName),dirname($fileName),$fileName);
$cache->set(self::TEMPLATE_CACHE_PREFIX.$fileName,array($template,time()));
return $template;
}
@@ -69,4 +122,512 @@ class TTemplateManager extends TComponent implements IModule
}
}
+/**
+ * TTemplate implements PRADO template parsing logic.
+ * A TTemplate object represents a parsed PRADO control template.
+ * It can instantiate the template as child controls of a specified control.
+ * The template format is like HTML, with the following special tags introduced,
+ * - component tags: a component tag represents the configuration of a component.
+ * The tag name is in the format of com:ComponentType, where ComponentType is the component
+ * class name. Component tags must be well-formed. Attributes of the component tag
+ * are treated as either property initial values, event handler attachment, or regular
+ * tag attributes.
+ * - property tags: property tags are used to set large block of attribute values.
+ * The property tag name is in the format of prop:AttributeName, where AttributeName
+ * can be a property name, an event name or a regular tag attribute name.
+ * - directive: directive specifies the property values for the template owner.
+ * It is in the format of &lt;% property name-value pairs %&gt;
+ * - expressions: expressions are shorthand of {@link TExpression} and {@link TStatements}
+ * controls. They are in the formate of &lt;= PHP expression &gt; and &lt; PHP statements &gt;
+ * - comments: There are two kinds of comments, regular HTML comments and special template comments.
+ * The former is in the format of &lt;!-- comments --&gt;, which will be treated as text strings.
+ * The latter is in the format of &lt;%* comments %&gt;, which will be stripped out.
+ *
+ * Tags other than the above are not required to be well-formed.
+ *
+ * A TTemplate object represents a parsed PRADO template. To instantiate the template
+ * for a particular control, call {@link instantiateIn($control)}, which
+ * will create and intialize all components specified in the template and
+ * set their parent as the control.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @version $Revision: $ $Date: $
+ * @package System.Web.UI
+ * @since 3.0
+ */
+class TTemplate extends TComponent implements ITemplate
+{
+ /**
+ * '<!.*?!>' - template comments
+ * '<!--.*?-->' - HTML comments
+ * '<\/?com:([\w\.]+)((?:\s*[\w\.]+=\'.*?\'|\s*[\w\.]+=".*?"|\s*[\w\.]+=<%.*?%>)*)\s*\/?>' - component tags
+ * '<\/?prop:([\w\.]+)\s*>' - property tags
+ * '<%@\s*(\w+)((?:\s*[\w\.]+=\'.*?\'|\s*[\w\.]+=".*?")*)\s*%>' - directives
+ * '<%=?(.*?)%>' - expressions
+ */
+ const REGEX_RULES='/<!.*?!>|<!--.*?-->|<\/?com:([\w\.]+)((?:\s*[\w\.]+=\'.*?\'|\s*[\w\.]+=".*?"|\s*[\w\.]+=<%.*?%>)*)\s*\/?>|<\/?prop:([\w\.]+)\s*>|<%@\s*((?:\s*[\w\.]+=\'.*?\'|\s*[\w\.]+=".*?")*)\s*%>|<%=?(.*?)%>/msS';
+
+ /**
+ * @var array list of component tags and strings
+ */
+ private $_tpl=array();
+ /**
+ * @var array list of directive settings
+ */
+ private $_directive=array();
+ /**
+ * @var string context path
+ */
+ private $_contextPath;
+ /**
+ * @var string template file path (if available)
+ */
+ private $_tplFile=null;
+
+ /**
+ * Constructor.
+ * The template will be parsed after construction.
+ * @param string the template string
+ */
+ public function __construct($template,$contextPath,$tplFile=null)
+ {
+ $this->_contextPath=$contextPath;
+ $this->_tplFile=$tplFile;
+ $this->parse($template);
+ }
+
+ /**
+ * @return array name-value pairs declared in the directive
+ */
+ public function getDirective()
+ {
+ return $this->_directive;
+ }
+
+ public function getItems()
+ {
+ return $this->_tpl;
+ }
+
+ /**
+ * Instantiates the template.
+ * Content in the template will be instantiated as components and text strings
+ * and passed to the specified parent control.
+ * @param TControl the parent control
+ * @throws TTemplateRuntimeException if an error is encountered during the instantiation.
+ */
+ public function instantiateIn($tplControl)
+ {
+ $page=$tplControl->getPage();
+ $assetManager=$page->getService()->getAssetManager();
+ $controls=array();
+ foreach($this->_tpl as $key=>$object)
+ {
+ if(isset($object[2])) // component
+ {
+ if(strpos($object[1],'.')===false)
+ {
+ if(class_exists($object[1],false))
+ $component=new $object[1];
+ else
+ {
+ include_once($object[1].Prado::CLASS_FILE_EXT);
+ if(class_exists($object[1],false))
+ $component=new $object[1];
+ else
+ throw new TTemplateRuntimeException('template_component_unknown',$object[1]);
+ }
+ }
+ else
+ $component=Prado::createComponent($object[1]);
+ if($component instanceof TControl)
+ {
+ $controls[$key]=$component;
+ $component->setTemplateControl($tplControl);
+ if(isset($object[2]['id']))
+ $tplControl->registerObject($object[2]['id'],$component);
+ if(isset($object[2]['skinid']))
+ {
+ $component->setSkinID($object[2]['skinid']);
+ unset($object[2]['skinid']);
+ }
+ $component->applyStyleSheetSkin($page);
+ // apply attributes
+ foreach($object[2] as $name=>$value)
+ {
+ if($component->hasEvent($name)) // is an event
+ {
+ if(is_string($value))
+ {
+ if(strpos($value,'.')===false)
+ $component->attachEventHandler($name,array($component,'TemplateControl.'.$value));
+ else
+ $component->attachEventHandler($name,array($component,$value));
+ }
+ else
+ throw new TTemplateRuntimeException('template_event_invalid',get_class($component),$name);
+ }
+ else if(strpos($name,'.')===false) // is simple property or custom attribute
+ {
+ if($component->hasProperty($name))
+ {
+ if($component->canSetProperty($name))
+ {
+ $setter='set'.$name;
+ if(is_string($value))
+ $component->$setter($value);
+ else if($value[0]===0)
+ $component->bindProperty($name,$value[1]);
+ else if($value[0]===1)
+ $component->$setter($component->evaluateExpression($value[1]));
+ else // url
+ {
+ $url=$assetManager->publishFilePath($this->_contextPath.'/'.$value[1]);
+ $component->$setter($url);
+ }
+ }
+ else
+ throw new TTemplateRuntimeException('template_property_readonly',get_class($component),$name);
+ }
+ else if($component->getAllowCustomAttributes())
+ {
+ if(is_array($value))
+ {
+ if($value[0]===1)
+ $value=$component->evaluateExpression($value[1]);
+ else if($value[0]===2)
+ $value=$assetManager->publishFilePath($this->_contextPath.'/'.$value[1]);
+ else
+ throw new TTemplateRuntimeException('template_attribute_unbindable',get_class($component),$name);
+ }
+ $component->getAttributes()->add($name,$value);
+ }
+ else
+ throw new TTemplateRuntimeException('template_property_undefined',get_class($component),$name);
+ }
+ else // complex property
+ {
+ if(is_string($value))
+ $component->setSubProperty($name,$value);
+ else if($value[0]===0)
+ $component->bindProperty($name,$value[1]);
+ else if($value[0]===1)
+ $component->setSubProperty($component->evaluateExpression($value[1]));
+ else
+ {
+ $url=$assetManager->publishFilePath($this->_contextPath.'/'.$value[1]);
+ $component->$setter($url);
+ }
+ }
+ }
+ $parent=isset($controls[$object[0]])?$controls[$object[0]]:$tplControl;
+ $component->createdOnTemplate($parent);
+ }
+ else if($component instanceof TComponent)
+ {
+ if(isset($object[2]['id']))
+ {
+ $tplControl->registerObject($object[2]['id'],$component);
+ if(!$component->hasProperty('id'))
+ unset($object[2]['id']);
+ }
+ foreach($object[2] as $name=>$value)
+ {
+ if($component->hasProperty($name))
+ {
+ if($component->canSetProperty($name))
+ {
+ $setter='set'.$name;
+ if(is_string($value))
+ $component->$setter($value);
+ else if($value[0]===1)
+ $component->$setter($component->evaluateExpression($value[1]));
+ else if($value[0]===2)
+ {
+ $url=$assetManager->publishFilePath($this->_contextPath.'/'.$value[1]);
+ $component->$setter($url);
+ }
+ else
+ throw new TTemplateRuntimeException('template_property_unbindable',get_class($component),$name);
+ }
+ else
+ throw new TTemplateRuntimeException('template_property_readonly',get_class($component),$name);
+ }
+ else
+ throw new TTemplateRuntimeException('template_property_undefined',get_class($component),$name);
+ }
+ $parent=isset($controls[$object[0]])?$controls[$object[0]]:$tplControl;
+ $parent->addParsedObject($component);
+ }
+ else
+ throw new TTemplateRuntimeException('template_component_required',$object[1]);
+ }
+ else // string
+ {
+ if(isset($controls[$object[0]]))
+ $controls[$object[0]]->addParsedObject($object[1]);
+ else
+ $tplControl->addParsedObject($object[1]);
+ }
+ }
+ }
+
+ /**
+ * Parses a template string.
+ *
+ * This template parser recognizes five types of data:
+ * regular string, well-formed component tags, well-formed property tags, directives, and expressions.
+ *
+ * The parsing result is returned as an array. Each array element can be of three types:
+ * - a string, 0: container index; 1: string content;
+ * - a component tag, 0: container index; 1: component type; 2: attributes (name=>value pairs)
+ * If a directive is found in the template, it will be parsed and can be
+ * retrieved via {@link getDirective}, which returns an array consisting of
+ * name-value pairs in the directive.
+ *
+ * Note, attribute names are treated as case-insensitive and will be turned into lower cases.
+ * Component and directive types are case-sensitive.
+ * Container index is the index to the array element that stores the container object.
+ * If an object has no container, its container index is -1.
+ *
+ * @param string the template string
+ * @return array the parsed result
+ * @throws TTemplateParsingException if a parsing error is encountered
+ */
+ protected function &parse($input)
+ {
+ $tpl=&$this->_tpl;
+ $n=preg_match_all(self::REGEX_RULES,$input,$matches,PREG_SET_ORDER|PREG_OFFSET_CAPTURE);
+ $expectPropEnd=false;
+ $textStart=0;
+ $stack=array();
+ $container=-1;
+ $c=0;
+ for($i=0;$i<$n;++$i)
+ {
+ $match=&$matches[$i];
+ $str=$match[0][0];
+ $matchStart=$match[0][1];
+ $matchEnd=$matchStart+strlen($str)-1;
+ if(strpos($str,'<com:')===0) // opening component tag
+ {
+ if($expectPropEnd)
+ continue;
+ if($matchStart>$textStart)
+ $tpl[$c++]=array($container,substr($input,$textStart,$matchStart-$textStart));
+ $textStart=$matchEnd+1;
+ $type=$match[1][0];
+ $attributes=$this->parseAttributes($match[2][0]);
+ $tpl[$c++]=array($container,$type,$attributes);
+ if($str[strlen($str)-2]!=='/') // open tag
+ {
+ array_push($stack,$type);
+ $container=$c-1;
+ }
+ }
+ else if(strpos($str,'</com:')===0) // closing component tag
+ {
+ if($expectPropEnd)
+ continue;
+ if($matchStart>$textStart)
+ $tpl[$c++]=array($container,substr($input,$textStart,$matchStart-$textStart));
+ $textStart=$matchEnd+1;
+ $type=$match[1][0];
+
+ if(empty($stack))
+ {
+ $line=count(explode("\n",substr($input,0,$matchEnd+1)));
+ if($this->_tplFile===null)
+ throw new TTemplateParsingException('template_closingtag_unexpected',"Line $line","</com:$type>");
+ else
+ throw new TTemplateParsingException('template_closingtag_unexpected',"{$this->_tplFile} (Line $line)","</com:$type>");
+ }
+
+ $name=array_pop($stack);
+ if($name!==$type)
+ {
+ if($name[0]==='@')
+ $tag='</prop:'.substr($name,1).'>';
+ else
+ $tag='</com:'.$name.'>';
+ $line=count(explode("\n",substr($input,0,$matchEnd+1)));
+ if($this->_tplFile===null)
+ throw new TTemplateParsingException('template_closingtag_expected',"Line $line",$tag);
+ else
+ throw new TTemplateParsingException('template_closingtag_expected',"{$this->_tplFile} (Line $line)",$tag);
+ }
+ $container=$tpl[$container][0];
+ }
+ else if(strpos($str,'<%@')===0) // directive
+ {
+ if($expectPropEnd)
+ continue;
+ if($matchStart>$textStart)
+ $tpl[$c++]=array($container,substr($input,$textStart,$matchStart-$textStart));
+ $textStart=$matchEnd+1;
+ if(isset($tpl[0]))
+ {
+ $line=count(explode("\n",substr($input,0,$matchEnd+1)));
+ if($this->_tplFile===null)
+ throw new TTemplateParsingException('template_directive_nonunique',"Line $line");
+ else
+ throw new TTemplateParsingException('template_directive_nonunique',"{$this->_tplFile} (Line $line)");
+ }
+ $this->_directive=$this->parseAttributes($match[4][0]);
+ }
+ else if(strpos($str,'<%')===0) // expression
+ {
+ if($expectPropEnd)
+ continue;
+ if($matchStart>$textStart)
+ $tpl[$c++]=array($container,substr($input,$textStart,$matchStart-$textStart));
+ $textStart=$matchEnd+1;
+ if($str[2]==='=')
+ $tpl[$c++]=array($container,'TExpression',array('Expression'=>$match[5][0]));
+ else
+ $tpl[$c++]=array($container,'TStatements',array('Statements'=>$match[5][0]));
+ }
+ else if(strpos($str,'<prop:')===0) // opening property
+ {
+ $prop=strtolower($match[3][0]);
+ array_push($stack,'@'.$prop);
+ if(!$expectPropEnd)
+ {
+ if($matchStart>$textStart)
+ $tpl[$c++]=array($container,substr($input,$textStart,$matchStart-$textStart));
+ $textStart=$matchEnd+1;
+ $expectPropEnd=true;
+ }
+ }
+ else if(strpos($str,'</prop:')===0) // closing property
+ {
+ $prop=strtolower($match[3][0]);
+ if(empty($stack))
+ {
+ $line=count(explode("\n",substr($input,0,$matchEnd+1)));
+ if($this->_tplFile===null)
+ throw new TTemplateParsingException('template_closingtag_unexpected',"Line $line","</prop:$prop>");
+ else
+ throw new TTemplateParsingException('template_closingtag_unexpected',"{$this->_tplFile} (Line $line)","</prop:$prop>");
+ }
+ $name=array_pop($stack);
+ if($name!=='@'.$prop)
+ {
+ if($name[0]==='@')
+ $tag='</prop:'.substr($name,1).'>';
+ else
+ $tag='</com:'.$name.'>';
+ $line=count(explode("\n",substr($input,0,$matchEnd+1)));
+ if($this->_tplFile===null)
+ throw new TTemplateParsingException('template_closingtag_expected',"Line $line",$tag);
+ else
+ throw new TTemplateParsingException('template_closingtag_expected',"{$this->_tplFile} (Line $line)",$tag);
+ }
+ if(($last=count($stack))<1 || $stack[$last-1][0]!=='@')
+ {
+ if($matchStart>$textStart && $container>=0)
+ {
+ $value=substr($input,$textStart,$matchStart-$textStart);
+ if(preg_match('/^<%.*?%>$/msS',$value))
+ {
+ if($value[2]==='#') // databind
+ $tpl[$container][2][$prop]=array(0,substr($value,3,strlen($value)-5));
+ else if($value[2]==='=') // a dynamic initialization
+ $tpl[$container][2][$prop]=array(1,substr($value,3,strlen($value)-5));
+ else
+ $tpl[$container][2][$prop]=$value;
+ }
+ else
+ $tpl[$container][2][$prop]=$value;
+ $textStart=$matchEnd+1;
+ }
+ $expectPropEnd=false;
+ }
+ }
+ else if(strpos($str,'<!--')===0) // HTML comments
+ {
+ $state=0;
+ }
+ else if(strpos($str,'<!')===0) // template comments
+ {
+ if($expectPropEnd)
+ {
+ $line=count(explode("\n",substr($input,0,$matchEnd+1)));
+ if($this->_tplFile===null)
+ throw new TTemplateParsingException('template_comments_forbidden',"Line $line");
+ else
+ throw new TTemplateParsingException('template_comments_forbidden',"{$this->_tplFile} (Line $line)");
+ }
+ if($matchStart>$textStart)
+ $tpl[$c++]=array($container,substr($input,$textStart,$matchStart-$textStart));
+ $textStart=$matchEnd+1;
+ }
+ else
+ {
+ $line=count(explode("\n",substr($input,0,$matchEnd+1)));
+ if($this->_tplFile===null)
+ throw new TTemplateParsingException('template_matching_unexpected',"Line $line",$match);
+ else
+ throw new TTemplateParsingException('template_matching_unexpected',"{$this->_tplFile} (Line $line)",$match);
+ }
+ }
+ if(!empty($stack))
+ {
+ $name=array_pop($stack);
+ if($name[0]==='@')
+ $tag='</prop:'.substr($name,1).'>';
+ else
+ $tag='</com:'.$name.'>';
+ $line=count(explode("\n",substr($input,0,$matchEnd+1)));
+ if($this->_tplFile===null)
+ throw new TTemplateParsingException('template_closingtag_expected',"Line $line",$tag);
+ else
+ throw new TTemplateParsingException('template_closingtag_expected',"{$this->_tplFile} (Line $line)",$tag);
+ }
+ if($textStart<strlen($input))
+ $tpl[$c++]=array($container,substr($input,$textStart));
+ return $tpl;
+ }
+
+ /**
+ * Parses the attributes of a tag from a string.
+ * @param string the string to be parsed.
+ * @return array attribute values indexed by names.
+ */
+ protected function parseAttributes($str)
+ {
+ if($str==='')
+ return array();
+ $pattern='/([\w\.]+)=(\'.*?\'|".*?"|<%.*?%>)/msS';
+ $attributes=array();
+ $n=preg_match_all($pattern,$str,$matches,PREG_SET_ORDER);
+ for($i=0;$i<$n;++$i)
+ {
+ $name=strtolower($matches[$i][1]);
+ $value=$matches[$i][2];
+ if($value[0]==='\'' || $value[0]==='"')
+ {
+ $value=substr($value,1,strlen($value)-2);
+ if(!preg_match('/(<%#.*?%>|<%=.*?%>|<%~.*?%>)/msS',$value))
+ {
+ $attributes[$name]=$value;
+ continue;
+ }
+ }
+ if($value[0]==='<')
+ {
+ if($value[2]==='#') // databind
+ $attributes[$name]=array(0,substr($value,3,strlen($value)-5));
+ else if($value[2]==='=') // a dynamic initialization
+ $attributes[$name]=array(1,substr($value,3,strlen($value)-5));
+ else if($value[2]==='~') // a URL
+ $attributes[$name]=array(2,trim(substr($value,3,strlen($value)-5)));
+ else
+ $attributes[$name]=substr($value,2,strlen($value)-4);
+ }
+ }
+ return $attributes;
+ }
+}
+
?> \ No newline at end of file
diff --git a/framework/Web/UI/TThemeManager.php b/framework/Web/UI/TThemeManager.php
index 5dcc640e..178cc91f 100644
--- a/framework/Web/UI/TThemeManager.php
+++ b/framework/Web/UI/TThemeManager.php
@@ -1,8 +1,5 @@
<?php
-// TODO: use namespace for BasePath
-// add ThemesUrl
-
class TThemeManager extends TComponent implements IModule
{
@@ -64,46 +61,19 @@ class TThemeManager extends TComponent implements IModule
{
if(($themePath=realpath($this->_basePath.'/'.$name))===false || !is_dir($themePath))
throw new TConfigurationException('thememanager_theme_inexistent',$name);
- if(($cache=$this->_application->getCache())!==null)
- {
- $array=$cache->get(self::THEME_CACHE_PREFIX.$themePath);
- if(is_array($array))
- {
- list($theme,$timestamp)=$array;
- $cacheValid=true;
- if($this->_application->getMode()!=='Performance')
- {
- if(($dir=opendir($themePath))===false)
- throw new TIOException('thememanager_theme_inexistent',$name);
- while(($file=readdir($dir))!==false)
- {
- if(basename($file,'.skin')!==$file && filemtime($themePath.'/'.$file)>$timestamp)
- {
- $cacheValid=false;
- break;
- }
- }
- closedir($dir);
- }
- if($cacheValid)
- return $theme;
- }
- }
- // not cached, so we collect all skin files
- $content='';
- if(($dir=opendir($themePath))===false)
- throw new TIOException('thememanager_theme_inexistent',$name);
- while(($file=readdir($dir))!==false)
+
+ if($this->_baseUrl===null)
{
- if(basename($file,'.skin')!==$file)
- $content.=file_get_contents($themePath.'/'.$file);
+ $appPath=dirname(Prado::getApplication()->getRequest()->getPhysicalApplicationPath());
+ if(strpos($themePath,$appPath)===false)
+ throw new TConfigurationException('theme_baseurl_required');
+ $appUrl=dirname(Prado::getApplication()->getRequest()->getApplicationPath());
+ $themeUrl=$appUrl.'/'.strtr(substr($themePath,strlen($appPath)),'\\','/');
}
- closedir($dir);
+ else
+ $themeUrl=$baseUrl.'/'.basename($themePath);
+ return new TTheme($this->_application,$themePath,$themeUrl);
- $theme=new TTheme($content,$themePath,$this->_baseUrl);
- if($cache!==null)
- $cache->set(self::THEME_CACHE_PREFIX.$themePath,array($theme,time()));
- return $theme;
}
public function getBasePath()
@@ -137,43 +107,73 @@ class TThemeManager extends TComponent implements IModule
class TTheme extends TTemplate
{
+ const THEME_CACHE_PREFIX='prado:theme:';
+ const SKIN_FILE_EXT='.skin';
private $_themePath;
private $_themeUrl;
- private $_skins=array();
+ private $_skins=null;
- public function __construct($content,$themePath,$baseUrl)
+ public function __construct($application,$themePath,$themeUrl)
{
- $this->_themePath=$themePath;
- if($baseUrl===null)
+ $this->_themeUrl=$themeUrl;
+ if(($cache=$application->getCache())!==null)
{
- $appPath=dirname(Prado::getApplication()->getRequest()->getPhysicalApplicationPath());
- if(strpos($themePath,$appPath)===false)
- throw new TConfigurationException('theme_baseurl_required');
- $appUrl=dirname(Prado::getApplication()->getRequest()->getApplicationPath());
- $this->_themeUrl=$appUrl.'/'.strtr(substr($themePath,strlen($appPath)),'\\','/');
+ $array=$cache->get(self::THEME_CACHE_PREFIX.$themePath);
+ if(is_array($array))
+ {
+ list($skins,$timestamp)=$array;
+ $cacheValid=true;
+ if($application->getMode()!=='Performance')
+ {
+ if(($dir=opendir($themePath))===false)
+ throw new TIOException('theme_path_inexistent',$themePath);
+ while(($file=readdir($dir))!==false)
+ {
+ if(basename($file,self::SKIN_FILE_EXT)!==$file && filemtime($themePath.'/'.$file)>$timestamp)
+ {
+ $cacheValid=false;
+ break;
+ }
+ }
+ closedir($dir);
+ }
+ if($cacheValid)
+ $this->_skins=$skins;
+ }
}
- else
- $this->_themeUrl=$baseUrl.'/'.basename($themePath);
-
- $theme=&$this->parse($content);
- foreach($theme as $skin)
+ if($this->_skins===null)
{
- if($skin[0]!==-1)
- throw new TConfigurationException('theme_control_nested',$skin[1]);
- else if(!isset($skin[2])) // a text string, ignored
- continue;
- $type=$skin[1];
- $id=isset($skin[2]['skinid'])?$skin[2]['skinid']:0;
- unset($skin[2]['skinid']);
- if(isset($this->_skins[$type][$id]))
- throw new TConfigurationException('theme_skinid_duplicated',$type,$id);
- foreach($skin[2] as $name=>$value)
+ if(($dir=opendir($themePath))===false)
+ throw new TIOException('theme_path_inexistent',$themePath);
+ while(($file=readdir($dir))!==false)
{
- if(is_array($value) && $value[0]===0)
- throw new TConfigurationException('theme_databind_unsupported',$type,$id,$name);
+ if(basename($file,self::SKIN_FILE_EXT)!==$file)
+ {
+ $template=new TTemplate(file_get_contents($themePath.'/'.$file),$themePath,$themePath.'/'.$file);
+ foreach($template->getItems() as $skin)
+ {
+ if($skin[0]!==-1)
+ throw new TConfigurationException('theme_control_nested',$skin[1]);
+ else if(!isset($skin[2])) // a text string, ignored
+ continue;
+ $type=$skin[1];
+ $id=isset($skin[2]['skinid'])?$skin[2]['skinid']:0;
+ unset($skin[2]['skinid']);
+ if(isset($this->_skins[$type][$id]))
+ throw new TConfigurationException('theme_skinid_duplicated',$type,$id);
+ foreach($skin[2] as $name=>$value)
+ {
+ if(is_array($value) && $value[0]===0)
+ throw new TConfigurationException('theme_databind_forbidden',$type,$id,$name);
+ }
+ $this->_skins[$type][$id]=$skin[2];
+ }
+ }
}
- $this->_skins[$type][$id]=$skin[2];
+ closedir($dir);
}
+ if($cache!==null)
+ $cache->set(self::THEME_CACHE_PREFIX.$themePath,array($this->_skins,time()));
}
public function applySkin($control)