-
Notifications
You must be signed in to change notification settings - Fork 2
/
View.php
108 lines (95 loc) 路 2.43 KB
/
View.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/*
* Inphinit
*
* Copyright (c) 2024 Guilherme Nascimento (brcontainer@yahoo.com.br)
*
* Released under the MIT license
*/
namespace Inphinit\Viewing;
use Inphinit\App;
class View
{
private static $force = false;
private static $views = array();
private static $shared = array();
/**
* Force the `View::render` method to render at the time it is called
*
* @return void
*/
public static function forceRender()
{
self::$force = true;
}
/**
* Starts rendering of registered views. After calling this method call it will automatically
* execute `View::forceRender()`
*
* @return void
*/
public static function dispatch()
{
if (self::$force === false) {
self::forceRender();
foreach (self::$views as &$value) {
if ($value) {
self::render($value[0], $value[1]);
}
}
}
}
/**
* Share or remove shared data to Views, shared variables will be added as variables to the views that will be executed later
*
* @param string $key
* @param mixed $value
* @return void
*/
public static function data($key, $value)
{
if ($value === null) {
unset(self::$shared[$key]);
} else {
self::$shared[$key] = $value;
}
}
/**
* Check if view exists in ./application/View/ folder
*
* @param string $view
* @return bool
*/
public static function exists($view)
{
return inphinit_path_check(INPHINIT_SYSTEM . '/views/' . str_replace('.', '/', $view) . '.php');
}
/**
* Register or render a View. If View is registered this method returns the index number from View
*
* @param string $view
* @param array $data
* @return int
*/
public static function render($view, array $data = array())
{
if (self::$force === false) {
return array_push(self::$views, array($view, $data)) - 1;
}
$data += self::$shared;
inphinit_sandbox('views/' . str_replace('.', '/', $view) . '.php', $data);
return -1;
}
/**
* Remove a registered View by index
*
* @param int $index
* @return void
*/
public static function remove($index)
{
if (isset(self::$views[$index])) {
unset(self::$views[$index]);
}
}
}