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: $view = _i(_ds(_cr(), $viewName . '.php'));
121: if ($view) {
122: extract($this->data);
123: include $view;
124: } else {
125: throw new \RuntimeException('View file is missing.');
126: }
127: }
128:
129: /**
130: * Display block view
131: * @param string $name Block view name to the file name with or without extension php
132: * @param array $data The data injected to the view
133: * @param bool $return To return html or not
134: * @return false|string|void
135: */
136: public function block($name, array $data = array(), $return = false)
137: {
138: $name = rtrim($name, '.php') . '.php';
139:
140: $paths = array();
141: if (strrpos($name, '/') !== false) {
142: $paths[] = $name; // in the given directory path
143: } else {
144: $paths[] = _ds(_cr(), $name); // in the current directory
145: }
146:
147: $paths[] = _ds('inc', 'tpl', $name); // in app/inc/tpl or /inc/tpl
148:
149: $this->data = array_merge($this->data, $data);
150:
151: foreach ($paths as $file) {
152: $block = _i($file);
153: if ($block) {
154: extract($this->data);
155: if ($return) {
156: ob_start();
157: include $block;
158: return ob_get_clean();
159: } else {
160: include $block;
161: }
162:
163: return;
164: }
165: }
166:
167: throw new \RuntimeException('Block view file "' . $name . '" is missing.');
168: }
169:
170: /**
171: * Include CSS files in head section. Make sure calling this method in head
172: */
173: public function headStyle()
174: {
175: foreach ($this->headStyles as $file) {
176: _css($file);
177: }
178: }
179:
180: /**
181: * Include JS files in head section. Make sure calling this method in head
182: */
183: public function headScript()
184: {
185: foreach ($this->headScripts as $file) {
186: _js($file);
187: }
188: }
189: }
190: