|
1
|
|
|
<?php |
|
2
|
|
|
namespace Ingenerator\KohanaView; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* The ViewTemplateSelector maps ViewModel classes to the appropriate template file. By default this is done by |
|
6
|
|
|
* converting the class name to a lowercased file path such that: |
|
7
|
|
|
* |
|
8
|
|
|
* View_Model_Something_WithMixedCase => model/something/with_mixed_case |
|
9
|
|
|
* \View\Stuff\In\NameSpace\Thing => stuff/in/name_space/thing |
|
10
|
|
|
* \Other\View\Hierarchy => other/view/hierarchy |
|
11
|
|
|
* |
|
12
|
|
|
* Views can also implement TemplateSpecifyingViewModel to provide a custom template file name when required. |
|
13
|
|
|
* |
|
14
|
|
|
* @author Andrew Coulton <[email protected]> |
|
15
|
|
|
* @copyright 2015 inGenerator Ltd |
|
16
|
|
|
* @license http://kohanaframework.org/license |
|
17
|
|
|
*/ |
|
18
|
|
|
class ViewTemplateSelector |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param ViewModel $view |
|
23
|
|
|
* |
|
24
|
|
|
* @return string |
|
25
|
|
|
*/ |
|
26
|
|
|
public function getTemplateName(ViewModel $view) |
|
27
|
|
|
{ |
|
28
|
|
|
if ($view instanceof TemplateSpecifyingViewModel) { |
|
29
|
|
|
return $this->validateSpecifiedTemplateName($view); |
|
30
|
|
|
} else { |
|
31
|
|
|
return $this->calculateTemplateFromClassName($view); |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param TemplateSpecifyingViewModel $view |
|
37
|
|
|
* |
|
38
|
|
|
* @return mixed |
|
39
|
|
|
* @throws \UnexpectedValueException if no template is provided |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function validateSpecifiedTemplateName(TemplateSpecifyingViewModel $view) |
|
42
|
|
|
{ |
|
43
|
|
|
$template = $view->getTemplateName(); |
|
44
|
|
|
$view_class = get_class($view); |
|
45
|
|
|
if ( ! $template) { |
|
46
|
|
|
throw new \UnexpectedValueException( |
|
47
|
|
|
"$view_class::getTemplateName() must return a template name, empty value returned" |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if ( ! is_string($template)) { |
|
52
|
|
|
$type = is_object($template) ? get_class($template) : gettype($template); |
|
53
|
|
|
throw new \UnexpectedValueException( |
|
54
|
|
|
"$view_class::getTemplateName() must return a string template name, $type value returned" |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $template; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param ViewModel $view |
|
63
|
|
|
* |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function calculateTemplateFromClassName(ViewModel $view) |
|
67
|
|
|
{ |
|
68
|
|
|
$template = preg_replace('/\\\\|_/', '/', get_class($view)); |
|
69
|
|
|
$template = preg_replace('/([a-z])([A-Z])/', '\1_\2', $template); |
|
70
|
|
|
$template = strtolower($template); |
|
71
|
|
|
if (substr($template, 0, 5) === 'view/') { |
|
72
|
|
|
$template = substr($template, 5); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $template; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|