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 <[email protected]>
  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.      * include path(s) to look into to find this template
  32.      *
  33.      * @var array 
  34.      */
  35.     protected $includePath = null;
  36.  
  37.     /**
  38.      * resolved path cache when looking for a file in multiple include paths
  39.      *
  40.      * this is reset when the include path is changed
  41.      *
  42.      * @var string 
  43.      */
  44.     protected $resolvedPath = null;
  45.  
  46.     /**
  47.      * creates a template from a file
  48.      *
  49.      * @param string $file the path to the template file, make sure it exists
  50.      * @param int $cacheTime duration of the cache validity for this template,
  51.      *                           if null it defaults to the Dwoo instance that will
  52.      *                           render this template
  53.      * @param string $cacheId the unique cache identifier of this page or anything else that
  54.      *                            makes this template's content unique, if null it defaults
  55.      *                            to the current url
  56.      * @param string $compileId the unique compiled identifier, which is used to distinguish this
  57.      *                              template from others, if null it defaults to the filename+bits of the path
  58.      * @param mixed $includePath a string for a single path to look into for the given file, or an array of paths
  59.      */
  60.     public function __construct($file$cacheTime null$cacheId null$compileId null$includePath null)
  61.     {
  62.         $this->file = $file;
  63.         $this->name = basename($file);
  64.         $this->cacheTime = $cacheTime;
  65.  
  66.         if ($compileId !== null{
  67.             $this->compileId = strtr($compileId'\\%?=!:;'.PATH_SEPARATOR'/-------');
  68.         }
  69.  
  70.         if ($cacheId !== null{
  71.             $this->cacheId = strtr($cacheId'\\%?=!:;'.PATH_SEPARATOR'/-------');
  72.         }
  73.  
  74.         if (is_string($includePath)) {
  75.             $this->includePath = array($includePath);
  76.         elseif (is_array($includePath)) {
  77.             $this->includePath = $includePath;
  78.         }
  79.     }
  80.  
  81.     /**
  82.      * sets the include path(s) to where the given template filename must be looked up
  83.      *
  84.      * @param mixed $paths the path to look into, can be string for a single path or an array of paths
  85.      */
  86.     public function setIncludePath($paths)
  87.     {
  88.         if (is_array($paths=== false{
  89.             $paths array($paths);
  90.         }
  91.  
  92.         $this->includePath = $paths;
  93.         $this->resolvedPath = null;
  94.     }
  95.  
  96.     /**
  97.      * return the current include path(s)
  98.      *
  99.      * @return array 
  100.      */
  101.     public function getIncludePath()
  102.     {
  103.         return $this->includePath;
  104.     }
  105.  
  106.     /**
  107.      * returns the compiled template file name
  108.      *
  109.      * @param Dwoo $dwoo the dwoo instance that requests it
  110.      * @param Dwoo_ICompiler $compiler the compiler that must be used
  111.      * @return string 
  112.      */
  113.     public function getCompiledTemplate(Dwoo $dwooDwoo_ICompiler $compiler null)
  114.     {
  115.         $compiledFile $this->getCompiledFilename($dwoo);
  116.  
  117.         if ($this->compilationEnforced !== true && isset(self::$cache['compiled'][$this->compileId]=== true{
  118.             // already checked, return compiled file
  119.         elseif ($this->compilationEnforced !== true && file_exists($compiledFile)===true && (int)$this->getUid(<= filemtime($compiledFile)) {
  120.             // template is compiled and has not been modified since the compilation
  121.             self::$cache['compiled'][$this->compileIdtrue;
  122.         else {
  123.             // compiles the template
  124.             $this->compilationEnforced = false;
  125.  
  126.             if ($compiler === null{
  127.                 $compiler $dwoo->getDefaultCompilerFactory('string');
  128.  
  129.                 if ($compiler === null || $compiler === array('Dwoo_Compiler''compilerFactory')) {
  130.                     if (class_exists('Dwoo_Compiler'false=== false{
  131.                         include 'Dwoo/Compiler.php';
  132.                     }
  133.                     $compiler Dwoo_Compiler::compilerFactory();
  134.                 else {
  135.                     $compiler call_user_func($compiler);
  136.                 }
  137.             }
  138.  
  139.             $this->compiler = $compiler;
  140.  
  141.             $compiler->setCustomPlugins($dwoo->getCustomPlugins());
  142.             $compiler->setSecurityPolicy($dwoo->getSecurityPolicy());
  143.             $this->makeDirectory(dirname($compiledFile));
  144.             file_put_contents($compiledFile$compiler->compile($dwoo$this));
  145.             chmod($compiledFileDWOO_CHMOD);
  146.  
  147.             self::$cache['compiled'][$this->compileIdtrue;
  148.         }
  149.  
  150.         return $compiledFile;
  151.     }
  152.  
  153.     /**
  154.      * returns the template source of this template
  155.      *
  156.      * @return string 
  157.      */
  158.     public function getSource()
  159.     {
  160.         return file_get_contents($this->getResourceIdentifier());
  161.     }
  162.  
  163.     /**
  164.      * returns the resource name for this template class
  165.      *
  166.      * @return string 
  167.      */
  168.     public function getResourceName()
  169.     {
  170.         return 'file';
  171.     }
  172.  
  173.     /**
  174.      * returns this template's source filename
  175.      *
  176.      * @return string 
  177.      */
  178.     public function getResourceIdentifier()
  179.     {
  180.         if ($this->resolvedPath !== null{
  181.             return $this->resolvedPath;
  182.         elseif ($this->includePath === null{
  183.             return $this->file;
  184.         else {
  185.             foreach ($this->includePath as $path{
  186.                 if (file_exists($path.DIRECTORY_SEPARATOR.$this->file=== true{
  187.                     $this->resolvedPath = $path DIRECTORY_SEPARATOR $this->file;
  188.                     return $this->resolvedPath;
  189.                 }
  190.             }
  191.  
  192.             throw new Dwoo_Exception('Template "'.$this->file.'" could not be found in any of your include path(s)');
  193.         }
  194.     }
  195.  
  196.     /**
  197.      * returns an unique value identifying the current version of this template,
  198.      * in this case it's the unix timestamp of the last modification
  199.      *
  200.      * @return string 
  201.      */
  202.     public function getUid()
  203.     {
  204.         return (string) filemtime($this->getResourceIdentifier());
  205.     }
  206.  
  207.     /**
  208.      * returns a new template object from the given include name, null if no include is
  209.      * possible (resource not found), or false if include is not permitted by this resource type
  210.      *
  211.      * @param Dwoo $dwoo the dwoo instance requiring it
  212.      * @param mixed $resourceId the filename (relative to this template's dir) of the template to include
  213.      * @param int $cacheTime duration of the cache validity for this template,
  214.      *                           if null it defaults to the Dwoo instance that will
  215.      *                           render this template
  216.      * @param string $cacheId the unique cache identifier of this page or anything else that
  217.      *                            makes this template's content unique, if null it defaults
  218.      *                            to the current url
  219.      * @param string $compileId the unique compiled identifier, which is used to distinguish this
  220.      *                              template from others, if null it defaults to the filename+bits of the path
  221.      * @return Dwoo_Template_File|null
  222.      */
  223.     public static function templateFactory(Dwoo $dwoo$resourceId$cacheTime null$cacheId null$compileId null)
  224.     {
  225.         $resourceId str_replace(array("\t""\n""\r")array('\\t''\\n''\\r')$resourceId);
  226.         if (file_exists($resourceId=== false{
  227.             $tpl $dwoo->getTemplate();
  228.             if ($tpl instanceof Dwoo_Template_File{
  229.                 $resourceId dirname($tpl->getResourceIdentifier()).DIRECTORY_SEPARATOR.$resourceId;
  230.                 if (file_exists($resourceId=== false{
  231.                     return null;
  232.                 }
  233.             else {
  234.                 return null;
  235.             }
  236.         }
  237.  
  238.         // prevent template recursion if security is in effect
  239.         if ($policy $dwoo->getSecurityPolicy()) {
  240.             $tpl $dwoo->getTemplate();
  241.             if ($tpl instanceof Dwoo_Template_File && $resourceId === $tpl->getResourceIdentifier()) {
  242.                 return $dwoo->triggerError('You can not include a template into itself'E_USER_WARNING);
  243.             }
  244.         }
  245.  
  246.         return new Dwoo_Template_File($resourceId$cacheTime$cacheId$compileId);
  247.     }
  248.  
  249.     /**
  250.      * returns the full compiled file name and assigns a default value to it if
  251.      * required
  252.      *
  253.      * @param Dwoo $dwoo the dwoo instance that requests the file name
  254.      * @return string the full path to the compiled file
  255.      */
  256.     protected function getCompiledFilename(Dwoo $dwoo)
  257.     {
  258.         // no compile id was provided, set default
  259.         if ($this->compileId===null{
  260.             $this->compileId = strtr($this->getResourceIdentifier()'\\:''/-');
  261.         }
  262.         return $dwoo->getCompileDir($this->compileId.'.d'.Dwoo::RELEASE_TAG.'.php';
  263.     }
  264. }

Documentation generated on Sun, 03 Aug 2008 15:12:35 +0200 by phpDocumentor 1.4.0