@@ -16,20 +16,20 @@ |
||
16 | 16 | class InvalidDisplayVariablesException extends \InvalidArgumentException |
17 | 17 | { |
18 | 18 | |
19 | - /** |
|
20 | - * @param string $view_class |
|
21 | - * @param string $errors |
|
22 | - * |
|
23 | - * @return static |
|
24 | - */ |
|
25 | - public static function passedToDisplay($view_class, $errors) |
|
26 | - { |
|
27 | - return new static( |
|
28 | - sprintf( |
|
29 | - "Invalid variables provided to %s::display()\n%s", |
|
30 | - $view_class, |
|
31 | - ' - '.implode("\n - ", $errors) |
|
32 | - ) |
|
33 | - ); |
|
34 | - } |
|
19 | + /** |
|
20 | + * @param string $view_class |
|
21 | + * @param string $errors |
|
22 | + * |
|
23 | + * @return static |
|
24 | + */ |
|
25 | + public static function passedToDisplay($view_class, $errors) |
|
26 | + { |
|
27 | + return new static( |
|
28 | + sprintf( |
|
29 | + "Invalid variables provided to %s::display()\n%s", |
|
30 | + $view_class, |
|
31 | + ' - '.implode("\n - ", $errors) |
|
32 | + ) |
|
33 | + ); |
|
34 | + } |
|
35 | 35 | } |
@@ -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\Exception; |
9 | 9 | |
@@ -15,22 +15,22 @@ discard block |
||
15 | 15 | */ |
16 | 16 | class InvalidTemplateContentException extends \InvalidArgumentException |
17 | 17 | { |
18 | - /** |
|
19 | - * @param string $escape_method |
|
20 | - * @param string $source_fragment |
|
21 | - * |
|
22 | - * @return static |
|
23 | - */ |
|
24 | - public static function containsImplicitDoubleEscape($escape_method, $source_fragment) |
|
25 | - { |
|
26 | - return new static( |
|
27 | - "Invalid implicit double-escape in template - remove $escape_method from `$source_fragment` or mark as raw" |
|
28 | - ); |
|
29 | - } |
|
18 | + /** |
|
19 | + * @param string $escape_method |
|
20 | + * @param string $source_fragment |
|
21 | + * |
|
22 | + * @return static |
|
23 | + */ |
|
24 | + public static function containsImplicitDoubleEscape($escape_method, $source_fragment) |
|
25 | + { |
|
26 | + return new static( |
|
27 | + "Invalid implicit double-escape in template - remove $escape_method from `$source_fragment` or mark as raw" |
|
28 | + ); |
|
29 | + } |
|
30 | 30 | |
31 | - public static function forEmptyTemplate() |
|
32 | - { |
|
33 | - return new static('Cannot compile empty template'); |
|
34 | - } |
|
31 | + public static function forEmptyTemplate() |
|
32 | + { |
|
33 | + return new static('Cannot compile empty template'); |
|
34 | + } |
|
35 | 35 | |
36 | 36 | } |
@@ -16,16 +16,16 @@ |
||
16 | 16 | class InvalidViewVarAssignmentException extends \BadMethodCallException |
17 | 17 | { |
18 | 18 | |
19 | - /** |
|
20 | - * @param string $view_class |
|
21 | - * @param string $var_name |
|
22 | - * |
|
23 | - * @return static |
|
24 | - */ |
|
25 | - public static function forReadOnlyVar($view_class, $var_name) |
|
26 | - { |
|
27 | - return new static( |
|
28 | - $view_class.' variables are read-only, cannot assign '.$var_name |
|
29 | - ); |
|
30 | - } |
|
19 | + /** |
|
20 | + * @param string $view_class |
|
21 | + * @param string $var_name |
|
22 | + * |
|
23 | + * @return static |
|
24 | + */ |
|
25 | + public static function forReadOnlyVar($view_class, $var_name) |
|
26 | + { |
|
27 | + return new static( |
|
28 | + $view_class.' variables are read-only, cannot assign '.$var_name |
|
29 | + ); |
|
30 | + } |
|
31 | 31 | } |
@@ -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\Exception; |
9 | 9 | |
@@ -15,26 +15,26 @@ discard block |
||
15 | 15 | */ |
16 | 16 | class TemplateCacheException extends \RuntimeException |
17 | 17 | { |
18 | - /** |
|
19 | - * @param string $path |
|
20 | - * |
|
21 | - * @return static |
|
22 | - */ |
|
23 | - public static function cannotCreateDirectory($path) |
|
24 | - { |
|
25 | - return new static( |
|
26 | - "Cannot create template cache directory in '$path'" |
|
27 | - ); |
|
28 | - } |
|
18 | + /** |
|
19 | + * @param string $path |
|
20 | + * |
|
21 | + * @return static |
|
22 | + */ |
|
23 | + public static function cannotCreateDirectory($path) |
|
24 | + { |
|
25 | + return new static( |
|
26 | + "Cannot create template cache directory in '$path'" |
|
27 | + ); |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * @param string $path |
|
32 | - * |
|
33 | - * @return static |
|
34 | - */ |
|
35 | - public static function pathNotWriteable($path) |
|
36 | - { |
|
37 | - return new static("Cannot write to compiled template path '$path'"); |
|
38 | - } |
|
30 | + /** |
|
31 | + * @param string $path |
|
32 | + * |
|
33 | + * @return static |
|
34 | + */ |
|
35 | + public static function pathNotWriteable($path) |
|
36 | + { |
|
37 | + return new static("Cannot write to compiled template path '$path'"); |
|
38 | + } |
|
39 | 39 | |
40 | 40 | } |
@@ -15,27 +15,27 @@ |
||
15 | 15 | class TemplateNotFoundException extends \InvalidArgumentException |
16 | 16 | { |
17 | 17 | |
18 | - /** |
|
19 | - * @param string $path |
|
20 | - * |
|
21 | - * @return static |
|
22 | - */ |
|
23 | - public static function forFullPath($path) |
|
24 | - { |
|
25 | - return new static( |
|
26 | - "Failed to include template '$path'" |
|
27 | - ); |
|
28 | - } |
|
18 | + /** |
|
19 | + * @param string $path |
|
20 | + * |
|
21 | + * @return static |
|
22 | + */ |
|
23 | + public static function forFullPath($path) |
|
24 | + { |
|
25 | + return new static( |
|
26 | + "Failed to include template '$path'" |
|
27 | + ); |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * @param string $rel_path |
|
32 | - * |
|
33 | - * @return static |
|
34 | - */ |
|
35 | - public static function forSourcePath($rel_path) |
|
36 | - { |
|
37 | - return new static( |
|
38 | - "Cannot find template source file '$rel_path'" |
|
39 | - ); |
|
40 | - } |
|
30 | + /** |
|
31 | + * @param string $rel_path |
|
32 | + * |
|
33 | + * @return static |
|
34 | + */ |
|
35 | + public static function forSourcePath($rel_path) |
|
36 | + { |
|
37 | + return new static( |
|
38 | + "Cannot find template source file '$rel_path'" |
|
39 | + ); |
|
40 | + } |
|
41 | 41 | } |
@@ -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\Exception; |
9 | 9 | |
@@ -16,33 +16,33 @@ discard block |
||
16 | 16 | class UnspecifiedTemplateNameException extends \UnexpectedValueException |
17 | 17 | { |
18 | 18 | |
19 | - /** |
|
20 | - * @param string $view_class |
|
21 | - * |
|
22 | - * @return static |
|
23 | - */ |
|
24 | - public static function forEmptyValue($view_class) |
|
25 | - { |
|
26 | - return new static( |
|
27 | - $view_class.'::getTemplateName() must return a template name, empty value returned' |
|
28 | - ); |
|
29 | - } |
|
19 | + /** |
|
20 | + * @param string $view_class |
|
21 | + * |
|
22 | + * @return static |
|
23 | + */ |
|
24 | + public static function forEmptyValue($view_class) |
|
25 | + { |
|
26 | + return new static( |
|
27 | + $view_class.'::getTemplateName() must return a template name, empty value returned' |
|
28 | + ); |
|
29 | + } |
|
30 | 30 | |
31 | - /** |
|
32 | - * @param string $view_class |
|
33 | - * @param string $template |
|
34 | - * |
|
35 | - * @return static |
|
36 | - */ |
|
37 | - public static function forNonStringValue($view_class, $template) |
|
38 | - { |
|
39 | - return new static( |
|
40 | - sprintf( |
|
41 | - '%s::getTemplateName() must return a string template name, %s value returned', |
|
42 | - $view_class, |
|
43 | - is_object($template) ? get_class($template) : gettype($template) |
|
44 | - ) |
|
45 | - ); |
|
46 | - } |
|
31 | + /** |
|
32 | + * @param string $view_class |
|
33 | + * @param string $template |
|
34 | + * |
|
35 | + * @return static |
|
36 | + */ |
|
37 | + public static function forNonStringValue($view_class, $template) |
|
38 | + { |
|
39 | + return new static( |
|
40 | + sprintf( |
|
41 | + '%s::getTemplateName() must return a string template name, %s value returned', |
|
42 | + $view_class, |
|
43 | + is_object($template) ? get_class($template) : gettype($template) |
|
44 | + ) |
|
45 | + ); |
|
46 | + } |
|
47 | 47 | |
48 | 48 | } |
@@ -33,83 +33,83 @@ |
||
33 | 33 | class TemplateCompiler |
34 | 34 | { |
35 | 35 | |
36 | - /** |
|
37 | - * @var array |
|
38 | - */ |
|
39 | - protected $options = [ |
|
40 | - 'raw_output_prefix' => '!', |
|
41 | - 'escape_method' => 'HTML::chars', |
|
42 | - ]; |
|
36 | + /** |
|
37 | + * @var array |
|
38 | + */ |
|
39 | + protected $options = [ |
|
40 | + 'raw_output_prefix' => '!', |
|
41 | + 'escape_method' => 'HTML::chars', |
|
42 | + ]; |
|
43 | 43 | |
44 | - /** |
|
45 | - * @param array $options |
|
46 | - */ |
|
47 | - public function __construct(array $options = []) |
|
48 | - { |
|
49 | - $this->options = array_merge($this->options, $options); |
|
50 | - } |
|
44 | + /** |
|
45 | + * @param array $options |
|
46 | + */ |
|
47 | + public function __construct(array $options = []) |
|
48 | + { |
|
49 | + $this->options = array_merge($this->options, $options); |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * Compile a string containing a PHP template, automatically escaping variables that are echoed in PHP short tags, |
|
54 | - * and return the compiled PHP string. |
|
55 | - * |
|
56 | - * @param string $source |
|
57 | - * |
|
58 | - * @return string |
|
59 | - * @throws \InvalidArgumentException if the template is empty or invalid |
|
60 | - */ |
|
61 | - public function compile($source) |
|
62 | - { |
|
63 | - if ( ! $source) { |
|
64 | - throw InvalidTemplateContentException::forEmptyTemplate(); |
|
65 | - } |
|
52 | + /** |
|
53 | + * Compile a string containing a PHP template, automatically escaping variables that are echoed in PHP short tags, |
|
54 | + * and return the compiled PHP string. |
|
55 | + * |
|
56 | + * @param string $source |
|
57 | + * |
|
58 | + * @return string |
|
59 | + * @throws \InvalidArgumentException if the template is empty or invalid |
|
60 | + */ |
|
61 | + public function compile($source) |
|
62 | + { |
|
63 | + if ( ! $source) { |
|
64 | + throw InvalidTemplateContentException::forEmptyTemplate(); |
|
65 | + } |
|
66 | 66 | |
67 | - return preg_replace_callback('/<\?=(.+?)(;|\?>)/s', [$this, 'compilePhpShortTag'], $source); |
|
68 | - } |
|
67 | + return preg_replace_callback('/<\?=(.+?)(;|\?>)/s', [$this, 'compilePhpShortTag'], $source); |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * @param string[] $matches |
|
72 | - * |
|
73 | - * @return string |
|
74 | - */ |
|
75 | - protected function compilePhpShortTag($matches) |
|
76 | - { |
|
77 | - $var = trim($matches[1]); |
|
78 | - $terminator = $matches[2]; |
|
79 | - $escape_method = $this->options['escape_method']; |
|
80 | - $raw_output_prefix = $this->options['raw_output_prefix']; |
|
70 | + /** |
|
71 | + * @param string[] $matches |
|
72 | + * |
|
73 | + * @return string |
|
74 | + */ |
|
75 | + protected function compilePhpShortTag($matches) |
|
76 | + { |
|
77 | + $var = trim($matches[1]); |
|
78 | + $terminator = $matches[2]; |
|
79 | + $escape_method = $this->options['escape_method']; |
|
80 | + $raw_output_prefix = $this->options['raw_output_prefix']; |
|
81 | 81 | |
82 | - if ($this->startsWith($var, $raw_output_prefix)) { |
|
83 | - // Remove prefix and echo unescaped |
|
84 | - $compiled = '<?='.trim(substr($var, strlen($raw_output_prefix))).';'; |
|
85 | - } elseif ($this->startsWith($var, '//')) { |
|
86 | - // Echo an empty string to prevent the comment causing a parse error |
|
87 | - $compiled = "<?='';$var;"; |
|
82 | + if ($this->startsWith($var, $raw_output_prefix)) { |
|
83 | + // Remove prefix and echo unescaped |
|
84 | + $compiled = '<?='.trim(substr($var, strlen($raw_output_prefix))).';'; |
|
85 | + } elseif ($this->startsWith($var, '//')) { |
|
86 | + // Echo an empty string to prevent the comment causing a parse error |
|
87 | + $compiled = "<?='';$var;"; |
|
88 | 88 | |
89 | - } elseif ($this->startsWith($var, $escape_method)) { |
|
90 | - throw InvalidTemplateContentException::containsImplicitDoubleEscape($escape_method, $matches[0]); |
|
89 | + } elseif ($this->startsWith($var, $escape_method)) { |
|
90 | + throw InvalidTemplateContentException::containsImplicitDoubleEscape($escape_method, $matches[0]); |
|
91 | 91 | |
92 | - } else { |
|
93 | - // Escape the value before echoing |
|
94 | - $compiled = "<?={$escape_method}($var);"; |
|
95 | - } |
|
92 | + } else { |
|
93 | + // Escape the value before echoing |
|
94 | + $compiled = "<?={$escape_method}($var);"; |
|
95 | + } |
|
96 | 96 | |
97 | - if ($terminator === '?>') { |
|
98 | - $compiled .= '?>'; |
|
99 | - } |
|
97 | + if ($terminator === '?>') { |
|
98 | + $compiled .= '?>'; |
|
99 | + } |
|
100 | 100 | |
101 | - return $compiled; |
|
102 | - } |
|
101 | + return $compiled; |
|
102 | + } |
|
103 | 103 | |
104 | - /** |
|
105 | - * @param string $string |
|
106 | - * @param string $prefix |
|
107 | - * |
|
108 | - * @return bool |
|
109 | - */ |
|
110 | - protected function startsWith($string, $prefix) |
|
111 | - { |
|
112 | - return (strncmp($string, $prefix, strlen($prefix)) === 0); |
|
113 | - } |
|
104 | + /** |
|
105 | + * @param string $string |
|
106 | + * @param string $prefix |
|
107 | + * |
|
108 | + * @return bool |
|
109 | + */ |
|
110 | + protected function startsWith($string, $prefix) |
|
111 | + { |
|
112 | + return (strncmp($string, $prefix, strlen($prefix)) === 0); |
|
113 | + } |
|
114 | 114 | |
115 | 115 | } |
@@ -24,117 +24,117 @@ |
||
24 | 24 | */ |
25 | 25 | class CFSTemplateManager implements TemplateManager |
26 | 26 | { |
27 | - /** |
|
28 | - * @var string |
|
29 | - */ |
|
30 | - protected $cache_dir; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var CFSWrapper |
|
34 | - */ |
|
35 | - protected $cascading_files; |
|
36 | - |
|
37 | - /** |
|
38 | - * @var array |
|
39 | - */ |
|
40 | - protected $compiled_paths = []; |
|
41 | - |
|
42 | - /** |
|
43 | - * @var TemplateCompiler |
|
44 | - */ |
|
45 | - protected $compiler; |
|
46 | - |
|
47 | - /** |
|
48 | - * @var boolean |
|
49 | - */ |
|
50 | - protected $recompile_always; |
|
51 | - |
|
52 | - /** |
|
53 | - * Valid options: |
|
54 | - * * cache_dir => the path where compiled templates will be cached |
|
55 | - * * recompile_always => whether to recompile each template on every execution, |
|
56 | - * |
|
57 | - * @param TemplateCompiler $compiler |
|
58 | - * @param array $options |
|
59 | - * @param CFSWrapper $cascading_files |
|
60 | - */ |
|
61 | - public function __construct(TemplateCompiler $compiler, array $options, CFSWrapper $cascading_files = NULL) |
|
62 | - { |
|
63 | - $this->cascading_files = $cascading_files ?: new CFSWrapper; |
|
64 | - $this->compiler = $compiler; |
|
65 | - $this->cache_dir = rtrim($options['cache_dir'], '/'); |
|
66 | - $this->recompile_always = \Arr::get($options, 'recompile_always', FALSE); |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * {@inheritdoc} |
|
71 | - */ |
|
72 | - public function getPath($template_name) |
|
73 | - { |
|
74 | - $compiled_path = $this->cache_dir.'/'.$template_name.'.php'; |
|
75 | - |
|
76 | - if ($this->isCompileRequired($compiled_path)) { |
|
77 | - $source = $this->requireSourceFileContent($template_name); |
|
78 | - $compiled = $this->compiler->compile($source); |
|
79 | - $this->writeFile($compiled_path, $compiled); |
|
80 | - $this->compiled_paths[$compiled_path] = TRUE; |
|
81 | - } |
|
82 | - |
|
83 | - return $compiled_path; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * @param string $compiled_path |
|
88 | - * |
|
89 | - * @return bool |
|
90 | - */ |
|
91 | - protected function isCompileRequired($compiled_path) |
|
92 | - { |
|
93 | - if ($this->recompile_always AND ! isset($this->compiled_paths[$compiled_path])) { |
|
94 | - return TRUE; |
|
95 | - } |
|
96 | - |
|
97 | - return ! file_exists($compiled_path); |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * @param string $template_name |
|
102 | - * |
|
103 | - * @return string |
|
104 | - */ |
|
105 | - protected function requireSourceFileContent($template_name) |
|
106 | - { |
|
107 | - if ( ! $source_file = $this->cascading_files->find_file('views', $template_name)) { |
|
108 | - throw TemplateNotFoundException::forSourcePath('views/'.$template_name); |
|
109 | - } |
|
110 | - |
|
111 | - return file_get_contents($source_file); |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @param string $compiled_path |
|
116 | - * @param string $compiled |
|
117 | - */ |
|
118 | - protected function writeFile($compiled_path, $compiled) |
|
119 | - { |
|
120 | - $this->ensureWriteableDirectory(dirname($compiled_path)); |
|
121 | - file_put_contents($compiled_path, $compiled); |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * @param string $path |
|
126 | - */ |
|
127 | - protected function ensureWriteableDirectory($path) |
|
128 | - { |
|
129 | - if (is_dir($path)) { |
|
130 | - if ( ! is_writeable($path)) { |
|
131 | - throw TemplateCacheException::pathNotWriteable($path); |
|
132 | - } |
|
133 | - } else { |
|
134 | - if ( ! mkdir($path, 0777, TRUE)) { |
|
135 | - throw TemplateCacheException::cannotCreateDirectory($path); |
|
136 | - } |
|
137 | - } |
|
138 | - } |
|
27 | + /** |
|
28 | + * @var string |
|
29 | + */ |
|
30 | + protected $cache_dir; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var CFSWrapper |
|
34 | + */ |
|
35 | + protected $cascading_files; |
|
36 | + |
|
37 | + /** |
|
38 | + * @var array |
|
39 | + */ |
|
40 | + protected $compiled_paths = []; |
|
41 | + |
|
42 | + /** |
|
43 | + * @var TemplateCompiler |
|
44 | + */ |
|
45 | + protected $compiler; |
|
46 | + |
|
47 | + /** |
|
48 | + * @var boolean |
|
49 | + */ |
|
50 | + protected $recompile_always; |
|
51 | + |
|
52 | + /** |
|
53 | + * Valid options: |
|
54 | + * * cache_dir => the path where compiled templates will be cached |
|
55 | + * * recompile_always => whether to recompile each template on every execution, |
|
56 | + * |
|
57 | + * @param TemplateCompiler $compiler |
|
58 | + * @param array $options |
|
59 | + * @param CFSWrapper $cascading_files |
|
60 | + */ |
|
61 | + public function __construct(TemplateCompiler $compiler, array $options, CFSWrapper $cascading_files = NULL) |
|
62 | + { |
|
63 | + $this->cascading_files = $cascading_files ?: new CFSWrapper; |
|
64 | + $this->compiler = $compiler; |
|
65 | + $this->cache_dir = rtrim($options['cache_dir'], '/'); |
|
66 | + $this->recompile_always = \Arr::get($options, 'recompile_always', FALSE); |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * {@inheritdoc} |
|
71 | + */ |
|
72 | + public function getPath($template_name) |
|
73 | + { |
|
74 | + $compiled_path = $this->cache_dir.'/'.$template_name.'.php'; |
|
75 | + |
|
76 | + if ($this->isCompileRequired($compiled_path)) { |
|
77 | + $source = $this->requireSourceFileContent($template_name); |
|
78 | + $compiled = $this->compiler->compile($source); |
|
79 | + $this->writeFile($compiled_path, $compiled); |
|
80 | + $this->compiled_paths[$compiled_path] = TRUE; |
|
81 | + } |
|
82 | + |
|
83 | + return $compiled_path; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * @param string $compiled_path |
|
88 | + * |
|
89 | + * @return bool |
|
90 | + */ |
|
91 | + protected function isCompileRequired($compiled_path) |
|
92 | + { |
|
93 | + if ($this->recompile_always AND ! isset($this->compiled_paths[$compiled_path])) { |
|
94 | + return TRUE; |
|
95 | + } |
|
96 | + |
|
97 | + return ! file_exists($compiled_path); |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * @param string $template_name |
|
102 | + * |
|
103 | + * @return string |
|
104 | + */ |
|
105 | + protected function requireSourceFileContent($template_name) |
|
106 | + { |
|
107 | + if ( ! $source_file = $this->cascading_files->find_file('views', $template_name)) { |
|
108 | + throw TemplateNotFoundException::forSourcePath('views/'.$template_name); |
|
109 | + } |
|
110 | + |
|
111 | + return file_get_contents($source_file); |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @param string $compiled_path |
|
116 | + * @param string $compiled |
|
117 | + */ |
|
118 | + protected function writeFile($compiled_path, $compiled) |
|
119 | + { |
|
120 | + $this->ensureWriteableDirectory(dirname($compiled_path)); |
|
121 | + file_put_contents($compiled_path, $compiled); |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * @param string $path |
|
126 | + */ |
|
127 | + protected function ensureWriteableDirectory($path) |
|
128 | + { |
|
129 | + if (is_dir($path)) { |
|
130 | + if ( ! is_writeable($path)) { |
|
131 | + throw TemplateCacheException::pathNotWriteable($path); |
|
132 | + } |
|
133 | + } else { |
|
134 | + if ( ! mkdir($path, 0777, TRUE)) { |
|
135 | + throw TemplateCacheException::cannotCreateDirectory($path); |
|
136 | + } |
|
137 | + } |
|
138 | + } |
|
139 | 139 | |
140 | 140 | } |
@@ -40,89 +40,89 @@ |
||
40 | 40 | */ |
41 | 41 | abstract class AbstractViewModel implements ViewModel |
42 | 42 | { |
43 | - /** |
|
44 | - * @var array The actual view data |
|
45 | - */ |
|
46 | - protected $variables = []; |
|
43 | + /** |
|
44 | + * @var array The actual view data |
|
45 | + */ |
|
46 | + protected $variables = []; |
|
47 | 47 | |
48 | - /** |
|
49 | - * @var string[] The names of the valid set of fields that must be passed to the display() method |
|
50 | - */ |
|
51 | - protected $expect_var_names = []; |
|
48 | + /** |
|
49 | + * @var string[] The names of the valid set of fields that must be passed to the display() method |
|
50 | + */ |
|
51 | + protected $expect_var_names = []; |
|
52 | 52 | |
53 | - public function __construct() |
|
54 | - { |
|
55 | - // Assign the expect_var_names to ensure that we don't accidentally start requiring compiled fields |
|
56 | - $this->expect_var_names = array_keys($this->variables); |
|
57 | - } |
|
53 | + public function __construct() |
|
54 | + { |
|
55 | + // Assign the expect_var_names to ensure that we don't accidentally start requiring compiled fields |
|
56 | + $this->expect_var_names = array_keys($this->variables); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Get field values |
|
61 | - * |
|
62 | - * @param string $name |
|
63 | - * |
|
64 | - * @return mixed |
|
65 | - */ |
|
66 | - public function __get($name) |
|
67 | - { |
|
68 | - if (array_key_exists($name, $this->variables)) { |
|
69 | - return $this->variables[$name]; |
|
70 | - } elseif (method_exists($this, 'var_'.$name)) { |
|
71 | - $method = 'var_'.$name; |
|
59 | + /** |
|
60 | + * Get field values |
|
61 | + * |
|
62 | + * @param string $name |
|
63 | + * |
|
64 | + * @return mixed |
|
65 | + */ |
|
66 | + public function __get($name) |
|
67 | + { |
|
68 | + if (array_key_exists($name, $this->variables)) { |
|
69 | + return $this->variables[$name]; |
|
70 | + } elseif (method_exists($this, 'var_'.$name)) { |
|
71 | + $method = 'var_'.$name; |
|
72 | 72 | |
73 | - return $this->$method(); |
|
74 | - } else { |
|
75 | - throw UndefinedViewVarException::forClassAndVar(static::class, $name); |
|
76 | - } |
|
77 | - } |
|
73 | + return $this->$method(); |
|
74 | + } else { |
|
75 | + throw UndefinedViewVarException::forClassAndVar(static::class, $name); |
|
76 | + } |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * @param string $name |
|
81 | - * @param mixed $value |
|
82 | - * |
|
83 | - * @throws \BadMethodCallException values cannot be assigned except with the display method |
|
84 | - */ |
|
85 | - public function __set($name, $value) |
|
86 | - { |
|
87 | - throw InvalidViewVarAssignmentException::forReadOnlyVar(static::class, $name); |
|
88 | - } |
|
79 | + /** |
|
80 | + * @param string $name |
|
81 | + * @param mixed $value |
|
82 | + * |
|
83 | + * @throws \BadMethodCallException values cannot be assigned except with the display method |
|
84 | + */ |
|
85 | + public function __set($name, $value) |
|
86 | + { |
|
87 | + throw InvalidViewVarAssignmentException::forReadOnlyVar(static::class, $name); |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * Set the data to be rendered in the view - note this does not actually render the view. |
|
92 | - * |
|
93 | - * @param array $variables |
|
94 | - */ |
|
95 | - public function display(array $variables) |
|
96 | - { |
|
97 | - if ($errors = $this->validateDisplayVariables($variables)) { |
|
98 | - throw InvalidDisplayVariablesException::passedToDisplay(static::class, $errors); |
|
99 | - } |
|
90 | + /** |
|
91 | + * Set the data to be rendered in the view - note this does not actually render the view. |
|
92 | + * |
|
93 | + * @param array $variables |
|
94 | + */ |
|
95 | + public function display(array $variables) |
|
96 | + { |
|
97 | + if ($errors = $this->validateDisplayVariables($variables)) { |
|
98 | + throw InvalidDisplayVariablesException::passedToDisplay(static::class, $errors); |
|
99 | + } |
|
100 | 100 | |
101 | - $this->variables = $variables; |
|
102 | - } |
|
101 | + $this->variables = $variables; |
|
102 | + } |
|
103 | 103 | |
104 | - /** |
|
105 | - * @param array $variables |
|
106 | - * |
|
107 | - * @return string[] of errors |
|
108 | - */ |
|
109 | - protected function validateDisplayVariables(array $variables) |
|
110 | - { |
|
111 | - $errors = []; |
|
112 | - $provided_variables = array_keys($variables); |
|
113 | - foreach (array_diff($provided_variables, $this->expect_var_names) as $unexpected_var) { |
|
114 | - if (method_exists($this, 'var_'.$unexpected_var)) { |
|
115 | - $errors[] = "'$unexpected_var' conflicts with ::var_$unexpected_var()"; |
|
116 | - } else { |
|
117 | - $errors[] = "'$unexpected_var' is not expected"; |
|
118 | - } |
|
119 | - } |
|
104 | + /** |
|
105 | + * @param array $variables |
|
106 | + * |
|
107 | + * @return string[] of errors |
|
108 | + */ |
|
109 | + protected function validateDisplayVariables(array $variables) |
|
110 | + { |
|
111 | + $errors = []; |
|
112 | + $provided_variables = array_keys($variables); |
|
113 | + foreach (array_diff($provided_variables, $this->expect_var_names) as $unexpected_var) { |
|
114 | + if (method_exists($this, 'var_'.$unexpected_var)) { |
|
115 | + $errors[] = "'$unexpected_var' conflicts with ::var_$unexpected_var()"; |
|
116 | + } else { |
|
117 | + $errors[] = "'$unexpected_var' is not expected"; |
|
118 | + } |
|
119 | + } |
|
120 | 120 | |
121 | - foreach (array_diff($this->expect_var_names, $provided_variables) as $missing_var) { |
|
122 | - $errors[] = "'$missing_var' is missing"; |
|
123 | - } |
|
121 | + foreach (array_diff($this->expect_var_names, $provided_variables) as $missing_var) { |
|
122 | + $errors[] = "'$missing_var' is missing"; |
|
123 | + } |
|
124 | 124 | |
125 | - return $errors; |
|
126 | - } |
|
125 | + return $errors; |
|
126 | + } |
|
127 | 127 | |
128 | 128 | } |