@@ -22,146 +22,146 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | abstract class Ingenerator_View_Layout extends View_Model |
| 24 | 24 | { |
| 25 | - // The name of the template model to use |
|
| 26 | - public $template = 'Global'; |
|
| 27 | - |
|
| 28 | - // Whether to render the view within the template or not, null for auto |
|
| 29 | - protected $_use_template = null; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Sets a new template to use for this view model. By default template names |
|
| 33 | - * become View_Template_$template classes. |
|
| 34 | - * |
|
| 35 | - * @param string $template |
|
| 36 | - */ |
|
| 37 | - public function set_template($template) |
|
| 38 | - { |
|
| 39 | - $this->template = $template; |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Returns the View_Model representing the template layout (instantiating it |
|
| 44 | - * if it has not previously been created). |
|
| 45 | - * |
|
| 46 | - * @return View_Model |
|
| 47 | - */ |
|
| 48 | - public function var_page() |
|
| 49 | - { |
|
| 50 | - if ( is_string($this->template)) |
|
| 51 | - { |
|
| 52 | - $class = 'View_Template_'.$this->template; |
|
| 53 | - if ( ! class_exists($class)) |
|
| 54 | - { |
|
| 55 | - $class = 'View'; |
|
| 56 | - } |
|
| 57 | - $this->template = new $class(); |
|
| 58 | - } |
|
| 59 | - return $this->template; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Hook called just before the body content is rendered, as a useful extension |
|
| 64 | - * point for transforming any data or setting variables on the page template. |
|
| 65 | - * |
|
| 66 | - * class View_Index extends View_Layout |
|
| 67 | - * { |
|
| 68 | - * public function pre_render() |
|
| 69 | - * { |
|
| 70 | - * $page = $this->var_page(); |
|
| 71 | - * $page->title = 'Index'; |
|
| 72 | - * } |
|
| 73 | - * } |
|
| 74 | - */ |
|
| 75 | - public function pre_render() |
|
| 76 | - { |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Hook called just after the body content is rendered, as a useful extension |
|
| 81 | - * point for transforming any data or setting variables on the page template. |
|
| 82 | - * |
|
| 83 | - * @see View_Layout::pre_render() |
|
| 84 | - */ |
|
| 85 | - public function post_render() |
|
| 86 | - { |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Renders the body content (calling [View_Layout::pre_render()] and |
|
| 92 | - * [View_Layout::post_render()] in the process). |
|
| 93 | - * |
|
| 94 | - * If required, the body content will then be passed to the template view |
|
| 95 | - * for rendering within the page layout and the fully templated page returned. |
|
| 96 | - * Otherwise, the body fragment is returned alone and the page template is |
|
| 97 | - * never rendered. |
|
| 98 | - * |
|
| 99 | - * @param string $file If wishing to generate a view to a file |
|
| 100 | - * @return string The formatted view content |
|
| 101 | - */ |
|
| 102 | - public function render($file = null) |
|
| 103 | - { |
|
| 104 | - $this->pre_render(); |
|
| 105 | - |
|
| 106 | - // Generate the body content |
|
| 107 | - $body = parent::render($file); |
|
| 108 | - |
|
| 109 | - $this->post_render(); |
|
| 110 | - |
|
| 111 | - // Render the template if required |
|
| 112 | - if ($this->use_template()) |
|
| 113 | - { |
|
| 114 | - $template = $this->var_page(); |
|
| 115 | - $template->set('var_body', $body); |
|
| 116 | - return $template->render(); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - return $body; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Setter/Getter for the use_template setting determining whether or not to |
|
| 124 | - * render the page template or just the body fragment. |
|
| 125 | - * |
|
| 126 | - * By default, $this-_use_template is null, meaning the class should determine |
|
| 127 | - * automatically based on whether the request is an AJAX request. |
|
| 128 | - * |
|
| 129 | - * If manually set true or false, the class will respect this value. |
|
| 130 | - * |
|
| 131 | - * @uses Request::is_ajax |
|
| 132 | - * @uses Request::current() |
|
| 133 | - * |
|
| 134 | - * @param boolean $use_template |
|
| 135 | - * @return boolean If called as getter |
|
| 136 | - * @return View_Layout If called as setter |
|
| 137 | - */ |
|
| 138 | - public function use_template($use_template = null) |
|
| 139 | - { |
|
| 140 | - // If we're being called as a setter |
|
| 141 | - if ($use_template !== null) |
|
| 142 | - { |
|
| 143 | - $this->_use_template = $use_template; |
|
| 144 | - return $this; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - // If an explicit value was set |
|
| 148 | - if ($this->_use_template !== null) |
|
| 149 | - { |
|
| 150 | - return $this->_use_template; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - // Guess based on is_ajax |
|
| 154 | - if (Request::current()) |
|
| 155 | - { |
|
| 156 | - if (Request::current()->is_ajax()) |
|
| 157 | - { |
|
| 158 | - return false; |
|
| 159 | - } |
|
| 160 | - else |
|
| 161 | - { |
|
| 162 | - return true; |
|
| 163 | - } |
|
| 164 | - } |
|
| 165 | - } |
|
| 25 | + // The name of the template model to use |
|
| 26 | + public $template = 'Global'; |
|
| 27 | + |
|
| 28 | + // Whether to render the view within the template or not, null for auto |
|
| 29 | + protected $_use_template = null; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Sets a new template to use for this view model. By default template names |
|
| 33 | + * become View_Template_$template classes. |
|
| 34 | + * |
|
| 35 | + * @param string $template |
|
| 36 | + */ |
|
| 37 | + public function set_template($template) |
|
| 38 | + { |
|
| 39 | + $this->template = $template; |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Returns the View_Model representing the template layout (instantiating it |
|
| 44 | + * if it has not previously been created). |
|
| 45 | + * |
|
| 46 | + * @return View_Model |
|
| 47 | + */ |
|
| 48 | + public function var_page() |
|
| 49 | + { |
|
| 50 | + if ( is_string($this->template)) |
|
| 51 | + { |
|
| 52 | + $class = 'View_Template_'.$this->template; |
|
| 53 | + if ( ! class_exists($class)) |
|
| 54 | + { |
|
| 55 | + $class = 'View'; |
|
| 56 | + } |
|
| 57 | + $this->template = new $class(); |
|
| 58 | + } |
|
| 59 | + return $this->template; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Hook called just before the body content is rendered, as a useful extension |
|
| 64 | + * point for transforming any data or setting variables on the page template. |
|
| 65 | + * |
|
| 66 | + * class View_Index extends View_Layout |
|
| 67 | + * { |
|
| 68 | + * public function pre_render() |
|
| 69 | + * { |
|
| 70 | + * $page = $this->var_page(); |
|
| 71 | + * $page->title = 'Index'; |
|
| 72 | + * } |
|
| 73 | + * } |
|
| 74 | + */ |
|
| 75 | + public function pre_render() |
|
| 76 | + { |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Hook called just after the body content is rendered, as a useful extension |
|
| 81 | + * point for transforming any data or setting variables on the page template. |
|
| 82 | + * |
|
| 83 | + * @see View_Layout::pre_render() |
|
| 84 | + */ |
|
| 85 | + public function post_render() |
|
| 86 | + { |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Renders the body content (calling [View_Layout::pre_render()] and |
|
| 92 | + * [View_Layout::post_render()] in the process). |
|
| 93 | + * |
|
| 94 | + * If required, the body content will then be passed to the template view |
|
| 95 | + * for rendering within the page layout and the fully templated page returned. |
|
| 96 | + * Otherwise, the body fragment is returned alone and the page template is |
|
| 97 | + * never rendered. |
|
| 98 | + * |
|
| 99 | + * @param string $file If wishing to generate a view to a file |
|
| 100 | + * @return string The formatted view content |
|
| 101 | + */ |
|
| 102 | + public function render($file = null) |
|
| 103 | + { |
|
| 104 | + $this->pre_render(); |
|
| 105 | + |
|
| 106 | + // Generate the body content |
|
| 107 | + $body = parent::render($file); |
|
| 108 | + |
|
| 109 | + $this->post_render(); |
|
| 110 | + |
|
| 111 | + // Render the template if required |
|
| 112 | + if ($this->use_template()) |
|
| 113 | + { |
|
| 114 | + $template = $this->var_page(); |
|
| 115 | + $template->set('var_body', $body); |
|
| 116 | + return $template->render(); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + return $body; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Setter/Getter for the use_template setting determining whether or not to |
|
| 124 | + * render the page template or just the body fragment. |
|
| 125 | + * |
|
| 126 | + * By default, $this-_use_template is null, meaning the class should determine |
|
| 127 | + * automatically based on whether the request is an AJAX request. |
|
| 128 | + * |
|
| 129 | + * If manually set true or false, the class will respect this value. |
|
| 130 | + * |
|
| 131 | + * @uses Request::is_ajax |
|
| 132 | + * @uses Request::current() |
|
| 133 | + * |
|
| 134 | + * @param boolean $use_template |
|
| 135 | + * @return boolean If called as getter |
|
| 136 | + * @return View_Layout If called as setter |
|
| 137 | + */ |
|
| 138 | + public function use_template($use_template = null) |
|
| 139 | + { |
|
| 140 | + // If we're being called as a setter |
|
| 141 | + if ($use_template !== null) |
|
| 142 | + { |
|
| 143 | + $this->_use_template = $use_template; |
|
| 144 | + return $this; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + // If an explicit value was set |
|
| 148 | + if ($this->_use_template !== null) |
|
| 149 | + { |
|
| 150 | + return $this->_use_template; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + // Guess based on is_ajax |
|
| 154 | + if (Request::current()) |
|
| 155 | + { |
|
| 156 | + if (Request::current()->is_ajax()) |
|
| 157 | + { |
|
| 158 | + return false; |
|
| 159 | + } |
|
| 160 | + else |
|
| 161 | + { |
|
| 162 | + return true; |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | 167 | } |
@@ -47,7 +47,7 @@ |
||
| 47 | 47 | */ |
| 48 | 48 | public function var_page() |
| 49 | 49 | { |
| 50 | - if ( is_string($this->template)) |
|
| 50 | + if (is_string($this->template)) |
|
| 51 | 51 | { |
| 52 | 52 | $class = 'View_Template_'.$this->template; |
| 53 | 53 | if ( ! class_exists($class)) |
@@ -156,8 +156,7 @@ |
||
| 156 | 156 | if (Request::current()->is_ajax()) |
| 157 | 157 | { |
| 158 | 158 | return false; |
| 159 | - } |
|
| 160 | - else |
|
| 159 | + } else |
|
| 161 | 160 | { |
| 162 | 161 | return true; |
| 163 | 162 | } |
@@ -16,5 +16,5 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | abstract class Ingenerator_View_Template_Global extends View_Model |
| 18 | 18 | { |
| 19 | - public $var_title = 'Page title'; |
|
| 19 | + public $var_title = 'Page title'; |
|
| 20 | 20 | } |
@@ -8,6 +8,6 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | return array( |
| 10 | 10 | 'modules' => array( |
| 11 | - 'kohana-view' => __DIR__, |
|
| 11 | + 'kohana-view' => __DIR__, |
|
| 12 | 12 | ) |
| 13 | 13 | ); |
@@ -10,9 +10,9 @@ |
||
| 10 | 10 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> |
| 11 | 11 | <html> |
| 12 | 12 | <head> |
| 13 | - <title><?=$title;?></title> |
|
| 13 | + <title><?=$title; ?></title> |
|
| 14 | 14 | </head> |
| 15 | 15 | <body> |
| 16 | - <?=$body;?> |
|
| 16 | + <?=$body; ?> |
|
| 17 | 17 | </body> |
| 18 | 18 | </html> |
@@ -16,8 +16,8 @@ |
||
| 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 | } |
@@ -18,61 +18,61 @@ |
||
| 18 | 18 | class ViewTemplateSelector |
| 19 | 19 | { |
| 20 | 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 | - } |
|
| 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 | 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 | - } |
|
| 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 | 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 | - } |
|
| 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 | 57 | |
| 58 | - return $template; |
|
| 59 | - } |
|
| 58 | + return $template; |
|
| 59 | + } |
|
| 60 | 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 | - } |
|
| 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 | 74 | |
| 75 | - return $template; |
|
| 76 | - } |
|
| 75 | + return $template; |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | 78 | } |
@@ -37,81 +37,81 @@ |
||
| 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 | - $errors = []; |
|
| 95 | - $provided_variables = array_keys($variables); |
|
| 96 | - foreach (array_diff($provided_variables, $this->expect_var_names) as $unexpected_var) { |
|
| 97 | - if (method_exists($this, 'var_'.$unexpected_var)) { |
|
| 98 | - $errors[] = "'$unexpected_var' conflicts with ::var_$unexpected_var()"; |
|
| 99 | - } else { |
|
| 100 | - $errors[] = "'$unexpected_var' is not expected"; |
|
| 101 | - } |
|
| 102 | - } |
|
| 103 | - foreach (array_diff($this->expect_var_names, $provided_variables) as $missing_var) { |
|
| 104 | - $errors[] = "'$missing_var' is missing"; |
|
| 105 | - } |
|
| 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 | + $errors = []; |
|
| 95 | + $provided_variables = array_keys($variables); |
|
| 96 | + foreach (array_diff($provided_variables, $this->expect_var_names) as $unexpected_var) { |
|
| 97 | + if (method_exists($this, 'var_'.$unexpected_var)) { |
|
| 98 | + $errors[] = "'$unexpected_var' conflicts with ::var_$unexpected_var()"; |
|
| 99 | + } else { |
|
| 100 | + $errors[] = "'$unexpected_var' is not expected"; |
|
| 101 | + } |
|
| 102 | + } |
|
| 103 | + foreach (array_diff($this->expect_var_names, $provided_variables) as $missing_var) { |
|
| 104 | + $errors[] = "'$missing_var' is missing"; |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - if ( ! empty($errors)) { |
|
| 108 | - throw new \InvalidArgumentException( |
|
| 109 | - "Invalid variables provided to ".static::class."::display()" |
|
| 110 | - ."\n - ".implode("\n - ", $errors) |
|
| 111 | - ); |
|
| 112 | - } |
|
| 107 | + if ( ! empty($errors)) { |
|
| 108 | + throw new \InvalidArgumentException( |
|
| 109 | + "Invalid variables provided to ".static::class."::display()" |
|
| 110 | + ."\n - ".implode("\n - ", $errors) |
|
| 111 | + ); |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - $this->variables = $variables; |
|
| 115 | - } |
|
| 114 | + $this->variables = $variables; |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | 117 | } |
@@ -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 | } |