|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PHPDaemon\Core; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class finder |
|
7
|
|
|
* |
|
8
|
|
|
* @package Core |
|
9
|
|
|
* |
|
10
|
|
|
* @author Vasily Zorin <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
class ClassFinder |
|
13
|
|
|
{ |
|
14
|
|
|
use \PHPDaemon\Traits\ClassWatchdog; |
|
15
|
|
|
use \PHPDaemon\Traits\StaticObjectWatchdog; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Get base class name of the given class or object |
|
19
|
|
|
* @param string|object $class Object or String |
|
20
|
|
|
* @return string Class |
|
21
|
|
|
*/ |
|
22
|
|
|
public static function getClassBasename($class) |
|
23
|
|
|
{ |
|
24
|
|
|
if (is_object($class)) { |
|
25
|
|
|
$class = get_class($class); |
|
26
|
|
|
} |
|
27
|
|
|
$e = explode('\\', $class); |
|
28
|
|
|
return end($e); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param string $class |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function getNamespace(string $class): string |
|
35
|
|
|
{ |
|
36
|
|
|
return substr($class, 0, strrpos($class, '\\')); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Find class |
|
41
|
|
|
* @param string $class Class |
|
42
|
|
|
* @param string $namespace Namespace |
|
|
|
|
|
|
43
|
|
|
* @param string $rootns Root namespace |
|
|
|
|
|
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function find($class, $namespace = null, $rootns = null) |
|
47
|
|
|
{ |
|
48
|
|
|
$e = explode('\\', $class); |
|
49
|
|
|
if ($e[0] === '') { |
|
50
|
|
|
return $class; |
|
51
|
|
|
} |
|
52
|
|
|
if ('Pool' === $class || 'TransportContext' === $class) { |
|
53
|
|
|
return '\\PHPDaemon\\Core\\' . $class; |
|
54
|
|
|
} |
|
55
|
|
|
if (mb_orig_strpos($class, '\\') === false && $namespace === null) { |
|
56
|
|
|
if ('Example' === substr($class, 0, 7)) { |
|
57
|
|
|
array_unshift($e, 'Examples'); |
|
58
|
|
|
} |
|
59
|
|
View Code Duplication |
if ('Server' === substr($class, -6)) { |
|
|
|
|
|
|
60
|
|
|
$path = '\\PHPDaemon\\Servers\\' . substr($class, 0, -6) . '\\Pool'; |
|
61
|
|
|
$r = str_replace('\\Servers\\Servers', '\\Servers', $path); |
|
62
|
|
|
Daemon::log('ClassFinder: \'' . $class . '\' -> \'' . $r . '\', you should change your code.'); |
|
63
|
|
|
return $r; |
|
64
|
|
|
} |
|
65
|
|
View Code Duplication |
if ('Client' === substr($class, -6)) { |
|
|
|
|
|
|
66
|
|
|
$path = '\\PHPDaemon\\Clients\\' . substr($class, 0, -6) . '\\Pool'; |
|
67
|
|
|
$r = str_replace('\\Clients\\Clients', '\\Clients', $path); |
|
68
|
|
|
Daemon::log('ClassFinder: \'' . $class . '\' -> \'' . $r . '\', you should change your code.'); |
|
69
|
|
|
return $r; |
|
70
|
|
|
} |
|
71
|
|
View Code Duplication |
if ('ClientAsync' === substr($class, -11)) { |
|
|
|
|
|
|
72
|
|
|
$path = '\\PHPDaemon\\Clients\\' . substr($class, 0, -11) . '\\Pool'; |
|
73
|
|
|
$r = str_replace('\\Client\\Clients', '\\Clients', $path); |
|
74
|
|
|
Daemon::log('ClassFinder: \'' . $class . '\' -> \'' . $r . '\', you should change your code.'); |
|
75
|
|
|
return $r; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
if ($namespace !== null && sizeof($e) < 2) { |
|
79
|
|
|
array_unshift($e, $namespace); |
|
80
|
|
|
} |
|
81
|
|
|
array_unshift($e, '\\' . ($rootns !== null ? $rootns : Daemon::$config->defaultns->value)); |
|
82
|
|
|
return implode('\\', $e); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.