Hi Kornel (and everyone else)

Herewith a patch for PHPTAL configuration methods. It adds very little new functionality - the main aim of these new methods is to simplify PHPTAL's integation into third party frameworks like Zend Framework, Symfony, etc. The secondary aim was 100% backward compatibility - no one need to change their existing code if the patch is applied.


I continually run into problems using defines for configuration options, without being able to use methods for configuration. The new methods solve most all of the problems I have encountered during third party framework integration.

Additionally, the patch enables a fluent interface for many access methods.

When the patch is applied, is will be possible to do things (the way I prefer) like this:

$t = PHPTAL::create('index.html')
   ->setPhpCodeDestination('application/tmp/')
   ->setPhpCodeExtension('tmp')
   ->setForceReparse(true)
   ->set('title', 'My Title')
   ->set('heading', 'My Heading');

try {
   echo $t->execute();
} catch (Exception $e) {
   echo $e;
}


Kind Regards,
Werner
Index: PHPTAL.php
===================================================================
--- PHPTAL.php  (revision 338)
+++ PHPTAL.php  (working copy)
@@ -1,24 +1,24 @@
 <?php
 /* vim: set expandtab tabstop=4 shiftwidth=4: */
-//  
+//
 //  Copyright (c) 2004-2005 Laurent Bedubourg
-//  
+//
 //  This library is free software; you can redistribute it and/or
 //  modify it under the terms of the GNU Lesser General Public
 //  License as published by the Free Software Foundation; either
 //  version 2.1 of the License, or (at your option) any later version.
-//  
+//
 //  This library is distributed in the hope that it will be useful,
 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 //  Lesser General Public License for more details.
-//  
+//
 //  You should have received a copy of the GNU Lesser General Public
 //  License along with this library; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-//  
+//
 //  Authors: Laurent Bedubourg <[EMAIL PROTECTED]>
-//  
+//
 
 define('PHPTAL_VERSION', '1_1_12');
 
@@ -70,7 +70,7 @@
 
 /**
  * PHPTAL template entry point.
- * 
+ *
  * <code>
  * <?php
  * require_once 'PHPTAL.php';
@@ -86,14 +86,14 @@
  * }
  * ?>
  * </code>
- * 
+ *
  * @author Laurent Bedubourg <[EMAIL PROTECTED]>
  */
-class PHPTAL 
+class PHPTAL
 {
     const XHTML = 1;
     const XML   = 2;
-    
+
     /**
      * PHPTAL Constructor.
      *
@@ -113,6 +113,18 @@
     }
 
     /**
+     * create
+     * returns a new PHPTAL object
+     *
+     * @param string $path Template file path.
+     * @return PHPTAL
+     */
+    public static function create($path=false)
+    {
+        return new PHPTAL($path);
+    }
+
+    /**
      * Clone template state and context.
      */
     public function __clone()
@@ -134,6 +146,7 @@
         $this->_functionName = null;
         $this->_path = $path;
         $this->_source = null;
+        return $this;
     }
 
     /**
@@ -149,12 +162,13 @@
     {
         if ($path == false)
             $path = '<string> '.md5($src);
-        
+
         require_once PHPTAL_DIR.'PHPTAL/StringSource.php';
         $this->_source = new PHPTAL_StringSource($src, $path);
         $this->_path = $path;
+        return $this;
     }
-    
+
     /**
      * Specify where to look for templates.
      *
@@ -168,19 +182,38 @@
         else {
             $this->_repositories[] = $rep;
         }
+        return $this;
     }
 
     /**
+     * Get template repositories.
+     */
+    public function getTemplateRepositories()
+    {
+        return $this->_repositories;
+    }
+
+    /**
+     * Clears the template repositories.
+     */
+    public function clearTemplateRepositories()
+    {
+        $this->_repositories = array();
+        return $this;
+    }
+
+    /**
      * Ignore XML/XHTML comments on parsing.
      * @param $bool bool
      */
     public function stripComments($bool)
     {
         $this->_stripComments = $bool;
+        return $this;
     }
-    
+
     /**
-     * Set output mode 
+     * Set output mode
      * @param $mode int (PHPTAL::XML or PHPTAL::XHTML).
      */
     public function setOutputMode($mode=PHPTAL_XHTML)
@@ -189,23 +222,104 @@
             throw new PHPTAL_Exception('Unsupported output mode '.$mode);
         }
         $this->_outputMode = $mode;
+        return $this;
     }
 
     /**
+     * Get output mode
+     */
+    public function getOutputMode()
+    {
+        return $this->_outputMode;
+    }
+
+    /**
      * Set ouput encoding.
      * @param $enc string example: 'UTF-8'
      */
     public function setEncoding($enc)
     {
-        $this->_encoding = $enc; 
+        $this->_encoding = $enc;
+        return $this;
     }
 
     /**
+     * Get ouput encoding.
+     * @param $enc string example: 'UTF-8'
+     */
+    public function getEncoding($enc)
+    {
+        return $this->_encoding;
+    }
+
+    /**
+     * Set the storage location for intermediate PHP files.
+     * @param string $path Intermediate file path.
+     */
+
+    public function setPhpCodeDestination($path)
+    {
+        $this->_phpCodeDestination = $path;
+        return $this;
+    }
+
+    /**
+     * Get the storage location for intermediate PHP files.
+     */
+
+    public function getPhpCodeDestination()
+    {
+        return $this->_phpCodeDestination;
+    }
+
+    /**
+     * Set the file extension for intermediate PHP files.
+     * @param string $extension The file extension.
+     */
+
+    public function setPhpCodeExtension($extension)
+    {
+        $this->_phpCodeExtension = $extension;
+        return $this;
+    }
+
+    /**
+     * Get the file extension for intermediate PHP files.
+     */
+
+    public function getPhpCodeExtension()
+    {
+        return $this->_phpCodeExtension;
+    }
+
+    /**
+     * Flags whether to ignore intermediate php files and to
+     * reparse templates every time (if set to true).
+     * @param bool bool Forced reparse state.
+     */
+
+    public function setForceReparse($bool)
+    {
+        $this->_forceReparse = (bool) $bool;
+        return $this;
+    }
+
+    /**
+     * Get the value of the force reparse state.
+     */
+
+    public function getForceReparse()
+    {
+        return $this->_forceReparse;
+    }
+
+    /**
      * Set I18N translator.
      */
     public function setTranslator(PHPTAL_TranslationService $t)
     {
         $this->_translator = $t;
+        return $this;
     }
 
     /**
@@ -214,6 +328,7 @@
     public function setPreFilter(PHPTAL_Filter $filter)
     {
         $this->_prefilter = $filter;
+        return $this;
     }
 
     /**
@@ -222,6 +337,7 @@
     public function setPostFilter(PHPTAL_Filter $filter)
     {
         $this->_postfilter = $filter;
+        return $this;
     }
 
     /**
@@ -231,6 +347,7 @@
     public function addTrigger($id, PHPTAL_Trigger $trigger)
     {
         $this->_triggers[$id] = $trigger;
+        return $this;
     }
 
     /**
@@ -263,24 +380,25 @@
     public function set($varname, $value)
     {
         $this->_context->__set($varname, $value);
+        return $this;
     }
-    
+
     /**
      * Execute the template code.
      *
      * @return string
      */
-    public function execute() 
+    public function execute()
     {
         if (!$this->_prepared) {
             $this->prepare();
         }
-       
+
         // includes generated template PHP code
         $this->_context->__file = $this->__file;
         require_once $this->_codeFile;
         $templateFunction = $this->_functionName;
-        try {            
+        try {
             ob_start();
             $templateFunction($this, $this->_context);
             $res = ob_get_clean();
@@ -300,7 +418,7 @@
         if ($xmlDec){
             $res = $xmlDec . "\n" . $res;
         }
-        
+
         if ($this->_postfilter != null){
             return $this->_postfilter->filter($res);
         }
@@ -318,7 +436,7 @@
         if (preg_match('/^(.*?)\/([a-z0-9_]*?)$/i', $path, $m)){
             list(,$file,$macroName) = $m;
 
-            // TODO: stores a list of already prepared macro to avoid this 
+            // TODO: stores a list of already prepared macro to avoid this
             // preparation on each call
             $tpl = new PHPTAL($file);
             $tpl->_encoding = $this->_encoding;
@@ -330,15 +448,15 @@
             $tpl->prepare();
 
             // save current file
-            $currentFile = $this->_context->__file;            
+            $currentFile = $this->_context->__file;
             $this->_context->__file = $tpl->__file;
-            
+
             // require PHP generated code and execute macro function
             require_once $tpl->getCodePath();
             $fun = $tpl->getFunctionName() . '_' . $macroName;
             if (!function_exists($fun)) throw new PHPTAL_Exception("Macro 
'$macroName' is not defined in $file",$this->_source->getRealPath());
             $fun($this, $this->_context);
-            
+
             // restore current file
             $this->_context->__file = $currentFile;
         }
@@ -346,14 +464,15 @@
             // call local macro
             $fun = $this->getFunctionName() . '_' . trim($path);
             if (!function_exists($fun)) throw new PHPTAL_Exception("Macro 
'$macroName' is not defined",$this->_source->getRealPath());
-            $fun( $this, $this->_context );            
+            $fun( $this, $this->_context );
         }
     }
 
        private function setCodeFile()
        {
                // where php generated code should reside
-        $this->_codeFile = PHPTAL_PHP_CODE_DESTINATION . 
$this->getFunctionName() . '.' . PHPTAL_PHP_CODE_EXTENSION;
+        $this->_codeFile = $this->getPhpCodeDestination() . 
$this->getFunctionName() . '.' . $this->getPhpCodeExtension();
+        return $this;
        }
 
     /**
@@ -368,14 +487,16 @@
         // parse template if php generated code does not exists or template
         // source file modified since last generation of PHPTAL_FORCE_REPARSE
         // is defined.
-        if (defined('PHPTAL_FORCE_REPARSE') 
-            || !file_exists($this->_codeFile) 
+        if (defined('PHPTAL_FORCE_REPARSE')
+            || $this->getForceReparse()
+            || !file_exists($this->_codeFile)
             || filemtime($this->_codeFile) < 
$this->_source->getLastModifiedTime())
                {
                        $this->cleanUpCache();
             $this->parse();
         }
         $this->_prepared = true;
+        return $this;
     }
 
        /**
@@ -384,7 +505,7 @@
         */
        public function cleanUpCache()
        {
-               if (!$this->_codeFile) 
+               if (!$this->_codeFile)
                {
                        $this->findTemplate(); $this->setCodeFile();
                        if (!$this->_codeFile) throw new PHPTAL_Exception("No 
codefile");
@@ -395,6 +516,7 @@
                        if (substr($file, 0, strlen($this->_codeFile)) !== 
$this->_codeFile) continue; // safety net
                        unlink($file);
                }
+        return $this;
        }
        }
 
@@ -403,7 +525,7 @@
      *
      * The returned file may be used to cleanup (unlink) temporary files
      * generated by temporary templates or more simply for debug.
-     * 
+     *
      * @return string
      */
     public function getCodePath()
@@ -431,7 +553,7 @@
     {
         return $this->_translator;
     }
-    
+
     /**
      * Returns array of exceptions catched by tal:on-error attribute.
      * @return array<Exception>
@@ -440,14 +562,15 @@
     {
         return $this->_errors;
     }
-    
+
     /**
      * Public for phptal templates, private for user.
      * @access private
      */
     public function addError(Exception $error)
     {
-        array_push($this->_errors, $error); 
+        array_push($this->_errors, $error);
+        return $this;
     }
 
     /**
@@ -475,18 +598,18 @@
         $this->_context = $this->_context->popContext();
         return $this->_context;
     }
-    
+
     protected function parse()
     {
         require_once PHPTAL_DIR.'PHPTAL/Dom/Parser.php';
-        
-        // instantiate the PHPTAL source parser 
+
+        // instantiate the PHPTAL source parser
         $parser = new PHPTAL_Dom_Parser();
         $parser->stripComments($this->_stripComments);
 
         $data = $this->_source->getData();
         $realpath = $this->_source->getRealPath();
-        
+
         if ($this->_prefilter)
             $data = $this->_prefilter->filter($data);
         $tree = $parser->parseString($data, $realpath);
@@ -500,10 +623,12 @@
         if ([EMAIL PROTECTED]($this->_codeFile, $generator->getResult())) {
             throw new PHPTAL_Exception('Unable to open '.$this->_codeFile.' 
for writing');
         }
+
+        return $this;
     }
 
-    /** 
-     * Search template source location. 
+    /**
+     * Search template source location.
      */
     protected function findTemplate()
     {
@@ -515,7 +640,7 @@
         if ($this->_source != null){
             return;
         }
-       
+
         array_push($this->_resolvers, new 
PHPTAL_FileSourceResolver($this->_repositories));
         foreach ($this->_resolvers as $resolver){
             $source = $resolver->resolve($this->_path);
@@ -548,7 +673,7 @@
     protected $_functionName = null;
     // set to true when template is ready for execution
     protected $_prepared = false;
-    
+
     // associative array of phptal:id => PHPTAL_Trigger
     protected $_triggers = array();
     // i18n translator
@@ -563,9 +688,15 @@
     // list of on-error caught exceptions
     protected $_errors = array();
 
-    protected $_encoding = PHPTAL_DEFAULT_ENCODING; 
+    protected $_encoding = PHPTAL_DEFAULT_ENCODING;
     protected $_outputMode = PHPTAL::XHTML;
     protected $_stripComments = false;
+
+    // configuration properties
+    protected $_forceReparse = false;
+    protected $_phpCodeDestination = PHPTAL_PHP_CODE_DESTINATION;
+    protected $_phpCodeExtension = PHPTAL_PHP_CODE_EXTENSION;
+
 }
 
 ?>
_______________________________________________
PHPTAL mailing list
PHPTAL@lists.motion-twin.com
http://lists.motion-twin.com/mailman/listinfo/phptal

Reply via email to