3 * patTemplate Reader that reads from a file
5 * $Id: File.php 440 2008-03-30 09:00:16Z fishbone $
9 * @author Stephan Schmidt <schst@php.net>
13 * patTemplate Reader that reads from a file
15 * $Id: File.php 440 2008-03-30 09:00:16Z fishbone $
17 * @package patTemplate
19 * @author Stephan Schmidt <schst@php.net>
21 class patTemplate_Reader_File extends patTemplate_Reader
31 * flag to indicate, that current file is remote
36 var $_isRemote = false;
39 * all files, that have been opened
44 var $_files = array();
47 * read templates from any input
51 * @param string file to parse
52 * @return array templates
54 function readTemplates( $input )
56 if (isset($this->_rootAtts['relative'])) {
57 $relative = $this->_rootAtts['relative'];
61 if ($relative === false) {
62 $this->_currentInput = $input;
64 $this->_currentInput = dirname($relative) . DIRECTORY_SEPARATOR . $input;
67 $fullPath = $this->_resolveFullPath($input, $relative);
68 if (patErrorManager::isError($fullPath)) {
71 $content = $this->_getFileContents($fullPath);
72 if (patErrorManager::isError($content)) {
76 $templates = $this->parseString($content);
82 * load template from any input
84 * If the a template is loaded, the content will not get
85 * analyzed but the whole content is returned as a string.
87 * @abstract must be implemented in the template readers
88 * @param mixed input to load from.
89 * This can be a string, a filename, a resource or whatever the derived class needs to read from
90 * @return string template content
92 function loadTemplate( $input )
94 if (isset($this->_rootAtts['relative'])) {
95 $relative = $this->_rootAtts['relative'];
99 $fullPath = $this->_resolveFullPath( $input, $relative );
100 if( patErrorManager::isError( $fullPath ) )
102 return $this->_getFileContents( $fullPath );
106 * resolve path for a template
109 * @param string filename
110 * @param boolean|string filename for relative path calculation
111 * @return string full path
113 function _resolveFullPath( $filename, $relativeTo = false )
115 if (preg_match( '/^[a-z]+:\/\//', $filename )) {
116 $this->_isRemote = true;
119 $rootFolders = $this->getTemplateRoot();
120 if (!is_array($rootFolders)) {
121 $rootFolders = array($rootFolders);
123 foreach ($rootFolders as $root) {
124 if ($relativeTo === false) {
127 $baseDir = $root . DIRECTORY_SEPARATOR . dirname($relativeTo);
129 $fullPath = $baseDir . DIRECTORY_SEPARATOR . $filename;
130 if (file_exists($fullPath)) {
135 return patErrorManager::raiseError(
136 PATTEMPLATE_READER_ERROR_NO_INPUT,
137 "Could not load templates from $filename."
142 * get the contents of a file
145 * @param string filename
146 * @return string file contents
148 function _getFileContents( $file )
150 if (!$this->_isRemote && (!file_exists($file) || !is_readable($file))) {
151 return patErrorManager::raiseError(
152 PATTEMPLATE_READER_ERROR_NO_INPUT,
153 "Could not load templates from $file."
157 if (function_exists('file_get_contents')) {
158 $content = @file_get_contents( $file );
160 $content = implode('', file($file));
164 * store the file name
166 array_push($this->_files, $file);