| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | Prado::using('System.Web.UI.WebControls.TOutputCache'); |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | class TTemplateManager extends TModule |
|---|
| 40 | { |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | const TEMPLATE_FILE_EXT='.tpl'; |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | const TEMPLATE_CACHE_PREFIX='prado:template:'; |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | public function init($config) |
|---|
| 57 | { |
|---|
| 58 | $this->getService()->setTemplateManager($this); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | public function getTemplateByClassName($className) |
|---|
| 66 | { |
|---|
| 67 | $class=new ReflectionClass($className); |
|---|
| 68 | $tplFile=dirname($class->getFileName()).DIRECTORY_SEPARATOR.$className.self::TEMPLATE_FILE_EXT; |
|---|
| 69 | return $this->getTemplateByFileName($tplFile); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | public function getTemplateByFileName($fileName) |
|---|
| 77 | { |
|---|
| 78 | if(($fileName=$this->getLocalizedTemplate($fileName))!==null) |
|---|
| 79 | { |
|---|
| 80 | Prado::trace("Loading template $fileName",'System.Web.UI.TTemplateManager'); |
|---|
| 81 | if(($cache=$this->getApplication()->getCache())===null) |
|---|
| 82 | return new TTemplate(file_get_contents($fileName),dirname($fileName),$fileName); |
|---|
| 83 | else |
|---|
| 84 | { |
|---|
| 85 | $array=$cache->get(self::TEMPLATE_CACHE_PREFIX.$fileName); |
|---|
| 86 | if(is_array($array)) |
|---|
| 87 | { |
|---|
| 88 | list($template,$timestamps)=$array; |
|---|
| 89 | if($this->getApplication()->getMode()===TApplicationMode::Performance) |
|---|
| 90 | return $template; |
|---|
| 91 | $cacheValid=true; |
|---|
| 92 | foreach($timestamps as $tplFile=>$timestamp) |
|---|
| 93 | { |
|---|
| 94 | if(!is_file($tplFile) || filemtime($tplFile)>$timestamp) |
|---|
| 95 | { |
|---|
| 96 | $cacheValid=false; |
|---|
| 97 | break; |
|---|
| 98 | } |
|---|
| 99 | } |
|---|
| 100 | if($cacheValid) |
|---|
| 101 | return $template; |
|---|
| 102 | } |
|---|
| 103 | $template=new TTemplate(file_get_contents($fileName),dirname($fileName),$fileName); |
|---|
| 104 | $includedFiles=$template->getIncludedFiles(); |
|---|
| 105 | $timestamps=array(); |
|---|
| 106 | $timestamps[$fileName]=filemtime($fileName); |
|---|
| 107 | foreach($includedFiles as $includedFile) |
|---|
| 108 | $timestamps[$includedFile]=filemtime($includedFile); |
|---|
| 109 | $cache->set(self::TEMPLATE_CACHE_PREFIX.$fileName,array($template,$timestamps)); |
|---|
| 110 | return $template; |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | else |
|---|
| 114 | return null; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | protected function getLocalizedTemplate($filename) |
|---|
| 123 | { |
|---|
| 124 | if(($app=$this->getApplication()->getGlobalization(false))===null) |
|---|
| 125 | return is_file($filename)?$filename:null; |
|---|
| 126 | foreach($app->getLocalizedResource($filename) as $file) |
|---|
| 127 | { |
|---|
| 128 | if(($file=realpath($file))!==false && is_file($file)) |
|---|
| 129 | return $file; |
|---|
| 130 | } |
|---|
| 131 | return null; |
|---|
| 132 | } |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | class TTemplate extends TApplicationComponent implements ITemplate |
|---|
| 170 | { |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | const REGEX_RULES='/<!--.*?--!>|<!---.*?--->|<\/?com:([\w\.]+)((?:\s*[\w\.]+\s*=\s*\'.*?\'|\s*[\w\.]+\s*=\s*".*?"|\s*[\w\.]+\s*=\s*<%.*?%>)*)\s*\/?>|<\/?prop:([\w\.]+)\s*>|<%@\s*((?:\s*[\w\.]+\s*=\s*\'.*?\'|\s*[\w\.]+\s*=\s*".*?")*)\s*%>|<%[%#~\\$=\\[](.*?)%>|<prop:([\w\.]+)((?:\s*[\w\.]+\s*=\s*\'.*?\'|\s*[\w\.]+\s*=\s*".*?"|\s*[\w\.]+\s*=\s*<%.*?%>)*)\s*\/>/msS'; |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | const CONFIG_DATABIND=0; |
|---|
| 186 | const CONFIG_EXPRESSION=1; |
|---|
| 187 | const CONFIG_ASSET=2; |
|---|
| 188 | const CONFIG_PARAMETER=3; |
|---|
| 189 | const CONFIG_LOCALIZATION=4; |
|---|
| 190 | const CONFIG_TEMPLATE=5; |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | private $_tpl=array(); |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | private $_directive=array(); |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | private $_contextPath; |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | private $_tplFile=null; |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | private $_startingLine=0; |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | private $_content; |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | private $_sourceTemplate=true; |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | private $_hashCode=''; |
|---|
| 224 | private $_tplControl=null; |
|---|
| 225 | private $_includedFiles=array(); |
|---|
| 226 | private $_includeAtLine=array(); |
|---|
| 227 | private $_includeLines=array(); |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | public function __construct($template,$contextPath,$tplFile=null,$startingLine=0,$sourceTemplate=true) |
|---|
| 241 | { |
|---|
| 242 | $this->_sourceTemplate=$sourceTemplate; |
|---|
| 243 | $this->_contextPath=$contextPath; |
|---|
| 244 | $this->_tplFile=$tplFile; |
|---|
| 245 | $this->_startingLine=$startingLine; |
|---|
| 246 | $this->_content=$template; |
|---|
| 247 | $this->_hashCode=md5($template); |
|---|
| 248 | $this->parse($template); |
|---|
| 249 | $this->_content=null; |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | public function getTemplateFile() |
|---|
| 256 | { |
|---|
| 257 | return $this->_tplFile; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | public function getIsSourceTemplate() |
|---|
| 265 | { |
|---|
| 266 | return $this->_sourceTemplate; |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | public function getContextPath() |
|---|
| 273 | { |
|---|
| 274 | return $this->_contextPath; |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | public function getDirective() |
|---|
| 281 | { |
|---|
| 282 | return $this->_directive; |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | public function getHashCode() |
|---|
| 289 | { |
|---|
| 290 | return $this->_hashCode; |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | public function &getItems() |
|---|
| 297 | { |
|---|
| 298 | return $this->_tpl; |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 | public function instantiateIn($tplControl,$parentControl=null) |
|---|
| 309 | { |
|---|
| 310 | $this->_tplControl=$tplControl; |
|---|
| 311 | if($parentControl===null) |
|---|
| 312 | $parentControl=$tplControl; |
|---|
| 313 | if(($page=$tplControl->getPage())===null) |
|---|
| 314 | $page=$this->getService()->getRequestedPage(); |
|---|
| 315 | $controls=array(); |
|---|
| 316 | $directChildren=array(); |
|---|
| 317 | foreach($this->_tpl as $key=>$object) |
|---|
| 318 | { |
|---|
| 319 | if($object[0]===-1) |
|---|
| 320 | $parent=$parentControl; |
|---|
| 321 | else if(isset($controls[$object[0]])) |
|---|
| 322 | $parent=$controls[$object[0]]; |
|---|
| 323 | else |
|---|
| 324 | continue; |
|---|
| 325 | if(isset($object[2])) |
|---|
| 326 | { |
|---|
| 327 | $component=Prado::createComponent($object[1]); |
|---|
| 328 | $properties=&$object[2]; |
|---|
| 329 | if($component instanceof TControl) |
|---|
| 330 | { |
|---|
| 331 | if($component instanceof TOutputCache) |
|---|
| 332 | $component->setCacheKeyPrefix($this->_hashCode.$key); |
|---|
| 333 | $component->setTemplateControl($tplControl); |
|---|
| 334 | if(isset($properties['id'])) |
|---|
| 335 | { |
|---|
| 336 | if(is_array($properties['id'])) |
|---|
| 337 | $properties['id']=$component->evaluateExpression($properties['id'][1]); |
|---|
| 338 | $tplControl->registerObject($properties['id'],$component); |
|---|
| 339 | } |
|---|
| 340 | if(isset($properties['skinid'])) |
|---|
| 341 | { |
|---|
| 342 | if(is_array($properties['skinid'])) |
|---|
| 343 | $component->setSkinID($component->evaluateExpression($properties['skinid'][1])); |
|---|
| 344 | else |
|---|
| 345 | $component->setSkinID($properties['skinid']); |
|---|
| 346 | unset($properties['skinid']); |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | $component->trackViewState(false); |
|---|
| 350 | |
|---|
| 351 | $component->applyStyleSheetSkin($page); |
|---|
| 352 | foreach($properties as $name=>$value) |
|---|
| 353 | $this->configureControl($component,$name,$value); |
|---|
| 354 | |
|---|
| 355 | $component->trackViewState(true); |
|---|
| 356 | |
|---|
| 357 | if($parent===$parentControl) |
|---|
| 358 | $directChildren[]=$component; |
|---|
| 359 | else |
|---|
| 360 | $component->createdOnTemplate($parent); |
|---|
| 361 | if($component->getAllowChildControls()) |
|---|
| 362 | $controls[$key]=$component; |
|---|
| 363 | } |
|---|
| 364 | else if($component instanceof TComponent) |
|---|
| 365 | { |
|---|
| 366 | $controls[$key]=$component; |
|---|
| 367 | if(isset($properties['id'])) |
|---|
| 368 | { |
|---|
| 369 | if(is_array($properties['id'])) |
|---|
| 370 | $properties['id']=$component->evaluateExpression($properties['id'][1]); |
|---|
| 371 | $tplControl->registerObject($properties['id'],$component); |
|---|
| 372 | if(!$component->hasProperty('id')) |
|---|
| 373 | unset($properties['id']); |
|---|
| 374 | } |
|---|
| 375 | foreach($properties as $name=>$value) |
|---|
| 376 | $this->configureComponent($component,$name,$value); |
|---|
| 377 | if($parent===$parentControl) |
|---|
| 378 | $directChildren[]=$component; |
|---|
| 379 | else |
|---|
| 380 | $component->createdOnTemplate($parent); |
|---|
| 381 | } |
|---|
| 382 | } |
|---|
| 383 | else |
|---|
| 384 | { |
|---|
| 385 | if($object[1] instanceof TCompositeLiteral) |
|---|
| 386 | { |
|---|
| 387 | |
|---|
| 388 | $o=clone $object[1]; |
|---|
| 389 | $o->setContainer($tplControl); |
|---|
| 390 | if($parent===$parentControl) |
|---|
| 391 | $directChildren[]=$o; |
|---|
| 392 | else |
|---|
| 393 | $parent->addParsedObject($o); |
|---|
| 394 | } |
|---|
| 395 | else |
|---|
| 396 | { |
|---|
| 397 | if($parent===$parentControl) |
|---|
| 398 | $directChildren[]=$object[1]; |
|---|
| 399 | else |
|---|
| 400 | $parent->addParsedObject($object[1]); |
|---|
| 401 | } |
|---|
| 402 | } |
|---|
| 403 | } |
|---|
| 404 | |
|---|
| 405 | |
|---|
| 406 | |
|---|
| 407 | foreach($directChildren as $control) |
|---|
| 408 | { |
|---|
| 409 | if($control instanceof TComponent) |
|---|
| 410 | $control->createdOnTemplate($parentControl); |
|---|
| 411 | else |
|---|
| 412 | $parentControl->addParsedObject($control); |
|---|
| 413 | } |
|---|
| 414 | } |
|---|
| 415 | |
|---|
| 416 | |
|---|
| 417 | |
|---|
| 418 | |
|---|
| 419 | |
|---|
| 420 | |
|---|
| 421 | |
|---|
| 422 | protected function configureControl($control,$name,$value) |
|---|
| 423 | { |
|---|
| 424 | if(strncasecmp($name,'on',2)===0) |
|---|
| 425 | $this->configureEvent($control,$name,$value,$control); |
|---|
| 426 | else if(($pos=strrpos($name,'.'))===false) |
|---|
| 427 | $this->configureProperty($control,$name,$value); |
|---|
| 428 | else |
|---|
| 429 | $this->configureSubProperty($control,$name,$value); |
|---|
| 430 | } |
|---|
| 431 | |
|---|
| 432 | |
|---|
| 433 | |
|---|
| 434 | |
|---|
| 435 | |
|---|
| 436 | |
|---|
| 437 | |
|---|
| 438 | protected function configureComponent($component,$name,$value) |
|---|
| 439 | { |
|---|
| 440 | if(strpos($name,'.')===false) |
|---|
| 441 | $this->configureProperty($component,$name,$value); |
|---|
| 442 | else |
|---|
| 443 | $this->configureSubProperty($component,$name,$value); |
|---|
| 444 | } |
|---|
| 445 | |
|---|
| 446 | |
|---|
| 447 | |
|---|
| 448 | |
|---|
| 449 | |
|---|
| 450 | |
|---|
| 451 | |
|---|
| 452 | |
|---|
| 453 | protected function configureEvent($control,$name,$value,$contextControl) |
|---|
| 454 | { |
|---|
| 455 | if(strpos($value,'.')===false) |
|---|
| 456 | $control->attachEventHandler($name,array($contextControl,'TemplateControl.'.$value)); |
|---|
| 457 | else |
|---|
| 458 | $control->attachEventHandler($name,array($contextControl,$value)); |
|---|
| 459 | } |
|---|
| 460 | |
|---|
| 461 | |
|---|
| 462 | |
|---|
| 463 | |
|---|
| 464 | |
|---|
| 465 | |
|---|
| 466 | |
|---|
| 467 | protected function configureProperty($component,$name,$value) |
|---|
| 468 | { |
|---|
| 469 | if(is_array($value)) |
|---|
| 470 | { |
|---|
| 471 | switch($value[0]) |
|---|
| 472 | { |
|---|
| 473 | case self::CONFIG_DATABIND: |
|---|
| 474 | $component->bindProperty($name,$value[1]); |
|---|
| 475 | break; |
|---|
| 476 | case self::CONFIG_EXPRESSION: |
|---|
| 477 | if($component instanceof TControl) |
|---|
| 478 | $component->autoBindProperty($name,$value[1]); |
|---|
| 479 | else |
|---|
| 480 | { |
|---|
| 481 | $setter='set'.$name; |
|---|
| 482 | $component->$setter($this->_tplControl->evaluateExpression($value[1])); |
|---|
| 483 | } |
|---|
| 484 | break; |
|---|
| 485 | case self::CONFIG_TEMPLATE: |
|---|
| 486 | $setter='set'.$name; |
|---|
| 487 | $component->$setter($value[1]); |
|---|
| 488 | break; |
|---|
| 489 | case self::CONFIG_ASSET: |
|---|
| 490 | $setter='set'.$name; |
|---|
| 491 | $url=$this->publishFilePath($this->_contextPath.DIRECTORY_SEPARATOR.$value[1]); |
|---|
| 492 | $component->$setter($url); |
|---|
| 493 | break; |
|---|
| 494 | case self::CONFIG_PARAMETER: |
|---|
| 495 | $setter='set'.$name; |
|---|
| 496 | $component->$setter($this->getApplication()->getParameters()->itemAt($value[1])); |
|---|
| 497 | break; |
|---|
| 498 | case self::CONFIG_LOCALIZATION: |
|---|
| 499 | $setter='set'.$name; |
|---|
| 500 | $component->$setter(Prado::localize($value[1])); |
|---|
| 501 | break; |
|---|
| 502 | default: |
|---|
| 503 | throw new TConfigurationException('template_tag_unexpected',$name,$value[1]); |
|---|
| 504 | break; |
|---|
| 505 | } |
|---|
| 506 | } |
|---|
| 507 | else |
|---|
| 508 | { |
|---|
| 509 | $setter='set'.$name; |
|---|
| 510 | $component->$setter($value); |
|---|
| 511 | } |
|---|
| 512 | } |
|---|
| 513 | |
|---|
| 514 | |
|---|
| 515 | |
|---|
| 516 | |
|---|
| 517 | |
|---|
| 518 | |
|---|
| 519 | |
|---|
| 520 | protected function configureSubProperty($component,$name,$value) |
|---|
| 521 | { |
|---|
| 522 | if(is_array($value)) |
|---|
| 523 | { |
|---|
| 524 | switch($value[0]) |
|---|
| 525 | { |
|---|
| 526 | case self::CONFIG_DATABIND: |
|---|
| 527 | $component->bindProperty($name,$value[1]); |
|---|
| 528 | break; |
|---|
| 529 | case self::CONFIG_EXPRESSION: |
|---|
| 530 | if($component instanceof TControl) |
|---|
| 531 | $component->autoBindProperty($name,$value[1]); |
|---|
| 532 | else |
|---|
| 533 | $component->setSubProperty($name,$this->_tplControl->evaluateExpression($value[1])); |
|---|
| 534 | break; |
|---|
| 535 | case self::CONFIG_TEMPLATE: |
|---|
| 536 | $component->setSubProperty($name,$value[1]); |
|---|
| 537 | break; |
|---|
| 538 | case self::CONFIG_ASSET: |
|---|
| 539 | $url=$this->publishFilePath($this->_contextPath.DIRECTORY_SEPARATOR.$value[1]); |
|---|
| 540 | $component->setSubProperty($name,$url); |
|---|
| 541 | break; |
|---|
| 542 | case self::CONFIG_PARAMETER: |
|---|
| 543 | $component->setSubProperty($name,$this->getApplication()->getParameters()->itemAt($value[1])); |
|---|
| 544 | break; |
|---|
| 545 | case self::CONFIG_LOCALIZATION: |
|---|
| 546 | $component->setSubProperty($name,Prado::localize($value[1])); |
|---|
| 547 | break; |
|---|
| 548 | default: |
|---|
| 549 | throw new TConfigurationException('template_tag_unexpected',$name,$value[1]); |
|---|
| 550 | break; |
|---|
| 551 | } |
|---|
| 552 | } |
|---|
| 553 | else |
|---|
| 554 | $component->setSubProperty($name,$value); |
|---|
| 555 | } |
|---|
| 556 | |
|---|
| 557 | |
|---|
| 558 | |
|---|
| 559 | | <
|---|