@@ -17,11 +17,11 @@ |
||
| 17 | 17 | interface TemplateManager |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @param string $template_name Name of the template to use (can include path separators) |
|
| 22 | - * |
|
| 23 | - * @return string Path to the compiled template file |
|
| 24 | - */ |
|
| 25 | - public function getPath($template_name); |
|
| 20 | + /** |
|
| 21 | + * @param string $template_name Name of the template to use (can include path separators) |
|
| 22 | + * |
|
| 23 | + * @return string Path to the compiled template file |
|
| 24 | + */ |
|
| 25 | + public function getPath($template_name); |
|
| 26 | 26 | |
| 27 | 27 | } |
@@ -22,117 +22,117 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | class CFSTemplateManager implements TemplateManager |
| 24 | 24 | { |
| 25 | - /** |
|
| 26 | - * @var string |
|
| 27 | - */ |
|
| 28 | - protected $cache_dir; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @var CFSWrapper |
|
| 32 | - */ |
|
| 33 | - protected $cascading_files; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @var array |
|
| 37 | - */ |
|
| 38 | - protected $compiled_paths = []; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @var TemplateCompiler |
|
| 42 | - */ |
|
| 43 | - protected $compiler; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @var boolean |
|
| 47 | - */ |
|
| 48 | - protected $recompile_always; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Valid options: |
|
| 52 | - * * cache_dir => the path where compiled templates will be cached |
|
| 53 | - * * recompile_always => whether to recompile each template on every execution, |
|
| 54 | - * |
|
| 55 | - * @param CFSWrapper $cascading_files |
|
| 56 | - * @param TemplateCompiler $compiler |
|
| 57 | - * @param array $options |
|
| 58 | - */ |
|
| 59 | - public function __construct(CFSWrapper $cascading_files, TemplateCompiler $compiler, array $options) |
|
| 60 | - { |
|
| 61 | - $this->cascading_files = $cascading_files; |
|
| 62 | - $this->compiler = $compiler; |
|
| 63 | - $this->cache_dir = trim($options['cache_dir'], '/'); |
|
| 64 | - $this->recompile_always = \Arr::get($options, 'recompile_always', FALSE); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * {@inheritdoc} |
|
| 69 | - */ |
|
| 70 | - public function getPath($template_name) |
|
| 71 | - { |
|
| 72 | - $compiled_path = $this->cache_dir.'/'.$template_name.'.php'; |
|
| 73 | - |
|
| 74 | - if ($this->isCompileRequired($compiled_path)) { |
|
| 75 | - $source = $this->requireSourceFileContent($template_name); |
|
| 76 | - $compiled = $this->compiler->compile($source); |
|
| 77 | - $this->writeFile($compiled_path, $compiled); |
|
| 78 | - $this->compiled_paths[$compiled_path] = TRUE; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - return $compiled_path; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * @param string $compiled_path |
|
| 86 | - * |
|
| 87 | - * @return bool |
|
| 88 | - */ |
|
| 89 | - protected function isCompileRequired($compiled_path) |
|
| 90 | - { |
|
| 91 | - if ($this->recompile_always AND ! isset($this->compiled_paths[$compiled_path])) { |
|
| 92 | - return TRUE; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - return ! file_exists($compiled_path); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * @param string $template_name |
|
| 100 | - * |
|
| 101 | - * @return string |
|
| 102 | - */ |
|
| 103 | - protected function requireSourceFileContent($template_name) |
|
| 104 | - { |
|
| 105 | - if ( ! $source_file = $this->cascading_files->find_file('views', $template_name)) { |
|
| 106 | - throw new \InvalidArgumentException("Cannot find template source file 'views/$template_name'"); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - return file_get_contents($source_file); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * @param string $compiled_path |
|
| 114 | - * @param string $compiled |
|
| 115 | - */ |
|
| 116 | - protected function writeFile($compiled_path, $compiled) |
|
| 117 | - { |
|
| 118 | - $this->ensureWriteableDirectory(dirname($compiled_path)); |
|
| 119 | - file_put_contents($compiled_path, $compiled); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * @param string $path |
|
| 124 | - */ |
|
| 125 | - protected function ensureWriteableDirectory($path) |
|
| 126 | - { |
|
| 127 | - if (is_dir($path)) { |
|
| 128 | - if ( ! is_writeable($path)) { |
|
| 129 | - throw new \RuntimeException("Cannot write to compiled template path '$path'"); |
|
| 130 | - } |
|
| 131 | - } else { |
|
| 132 | - if ( ! mkdir($path, 0777, TRUE)) { |
|
| 133 | - throw new \RuntimeException("Cannot create template cache directory in '$path'"); |
|
| 134 | - } |
|
| 135 | - } |
|
| 136 | - } |
|
| 25 | + /** |
|
| 26 | + * @var string |
|
| 27 | + */ |
|
| 28 | + protected $cache_dir; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @var CFSWrapper |
|
| 32 | + */ |
|
| 33 | + protected $cascading_files; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @var array |
|
| 37 | + */ |
|
| 38 | + protected $compiled_paths = []; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @var TemplateCompiler |
|
| 42 | + */ |
|
| 43 | + protected $compiler; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @var boolean |
|
| 47 | + */ |
|
| 48 | + protected $recompile_always; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Valid options: |
|
| 52 | + * * cache_dir => the path where compiled templates will be cached |
|
| 53 | + * * recompile_always => whether to recompile each template on every execution, |
|
| 54 | + * |
|
| 55 | + * @param CFSWrapper $cascading_files |
|
| 56 | + * @param TemplateCompiler $compiler |
|
| 57 | + * @param array $options |
|
| 58 | + */ |
|
| 59 | + public function __construct(CFSWrapper $cascading_files, TemplateCompiler $compiler, array $options) |
|
| 60 | + { |
|
| 61 | + $this->cascading_files = $cascading_files; |
|
| 62 | + $this->compiler = $compiler; |
|
| 63 | + $this->cache_dir = trim($options['cache_dir'], '/'); |
|
| 64 | + $this->recompile_always = \Arr::get($options, 'recompile_always', FALSE); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * {@inheritdoc} |
|
| 69 | + */ |
|
| 70 | + public function getPath($template_name) |
|
| 71 | + { |
|
| 72 | + $compiled_path = $this->cache_dir.'/'.$template_name.'.php'; |
|
| 73 | + |
|
| 74 | + if ($this->isCompileRequired($compiled_path)) { |
|
| 75 | + $source = $this->requireSourceFileContent($template_name); |
|
| 76 | + $compiled = $this->compiler->compile($source); |
|
| 77 | + $this->writeFile($compiled_path, $compiled); |
|
| 78 | + $this->compiled_paths[$compiled_path] = TRUE; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + return $compiled_path; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * @param string $compiled_path |
|
| 86 | + * |
|
| 87 | + * @return bool |
|
| 88 | + */ |
|
| 89 | + protected function isCompileRequired($compiled_path) |
|
| 90 | + { |
|
| 91 | + if ($this->recompile_always AND ! isset($this->compiled_paths[$compiled_path])) { |
|
| 92 | + return TRUE; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + return ! file_exists($compiled_path); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * @param string $template_name |
|
| 100 | + * |
|
| 101 | + * @return string |
|
| 102 | + */ |
|
| 103 | + protected function requireSourceFileContent($template_name) |
|
| 104 | + { |
|
| 105 | + if ( ! $source_file = $this->cascading_files->find_file('views', $template_name)) { |
|
| 106 | + throw new \InvalidArgumentException("Cannot find template source file 'views/$template_name'"); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + return file_get_contents($source_file); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * @param string $compiled_path |
|
| 114 | + * @param string $compiled |
|
| 115 | + */ |
|
| 116 | + protected function writeFile($compiled_path, $compiled) |
|
| 117 | + { |
|
| 118 | + $this->ensureWriteableDirectory(dirname($compiled_path)); |
|
| 119 | + file_put_contents($compiled_path, $compiled); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * @param string $path |
|
| 124 | + */ |
|
| 125 | + protected function ensureWriteableDirectory($path) |
|
| 126 | + { |
|
| 127 | + if (is_dir($path)) { |
|
| 128 | + if ( ! is_writeable($path)) { |
|
| 129 | + throw new \RuntimeException("Cannot write to compiled template path '$path'"); |
|
| 130 | + } |
|
| 131 | + } else { |
|
| 132 | + if ( ! mkdir($path, 0777, TRUE)) { |
|
| 133 | + throw new \RuntimeException("Cannot create template cache directory in '$path'"); |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | 138 | } |
@@ -15,11 +15,11 @@ |
||
| 15 | 15 | class CFSWrapper |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @see \Kohana::find_file |
|
| 20 | - */ |
|
| 21 | - public function find_file($dir, $file) |
|
| 22 | - { |
|
| 23 | - return \Kohana::find_file($dir, $file, NULL, FALSE); |
|
| 24 | - } |
|
| 18 | + /** |
|
| 19 | + * @see \Kohana::find_file |
|
| 20 | + */ |
|
| 21 | + public function find_file($dir, $file) |
|
| 22 | + { |
|
| 23 | + return \Kohana::find_file($dir, $file, NULL, FALSE); |
|
| 24 | + } |
|
| 25 | 25 | } |
@@ -1,9 +1,9 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * @author Andrew Coulton <[email protected]> |
|
| 4 | - * @copyright 2015 inGenerator Ltd |
|
| 5 | - * @license http://kohanaframework.org/license |
|
| 6 | - */ |
|
| 3 | + * @author Andrew Coulton <[email protected]> |
|
| 4 | + * @copyright 2015 inGenerator Ltd |
|
| 5 | + * @license http://kohanaframework.org/license |
|
| 6 | + */ |
|
| 7 | 7 | |
| 8 | 8 | namespace Ingenerator\KohanaView; |
| 9 | 9 | |
@@ -16,8 +16,8 @@ discard block |
||
| 16 | 16 | interface TemplateSpecifyingViewModel |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * @return string |
|
| 21 | - */ |
|
| 22 | - public function getTemplateName(); |
|
| 19 | + /** |
|
| 20 | + * @return string |
|
| 21 | + */ |
|
| 22 | + public function getTemplateName(); |
|
| 23 | 23 | } |