OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / include / lib / patTemplate / patTemplate / Reader / File.php
1 <?PHP
2 /**
3  * patTemplate Reader that reads from a file
4  *
5  * $Id: File.php 440 2008-03-30 09:00:16Z fishbone $
6  *
7  * @package             patTemplate
8  * @subpackage  Readers
9  * @author              Stephan Schmidt <schst@php.net>
10  */
11  
12 /**
13  * patTemplate Reader that reads from a file
14  *
15  * $Id: File.php 440 2008-03-30 09:00:16Z fishbone $
16  *
17  * @package             patTemplate
18  * @subpackage  Readers
19  * @author              Stephan Schmidt <schst@php.net>
20  */
21 class patTemplate_Reader_File extends patTemplate_Reader
22 {
23    /**
24     * reader name
25         * @access       private
26         * @var          string
27         */
28         var     $_name  =       'File';
29
30    /**
31         * flag to indicate, that current file is remote
32         *
33         * @access       private
34         * @var          boolean
35         */
36         var $_isRemote = false;
37         
38    /**
39         * all files, that have been opened
40         *
41         * @access       private
42         * @var          array
43         */
44         var $_files = array();
45         
46    /**
47     * read templates from any input 
48         *
49         * @final
50         * @access       public
51         * @param        string  file to parse
52         * @return       array   templates
53         */
54         function readTemplates( $input )
55         {
56                 if (isset($this->_rootAtts['relative'])) {
57                         $relative = $this->_rootAtts['relative'];
58                 } else {
59                         $relative = false;
60                 }
61                 if ($relative === false) {
62                 $this->_currentInput = $input;
63                 } else {
64                         $this->_currentInput = dirname($relative) . DIRECTORY_SEPARATOR . $input;
65                 }
66                 
67                 $fullPath = $this->_resolveFullPath($input, $relative);
68                 if (patErrorManager::isError($fullPath)) {
69                         return $fullPath;
70                 }
71                 $content = $this->_getFileContents($fullPath);
72                 if (patErrorManager::isError($content)) {
73                         return $content;
74                 }
75
76                 $templates = $this->parseString($content);
77                 
78                 return  $templates;
79         }
80
81    /**
82     * load template from any input 
83     *
84     * If the a template is loaded, the content will not get
85     * analyzed but the whole content is returned as a string.
86         *
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
91         */
92         function loadTemplate( $input )
93         {
94                 if (isset($this->_rootAtts['relative'])) {
95                         $relative = $this->_rootAtts['relative'];
96                 } else {
97                         $relative = false;
98                 }
99                 $fullPath       =       $this->_resolveFullPath( $input, $relative );
100                 if( patErrorManager::isError( $fullPath ) )
101                         return $fullPath;
102                 return $this->_getFileContents( $fullPath );
103         }
104
105    /**
106         * resolve path for a template
107         *
108         * @access       private
109         * @param        string              filename
110         * @param    boolean|string  filename for relative path calculation
111         * @return       string              full path
112         */      
113         function _resolveFullPath( $filename, $relativeTo = false )
114         {
115                 if (preg_match( '/^[a-z]+:\/\//', $filename )) {
116                         $this->_isRemote = true;
117                         return $filename;
118                 } else {
119                     $rootFolders = $this->getTemplateRoot();
120                     if (!is_array($rootFolders)) {
121                 $rootFolders = array($rootFolders);
122                     }
123                     foreach ($rootFolders as $root) {
124                     if ($relativeTo === false) {
125                                 $baseDir = $root;
126                     } else {
127                     $baseDir = $root . DIRECTORY_SEPARATOR . dirname($relativeTo);
128                     }
129                         $fullPath = $baseDir . DIRECTORY_SEPARATOR . $filename;
130                         if (file_exists($fullPath)) {
131                                 return $fullPath;
132                         }
133                     }
134                 }
135                 return patErrorManager::raiseError(
136                                                                         PATTEMPLATE_READER_ERROR_NO_INPUT,
137                                                                         "Could not load templates from $filename."
138                                                                         );
139         }
140
141    /**
142         * get the contents of a file
143         *
144         * @access       private
145         * @param        string          filename
146         * @return       string          file contents
147         */      
148         function _getFileContents( $file )
149         {
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."
154                                                                                 );
155                 }
156
157                 if (function_exists('file_get_contents')) {
158                         $content = @file_get_contents( $file );
159                 } else {
160                         $content = implode('', file($file));
161                 }
162
163                 /**
164                  * store the file name
165                  */
166                 array_push($this->_files, $file);
167
168                 return $content;
169         }
170 }
171 ?>