|
1
|
|
|
<?php |
|
2
|
|
|
namespace WebServCo\Framework; |
|
3
|
|
|
|
|
4
|
|
|
final class Framework |
|
5
|
|
|
{ |
|
6
|
|
|
const OS_WINDOWS = 'Windows'; |
|
7
|
|
|
const OS_LINUX = 'Linux'; |
|
8
|
|
|
const OS_UNSUPPORTED = 'Unsupported'; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Stores library instances. |
|
12
|
|
|
*/ |
|
13
|
|
|
private static $libraries = []; |
|
14
|
|
|
|
|
15
|
|
|
private static function getFullClassName($className, $classType) |
|
16
|
|
|
{ |
|
17
|
|
|
switch ($classType) { |
|
18
|
|
|
case 'Library': |
|
19
|
|
|
return __NAMESPACE__ . '\\Libraries\\' . $className; |
|
20
|
|
|
break; |
|
|
|
|
|
|
21
|
|
|
default: |
|
22
|
|
|
return $className; |
|
23
|
|
|
break; |
|
|
|
|
|
|
24
|
|
|
} |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Returns the path the framework project is located in. |
|
29
|
|
|
* |
|
30
|
|
|
* @return string |
|
31
|
|
|
*/ |
|
32
|
|
|
public static function getPath() |
|
33
|
|
|
{ |
|
34
|
|
|
return str_replace('src/WebServCo/Framework', '', __DIR__); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
private static function loadLibraryConfiguration($configName) |
|
38
|
|
|
{ |
|
39
|
|
|
if ('Config' == $configName) { |
|
40
|
|
|
return false; |
|
41
|
|
|
} |
|
42
|
|
|
$projectPath = self::getLibrary('Config')->get( |
|
43
|
|
|
sprintf( |
|
44
|
|
|
'app%1$spath%1$sproject', |
|
45
|
|
|
\WebServCo\Framework\Settings::DIVIDER |
|
46
|
|
|
) |
|
47
|
|
|
); |
|
48
|
|
|
if (empty($projectPath)) { |
|
49
|
|
|
return false; |
|
50
|
|
|
} |
|
51
|
|
|
return self::getLibrary('Config')->load($configName, $projectPath); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private static function loadLibrary($className, $fullClassName, $configName = null) |
|
|
|
|
|
|
55
|
|
|
{ |
|
56
|
|
|
if (!class_exists($fullClassName)) { |
|
57
|
|
|
throw new \ErrorException( |
|
58
|
|
|
sprintf('Library %s not found', $fullClassName) |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
$configName = $configName ?: $className; |
|
62
|
|
|
$config = self::loadLibraryConfiguration($configName); |
|
63
|
|
|
/** |
|
64
|
|
|
* Libraries can have custom parameters to constructor, |
|
65
|
|
|
* however the configuration array is always the first. |
|
66
|
|
|
* $args = is_array($args) ? array_merge([$config], $args) : [$config]; |
|
67
|
|
|
*/ |
|
68
|
|
|
switch ($className) { |
|
69
|
|
|
case 'Request': |
|
70
|
|
|
$args = [$config, $_SERVER, $_POST]; |
|
71
|
|
|
break; |
|
72
|
|
|
default: |
|
73
|
|
|
$args = [$config]; |
|
74
|
|
|
break; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$reflection = new \ReflectionClass($fullClassName); |
|
78
|
|
|
return $reflection->newInstanceArgs($args); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public static function getLibrary($className, $storageKey = null, $configName = null) |
|
82
|
|
|
{ |
|
83
|
|
|
$fullClassName = self::getFullClassName($className, 'Library'); |
|
84
|
|
|
|
|
85
|
|
|
$storageKey = $storageKey ?: $fullClassName; |
|
86
|
|
|
|
|
87
|
|
|
if (!isset(self::$libraries[$storageKey])) { |
|
88
|
|
|
self::$libraries[$storageKey] = self::loadLibrary($className, $fullClassName, $configName); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return self::$libraries[$storageKey]; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Checks if interface type is CLI |
|
96
|
|
|
*/ |
|
97
|
|
|
public static function isCLI() |
|
98
|
|
|
{ |
|
99
|
|
|
return 'cli' === PHP_SAPI; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Get operating system (if supported). |
|
104
|
|
|
*/ |
|
105
|
|
|
public static function getOS() |
|
106
|
|
|
{ |
|
107
|
|
|
$uname = php_uname('s'); |
|
108
|
|
|
if (0 === strncasecmp($uname, 'Win', 3)) { |
|
109
|
|
|
return self::OS_WINDOWS; |
|
110
|
|
|
} elseif (0 === strncasecmp($uname, 'Linux', 5)) { |
|
111
|
|
|
return self::OS_LINUX; |
|
112
|
|
|
} else { |
|
113
|
|
|
return self::OS_UNSUPPORTED; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.