1
|
|
|
<?php |
2
|
|
|
namespace WebServCo\Framework; |
3
|
|
|
|
4
|
|
|
use WebServCo\Framework\Exceptions\ApplicationException; |
5
|
|
|
|
6
|
|
|
final class Framework |
7
|
|
|
{ |
8
|
|
|
const OS_WINDOWS = 'Windows'; |
9
|
|
|
const OS_LINUX = 'Linux'; |
10
|
|
|
const OS_UNSUPPORTED = 'Unsupported'; |
11
|
|
|
|
12
|
|
|
const TYPE_FRAMEWORK = 'Framework'; |
13
|
|
|
const TYPE_PROJECT = 'Project'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Stores Framework Library instances. |
17
|
|
|
*/ |
18
|
|
|
private static $frameworkLibraries = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Stores Project Library instances. |
22
|
|
|
*/ |
23
|
|
|
protected static $projectLibraries = []; |
24
|
|
|
|
25
|
|
|
private static function getFullClassName($className, $classType = null) |
26
|
|
|
{ |
27
|
|
|
switch ($classType) { |
28
|
|
|
case self::TYPE_FRAMEWORK: |
29
|
|
|
return sprintf('\\%s\\Libraries\\%s', __NAMESPACE__, $className); |
30
|
|
|
break; |
|
|
|
|
31
|
|
|
case self::TYPE_PROJECT: |
32
|
|
|
return sprintf('\\Project\\Libraries\\%s', $className); |
33
|
|
|
break; |
34
|
|
|
default: |
35
|
|
|
return $className; |
36
|
|
|
break; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Returns the path the framework project is located in. |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
public static function getPath() |
46
|
|
|
{ |
47
|
|
|
return str_replace('src/WebServCo/Framework', '', __DIR__); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private static function loadLibraryConfiguration($configName) |
51
|
|
|
{ |
52
|
|
|
if ('Config' == $configName) { |
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
$projectPath = self::library('Config')->get( |
56
|
|
|
sprintf( |
57
|
|
|
'app%1$spath%1$sproject', |
58
|
|
|
\WebServCo\Framework\Settings::DIVIDER |
59
|
|
|
) |
60
|
|
|
); |
61
|
|
|
if (empty($projectPath)) { |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
return self::library('Config')->load($configName, $projectPath); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private static function loadLibrary($className, $fullClassName, $configName = null) |
68
|
|
|
{ |
69
|
|
|
if (!class_exists($fullClassName)) { |
70
|
|
|
throw new ApplicationException( |
71
|
|
|
sprintf('Library %s not found', $fullClassName) |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
switch ($className) { |
76
|
|
|
case 'I18n': |
77
|
|
|
self::loadHelper($className); |
78
|
|
|
break; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$configName = $configName ?: $className; |
82
|
|
|
$config = self::loadLibraryConfiguration($configName); |
83
|
|
|
/** |
84
|
|
|
* Libraries can have custom parameters to constructor, |
85
|
|
|
* however the configuration array is always the first. |
86
|
|
|
* $args = is_array($args) ? array_merge([$config], $args) : [$config]; |
87
|
|
|
*/ |
88
|
|
|
switch ($className) { |
89
|
|
|
case 'Request': |
90
|
|
|
$args = [$config, $_SERVER, $_POST]; |
91
|
|
|
break; |
92
|
|
|
default: |
93
|
|
|
$args = [$config]; |
94
|
|
|
break; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$reflection = new \ReflectionClass($fullClassName); |
98
|
|
|
return $reflection->newInstanceArgs($args); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
protected static function loadHelper($className) |
102
|
|
|
{ |
103
|
|
|
$path = self::getPath() . 'src/WebServCo/Framework/Helpers/' . $className . 'Helper.php'; |
104
|
|
|
if (!is_readable($path)) { |
105
|
|
|
throw new ApplicationException( |
106
|
|
|
sprintf('Helper for %s Library not found', $className) |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
require $path; |
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public static function library($className, $storageKey = null, $configName = null) |
114
|
|
|
{ |
115
|
|
|
$fullClassName = self::getFullClassName($className, self::TYPE_FRAMEWORK); |
116
|
|
|
|
117
|
|
|
$storageKey = $storageKey ?: $fullClassName; |
118
|
|
|
|
119
|
|
|
if (!isset(self::$frameworkLibraries[$storageKey])) { |
120
|
|
|
self::$frameworkLibraries[$storageKey] = self::loadLibrary($className, $fullClassName, $configName); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return self::$frameworkLibraries[$storageKey]; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public static function projectLibrary($className, $storageKey = null, $configName = null) |
127
|
|
|
{ |
128
|
|
|
$fullClassName = self::getFullClassName($className, self::TYPE_PROJECT); |
129
|
|
|
|
130
|
|
|
$storageKey = $storageKey ?: $fullClassName; |
131
|
|
|
|
132
|
|
|
if (!isset(self::$projectLibraries[$storageKey])) { |
133
|
|
|
self::$projectLibraries[$storageKey] = self::loadLibrary($className, $fullClassName, $configName); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return self::$projectLibraries[$storageKey]; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Checks if interface type is CLI |
141
|
|
|
*/ |
142
|
|
|
public static function isCLI() |
143
|
|
|
{ |
144
|
|
|
return 'cli' === PHP_SAPI; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get operating system (if supported). |
149
|
|
|
*/ |
150
|
|
|
public static function getOS() |
151
|
|
|
{ |
152
|
|
|
$uname = php_uname('s'); |
153
|
|
|
if (0 === strncasecmp($uname, 'Win', 3)) { |
154
|
|
|
return self::OS_WINDOWS; |
155
|
|
|
} elseif (0 === strncasecmp($uname, 'Linux', 5)) { |
156
|
|
|
return self::OS_LINUX; |
157
|
|
|
} else { |
158
|
|
|
return self::OS_UNSUPPORTED; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
The
break
statement is not necessary if it is preceded for example by areturn
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.