@@ -11,23 +11,23 @@ |
||
11 | 11 | |
12 | 12 | class StaticPageContentView extends AbstractPageContentView implements TemplateSpecifyingViewModel |
13 | 13 | { |
14 | - protected $variables = [ |
|
15 | - 'page_path' => NULL, |
|
16 | - ]; |
|
14 | + protected $variables = [ |
|
15 | + 'page_path' => NULL, |
|
16 | + ]; |
|
17 | 17 | |
18 | - /** |
|
19 | - * {@inheritdoc} |
|
20 | - */ |
|
21 | - public function getTemplateName() |
|
22 | - { |
|
23 | - if ( ! $this->variables['page_path']) { |
|
24 | - throw new \BadMethodCallException( |
|
25 | - 'Call '.static::class.'::display(["page_path" => "name/of/view"]) before rendering static views' |
|
26 | - ); |
|
27 | - } |
|
18 | + /** |
|
19 | + * {@inheritdoc} |
|
20 | + */ |
|
21 | + public function getTemplateName() |
|
22 | + { |
|
23 | + if ( ! $this->variables['page_path']) { |
|
24 | + throw new \BadMethodCallException( |
|
25 | + 'Call '.static::class.'::display(["page_path" => "name/of/view"]) before rendering static views' |
|
26 | + ); |
|
27 | + } |
|
28 | 28 | |
29 | - return $this->variables['page_path']; |
|
30 | - } |
|
29 | + return $this->variables['page_path']; |
|
30 | + } |
|
31 | 31 | |
32 | 32 | |
33 | 33 | } |
@@ -34,82 +34,82 @@ |
||
34 | 34 | */ |
35 | 35 | class PageLayoutRenderer |
36 | 36 | { |
37 | - /** |
|
38 | - * @var bool Whether to force (or not force) embedding the content in the layout |
|
39 | - */ |
|
40 | - protected $use_layout; |
|
37 | + /** |
|
38 | + * @var bool Whether to force (or not force) embedding the content in the layout |
|
39 | + */ |
|
40 | + protected $use_layout; |
|
41 | 41 | |
42 | - /** |
|
43 | - * @var Renderer |
|
44 | - */ |
|
45 | - protected $view_renderer; |
|
42 | + /** |
|
43 | + * @var Renderer |
|
44 | + */ |
|
45 | + protected $view_renderer; |
|
46 | 46 | |
47 | - /** |
|
48 | - * @var \Request |
|
49 | - */ |
|
50 | - protected $current_request; |
|
47 | + /** |
|
48 | + * @var \Request |
|
49 | + */ |
|
50 | + protected $current_request; |
|
51 | 51 | |
52 | - public function __construct(Renderer $view_renderer, \Request $current_request = NULL) |
|
53 | - { |
|
54 | - $this->view_renderer = $view_renderer; |
|
55 | - $this->current_request = $current_request; |
|
56 | - } |
|
52 | + public function __construct(Renderer $view_renderer, \Request $current_request = NULL) |
|
53 | + { |
|
54 | + $this->view_renderer = $view_renderer; |
|
55 | + $this->current_request = $current_request; |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * @param PageContentView $content_view |
|
60 | - * |
|
61 | - * @return string |
|
62 | - */ |
|
63 | - public function render(PageContentView $content_view) |
|
64 | - { |
|
65 | - $content = $this->view_renderer->render($content_view); |
|
66 | - if ($this->shouldUseLayout()) { |
|
67 | - return $this->renderInLayout($content_view->var_page(), $content); |
|
68 | - } else { |
|
69 | - return $content; |
|
70 | - } |
|
71 | - } |
|
58 | + /** |
|
59 | + * @param PageContentView $content_view |
|
60 | + * |
|
61 | + * @return string |
|
62 | + */ |
|
63 | + public function render(PageContentView $content_view) |
|
64 | + { |
|
65 | + $content = $this->view_renderer->render($content_view); |
|
66 | + if ($this->shouldUseLayout()) { |
|
67 | + return $this->renderInLayout($content_view->var_page(), $content); |
|
68 | + } else { |
|
69 | + return $content; |
|
70 | + } |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * @return bool |
|
75 | - */ |
|
76 | - protected function shouldUseLayout() |
|
77 | - { |
|
78 | - if ($this->use_layout !== NULL) { |
|
79 | - return $this->use_layout; |
|
80 | - } |
|
73 | + /** |
|
74 | + * @return bool |
|
75 | + */ |
|
76 | + protected function shouldUseLayout() |
|
77 | + { |
|
78 | + if ($this->use_layout !== NULL) { |
|
79 | + return $this->use_layout; |
|
80 | + } |
|
81 | 81 | |
82 | - if ($this->current_request AND $this->current_request->is_ajax()) { |
|
83 | - return FALSE; |
|
84 | - } else { |
|
85 | - return TRUE; |
|
86 | - } |
|
87 | - } |
|
82 | + if ($this->current_request AND $this->current_request->is_ajax()) { |
|
83 | + return FALSE; |
|
84 | + } else { |
|
85 | + return TRUE; |
|
86 | + } |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * @param PageLayoutView $layout |
|
91 | - * @param string $content |
|
92 | - * |
|
93 | - * @return string |
|
94 | - */ |
|
95 | - protected function renderInLayout(PageLayoutView $layout, $content) |
|
96 | - { |
|
97 | - $layout->setBodyHTML($content); |
|
89 | + /** |
|
90 | + * @param PageLayoutView $layout |
|
91 | + * @param string $content |
|
92 | + * |
|
93 | + * @return string |
|
94 | + */ |
|
95 | + protected function renderInLayout(PageLayoutView $layout, $content) |
|
96 | + { |
|
97 | + $layout->setBodyHTML($content); |
|
98 | 98 | |
99 | - return $this->view_renderer->render($layout); |
|
100 | - } |
|
99 | + return $this->view_renderer->render($layout); |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * Configure whether to always wrap the content in the layout (TRUE), never (FALSE) or automatically for |
|
104 | - * non-AJAX requests (NULL) |
|
105 | - * |
|
106 | - * @param bool $use_layout |
|
107 | - * |
|
108 | - * @return void |
|
109 | - */ |
|
110 | - public function setUseLayout($use_layout) |
|
111 | - { |
|
112 | - $this->use_layout = $use_layout; |
|
113 | - } |
|
102 | + /** |
|
103 | + * Configure whether to always wrap the content in the layout (TRUE), never (FALSE) or automatically for |
|
104 | + * non-AJAX requests (NULL) |
|
105 | + * |
|
106 | + * @param bool $use_layout |
|
107 | + * |
|
108 | + * @return void |
|
109 | + */ |
|
110 | + public function setUseLayout($use_layout) |
|
111 | + { |
|
112 | + $this->use_layout = $use_layout; |
|
113 | + } |
|
114 | 114 | |
115 | 115 | } |
@@ -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 TemplateCompiler $compiler |
|
56 | - * @param array $options |
|
57 | - * @param CFSWrapper $cascading_files |
|
58 | - */ |
|
59 | - public function __construct(TemplateCompiler $compiler, array $options, CFSWrapper $cascading_files = NULL) |
|
60 | - { |
|
61 | - $this->cascading_files = $cascading_files ?: new CFSWrapper; |
|
62 | - $this->compiler = $compiler; |
|
63 | - $this->cache_dir = rtrim($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 TemplateCompiler $compiler |
|
56 | + * @param array $options |
|
57 | + * @param CFSWrapper $cascading_files |
|
58 | + */ |
|
59 | + public function __construct(TemplateCompiler $compiler, array $options, CFSWrapper $cascading_files = NULL) |
|
60 | + { |
|
61 | + $this->cascading_files = $cascading_files ?: new CFSWrapper; |
|
62 | + $this->compiler = $compiler; |
|
63 | + $this->cache_dir = rtrim($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 | } |
@@ -7,38 +7,38 @@ |
||
7 | 7 | * @license http://kohanaframework.org/license |
8 | 8 | */ |
9 | 9 | return [ |
10 | - 'kohanaview' => [ |
|
11 | - 'renderer' => [ |
|
12 | - 'html' => [ |
|
13 | - '_settings' => [ |
|
14 | - 'class' => '\Ingenerator\KohanaView\Renderer\HTMLRenderer', |
|
15 | - 'arguments' => ['%kohanaview.template.selector%', '%kohanaview.template.manager%'], |
|
16 | - 'shared' => TRUE, |
|
17 | - ], |
|
18 | - ], |
|
19 | - ], |
|
20 | - 'template' => [ |
|
21 | - 'compiler' => [ |
|
22 | - '_settings' => [ |
|
23 | - 'class' => '\Ingenerator\KohanaView\TemplateCompiler', |
|
24 | - 'arguments' => [], |
|
25 | - 'shared' => TRUE, |
|
26 | - ], |
|
27 | - ], |
|
28 | - 'manager' => [ |
|
29 | - '_settings' => [ |
|
30 | - 'class' => '\Ingenerator\KohanaView\TemplateManager\CFSTemplateManager', |
|
31 | - 'arguments' => ['%kohanaview.template.compiler%', '@kohanaview.template_manager@'], |
|
32 | - 'shared' => TRUE, |
|
33 | - ], |
|
34 | - ], |
|
35 | - 'selector' => [ |
|
36 | - '_settings' => [ |
|
37 | - 'class' => '\Ingenerator\KohanaView\ViewTemplateSelector', |
|
38 | - 'arguments' => [], |
|
39 | - 'shared' => TRUE, |
|
40 | - ], |
|
41 | - ], |
|
42 | - ], |
|
43 | - ], |
|
10 | + 'kohanaview' => [ |
|
11 | + 'renderer' => [ |
|
12 | + 'html' => [ |
|
13 | + '_settings' => [ |
|
14 | + 'class' => '\Ingenerator\KohanaView\Renderer\HTMLRenderer', |
|
15 | + 'arguments' => ['%kohanaview.template.selector%', '%kohanaview.template.manager%'], |
|
16 | + 'shared' => TRUE, |
|
17 | + ], |
|
18 | + ], |
|
19 | + ], |
|
20 | + 'template' => [ |
|
21 | + 'compiler' => [ |
|
22 | + '_settings' => [ |
|
23 | + 'class' => '\Ingenerator\KohanaView\TemplateCompiler', |
|
24 | + 'arguments' => [], |
|
25 | + 'shared' => TRUE, |
|
26 | + ], |
|
27 | + ], |
|
28 | + 'manager' => [ |
|
29 | + '_settings' => [ |
|
30 | + 'class' => '\Ingenerator\KohanaView\TemplateManager\CFSTemplateManager', |
|
31 | + 'arguments' => ['%kohanaview.template.compiler%', '@kohanaview.template_manager@'], |
|
32 | + 'shared' => TRUE, |
|
33 | + ], |
|
34 | + ], |
|
35 | + 'selector' => [ |
|
36 | + '_settings' => [ |
|
37 | + 'class' => '\Ingenerator\KohanaView\ViewTemplateSelector', |
|
38 | + 'arguments' => [], |
|
39 | + 'shared' => TRUE, |
|
40 | + ], |
|
41 | + ], |
|
42 | + ], |
|
43 | + ], |
|
44 | 44 | ]; |
@@ -1,17 +1,17 @@ |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * Configuration for the KohanaView module |
|
4 | - * |
|
5 | - * @author Andrew Coulton <[email protected]> |
|
6 | - * @copyright 2015 inGenerator Ltd |
|
7 | - * @license http://kohanaframework.org/license |
|
8 | - */ |
|
3 | + * Configuration for the KohanaView module |
|
4 | + * |
|
5 | + * @author Andrew Coulton <[email protected]> |
|
6 | + * @copyright 2015 inGenerator Ltd |
|
7 | + * @license http://kohanaframework.org/license |
|
8 | + */ |
|
9 | 9 | return [ |
10 | - 'template_manager' => [ |
|
11 | - // Where compiled templates should be stored |
|
12 | - 'cache_dir' => \Kohana::$cache_dir.'/compiled_templates', |
|
10 | + 'template_manager' => [ |
|
11 | + // Where compiled templates should be stored |
|
12 | + 'cache_dir' => \Kohana::$cache_dir.'/compiled_templates', |
|
13 | 13 | |
14 | - // Whether to recompile all templates on the first use of that template even if it exists |
|
15 | - 'recompile_always' => (\Kohana::$environment === \Kohana::DEVELOPMENT), |
|
16 | - ], |
|
14 | + // Whether to recompile all templates on the first use of that template even if it exists |
|
15 | + 'recompile_always' => (\Kohana::$environment === \Kohana::DEVELOPMENT), |
|
16 | + ], |
|
17 | 17 | ]; |
@@ -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 | |
@@ -17,11 +17,11 @@ discard block |
||
17 | 17 | interface ViewModel |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * @param array $variables |
|
22 | - * |
|
23 | - * @return void |
|
24 | - */ |
|
25 | - public function display(array $variables); |
|
20 | + /** |
|
21 | + * @param array $variables |
|
22 | + * |
|
23 | + * @return void |
|
24 | + */ |
|
25 | + public function display(array $variables); |
|
26 | 26 | |
27 | 27 | } |
@@ -20,60 +20,60 @@ |
||
20 | 20 | class ViewTemplateSelector |
21 | 21 | { |
22 | 22 | |
23 | - /** |
|
24 | - * @param ViewModel $view |
|
25 | - * |
|
26 | - * @return string |
|
27 | - */ |
|
28 | - public function getTemplateName(ViewModel $view) |
|
29 | - { |
|
30 | - if ($view instanceof TemplateSpecifyingViewModel) { |
|
31 | - return $this->validateSpecifiedTemplateName($view); |
|
32 | - } else { |
|
33 | - return $this->calculateTemplateFromClassName($view); |
|
34 | - } |
|
35 | - } |
|
23 | + /** |
|
24 | + * @param ViewModel $view |
|
25 | + * |
|
26 | + * @return string |
|
27 | + */ |
|
28 | + public function getTemplateName(ViewModel $view) |
|
29 | + { |
|
30 | + if ($view instanceof TemplateSpecifyingViewModel) { |
|
31 | + return $this->validateSpecifiedTemplateName($view); |
|
32 | + } else { |
|
33 | + return $this->calculateTemplateFromClassName($view); |
|
34 | + } |
|
35 | + } |
|
36 | 36 | |
37 | - /** |
|
38 | - * @param TemplateSpecifyingViewModel $view |
|
39 | - * |
|
40 | - * @return mixed |
|
41 | - * @throws \UnexpectedValueException if no template is provided |
|
42 | - */ |
|
43 | - protected function validateSpecifiedTemplateName(TemplateSpecifyingViewModel $view) |
|
44 | - { |
|
45 | - $template = $view->getTemplateName(); |
|
46 | - $view_class = get_class($view); |
|
47 | - if ( ! $template) { |
|
48 | - throw new \UnexpectedValueException( |
|
49 | - "$view_class::getTemplateName() must return a template name, empty value returned" |
|
50 | - ); |
|
51 | - } |
|
37 | + /** |
|
38 | + * @param TemplateSpecifyingViewModel $view |
|
39 | + * |
|
40 | + * @return mixed |
|
41 | + * @throws \UnexpectedValueException if no template is provided |
|
42 | + */ |
|
43 | + protected function validateSpecifiedTemplateName(TemplateSpecifyingViewModel $view) |
|
44 | + { |
|
45 | + $template = $view->getTemplateName(); |
|
46 | + $view_class = get_class($view); |
|
47 | + if ( ! $template) { |
|
48 | + throw new \UnexpectedValueException( |
|
49 | + "$view_class::getTemplateName() must return a template name, empty value returned" |
|
50 | + ); |
|
51 | + } |
|
52 | 52 | |
53 | - if ( ! is_string($template)) { |
|
54 | - $type = is_object($template) ? get_class($template) : gettype($template); |
|
55 | - throw new \UnexpectedValueException( |
|
56 | - "$view_class::getTemplateName() must return a string template name, $type value returned" |
|
57 | - ); |
|
58 | - } |
|
53 | + if ( ! is_string($template)) { |
|
54 | + $type = is_object($template) ? get_class($template) : gettype($template); |
|
55 | + throw new \UnexpectedValueException( |
|
56 | + "$view_class::getTemplateName() must return a string template name, $type value returned" |
|
57 | + ); |
|
58 | + } |
|
59 | 59 | |
60 | - return $template; |
|
61 | - } |
|
60 | + return $template; |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * @param ViewModel $view |
|
65 | - * |
|
66 | - * @return string |
|
67 | - */ |
|
68 | - protected function calculateTemplateFromClassName(ViewModel $view) |
|
69 | - { |
|
70 | - $template = get_class($view); |
|
71 | - $template = preg_replace('/\\\\|_/', '/', $template); |
|
72 | - $template = preg_replace('#(^view/?(model)?/)|(?<!/)(view/?(model)?$)#i', '', $template); |
|
73 | - $template = preg_replace('/([a-z])([A-Z])/', '\1_\2', $template); |
|
74 | - $template = strtolower($template); |
|
63 | + /** |
|
64 | + * @param ViewModel $view |
|
65 | + * |
|
66 | + * @return string |
|
67 | + */ |
|
68 | + protected function calculateTemplateFromClassName(ViewModel $view) |
|
69 | + { |
|
70 | + $template = get_class($view); |
|
71 | + $template = preg_replace('/\\\\|_/', '/', $template); |
|
72 | + $template = preg_replace('#(^view/?(model)?/)|(?<!/)(view/?(model)?$)#i', '', $template); |
|
73 | + $template = preg_replace('/([a-z])([A-Z])/', '\1_\2', $template); |
|
74 | + $template = strtolower($template); |
|
75 | 75 | |
76 | - return $template; |
|
77 | - } |
|
76 | + return $template; |
|
77 | + } |
|
78 | 78 | |
79 | 79 | } |
@@ -37,92 +37,92 @@ |
||
37 | 37 | */ |
38 | 38 | abstract class AbstractViewModel implements ViewModel |
39 | 39 | { |
40 | - /** |
|
41 | - * @var array The actual view data |
|
42 | - */ |
|
43 | - protected $variables = []; |
|
40 | + /** |
|
41 | + * @var array The actual view data |
|
42 | + */ |
|
43 | + protected $variables = []; |
|
44 | 44 | |
45 | - /** |
|
46 | - * @var string[] The names of the valid set of fields that must be passed to the display() method |
|
47 | - */ |
|
48 | - protected $expect_var_names = []; |
|
45 | + /** |
|
46 | + * @var string[] The names of the valid set of fields that must be passed to the display() method |
|
47 | + */ |
|
48 | + protected $expect_var_names = []; |
|
49 | 49 | |
50 | - public function __construct() |
|
51 | - { |
|
52 | - // Assign the expect_var_names to ensure that we don't accidentally start requiring compiled fields |
|
53 | - $this->expect_var_names = array_keys($this->variables); |
|
54 | - } |
|
50 | + public function __construct() |
|
51 | + { |
|
52 | + // Assign the expect_var_names to ensure that we don't accidentally start requiring compiled fields |
|
53 | + $this->expect_var_names = array_keys($this->variables); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * Get field values |
|
58 | - * |
|
59 | - * @param string $name |
|
60 | - * |
|
61 | - * @return mixed |
|
62 | - */ |
|
63 | - public function __get($name) |
|
64 | - { |
|
65 | - if (array_key_exists($name, $this->variables)) { |
|
66 | - return $this->variables[$name]; |
|
67 | - } elseif (method_exists($this, 'var_'.$name)) { |
|
68 | - $method = 'var_'.$name; |
|
56 | + /** |
|
57 | + * Get field values |
|
58 | + * |
|
59 | + * @param string $name |
|
60 | + * |
|
61 | + * @return mixed |
|
62 | + */ |
|
63 | + public function __get($name) |
|
64 | + { |
|
65 | + if (array_key_exists($name, $this->variables)) { |
|
66 | + return $this->variables[$name]; |
|
67 | + } elseif (method_exists($this, 'var_'.$name)) { |
|
68 | + $method = 'var_'.$name; |
|
69 | 69 | |
70 | - return $this->$method(); |
|
71 | - } else { |
|
72 | - throw new \BadMethodCallException(static::class." does not define a '$name' field"); |
|
73 | - } |
|
74 | - } |
|
70 | + return $this->$method(); |
|
71 | + } else { |
|
72 | + throw new \BadMethodCallException(static::class." does not define a '$name' field"); |
|
73 | + } |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * @param string $name |
|
78 | - * @param mixed $value |
|
79 | - * |
|
80 | - * @throws \BadMethodCallException values cannot be assigned except with the display method |
|
81 | - */ |
|
82 | - public function __set($name, $value) |
|
83 | - { |
|
84 | - throw new \BadMethodCallException(static::class.' variables are read-only, cannot assign '.$name); |
|
85 | - } |
|
76 | + /** |
|
77 | + * @param string $name |
|
78 | + * @param mixed $value |
|
79 | + * |
|
80 | + * @throws \BadMethodCallException values cannot be assigned except with the display method |
|
81 | + */ |
|
82 | + public function __set($name, $value) |
|
83 | + { |
|
84 | + throw new \BadMethodCallException(static::class.' variables are read-only, cannot assign '.$name); |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * Set the data to be rendered in the view - note this does not actually render the view. |
|
89 | - * |
|
90 | - * @param array $variables |
|
91 | - */ |
|
92 | - public function display(array $variables) |
|
93 | - { |
|
94 | - if ($errors = $this->validateDisplayVariables($variables)) { |
|
95 | - throw new \InvalidArgumentException( |
|
96 | - "Invalid variables provided to ".static::class."::display()" |
|
97 | - ."\n - ".implode("\n - ", $errors) |
|
98 | - ); |
|
99 | - } |
|
87 | + /** |
|
88 | + * Set the data to be rendered in the view - note this does not actually render the view. |
|
89 | + * |
|
90 | + * @param array $variables |
|
91 | + */ |
|
92 | + public function display(array $variables) |
|
93 | + { |
|
94 | + if ($errors = $this->validateDisplayVariables($variables)) { |
|
95 | + throw new \InvalidArgumentException( |
|
96 | + "Invalid variables provided to ".static::class."::display()" |
|
97 | + ."\n - ".implode("\n - ", $errors) |
|
98 | + ); |
|
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 | } |