-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNano.php
More file actions
153 lines (127 loc) · 3.22 KB
/
Copy pathNano.php
File metadata and controls
153 lines (127 loc) · 3.22 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
namespace framework;
use nano\Components\BaseObject;
use nano\Components\Web\App;
use nano\Interfaces\BaseObjectInterface;
use nano\Interfaces\Core\AppInterface;
include_once __DIR__ . DIRECTORY_SEPARATOR . "const.php";
/**
* class `Nano`
* Main framework class.
*/
class Nano extends BaseObject
{
// Constants
/** @var string current version of the Nano framework */
public const VERSION = '1.0.0';
/** @var string */
private const PHP_CRITERIA = '8.0.0';
// Property
/** @var array $config array with config */
public static array $config = [];
/** @var AppInterface $app main App object */
public static AppInterface $app;
// Methods
/**
* Method - headliner this party.
*
* First call on App.
*
* @param array $configs
*
* @return AppInterface
*/
public static function setup(array $configs = []): AppInterface
{
self::checkPhpVersion();
foreach ($configs as $config)
{
static::$config = self::merge(static::$config, $config);
}
/** @var App $app */
$app = static::getComponent(APP);
static::$app = $app;
return static::$app;
}
/**
* @param array $parent
* @param array $children
*
* @return array
*/
public static function merge( array $parent, array $children )
{
foreach ( $children as $key => $value )
{
$parent[$key] = ( is_array($value) && isset($parent[$key]) )
? self::merge( $parent[$key], $value )
: $value;
}
return $parent;
}
/**
* Checker PHP version.
* Stop App if version incorrect
*
* @return void
*/
public static function checkPhpVersion(): void
{
if (version_compare(phpversion(), self::PHP_CRITERIA, '<'))
{
die(
sprintf(
"Server PHP version is less than %s it is impossible to continue.",
self::PHP_CRITERIA
)
);
}
}
/**
* get path to directory by ID on context "config"
*
* @param string $id
*
* @return ?string
*/
public static function dir(string $id ): ?string
{
return static::cfg(DIR, $id);
}
/**
* get system component by ID on context "config"
*
* @param $id
*
* @return ?BaseObjectInterface
*/
public static function getComponent($id): ?BaseObjectInterface
{
return ( $className = static::findClass($id) ) ? new $className() : null;
}
/**
* get `class name` from "config"
*
* @param string $id
* @param null $default
*
* @return ?string
*/
public static function findClass(string $id, $default = NULL): ?string
{
return static::cfg(COMPONENTS, $id, $default );
}
/**
* get `config value` from "config"
*
* @param string $group
* @param string $id
* @param ?string $default
*
* @return ?string
*/
public static function cfg(string $group, string $id, ?string $default = null ): ?string
{
return static::$config[$group][$id] ?? $default;
}
}