Source for file File.php

Documentation is available at File.php

  1. <?php
  2.  
  3. /**
  4.  * represents a Dwoo template contained in a file
  5.  *
  6.  * This software is provided 'as-is', without any express or implied warranty.
  7.  * In no event will the authors be held liable for any damages arising from the use of this software.
  8.  *
  9.  * This file is released under the LGPL
  10.  * "GNU Lesser General Public License"
  11.  * More information can be found here:
  12.  * {@link http://www.gnu.org/copyleft/lesser.html}
  13.  *
  14.  * @author     Jordi Boggiano <j.boggiano@seld.be>
  15.  * @copyright  Copyright (c) 2008, Jordi Boggiano
  16.  * @license    http://www.gnu.org/copyleft/lesser.html  GNU Lesser General Public License
  17.  * @link       http://dwoo.org/
  18.  * @version    0.9.1
  19.  * @date       2008-05-30
  20.  * @package    Dwoo
  21.  */
  22. {
  23.     /**
  24.      * template filename
  25.      *
  26.      * @var string 
  27.      */
  28.     protected $file;
  29.  
  30.     /**
  31.      * creates a template from a file
  32.      *
  33.      * @param string $file the path to the template file, make sure it exists
  34.      * @param int $cacheTime duration of the cache validity for this template,
  35.      *                           if null it defaults to the Dwoo instance that will
  36.      *                           render this template
  37.      * @param string $cacheId the unique cache identifier of this page or anything else that
  38.      *                            makes this template's content unique, if null it defaults
  39.      *                            to the current url
  40.      * @param string $compileId the unique compiled identifier, which is used to distinguish this
  41.      *                              template from others, if null it defaults to the filename+bits of the path
  42.      */
  43.     public function __construct($file$cacheTime null$cacheId null$compileId null)
  44.     {
  45.         $this->file = $file;
  46.         $this->name = basename($file);
  47.         $this->cacheTime = $cacheTime;
  48.  
  49.         if ($compileId !== null{
  50.             $this->compileId = strtr($compileId'\\%?=!:;'.PATH_SEPARATOR'/-------');
  51.         }
  52.  
  53.         if ($cacheId !== null{
  54.             $this->cacheId = strtr($cacheId'\\%?=!:;'.PATH_SEPARATOR'/-------');
  55.         }
  56.     }
  57.  
  58.     /**
  59.      * returns the compiled template file name
  60.      *
  61.      * @param Dwoo $dwoo the dwoo instance that requests it
  62.      * @param Dwoo_ICompiler $compiler the compiler that must be used
  63.      * @return string 
  64.      */
  65.     public function getCompiledTemplate(Dwoo $dwooDwoo_ICompiler $compiler null)
  66.     {
  67.         $compiledFile $this->getCompiledFilename($dwoo);
  68.  
  69.         if ($this->compilationEnforced !== true && isset(self::$cache['compiled'][$this->compileId]=== true{
  70.             // already checked, return compiled file
  71.         elseif ($this->compilationEnforced !== true && file_exists($compiledFile)===true && (int)$this->getUid(<= filemtime($compiledFile)) {
  72.             // template is compiled and has not been modified since the compilation
  73.             self::$cache['compiled'][$this->compileIdtrue;
  74.         else {
  75.             // compiles the template
  76.             $this->compilationEnforced = false;
  77.  
  78.             if ($compiler === null{
  79.                 $compiler $dwoo->getDefaultCompilerFactory('string');
  80.  
  81.                 if ($compiler === null || $compiler === array('Dwoo_Compiler''compilerFactory')) {
  82.                     if (class_exists('Dwoo_Compiler'false=== false{
  83.                         include 'Dwoo/Compiler.php';
  84.                     }
  85.                     $compiler Dwoo_Compiler::compilerFactory();
  86.                 else {
  87.                     $compiler call_user_func($compiler);
  88.                 }
  89.             }
  90.  
  91.             $this->compiler = $compiler;
  92.  
  93.             $compiler->setCustomPlugins($dwoo->getCustomPlugins());
  94.             $compiler->setSecurityPolicy($dwoo->getSecurityPolicy());
  95.             $this->makeDirectory(dirname($compiledFile));
  96.             file_put_contents($compiledFile$compiler->compile($dwoo$this));
  97.             chmod($compiledFileDWOO_CHMOD);
  98.  
  99.             self::$cache['compiled'][$this->compileIdtrue;
  100.         }
  101.  
  102.         return $compiledFile;
  103.     }
  104.  
  105.     /**
  106.      * returns the template source of this template
  107.      *
  108.      * @return string 
  109.      */
  110.     public function getSource()
  111.     {
  112.         return file_get_contents($this->file);
  113.     }
  114.  
  115.     /**
  116.      * returns the resource name for this template class
  117.      *
  118.      * @return string 
  119.      */
  120.     public function getResourceName()
  121.     {
  122.         return 'file';
  123.     }
  124.  
  125.     /**
  126.      * returns this template's source filename
  127.      *
  128.      * @return string 
  129.      */
  130.     public function getResourceIdentifier()
  131.     {
  132.         return $this->file;
  133.     }
  134.  
  135.     /**
  136.      * returns an unique value identifying the current version of this template,
  137.      * in this case it's the unix timestamp of the last modification
  138.      *
  139.      * @return string 
  140.      */
  141.     public function getUid()
  142.     {
  143.         return (string) filemtime($this->file);
  144.     }
  145.  
  146.     /**
  147.      * returns a new template object from the given include name, null if no include is
  148.      * possible (resource not found), or false if include is not permitted by this resource type
  149.      *
  150.      * @param Dwoo $dwoo the dwoo instance requiring it
  151.      * @param mixed $resourceId the filename (relative to this template's dir) of the template to include
  152.      * @param int $cacheTime duration of the cache validity for this template,
  153.      *                           if null it defaults to the Dwoo instance that will
  154.      *                           render this template
  155.      * @param string $cacheId the unique cache identifier of this page or anything else that
  156.      *                            makes this template's content unique, if null it defaults
  157.      *                            to the current url
  158.      * @param string $compileId the unique compiled identifier, which is used to distinguish this
  159.      *                              template from others, if null it defaults to the filename+bits of the path
  160.      * @return Dwoo_Template_File|null
  161.      */
  162.     public static function templateFactory(Dwoo $dwoo$resourceId$cacheTime null$cacheId null$compileId null)
  163.     {
  164.         $resourceId str_replace(array("\t""\n""\r")array('\\t''\\n''\\r')$resourceId);
  165.         if (file_exists($resourceId=== false{
  166.             $tpl $dwoo->getTemplate();
  167.             if ($tpl instanceof Dwoo_Template_File{
  168.                 $resourceId dirname($tpl->getResourceIdentifier()).DIRECTORY_SEPARATOR.$resourceId;
  169.                 if (file_exists($resourceId=== false{
  170.                     return null;
  171.                 }
  172.             else {
  173.                 return null;
  174.             }
  175.         }
  176.  
  177.         // prevent template recursion if security is in effect
  178.         if ($policy $dwoo->getSecurityPolicy()) {
  179.             $tpl $dwoo->getTemplate();
  180.             if ($tpl instanceof Dwoo_Template_File && $resourceId === $tpl->getResourceIdentifier()) {
  181.                 return $dwoo->triggerError('You can not include a template into itself'E_USER_WARNING);
  182.             }
  183.         }
  184.  
  185.         return new Dwoo_Template_File($resourceId$cacheTime$cacheId$compileId);
  186.     }
  187.  
  188.     /**
  189.      * returns the full compiled file name and assigns a default value to it if
  190.      * required
  191.      *
  192.      * @param Dwoo $dwoo the dwoo instance that requests the file name
  193.      * @return string the full path to the compiled file
  194.      */
  195.     protected function getCompiledFilename(Dwoo $dwoo)
  196.     {
  197.         // no compile id was provided, set default
  198.         if ($this->compileId===null{
  199.             $this->compileId = implode('/'array_slice(explode('/'strtr($this->file'\\''/'))-3));
  200.         }
  201.         return $dwoo->getCompileDir($this->compileId.'.d'.Dwoo::RELEASE_TAG.'.php';
  202.     }
  203. }

Documentation generated on Sat, 28 Jun 2008 01:38:23 +0200 by phpDocumentor 1.4.0