|
2 | 2 |
|
3 | 3 | use lang\meta\{MetaInformation, FromSyntaxTree, FromAttributes}; |
4 | 4 | use lang\reflection\{Type, Package}; |
5 | | -use lang\{ClassLoader, ClassNotFoundException}; |
| 5 | +use lang\{ClassLoader, ClassNotFoundException, IllegalArgumentException}; |
6 | 6 |
|
7 | 7 | /** |
8 | 8 | * Factory for reflection instances. |
@@ -56,6 +56,35 @@ public static function type($arg) { |
56 | 56 | } |
57 | 57 | } |
58 | 58 |
|
| 59 | + /** |
| 60 | + * Returns a reflection package for a given argument. |
| 61 | + * |
| 62 | + * @param string|object|lang.XPClass|lang.reflection.Type|ReflectionClass $arg |
| 63 | + * @return lang.reflection.Type |
| 64 | + * @throws lang.IllegalArgumentException |
| 65 | + */ |
| 66 | + public static function package($arg) { |
| 67 | + if ($arg instanceof XPClass) { |
| 68 | + return new Package($arg->getPackage()->getName()); |
| 69 | + } else if ($arg instanceof \ReflectionClass) { |
| 70 | + return new Package($arg->getNamespaceName()); |
| 71 | + } else if ($arg instanceof Type) { |
| 72 | + return $arg->package(); |
| 73 | + } else if (is_object($arg)) { |
| 74 | + $class= get_class($arg); |
| 75 | + return new Package(substr($class, 0, strrpos($class, '\\'))); |
| 76 | + } else { |
| 77 | + $cl= ClassLoader::getDefault(); |
| 78 | + $name= strtr($arg, '\\', '.'); |
| 79 | + if ($cl->providesPackage($name)) { |
| 80 | + return new Package($name); |
| 81 | + } else if ($cl->providesClass($name)) { |
| 82 | + return new Package(substr($name, 0, strrpos($name, '.'))); |
| 83 | + } |
| 84 | + throw new IllegalArgumentException('No package named '.$name); |
| 85 | + } |
| 86 | + } |
| 87 | + |
59 | 88 | /** |
60 | 89 | * Creates a new reflection instance, which may either refer to a type |
61 | 90 | * or to a package. |
|
0 commit comments