@@ -19,75 +19,75 @@ |
||
19 | 19 | */ |
20 | 20 | class View |
21 | 21 | { |
22 | - use SingletonTrait; |
|
23 | - |
|
24 | - const DIRECTORY_SEPARATOR = \DIRECTORY_SEPARATOR; |
|
25 | - |
|
26 | - /** @var string|null the base path. */ |
|
27 | - private $basePath; |
|
28 | - |
|
29 | - /** |
|
30 | - * Construct a object. |
|
31 | - */ |
|
32 | - protected function __construct() |
|
33 | - { |
|
34 | - $this->basePath = null; |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * Returns the base path. |
|
39 | - * |
|
40 | - * @return string|null the base path. |
|
41 | - */ |
|
42 | - public static function getBasePath() |
|
43 | - { |
|
44 | - return static::getInstance()->basePath; |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * Set the base path. |
|
49 | - * |
|
50 | - * @param string|null $basePath = null |
|
51 | - */ |
|
52 | - public static function setBasePath($basePath = null) |
|
53 | - { |
|
54 | - static::getInstance()->basePath = $basePath; |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * Returns the view at the given path with the given data. |
|
59 | - * |
|
60 | - * @param string $path |
|
61 | - * @param string[] $data = [] |
|
62 | - * @return string a string representation of the view. |
|
63 | - */ |
|
64 | - public static function get($path, $data = []) |
|
65 | - { |
|
66 | - ob_start(); |
|
67 | - |
|
68 | - extract($data); |
|
69 | - |
|
70 | - try { |
|
71 | - $basePath = static::getInstance()->basePath; |
|
72 | - |
|
73 | - if ($basePath !== null) { |
|
74 | - if (mb_substr($path, 0, 1) === static::DIRECTORY_SEPARATOR) { |
|
75 | - $path = mb_substr($path, 1); |
|
76 | - } |
|
77 | - |
|
78 | - if (mb_substr($basePath, -1) === static::DIRECTORY_SEPARATOR) { |
|
79 | - $basePath = mb_substr($basePath, 0, -1); |
|
80 | - } |
|
81 | - |
|
82 | - $path = $basePath . static::DIRECTORY_SEPARATOR . $path; |
|
83 | - } |
|
84 | - |
|
85 | - include $path; |
|
86 | - } catch (\Exception $e) { |
|
87 | - ob_get_clean(); |
|
88 | - throw $e; |
|
89 | - } |
|
90 | - |
|
91 | - return ob_get_clean(); |
|
92 | - } |
|
22 | + use SingletonTrait; |
|
23 | + |
|
24 | + const DIRECTORY_SEPARATOR = \DIRECTORY_SEPARATOR; |
|
25 | + |
|
26 | + /** @var string|null the base path. */ |
|
27 | + private $basePath; |
|
28 | + |
|
29 | + /** |
|
30 | + * Construct a object. |
|
31 | + */ |
|
32 | + protected function __construct() |
|
33 | + { |
|
34 | + $this->basePath = null; |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * Returns the base path. |
|
39 | + * |
|
40 | + * @return string|null the base path. |
|
41 | + */ |
|
42 | + public static function getBasePath() |
|
43 | + { |
|
44 | + return static::getInstance()->basePath; |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * Set the base path. |
|
49 | + * |
|
50 | + * @param string|null $basePath = null |
|
51 | + */ |
|
52 | + public static function setBasePath($basePath = null) |
|
53 | + { |
|
54 | + static::getInstance()->basePath = $basePath; |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * Returns the view at the given path with the given data. |
|
59 | + * |
|
60 | + * @param string $path |
|
61 | + * @param string[] $data = [] |
|
62 | + * @return string a string representation of the view. |
|
63 | + */ |
|
64 | + public static function get($path, $data = []) |
|
65 | + { |
|
66 | + ob_start(); |
|
67 | + |
|
68 | + extract($data); |
|
69 | + |
|
70 | + try { |
|
71 | + $basePath = static::getInstance()->basePath; |
|
72 | + |
|
73 | + if ($basePath !== null) { |
|
74 | + if (mb_substr($path, 0, 1) === static::DIRECTORY_SEPARATOR) { |
|
75 | + $path = mb_substr($path, 1); |
|
76 | + } |
|
77 | + |
|
78 | + if (mb_substr($basePath, -1) === static::DIRECTORY_SEPARATOR) { |
|
79 | + $basePath = mb_substr($basePath, 0, -1); |
|
80 | + } |
|
81 | + |
|
82 | + $path = $basePath . static::DIRECTORY_SEPARATOR . $path; |
|
83 | + } |
|
84 | + |
|
85 | + include $path; |
|
86 | + } catch (\Exception $e) { |
|
87 | + ob_get_clean(); |
|
88 | + throw $e; |
|
89 | + } |
|
90 | + |
|
91 | + return ob_get_clean(); |
|
92 | + } |
|
93 | 93 | } |