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.  * @author     Jordi Boggiano <j.boggiano@seld.be>
  10.  * @copyright  Copyright (c) 2008, Jordi Boggiano
  11.  * @license    http://dwoo.org/LICENSE   Modified BSD License
  12.  * @link       http://dwoo.org/
  13.  * @version    1.1.0
  14.  * @date       2009-07-18
  15.  * @package    Dwoo
  16.  */
  17. {
  18.     /**
  19.      * template filename
  20.      *
  21.      * @var string 
  22.      */
  23.     protected $file;
  24.  
  25.     /**
  26.      * include path(s) to look into to find this template
  27.      *
  28.      * @var array 
  29.      */
  30.     protected $includePath = null;
  31.  
  32.     /**
  33.      * resolved path cache when looking for a file in multiple include paths
  34.      *
  35.      * this is reset when the include path is changed
  36.      *
  37.      * @var string 
  38.      */
  39.     protected $resolvedPath = null;
  40.  
  41.     /**
  42.      * creates a template from a file
  43.      *
  44.      * @param string $file the path to the template file, make sure it exists
  45.      * @param int $cacheTime duration of the cache validity for this template,
  46.      *                           if null it defaults to the Dwoo instance that will
  47.      *                           render this template
  48.      * @param string $cacheId the unique cache identifier of this page or anything else that
  49.      *                            makes this template's content unique, if null it defaults
  50.      *                            to the current url
  51.      * @param string $compileId the unique compiled identifier, which is used to distinguish this
  52.      *                              template from others, if null it defaults to the filename+bits of the path
  53.      * @param mixed $includePath a string for a single path to look into for the given file, or an array of paths
  54.      */
  55.     public function __construct($file$cacheTime null$cacheId null$compileId null$includePath null)
  56.     {
  57.         $this->file = $file;
  58.         $this->name = basename($file);
  59.         $this->cacheTime = $cacheTime;
  60.  
  61.         if ($compileId !== null{
  62.             $this->compileId = str_replace('../''__'strtr($compileId'\\%?=!:;'.PATH_SEPARATOR'/-------'));
  63.         }
  64.  
  65.         if ($cacheId !== null{
  66.             $this->cacheId = str_replace('../''__'strtr($cacheId'\\%?=!:;'.PATH_SEPARATOR'/-------'));
  67.         }
  68.  
  69.         if (is_string($includePath)) {
  70.             $this->includePath = array($includePath);
  71.         elseif (is_array($includePath)) {
  72.             $this->includePath = $includePath;
  73.         }
  74.     }
  75.  
  76.     /**
  77.      * sets the include path(s) to where the given template filename must be looked up
  78.      *
  79.      * @param mixed $paths the path to look into, can be string for a single path or an array of paths
  80.      */
  81.     public function setIncludePath($paths)
  82.     {
  83.         if (is_array($paths=== false{
  84.             $paths array($paths);
  85.         }
  86.  
  87.         $this->includePath = $paths;
  88.         $this->resolvedPath = null;
  89.     }
  90.  
  91.     /**
  92.      * return the current include path(s)
  93.      *
  94.      * @return array 
  95.      */
  96.     public function getIncludePath()
  97.     {
  98.         return $this->includePath;
  99.     }
  100.  
  101.     /**
  102.      * Checks if compiled file is valid (exists and it's the modification is greater or
  103.      * equal to the modification time of the template file)
  104.      *
  105.      * @param string file
  106.      * @return boolean True cache file existance and it's modification time
  107.      */
  108.     protected function isValidCompiledFile($file{
  109.         return parent::isValidCompiledFile($file&& (int)$this->getUid(<= filemtime($file);
  110.     }
  111.  
  112.     /**
  113.      * returns the template source of this template
  114.      *
  115.      * @return string 
  116.      */
  117.     public function getSource()
  118.     {
  119.         return file_get_contents($this->getResourceIdentifier());
  120.     }
  121.  
  122.     /**
  123.      * returns the resource name for this template class
  124.      *
  125.      * @return string 
  126.      */
  127.     public function getResourceName()
  128.     {
  129.         return 'file';
  130.     }
  131.  
  132.     /**
  133.      * returns this template's source filename
  134.      *
  135.      * @return string 
  136.      */
  137.     public function getResourceIdentifier()
  138.     {
  139.         if ($this->resolvedPath !== null{
  140.             return $this->resolvedPath;
  141.         elseif ($this->includePath === null{
  142.             return $this->file;
  143.         else {
  144.             foreach ($this->includePath as $path{
  145.                 $path rtrim($pathDIRECTORY_SEPARATOR);
  146.                 if (file_exists($path.DIRECTORY_SEPARATOR.$this->file=== true{
  147.                     $this->resolvedPath = $path DIRECTORY_SEPARATOR $this->file;
  148.                     return $this->resolvedPath;
  149.                 }
  150.             }
  151.  
  152.             throw new Dwoo_Exception('Template "'.$this->file.'" could not be found in any of your include path(s)');
  153.         }
  154.     }
  155.  
  156.     /**
  157.      * returns an unique value identifying the current version of this template,
  158.      * in this case it's the unix timestamp of the last modification
  159.      *
  160.      * @return string 
  161.      */
  162.     public function getUid()
  163.     {
  164.         return (string) filemtime($this->getResourceIdentifier());
  165.     }
  166.  
  167.     /**
  168.      * returns a new template object from the given include name, null if no include is
  169.      * possible (resource not found), or false if include is not permitted by this resource type
  170.      *
  171.      * @param Dwoo $dwoo the dwoo instance requiring it
  172.      * @param mixed $resourceId the filename (relative to this template's dir) of the template to include
  173.      * @param int $cacheTime duration of the cache validity for this template,
  174.      *                           if null it defaults to the Dwoo instance that will
  175.      *                           render this template
  176.      * @param string $cacheId the unique cache identifier of this page or anything else that
  177.      *                            makes this template's content unique, if null it defaults
  178.      *                            to the current url
  179.      * @param string $compileId the unique compiled identifier, which is used to distinguish this
  180.      *                              template from others, if null it defaults to the filename+bits of the path
  181.      * @param Dwoo_ITemplate $parentTemplate the template that is requesting a new template object (through
  182.      *                                              an include, extends or any other plugin)
  183.      * @return Dwoo_Template_File|null
  184.      */
  185.     public static function templateFactory(Dwoo $dwoo$resourceId$cacheTime null$cacheId null$compileId nullDwoo_ITemplate $parentTemplate null)
  186.     {
  187.         if (DIRECTORY_SEPARATOR === '\\'{
  188.             $resourceId str_replace(array("\t""\n""\r""\f""\v")array('\\t''\\n''\\r''\\f''\\v')$resourceId);
  189.         }
  190.         $resourceId strtr($resourceId'\\''/');
  191.  
  192.         $includePath null;
  193.  
  194.         if (file_exists($resourceId=== false{
  195.             if ($parentTemplate === null{
  196.                 $parentTemplate $dwoo->getTemplate();
  197.             }
  198.             if ($parentTemplate instanceof Dwoo_Template_File{
  199.                 if ($includePath $parentTemplate->getIncludePath()) {
  200.                     if (strstr($resourceId'../')) {
  201.                         throw new Dwoo_Exception('When using an include path you can not reference a template into a parent directory (using ../)');
  202.                     }
  203.                 else {
  204.                     $resourceId dirname($parentTemplate->getResourceIdentifier()).DIRECTORY_SEPARATOR.$resourceId;
  205.                     if (file_exists($resourceId=== false{
  206.                         return null;
  207.                     }
  208.                 }
  209.             else {
  210.                 return null;
  211.             }
  212.         }
  213.  
  214.         if ($policy $dwoo->getSecurityPolicy()) {
  215.             while (true{
  216.                 if (preg_match('{^([a-z]+?)://}i'$resourceId)) {
  217.                     throw new Dwoo_Security_Exception('The security policy prevents you to read files from external sources : <em>'.$resourceId.'</em>.');
  218.                 }
  219.  
  220.                 if ($includePath{
  221.                     break;
  222.                 }
  223.  
  224.                 $resourceId realpath($resourceId);
  225.                 $dirs $policy->getAllowedDirectories();
  226.                 foreach ($dirs as $dir=>$dummy{
  227.                     if (strpos($resourceId$dir=== 0{
  228.                         break 2;
  229.                     }
  230.                 }
  231.                 throw new Dwoo_Security_Exception('The security policy prevents you to read <em>'.$resourceId.'</em>');
  232.             }
  233.         }
  234.  
  235.         $class 'Dwoo_Template_File';
  236.         if ($parentTemplate{
  237.             $class get_class($parentTemplate);
  238.         }
  239.         return new $class($resourceId$cacheTime$cacheId$compileId$includePath);
  240.     }
  241.  
  242.     /**
  243.      * returns the full compiled file name and assigns a default value to it if
  244.      * required
  245.      *
  246.      * @param Dwoo $dwoo the dwoo instance that requests the file name
  247.      * @return string the full path to the compiled file
  248.      */
  249.     protected function getCompiledFilename(Dwoo $dwoo)
  250.     {
  251.         // no compile id was provided, set default
  252.         if ($this->compileId===null{
  253.             $this->compileId = str_replace('../''__'strtr($this->getResourceIdentifier()'\\:''/-'));
  254.         }
  255.         return $dwoo->getCompileDir($this->compileId.'.d'.Dwoo::RELEASE_TAG.'.php';
  256.     }
  257.  
  258.     /**
  259.      * returns some php code that will check if this template has been modified or not
  260.      *
  261.      * if the function returns null, the template will be instanciated and then the Uid checked
  262.      *
  263.      * @return string 
  264.      */
  265.     public function getIsModifiedCode()
  266.     {
  267.         return '"'.$this->getUid().'" == filemtime('.var_export($this->getResourceIdentifier()true).')';
  268.     }
  269. }

Documentation generated on Sat, 18 Jul 2009 21:05:01 +0200 by phpDocumentor 1.4.0