-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflection.class.php
More file actions
executable file
·89 lines (82 loc) · 2.68 KB
/
Reflection.class.php
File metadata and controls
executable file
·89 lines (82 loc) · 2.68 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
<?php namespace lang;
use lang\meta\{MetaInformation, FromSyntaxTree, FromAttributes};
use lang\reflection\{Type, Package};
use lang\{ClassLoader, ClassNotFoundException};
/**
* Factory for reflection instances.
*
* ```php
* // Types can be instantiated by names, instances or via lang.XPClass
* $type= Reflection::of(Runnable::class);
* $type= Reflection::of($instance);
* $type= Reflection::of(XPClass::forName('lang.Value'));
*
* // Packages can be instantiated via their name
* $package= Reflection::of('lang.reflection');
* ```
*
* @test lang.reflection.unittest.ReflectionTest
*/
abstract class Reflection {
private static $meta= null;
/** Lazy-loads meta information extraction */
public static function meta(): MetaInformation {
return self::$meta ?? self::$meta= new MetaInformation();
}
/**
* Returns a reflection type for a given argument.
*
* @param string|object|lang.XPClass|lang.reflection.Type|ReflectionClass $arg
* @return lang.reflection.Type
* @throws lang.ClassNotFoundException
*/
public static function type($arg) {
if ($arg instanceof XPClass) {
return new Type($arg->reflect());
} else if ($arg instanceof \ReflectionClass) {
return new Type($arg);
} else if ($arg instanceof Type) {
return $arg;
} else if (is_object($arg)) {
return new Type(new \ReflectionObject($arg));
} else {
try {
return new Type(new \ReflectionClass(strtr($arg, '.', '\\')));
} catch (\ReflectionException $e) {
throw new ClassNotFoundException($arg, [ClassLoader::getDefault()]);
}
}
}
/**
* Creates a new reflection instance, which may either refer to a type
* or to a package.
*
* @param string|object|lang.XPClass|lang.reflection.Type|ReflectionClass $arg
* @return lang.reflection.Type|lang.reflection.Package
* @throws lang.ClassNotFoundException
*/
public static function of($arg) {
if ($arg instanceof XPClass) {
return new Type($arg->reflect());
} else if ($arg instanceof \ReflectionClass) {
return new Type($arg);
} else if ($arg instanceof Type) {
return $arg;
} else if (is_object($arg)) {
return new Type(new \ReflectionObject($arg));
} else {
$cl= ClassLoader::getDefault();
$name= strtr($arg, '\\', '.');
if ($cl->providesClass($name)) {
return new Type(new \ReflectionClass($cl->loadClass0($name)));
} else if ($cl->providesPackage($name)) {
return new Package($name);
}
try {
return new Type(new \ReflectionClass(strtr($arg, '.', '\\')));
} catch (\ReflectionException $e) {
throw new ClassNotFoundException($name, [$cl]);
}
}
}
}