|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the core-library package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2018 WEBEWEB |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace WBW\Library\Core\Argument; |
|
13
|
|
|
|
|
14
|
|
|
use Error; |
|
15
|
|
|
use Exception; |
|
16
|
|
|
use ReflectionClass; |
|
17
|
|
|
use ReflectionException; |
|
18
|
|
|
use WBW\Library\Core\Exception\Argument\ObjectArgumentException; |
|
19
|
|
|
use WBW\Library\Core\Exception\Reflection\ClassNotFoundException; |
|
20
|
|
|
use WBW\Library\Core\Exception\Reflection\MethodNotFoundException; |
|
21
|
|
|
use WBW\Library\Core\Exception\Reflection\SyntaxErrorException; |
|
22
|
|
|
use WBW\Library\Core\FileSystem\FileHelper; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Object helper. |
|
26
|
|
|
* |
|
27
|
|
|
* @author webeweb <https://github.com/webeweb/> |
|
28
|
|
|
* @package WBW\Library\Core\Argument |
|
29
|
|
|
*/ |
|
30
|
|
|
class ObjectHelper { |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get a class directory. |
|
34
|
|
|
* |
|
35
|
|
|
* @param mixed $object The class name or object. |
|
36
|
|
|
* @return string Returns the class directory. |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function getDirectory($object) { |
|
39
|
|
|
$filename = (new ReflectionClass($object))->getFileName(); |
|
40
|
|
|
return dirname($filename); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get the hooks. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $classpath The class path. |
|
47
|
|
|
* @param string $namespace The namespace. |
|
48
|
|
|
* @param string $classname The class name. |
|
49
|
|
|
* @param string $extends The class extend. |
|
50
|
|
|
* @param string $method The class method. |
|
51
|
|
|
* @return array Returns the hooks array. |
|
52
|
|
|
* @throws ClassNotFoundException Throws a hook class not found if the classname is not found. |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function getHooks($classpath, $namespace, $classname = null, $extends = null, $method = null) { |
|
55
|
|
|
|
|
56
|
|
|
// |
|
57
|
|
|
$hooks = []; |
|
58
|
|
|
|
|
59
|
|
|
// Get the filenames. |
|
60
|
|
|
$filenames = FileHelper::getFileNames($classpath, ".php"); |
|
61
|
|
|
|
|
62
|
|
|
// Handle each filenames. |
|
63
|
|
|
foreach ($filenames as $filename) { |
|
64
|
|
|
|
|
65
|
|
|
// Check the class name. |
|
66
|
|
|
if (null !== $classname && 0 === preg_match($classname, $filename)) { |
|
67
|
|
|
continue; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// Import the class. |
|
71
|
|
|
try { |
|
72
|
|
|
require_once $classpath . "/" . $filename; |
|
73
|
|
|
} catch (Error $ex) { |
|
|
|
|
|
|
74
|
|
|
throw new SyntaxErrorException($classpath . "/" . $filename); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// Init. the complete class name. |
|
78
|
|
|
$completeClassname = $namespace . basename($filename, ".php"); |
|
79
|
|
|
|
|
80
|
|
|
try { |
|
81
|
|
|
$rc = new ReflectionClass($completeClassname); |
|
82
|
|
|
} catch (Exception $ex) { |
|
83
|
|
|
throw new ClassNotFoundException($completeClassname, $ex); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// Check the extends. |
|
87
|
|
|
if (false === (null === $extends || true === $rc->isSubclassOf($extends))) { |
|
88
|
|
|
continue; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
// Initialize the hook. |
|
92
|
|
|
$hook = []; |
|
93
|
|
|
|
|
94
|
|
|
$hook["classpath"] = $classpath; |
|
95
|
|
|
$hook["namespace"] = $namespace; |
|
96
|
|
|
$hook["filename"] = $filename; |
|
97
|
|
|
$hook["class"] = $rc; |
|
98
|
|
|
$hook["method"] = null; |
|
99
|
|
|
|
|
100
|
|
|
$hooks[] = $hook; |
|
101
|
|
|
|
|
102
|
|
|
// Check the method. |
|
103
|
|
|
if (null === $method) { |
|
104
|
|
|
continue; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
try { |
|
108
|
|
|
$rm = $rc->getMethod($method); |
|
109
|
|
|
|
|
110
|
|
|
$hooks[count($hooks) - 1]["method"] = $rm; |
|
111
|
|
|
} catch (ReflectionException $ex) { |
|
112
|
|
|
throw new MethodNotFoundException($method, $ex); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
// Returns the hooks. |
|
117
|
|
|
return $hooks; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get the class name. |
|
122
|
|
|
* |
|
123
|
|
|
* @param mixed $object The class name or object. |
|
124
|
|
|
* @return string Returns the class name. |
|
125
|
|
|
* @throws ReflectionException Throws a Reflection exception if an error occurs. |
|
126
|
|
|
*/ |
|
127
|
|
|
public static function getName($object) { |
|
128
|
|
|
return (new ReflectionClass($object))->name; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Get the short class name. |
|
133
|
|
|
* |
|
134
|
|
|
* @param mixed $object The class name or object. |
|
135
|
|
|
* @return string Returns the short class name. |
|
136
|
|
|
* @throws ReflectionException Throws a Reflection exception if an error occurs. |
|
137
|
|
|
*/ |
|
138
|
|
|
public static function getShortName($object) { |
|
139
|
|
|
return (new ReflectionClass($object))->getShortName(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Determines if a value is an object. |
|
144
|
|
|
* |
|
145
|
|
|
* @param mixed $value The value. |
|
146
|
|
|
* @throws ObjectArgumentException Throws an Object argument exception if the value is not of expected type. |
|
147
|
|
|
*/ |
|
148
|
|
|
public static function isObject($value) { |
|
149
|
|
|
if (false === is_object($value)) { |
|
150
|
|
|
throw new ObjectArgumentException($value); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* URL decode a short name. |
|
156
|
|
|
* |
|
157
|
|
|
* @param string $string The string. |
|
158
|
|
|
* @return string Returns the URL decoded short name. |
|
159
|
|
|
*/ |
|
160
|
|
|
public static function urlDecodeShortName($string) { |
|
161
|
|
|
return ucfirst(preg_replace_callback("/(\-[a-z]{1})/", function($matches) { |
|
162
|
|
|
return strtoupper(str_replace("-", "", $matches[0])); |
|
163
|
|
|
}, $string)); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* URL encode a short name. |
|
168
|
|
|
* |
|
169
|
|
|
* @param mixed $object The object. |
|
170
|
|
|
* @return string Returns the URL encoded short name. |
|
171
|
|
|
* @throws ReflectionException Throws a Reflection exception if an error occurs. |
|
172
|
|
|
*/ |
|
173
|
|
|
public static function urlEncodeShortName($object) { |
|
174
|
|
|
$classname = static::getShortName($object); |
|
175
|
|
|
$explode = preg_replace("/([a-z]{1})([A-Z]{1})/", "$1-$2", $classname); |
|
176
|
|
|
return strtolower($explode); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
} |
|
180
|
|
|
|