Slimore
  • Namespace
  • Class

Namespaces

  • None
  • Slimore
    • Cache
      • Exception
    • Captcha
    • Database
    • Debug
    • Http
    • Image
    • Log
    • Middleware
    • Mvc
    • Pagination
    • Upload

Classes

  • Slimore\Cache\File
  • Slimore\Captcha\Builder
  • Slimore\Database\Manager
  • Slimore\Debug\Simpler
  • Slimore\Http\Client
  • Slimore\Image\Gd
  • Slimore\Log\Writer
  • Slimore\Middleware\Exceptions
  • Slimore\Mvc\Application
  • Slimore\Mvc\Controller
  • Slimore\Mvc\Model
  • Slimore\Mvc\View
  • Slimore\Pagination\Paginator
  • Slimore\Upload\Uploader

Exceptions

  • Slimore\Cache\Exception\File
  • Slimore\Captcha\Exception
  • Slimore\Database\Exception
  • Slimore\Http\Exception
  • Slimore\Mvc\Exception
  • Slimore\Pagination\Exception
  • Slimore\Upload\Exception

Functions

  • arrayToObject
  • console
  • controller
  • ctl
  • currentUrl
  • decrypt
  • detectDevice
  • encrypt
  • fileFormatSize
  • getDirectoryItems
  • getDpi
  • hexToRGB
  • html
  • imageDataUrl
  • iPad
  • iPhone
  • iPod
  • isAndroid
  • isApache
  • isBlackberry
  • isChrome
  • isCli
  • isFirefox
  • isFreeBSD
  • isIE
  • isIIS
  • isJson
  • isLinux
  • isMacOSX
  • isMobile
  • isNginx
  • isOpera
  • isSafari
  • isTablet
  • isUnix
  • isUnixLike
  • isWebOS
  • isWindows
  • js
  • jsonToArray
  • linkTag
  • password
  • phoneMaskCode
  • randomCharacters
  • replaceDirSeparator
  • rgbToHex
  • script
  • shortUrl
  • style
  1 <?php
  2 
  3 /**
  4  * Slimore - The fully (H)MVC framework based on the Slim PHP framework.
  5  *
  6  * @author      Pandao <slimore@ipandao.com>
  7  * @copyright   2015 Pandao
  8  * @link        http://github.com/slimore/slimore
  9  * @license     MIT License https://github.com/slimore/slimore#license
 10  * @version     0.1.0
 11  * @package     Slimore\Mvc
 12  */
 13 
 14 namespace Slimore\Mvc;
 15 
 16 /**
 17  * Class View
 18  *
 19  * @author Pandao
 20  * @package Slimore\Mvc
 21  */
 22 
 23 class View extends \Slim\View
 24 {
 25     /**
 26      * @var string
 27      */
 28     public    $compileFileSuffix = '.php';
 29 
 30     /**
 31      * @var bool
 32      */
 33     public    $compileFileNameMd5 = true;
 34 
 35     /**
 36      * @var string
 37      */
 38     public    $compileDirectoryName = '.compiles';
 39 
 40     /**
 41      * @var string
 42      */
 43     public    $cacheDirectoryName = '.caches';
 44 
 45     /**
 46      * @var string
 47      */
 48     protected $templateName;
 49 
 50     /**
 51      * @var string
 52      */
 53     protected $templateFile;
 54 
 55     /**
 56      * @var string
 57      */
 58     protected $compilePath;
 59 
 60     /**
 61      * @var bool
 62      */
 63     public $compileCached = true;
 64 
 65     /**
 66      * @var string
 67      */
 68     protected $templateCompileFile;
 69 
 70     /**
 71      * Global vars for insert view
 72      *
 73      * @var array
 74      */
 75     protected static $globals    = [];
 76 
 77     /**
 78      * @var array
 79      */
 80     protected static $properties = [];
 81 
 82     /**
 83      * Set/Create template compile directory
 84      *
 85      * @return void
 86      */
 87 
 88     public function setCompileDirectory()
 89     {
 90         $compilePath = $this->getTemplatesDirectory() . DIRECTORY_SEPARATOR . $this->compileDirectoryName . DIRECTORY_SEPARATOR;
 91 
 92         if (!file_exists($compilePath))
 93         {
 94             static::mkdir($compilePath);
 95         }
 96 
 97         $this->compilePath = $compilePath;
 98     }
 99 
100     /**
101      * Set template compile file path
102      *
103      * @param string $filename
104      * @return void
105      */
106 
107     public function setCompileFile($filename)
108     {
109         $filename = (($this->compileFileNameMd5) ? md5(md5($filename)) : $filename);
110         $this->templateCompileFile = $this->compilePath . $filename . $this->compileFileSuffix;
111     }
112 
113     public static function getProperties(array $array)
114     {
115         return static::$properties = $array;
116     }
117 
118     /**
119      * Template render
120      *
121      * @param string $template
122      * @param array $data
123      * @return mixed
124      */
125 
126     public function render($template, $data = null)
127     {
128         $this->templateName = $template;
129         $this->templateFile = $this->getTemplatePathname($template);
130 
131         if (!is_file($this->templateFile))
132         {
133             throw new \RuntimeException("Slimore\\Mvc\\View cannot render `$template` because the template does not exist");
134         }
135 
136         $this->setCompileDirectory();
137         $this->setCompileFile($template);
138 
139         ob_start();
140 
141         $data = array_merge($this->data->all(), (array) $data);
142         $data = array_merge(static::$globals, $data);
143 
144         static::getProperties([
145             'data'               => $data,
146             'compileCached'      => $this->compileCached,
147             'templatePath'       => $this->getTemplatesDirectory() . DIRECTORY_SEPARATOR,
148             'compilePath'        => $this->compilePath,
149             'compileFileNameMd5' => $this->compileFileNameMd5,
150             'compileFileSuffix'  => $this->compileFileSuffix
151         ]);
152 
153         extract($data);
154 
155         //echo "this->compileCached =>" . ($this->compileCached ? 'true' : 'false');
156 
157         if (!$this->compileCached)
158         {
159             $tpl = $this->read($this->templateFile);
160             $tpl = static::parser($tpl);
161             $this->write($this->templateCompileFile, $tpl);
162         }
163         else
164         {
165             if (!file_exists($this->templateCompileFile) ||
166                 (filemtime($this->templateFile) > filemtime($this->templateCompileFile))
167             ) {
168                 $tpl = $this->read($this->templateFile);
169                 $tpl = static::parser($tpl);
170                 $this->write($this->templateCompileFile, $tpl);
171             }
172         }
173 
174         require $this->templateCompileFile;
175 
176         echo ob_get_clean();
177     }
178 
179     /**
180      * Create template compile directory
181      *
182      * @param string $dir
183      * @return string
184      */
185 
186     public static function mkdir($dir)
187     {
188         if ( file_exists($dir) )
189         {
190             return $dir;
191         }
192 
193         mkdir($dir, 0777, true);
194         chmod($dir, 0777);
195 
196         return $dir;
197     }
198 
199     /**
200      * Read template file
201      *
202      * @param string $filename
203      * @return string
204      */
205 
206     protected function read($filename)
207     {
208         return file_get_contents($filename);
209     }
210 
211     /**
212      * Write template compile file
213      *
214      * @param string $filename
215      * @param string $content
216      */
217 
218     protected function write($filename, $content)
219     {
220         file_put_contents($filename, $content);
221     }
222 
223     /**
224      * Parse & require included template file
225      *
226      * @param string $file
227      * @return string
228      */
229 
230     public static function includeFile($file)
231     {
232         $data               = static::$properties['data'];
233         $templatePath       = static::$properties['templatePath'];
234         $compilePath        = static::$properties['compilePath'];
235         $compileCached      = static::$properties['compileCached'];
236         $compileFileNameMd5 = static::$properties['compileFileNameMd5'];
237         $compileFileSuffix  = static::$properties['compileFileSuffix'];
238         $templateFile       = $templatePath . $file;
239 
240         if ( !file_exists($templateFile) )
241         {
242             throw new \InvalidArgumentException('included template file ' . $file . ' not found.');
243         }
244 
245         $pathInfo = pathinfo($file);
246         $filename = $pathInfo['filename'];
247 
248         if (!$compileFileNameMd5 && !file_exists($compilePath . $pathInfo['dirname']))
249         {
250             static::mkdir($compilePath . $pathInfo['dirname']);
251         }
252 
253         $filenameMd5 = (($compileFileNameMd5) ? md5(md5($filename)) : $pathInfo['dirname'] . DIRECTORY_SEPARATOR . $filename);
254         $compileFile = $compilePath . $filenameMd5 . $compileFileSuffix;
255 
256         if ($compileCached)
257         {
258             if (!file_exists($compileFile) || (filemtime($templateFile) > filemtime($compileFile)))
259             {
260                 $tpl = file_get_contents($templateFile);
261                 $tpl = static::parser($tpl);
262                 file_put_contents($compileFile, $tpl);
263             }
264         }
265         else
266         {
267             $tpl = file_get_contents($templateFile);
268             $tpl = static::parser($tpl);
269             file_put_contents($compileFile, $tpl);
270         }
271 
272         return $compileFile;
273     }
274 
275     /**
276      * Template parser
277      *
278      * @param string $tpl
279      * @return string
280      */
281 
282     public static function parser($tpl)
283     {
284         $viewClass = '\Slimore\Mvc\View::';
285 
286         // Parse for in
287         $tpl = preg_replace_callback('/\<\!\-\-\s*\{for\s+(\S+)\s+in\s+(\S+)\}\s*\-\-\>/is', function($matchs) {
288             //print_r($matchs);
289             $output = '<?php if (isset(' . $matchs[2] . ') && is_array(' . $matchs[2] . ')) { ?>';
290             $output .= "\r";
291             $output .= '<?php foreach (' . $matchs[2] . ' as $key => ' . $matchs[1] . ') { ?>';
292             $output .= "\r";
293 
294             return $output;
295         }, $tpl);
296 
297         // Parse foreach
298         $tpl = preg_replace_callback('/\<\!\-\-\s*\{foreach\s+(\S+)\s+(\S+)\s+(\S+)\}\s*\-\-\>/is', function($matchs) {
299             print_r($matchs);
300             $output = '<?php if (isset(' . $matchs[1] . ') && is_array(' . $matchs[1] . ')) { ?>';
301             $output .= "\r";
302             $output .= '<?php foreach (' . $matchs[1] . ' as ' . $matchs[2] . ' => ' . $matchs[3] . ') { ?>';
303             $output .= "\r";
304 
305             return $output;
306         }, $tpl);
307 
308         $regexs = [
309             '/\<\!\-\-\s*\{if\s+(\S+)\}\s*\-\-\>/is',
310             '/\<\!\-\-\s*\{elseif\s+(\S+)\}\s*\-\-\>/is',
311             '/\<\!\-\-\s*\{else}\s*\-\-\>/is',
312             '/\<\!\-\-\s*\{\/if}\s*\-\-\>/is',
313             '/\<\!\-\-\s*\{\/for}\s*\-\-\>/is',
314             '/\<\!\-\-\s*\{\/foreach}\s*\-\-\>/is',
315             '/\<\!\-\-\s*\{(.+?)\}\s*\-\-\>/is',
316             '/\{include\s+(.+?)\}/is',
317             '/\{([A-Z][A-Z0-9_]*)\}/s',
318             '/\{(\$[a-zA-Z0-9_\[\]\'\"\$\x7f-\xff]+)\}/is'
319         ];
320 
321         $replacements = [
322             '<?php if (\\1) { ?>',
323             '<?php } elseif (\\1) { ?>',
324             '<?php } else { ?>',
325             '<?php } ?>',
326             '<?php } } ?>',
327             '<?php } } ?>',
328             '{\\1}',
329             '<?php include ' . $viewClass . 'includeFile(\\1); ?>',
330             '<?php if ( defined(\'\\1\') ) { echo \\1; } ?>',
331             '<?php if ( isset(\\1) && !is_array(\\1)) { echo \\1; } ?>'
332         ];
333 
334         $tpl = preg_replace($regexs, $replacements, $tpl);
335 
336         // Parse functions
337         $tpl = preg_replace('/\{(([@&\\\$a-zA-Z0-9_]+)\(([^\}]*)\))?\}/is', '<?php echo \\1; ?>', $tpl);
338 
339         // Parse ternary
340         $tpl = preg_replace_callback('/\{(([^\}]*)\s+\?\s+([^\}]*)\s+:\s+([^\}]*))?\}/is', function($matchs) {
341             //print_r($matchs);
342             $output = '<?php echo ' . $matchs[1] . '; ?>';
343 
344             return str_replace(';; ?>', '; ?>', $output);
345         }, $tpl);
346 
347         // Parse Objects
348         $tpl = preg_replace_callback('/\{(([\$\\\w+]+)([:-\>]*)([^\}]*))?\}/is', function($matchs) {
349             //print_r($matchs);
350             $output = '<?php echo ' . $matchs[1] . '; ?>';
351 
352             return str_replace(';; ?>', '; ?>', $output);
353         }, $tpl);
354 
355         // Parse line comment
356         $tpl = preg_replace('/\{(\/\/([^\}]*))?\}/is', '<?php \\1; ?>', $tpl);
357 
358         return $tpl;
359     }
360 
361     /**
362      * Insert global variables
363      *
364      * @param array $array
365      * @return void
366      */
367 
368     public static function addGlobals(array $array)
369     {
370         static::$globals = array_merge($array, static::$globals);
371     }
372 }
Slimore API documentation generated by ApiGen