| @@ 11-122 (lines=112) @@ | ||
| 8 | /** |
|
| 9 | * A view connected to a template file. |
|
| 10 | */ |
|
| 11 | class View |
|
| 12 | { |
|
| 13 | /** |
|
| 14 | * @var $template Template file or array |
|
| 15 | * @var $templateData Data to send to template file |
|
| 16 | * @var $sortOrder For sorting views |
|
| 17 | * @var $type Type of view |
|
| 18 | */ |
|
| 19 | private $template; |
|
| 20 | private $templateData = []; |
|
| 21 | private $sortOrder; |
|
| 22 | private $type; |
|
| 23 | ||
| 24 | ||
| 25 | ||
| 26 | /** |
|
| 27 | * Set values for the view. |
|
| 28 | * |
|
| 29 | * @param array|string $template the template file, or array |
|
| 30 | * @param array $data variables to make available to the |
|
| 31 | * view, default is empty |
|
| 32 | * @param integer $sort which order to display the views, |
|
| 33 | * if suitable |
|
| 34 | * @param string $type which type of view |
|
| 35 | * |
|
| 36 | * @return self |
|
| 37 | */ |
|
| 38 | public function set($template, $data = [], $sort = 0, $type = "file") |
|
| 39 | { |
|
| 40 | if (is_array($template)) { |
|
| 41 | if (isset($template["callback"])) { |
|
| 42 | $type = "callback"; |
|
| 43 | $this->template = $template; |
|
| 44 | } else { |
|
| 45 | $this->template = $template["template"]; |
|
| 46 | } |
|
| 47 | ||
| 48 | $this->templateData = isset($template["data"]) |
|
| 49 | ? $template["data"] |
|
| 50 | : $data; |
|
| 51 | ||
| 52 | $this->sortOrder = isset($template["sort"]) |
|
| 53 | ? $template["sort"] |
|
| 54 | : $sort; |
|
| 55 | ||
| 56 | $this->type = isset($template["type"]) |
|
| 57 | ? $template["type"] |
|
| 58 | : $type; |
|
| 59 | ||
| 60 | return; |
|
| 61 | } |
|
| 62 | ||
| 63 | $this->template = $template; |
|
| 64 | $this->templateData = $data; |
|
| 65 | $this->sortOrder = $sort; |
|
| 66 | $this->type = $type; |
|
| 67 | ||
| 68 | return $this; |
|
| 69 | } |
|
| 70 | ||
| 71 | ||
| 72 | ||
| 73 | /** |
|
| 74 | * Render the view. |
|
| 75 | * |
|
| 76 | * @param object $app optional with access to the framework resources. |
|
| 77 | * |
|
| 78 | * @return void |
|
| 79 | */ |
|
| 80 | public function render($app = null) |
|
| 81 | { |
|
| 82 | switch ($this->type) { |
|
| 83 | case "file": |
|
| 84 | if (!$app) { |
|
| 85 | throw new Exception("View missing \$app."); |
|
| 86 | } |
|
| 87 | $viewRender = new ViewRenderFile(); |
|
| 88 | $viewRender->setApp($app); |
|
| 89 | $viewRender->render($this->template, $this->templateData); |
|
| 90 | break; |
|
| 91 | ||
| 92 | case "callback": |
|
| 93 | if (!isset($this->template["callback"]) || !is_callable($this->template["callback"])) { |
|
| 94 | throw new Exception("View missing callback."); |
|
| 95 | } |
|
| 96 | ||
| 97 | echo call_user_func($this->template["callback"]); |
|
| 98 | ||
| 99 | break; |
|
| 100 | ||
| 101 | case "string": |
|
| 102 | echo $this->template; |
|
| 103 | ||
| 104 | break; |
|
| 105 | ||
| 106 | default: |
|
| 107 | throw new Exception("Not a valid template type: {$this->type}"); |
|
| 108 | } |
|
| 109 | } |
|
| 110 | ||
| 111 | ||
| 112 | ||
| 113 | /** |
|
| 114 | * Give the sort order for this view. |
|
| 115 | * |
|
| 116 | * @return int |
|
| 117 | */ |
|
| 118 | public function sortOrder() |
|
| 119 | { |
|
| 120 | return $this->sortOrder; |
|
| 121 | } |
|
| 122 | } |
|
| 123 | ||
| @@ 12-123 (lines=112) @@ | ||
| 9 | /** |
|
| 10 | * A view connected to a template file, supporting Anax DI. |
|
| 11 | */ |
|
| 12 | class View2 |
|
| 13 | { |
|
| 14 | /** |
|
| 15 | * @var $template Template file or array |
|
| 16 | * @var $templateData Data to send to template file |
|
| 17 | * @var $sortOrder For sorting views |
|
| 18 | * @var $type Type of view |
|
| 19 | */ |
|
| 20 | private $template; |
|
| 21 | private $templateData = []; |
|
| 22 | private $sortOrder; |
|
| 23 | private $type; |
|
| 24 | ||
| 25 | ||
| 26 | ||
| 27 | /** |
|
| 28 | * Set values for the view. |
|
| 29 | * |
|
| 30 | * @param array|string $template the template file, or array |
|
| 31 | * @param array $data variables to make available to the |
|
| 32 | * view, default is empty |
|
| 33 | * @param integer $sort which order to display the views, |
|
| 34 | * if suitable |
|
| 35 | * @param string $type which type of view |
|
| 36 | * |
|
| 37 | * @return self |
|
| 38 | */ |
|
| 39 | public function set($template, $data = [], $sort = 0, $type = "file") |
|
| 40 | { |
|
| 41 | if (is_array($template)) { |
|
| 42 | if (isset($template["callback"])) { |
|
| 43 | $type = "callback"; |
|
| 44 | $this->template = $template; |
|
| 45 | } else { |
|
| 46 | $this->template = $template["template"]; |
|
| 47 | } |
|
| 48 | ||
| 49 | $this->templateData = isset($template["data"]) |
|
| 50 | ? $template["data"] |
|
| 51 | : $data; |
|
| 52 | ||
| 53 | $this->sortOrder = isset($template["sort"]) |
|
| 54 | ? $template["sort"] |
|
| 55 | : $sort; |
|
| 56 | ||
| 57 | $this->type = isset($template["type"]) |
|
| 58 | ? $template["type"] |
|
| 59 | : $type; |
|
| 60 | ||
| 61 | return; |
|
| 62 | } |
|
| 63 | ||
| 64 | $this->template = $template; |
|
| 65 | $this->templateData = $data; |
|
| 66 | $this->sortOrder = $sort; |
|
| 67 | $this->type = $type; |
|
| 68 | ||
| 69 | return $this; |
|
| 70 | } |
|
| 71 | ||
| 72 | ||
| 73 | ||
| 74 | /** |
|
| 75 | * Render the view. |
|
| 76 | * |
|
| 77 | * @param object $di optional with access to the framework resources. |
|
| 78 | * |
|
| 79 | * @return void |
|
| 80 | */ |
|
| 81 | public function render(DIInterface $di = null) |
|
| 82 | { |
|
| 83 | switch ($this->type) { |
|
| 84 | case "file": |
|
| 85 | if ($di->has("viewRenderFile")) { |
|
| 86 | $viewRender = $di->get("viewRenderFile"); |
|
| 87 | } else { |
|
| 88 | $viewRender = new ViewRenderFile($di); |
|
| 89 | } |
|
| 90 | $viewRender->render($this->template, $this->templateData); |
|
| 91 | break; |
|
| 92 | ||
| 93 | case "callback": |
|
| 94 | if (!isset($this->template["callback"]) || !is_callable($this->template["callback"])) { |
|
| 95 | throw new Exception("View missing callback."); |
|
| 96 | } |
|
| 97 | ||
| 98 | echo call_user_func($this->template["callback"]); |
|
| 99 | ||
| 100 | break; |
|
| 101 | ||
| 102 | case "string": |
|
| 103 | echo $this->template; |
|
| 104 | ||
| 105 | break; |
|
| 106 | ||
| 107 | default: |
|
| 108 | throw new Exception("Not a valid template type: {$this->type}"); |
|
| 109 | } |
|
| 110 | } |
|
| 111 | ||
| 112 | ||
| 113 | ||
| 114 | /** |
|
| 115 | * Give the sort order for this view. |
|
| 116 | * |
|
| 117 | * @return int |
|
| 118 | */ |
|
| 119 | public function sortOrder() |
|
| 120 | { |
|
| 121 | return $this->sortOrder; |
|
| 122 | } |
|
| 123 | } |
|
| 124 | ||