Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
AttributesEngine.php
Go to the documentation of this file.
1<?php
2
4
31
33
34 protected static array $registry;
35
36 protected function attributesNewInstances(array $attributes): array {
37 $result = [];
38 foreach ($attributes as $attribute) {
39 $result[] = $attribute->newInstance();
40 }
41 return $result;
42 }
43
44 private function getAnnotsOf(\Reflector $reflector, ?string $annotationType = null): array {
45 if ($annotationType === null) {
46 return $this->attributesNewInstances($reflector->getAttributes());
47 }
48 $annotClass = $this->getAnnotationByKey($annotationType);
49 if (isset($annotClass)) {
50 return $this->attributesNewInstances($reflector->getAttributes($annotClass));
51 }
52 return [];
53 }
54
55 public function getAnnotsOfClass(string $class, ?string $annotationType = null): array {
56 return $this->getAnnotsOf(new \ReflectionClass($class), $annotationType);
57 }
58
59 public function getAnnotsOfMethod(string $class, string $method, ?string $annotationType = null): array {
60 return $this->getAnnotsOf(new \ReflectionMethod($class, $method), $annotationType);
61 }
62
63 public function getAnnotsOfProperty(string $class, string $property, ?string $annotationType = null): array {
64 return $this->getAnnotsOf(new \ReflectionProperty($class, $property), $annotationType);
65 }
66
67 public function start(string $cacheDirectory): void {
68 self::$registry = [
69 'id' => Id::class,
70 'column' => Column::class,
71 'joinColumn' => JoinColumn::class,
72 'joinTable' => JoinTable::class,
73 'manyToMany' => ManyToMany::class,
74 'manyToOne' => ManyToOne::class,
75 'oneToMany' => OneToMany::class,
76 'table' => Table::class,
77 'database' => Database::class,
78 'transient' => Transient::class,
79 'yuml' => Yuml::class,
80 'transformer' => Transformer::class,
81 'validator' => Validator::class,
82 'route' => Route::class,
83 'get' => Get::class,
84 'post' => Post::class,
85 'put' => Put::class,
86 'patch' => Patch::class,
87 'delete' => Delete::class,
88 'options' => Options::class,
89 'noRoute' => NoRoute::class,
90 'rest' => Rest::class,
91 'authorization' => Authorization::class,
92 'autowired' => Autowired::class,
93 'injected' => Injected::class
94 ];
95 }
96
97 public function getAnnotationByKey(?string $key = null): ?string {
98 return self::$registry[$key] ?? null;
99 }
100
101 public function registerAnnotations(array $nameClasses): void {
102 self::$registry = \array_merge(self::$registry, $nameClasses);
103 }
104
105 public function getAnnotation(?object $container, string $key, array $parameters = []): ?object {
106 if (isset(self::$registry[$key])) {
107 $classname = self::$registry[$key];
108 if (isset($container) && \method_exists($container, 'addUse')) {
109 $container->addUse($classname);
110 }
111 $reflect = new \ReflectionClass($classname);
112 return $reflect->newInstanceArgs($parameters);
113 }
114 return null;
115 }
116
117 public function getAnnotationsStr(array $annotations, string $prefix = "\t"): string {
118 $annotationsStr = '';
119 $size = \count($annotations);
120 if ($size > 0) {
121 $annotationsStr = $prefix;
122 \array_walk($annotations, function ($item) {
123 return $item . '';
124 });
125 if ($size > 1) {
126 $annotationsStr .= "\n{$prefix}" . implode("\n{$prefix}", $annotations);
127 } else {
128 $annotationsStr .= "\n{$prefix}" . \end($annotations);
129 }
130 }
131 return $annotationsStr;
132 }
133
134 public static function isManyToOne(object $annotation): bool {
135 return $annotation instanceof JoinColumn;
136 }
137
138 public static function isMany(object $annotation): bool {
139 return ($annotation instanceof OneToMany) || ($annotation instanceof ManyToMany);
140 }
141
142 public static function isOneToMany(object $annotation): bool {
143 return $annotation instanceof OneToMany;
144 }
145
146 public static function isManyToMany(object $annotation): bool {
147 return $annotation instanceof ManyToMany;
148 }
149
150 public function is(string $key, object $annotation): bool {
151 $class = self::$registry[$key] ?? null;
152 if ($class !== null) {
153 return $annotation instanceof $class;
154 }
155 return false;
156 }
157
158 public function registerAcls(): void {
159 self::$registry = \array_merge(self::$registry, [
160 'allow' => \Ubiquity\attributes\items\acl\Allow::class,
161 'resource' => \Ubiquity\attributes\items\acl\Resource::class,
162 'permission' => \Ubiquity\attributes\items\acl\Permission::class
163 ]);
164 }
165}
getAnnotsOfClass(string $class, ?string $annotationType=null)
static isOneToMany(object $annotation)
start(string $cacheDirectory)
Start the annotations engine for dev mode.
getAnnotationsStr(array $annotations, string $prefix="\t")
is(string $key, object $annotation)
getAnnotsOf(\Reflector $reflector, ?string $annotationType=null)
static isManyToMany(object $annotation)
getAnnotation(?object $container, string $key, array $parameters=[])
getAnnotsOfProperty(string $class, string $property, ?string $annotationType=null)
static isManyToOne(object $annotation)
getAnnotsOfMethod(string $class, string $method, ?string $annotationType=null)
Attribute for autowiring.
Definition Autowired.php:21
Attribute for dependency injection.
Definition Injected.php:21
Defines a route with the delete method Ubiquity\attributes\items\router$Post This class is part of Ub...
Definition Delete.php:17
Defines a route with the get method Ubiquity\attributes\items\router$Get This class is part of Ubiqui...
Definition Get.php:21
Defines a route with the options method Ubiquity\attributes\items\router$Options This class is part o...
Definition Options.php:17
Defines a route with the patch method Ubiquity\attributes\items\router$Patch This class is part of Ub...
Definition Patch.php:17
Defines a route with the post method Ubiquity\attributes\items\router$Post This class is part of Ubiq...
Definition Post.php:17
Defines a route with the put method Ubiquity\attributes\items\router$Put This class is part of Ubiqui...
Definition Put.php:17
Ubiquity\annotations$AnnotationsInterface This class is part of Ubiquity.
Class Configuration \config.