Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
AnnotationsEngine.php
Go to the documentation of this file.
1<?php
3
7use mindplay\annotations\AnnotationCache;
8use mindplay\annotations\AnnotationManager;
9use mindplay\annotations\Annotations;
10
12
17 protected static $registry;
18
19 public function start(string $cacheDirectory): void {
20 self::$registry = [
21 'id' => 'Ubiquity\annotations\items\IdAnnotation',
22 'manyToOne' => 'Ubiquity\annotations\items\ManyToOneAnnotation',
23 'oneToMany' => 'Ubiquity\annotations\items\OneToManyAnnotation',
24 'manyToMany' => 'Ubiquity\annotations\items\ManyToManyAnnotation',
25 'joinColumn' => 'Ubiquity\annotations\items\JoinColumnAnnotation',
26 'table' => 'Ubiquity\annotations\items\TableAnnotation',
27 'database' => 'Ubiquity\annotations\items\DatabaseAnnotation',
28 'transient' => 'Ubiquity\annotations\items\TransientAnnotation',
29 'column' => 'Ubiquity\annotations\items\ColumnAnnotation',
30 'validator' => 'Ubiquity\annotations\items\ValidatorAnnotation',
31 'transformer' => 'Ubiquity\annotations\items\TransformerAnnotation',
32 'joinTable' => 'Ubiquity\annotations\items\JoinTableAnnotation',
33 'requestMapping' => 'Ubiquity\annotations\items\router\RouteAnnotation',
34 'route' => 'Ubiquity\annotations\items\router\RouteAnnotation',
35 'get' => 'Ubiquity\annotations\items\router\GetAnnotation',
36 'getMapping' => 'Ubiquity\annotations\items\router\GetAnnotation',
37 'post' => 'Ubiquity\annotations\items\router\PostAnnotation',
38 'postMapping' => 'Ubiquity\annotations\items\router\PostAnnotation',
39 'put' => 'Ubiquity\annotations\items\router\PutAnnotation',
40 'putMapping' => 'Ubiquity\annotations\items\router\PutAnnotation',
41 'patch' => 'Ubiquity\annotations\items\router\PatchAnnotation',
42 'patchMapping' => 'Ubiquity\annotations\items\router\PatchAnnotation',
43 'delete' => 'Ubiquity\annotations\items\router\DeleteAnnotation',
44 'deleteMapping' => 'Ubiquity\annotations\items\router\DeleteAnnotation',
45 'options' => 'Ubiquity\annotations\items\router\OptionsAnnotation',
46 'optionsMapping' => 'Ubiquity\annotations\items\router\OptionsAnnotation',
47 'noRoute' => 'Ubiquity\annotations\items\router\NoRouteAnnotation',
48 'var' => 'mindplay\annotations\standard\VarAnnotation',
49 'yuml' => 'Ubiquity\annotations\items\YumlAnnotation',
50 'rest' => 'Ubiquity\annotations\items\rest\RestAnnotation',
51 'authorization' => 'Ubiquity\annotations\items\rest\AuthorizationAnnotation',
52 'injected' => 'Ubiquity\annotations\items\di\InjectedAnnotation',
53 'autowired' => 'Ubiquity\annotations\items\di\AutowiredAnnotation'
54 ];
55 Annotations::$config['cache'] = new AnnotationCache($cacheDirectory . '/annotations');
56 self::register(Annotations::getManager());
57 }
58
59 public function registerAnnotations(array $nameClasses): void {
60 $annotationManager = Annotations::getManager();
61 foreach ($nameClasses as $name => $class) {
62 self::$registry[$name] = $class;
63 $annotationManager->registry[$name] = $class;
64 }
65 }
66
67 protected function register(AnnotationManager $annotationManager) {
68 $annotationManager->registry = \array_merge($annotationManager->registry, self::$registry);
69 }
70
71 public function getAnnotsOfClass(string $class, ?string $annotationType = null): array {
72 return Annotations::ofClass($class, $this->getAnnotationByKey($annotationType));
73 }
74
75 public function getAnnotationByKey(?string $key = null): ?string {
76 if ($key !== null) {
77 if (\array_key_exists($key, self::$registry)) {
78 return '@' . \ltrim($key, '@');
79 }
80 }
81 return null;
82 }
83
84 public function getAnnotsOfProperty(string $class, string $property, ?string $annotationType = null): array {
85 return Annotations::ofProperty($class, $property, $this->getAnnotationByKey($annotationType));
86 }
87
88 public function getAnnotsOfMethod(string $class, string $method, ?string $annotationType = null): array {
89 return Annotations::ofMethod($class, $method, $this->getAnnotationByKey($annotationType));
90 }
91
92 public function getAnnotation(?object $container, string $key, array $parameters = []): ?object {
93 if (isset(self::$registry[$key])) {
94 $classname = self::$registry[$key];
95 $annot = new $classname();
96 $annot->initAnnotation($parameters);
97 return $annot;
98 }
99 return null;
100 }
101
102 public function getAnnotationsStr(array $annotations, string $prefix = "\t"): string {
103 $annotationsStr = '';
104 $size = \count($annotations);
105 if ($size > 0) {
106 $annotationsStr = $prefix . "/**";
107 \array_walk($annotations, function ($item) {
108 return $item . '';
109 });
110 if ($size > 1) {
111 $annotationsStr .= "\n{$prefix} * " . implode("\n{$prefix} * ", $annotations);
112 } else {
113 $annotationsStr .= "\n{$prefix} * " . \end($annotations);
114 }
115 $annotationsStr .= "\n{$prefix} */";
116 }
117
118 return $annotationsStr;
119 }
120
121 public static function isManyToOne(object $annotation): bool {
122 return $annotation instanceof JoinColumnAnnotation;
123 }
124
125 public static function isMany(object $annotation): bool {
126 return ($annotation instanceof OneToManyAnnotation) || ($annotation instanceof ManyToManyAnnotation);
127 }
128
129 public static function isOneToMany(object $annotation): bool {
130 return $annotation instanceof OneToManyAnnotation;
131 }
132
133 public static function isManyToMany(object $annotation): bool {
134 return $annotation instanceof ManyToManyAnnotation;
135 }
136
137 public function is(string $key, object $annotation): bool {
138 $class = self::$registry[$key] ?? null;
139 if ($class !== null) {
140 return $annotation instanceof $class;
141 }
142 return false;
143 }
144
145 public function getUses(): array {
146 return [];
147 }
148
149 public function registerAcls(): void {
150 self::registerAnnotations([
151 'allow' => \Ubiquity\annotations\items\acl\AllowAnnotation::class,
152 'resource' => \Ubiquity\annotations\items\acl\ResourceAnnotation::class,
153 'permission' => \Ubiquity\annotations\items\acl\PermissionAnnotation::class
154 ]);
155 }
156}
157
getAnnotsOfClass(string $class, ?string $annotationType=null)
start(string $cacheDirectory)
Start the annotations engine for dev mode.
getAnnotationsStr(array $annotations, string $prefix="\t")
is(string $key, object $annotation)
getAnnotation(?object $container, string $key, array $parameters=[])
getAnnotsOfProperty(string $class, string $property, ?string $annotationType=null)
register(AnnotationManager $annotationManager)
getAnnotsOfMethod(string $class, string $method, ?string $annotationType=null)
Ubiquity\annotations$AnnotationsInterface This class is part of Ubiquity.
Class Configuration \config.