@@ -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 | } |
@@ -17,11 +17,11 @@ |
||
| 17 | 17 | interface Renderer |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @param ViewModel $view |
|
| 22 | - * |
|
| 23 | - * @return string |
|
| 24 | - */ |
|
| 25 | - public function render(ViewModel $view); |
|
| 20 | + /** |
|
| 21 | + * @param ViewModel $view |
|
| 22 | + * |
|
| 23 | + * @return string |
|
| 24 | + */ |
|
| 25 | + public function render(ViewModel $view); |
|
| 26 | 26 | |
| 27 | 27 | } |
@@ -22,72 +22,72 @@ |
||
| 22 | 22 | class HTMLRenderer implements Renderer |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @var TemplateManager |
|
| 27 | - */ |
|
| 28 | - protected $template_manager; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @var ViewTemplateSelector |
|
| 32 | - */ |
|
| 33 | - protected $template_selector; |
|
| 34 | - |
|
| 35 | - public function __construct(ViewTemplateSelector $template_selector, TemplateManager $template_manager) |
|
| 36 | - { |
|
| 37 | - $this->template_selector = $template_selector; |
|
| 38 | - $this->template_manager = $template_manager; |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * {@inheritdoc} |
|
| 44 | - */ |
|
| 45 | - public function render(ViewModel $view) |
|
| 46 | - { |
|
| 47 | - $template_path = $this->getTemplatePath($view); |
|
| 48 | - |
|
| 49 | - ob_start(); |
|
| 50 | - try { |
|
| 51 | - $this->includeWithAnonymousScope($view, $template_path); |
|
| 52 | - } finally { |
|
| 53 | - $output = ob_get_clean(); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - return $output; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * @param ViewModel $view |
|
| 61 | - * |
|
| 62 | - * @return string |
|
| 63 | - */ |
|
| 64 | - protected function getTemplatePath(ViewModel $view) |
|
| 65 | - { |
|
| 66 | - $template_name = $this->template_selector->getTemplateName($view); |
|
| 67 | - $template = $this->template_manager->getPath($template_name); |
|
| 68 | - |
|
| 69 | - return $template; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * @param ViewModel $view |
|
| 74 | - * @param string $template_path |
|
| 75 | - */ |
|
| 76 | - protected function includeWithAnonymousScope(ViewModel $view, $template_path) |
|
| 77 | - { |
|
| 78 | - /** @noinspection PhpUnusedParameterInspection */ |
|
| 79 | - /** @noinspection PhpDocSignatureInspection */ |
|
| 80 | - $bound_capture = function (ViewModel $view, Renderer $renderer, $template) { |
|
| 81 | - /** @noinspection PhpIncludeInspection */ |
|
| 82 | - return include $template; |
|
| 83 | - }; |
|
| 84 | - $anon_capture = $bound_capture->bindTo(NULL); |
|
| 85 | - |
|
| 86 | - // A user's own error handler may throw an exception here if the include fails - which we will bubble as-is. |
|
| 87 | - // If they have not configured an error handler, we need to throw an exception of our own. |
|
| 88 | - if ($anon_capture($view, $this, $template_path) === FALSE) { |
|
| 89 | - throw new \UnexpectedValueException('Failed to include template '.$template_path); |
|
| 90 | - } |
|
| 91 | - } |
|
| 25 | + /** |
|
| 26 | + * @var TemplateManager |
|
| 27 | + */ |
|
| 28 | + protected $template_manager; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @var ViewTemplateSelector |
|
| 32 | + */ |
|
| 33 | + protected $template_selector; |
|
| 34 | + |
|
| 35 | + public function __construct(ViewTemplateSelector $template_selector, TemplateManager $template_manager) |
|
| 36 | + { |
|
| 37 | + $this->template_selector = $template_selector; |
|
| 38 | + $this->template_manager = $template_manager; |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * {@inheritdoc} |
|
| 44 | + */ |
|
| 45 | + public function render(ViewModel $view) |
|
| 46 | + { |
|
| 47 | + $template_path = $this->getTemplatePath($view); |
|
| 48 | + |
|
| 49 | + ob_start(); |
|
| 50 | + try { |
|
| 51 | + $this->includeWithAnonymousScope($view, $template_path); |
|
| 52 | + } finally { |
|
| 53 | + $output = ob_get_clean(); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + return $output; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * @param ViewModel $view |
|
| 61 | + * |
|
| 62 | + * @return string |
|
| 63 | + */ |
|
| 64 | + protected function getTemplatePath(ViewModel $view) |
|
| 65 | + { |
|
| 66 | + $template_name = $this->template_selector->getTemplateName($view); |
|
| 67 | + $template = $this->template_manager->getPath($template_name); |
|
| 68 | + |
|
| 69 | + return $template; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * @param ViewModel $view |
|
| 74 | + * @param string $template_path |
|
| 75 | + */ |
|
| 76 | + protected function includeWithAnonymousScope(ViewModel $view, $template_path) |
|
| 77 | + { |
|
| 78 | + /** @noinspection PhpUnusedParameterInspection */ |
|
| 79 | + /** @noinspection PhpDocSignatureInspection */ |
|
| 80 | + $bound_capture = function (ViewModel $view, Renderer $renderer, $template) { |
|
| 81 | + /** @noinspection PhpIncludeInspection */ |
|
| 82 | + return include $template; |
|
| 83 | + }; |
|
| 84 | + $anon_capture = $bound_capture->bindTo(NULL); |
|
| 85 | + |
|
| 86 | + // A user's own error handler may throw an exception here if the include fails - which we will bubble as-is. |
|
| 87 | + // If they have not configured an error handler, we need to throw an exception of our own. |
|
| 88 | + if ($anon_capture($view, $this, $template_path) === FALSE) { |
|
| 89 | + throw new \UnexpectedValueException('Failed to include template '.$template_path); |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | 93 | } |
@@ -77,11 +77,11 @@ |
||
| 77 | 77 | { |
| 78 | 78 | /** @noinspection PhpUnusedParameterInspection */ |
| 79 | 79 | /** @noinspection PhpDocSignatureInspection */ |
| 80 | - $bound_capture = function (ViewModel $view, Renderer $renderer, $template) { |
|
| 80 | + $bound_capture = function(ViewModel $view, Renderer $renderer, $template) { |
|
| 81 | 81 | /** @noinspection PhpIncludeInspection */ |
| 82 | 82 | return include $template; |
| 83 | 83 | }; |
| 84 | - $anon_capture = $bound_capture->bindTo(NULL); |
|
| 84 | + $anon_capture = $bound_capture->bindTo(NULL); |
|
| 85 | 85 | |
| 86 | 86 | // A user's own error handler may throw an exception here if the include fails - which we will bubble as-is. |
| 87 | 87 | // If they have not configured an error handler, we need to throw an exception of our own. |