1: <?php
2: /**
3: * This file is part of the PHPLucidFrame library.
4: * SchemaManager manages your database schema.
5: *
6: * @package PHPLucidFrame\Core
7: * @since PHPLucidFrame v 3.0.0
8: * @copyright Copyright (c), PHPLucidFrame.
9: * @link http://phplucidframe.com
10: * @license http://www.opensource.org/licenses/mit-license.php MIT License
11: * This source file is subject to the MIT license that is bundled
12: * with this source code in the file LICENSE
13: */
14:
15: namespace LucidFrame\Core;
16:
17: class View
18: {
19: /**
20: * @var string Layout file name
21: */
22: private $layout;
23: /**
24: * @var string View name to append to the file name such as view_{$name}.php
25: */
26: private $name;
27: /**
28: * @var array Array of data passed into view
29: */
30: private $data = array();
31: /**
32: * @var array Array of css file names
33: */
34: private $headStyles = array();
35: /**
36: * @var array Array of js file names
37: */
38: private $headScripts = array();
39:
40: /**
41: * View constructor.
42: */
43: public function __construct()
44: {
45: $this->layout = _cfg('layoutName');
46: }
47:
48: /**
49: * Setter
50: *
51: * @param string $name The property name
52: * @param mixed $value The property value
53: */
54: public function __set($name, $value)
55: {
56: $this->$name = $value;
57: }
58:
59: /**
60: * Getter
61: *
62: * @param string $name The property name
63: * @return mixed
64: */
65: public function __get($name)
66: {
67: return $this->$name;
68: }
69:
70: /**
71: * Add data into view
72: *
73: * @param string $key The variable name to be accessible in view
74: * @param mixed $value The value of the variable
75: */
76:
77: public function addData($key, $value)
78: {
79: $this->data[$key] = $value;
80: }
81:
82: /**
83: * Add CSS file to be included in head section
84: * @param string $file An absolute file path or file name only.
85: * The file name only will be prepended the folder name css/ and it will be looked in every sub-sites "css" folder
86: */
87: public function addHeadStyle($file)
88: {
89: $this->headStyles[] = $file;
90: $this->headStyles = array_unique($this->headStyles);
91: }
92:
93: /**
94: * Add JS file to be included in head section
95: * @param string $file An absolute file path or file name only.
96: * The file name only will be prepended the folder name js/ and it will be looked in every sub-sites "js" folder
97: */
98: public function addHeadScript($file)
99: {
100: $this->headScripts[] = $file;
101: $this->headScripts = array_unique($this->headScripts);
102: }
103:
104: /**
105: * Display view
106: *
107: * @param string $name Optional view name to append to the file name such as view_{$name}.php
108: * @return void
109: */
110: public function load($name = '')
111: {
112: $name = $name ?: $this->name;
113:
114: if ($name) {
115: $viewName = 'view_' . $name;
116: } else {
117: $viewName = 'view';
118: }
119:
120: $page = _app('page');
121: if ($page instanceof \Closure) {
122: echo $page();
123: } else {
124: $view = _i(_ds(_cr(), $viewName . '.php'));
125: if ($view) {
126: extract($this->data);
127: include $view;
128: } else {
129: throw new \RuntimeException('View file is missing.');
130: }
131: }
132: }
133:
134: /**
135: * Display block view
136: * @param string $name Block view name to the file name with or without extension php
137: * @param array $data The data injected to the view
138: * @param bool $return To return html or not
139: * @return false|string|void
140: */
141: public function block($name, array $data = array(), $return = false)
142: {
143: $name = rtrim($name, '.php') . '.php';
144:
145: $paths = array();
146: if (strrpos($name, '/') !== false) {
147: $paths[] = $name; // in the given directory path
148: } else {
149: $paths[] = _ds(_cr(), $name); // in the current directory
150: }
151:
152: $paths[] = _ds('inc', 'tpl', $name); // in app/inc/tpl or /inc/tpl
153:
154: $this->data = array_merge($this->data, $data);
155:
156: foreach ($paths as $file) {
157: if (is_file($file) && file_exists($file)) {
158: $block = $file;
159: } else {
160: $block = _i($file);
161: }
162:
163: if ($block) {
164: extract($this->data);
165: if ($return) {
166: ob_start();
167: include $block;
168: return ob_get_clean();
169: } else {
170: include $block;
171: }
172:
173: return;
174: }
175: }
176:
177: throw new \RuntimeException('Block view file "' . $name . '" is missing.');
178: }
179:
180: /**
181: * Include CSS files in head section. Make sure calling this method in head
182: */
183: public function headStyle()
184: {
185: foreach ($this->headStyles as $file) {
186: _css($file);
187: }
188: }
189:
190: /**
191: * Include JS files in head section. Make sure calling this method in head
192: */
193: public function headScript()
194: {
195: foreach ($this->headScripts as $file) {
196: _js($file);
197: }
198: }
199: }
200: