1: | <?php declare(strict_types=1); |
2: | |
3: | namespace Salient\Core; |
4: | |
5: | use Salient\Contract\Core\Cardinality; |
6: | use Salient\Contract\Core\Extensible; |
7: | use Salient\Contract\Core\Normalisable; |
8: | use Salient\Contract\Core\NormaliserFactory; |
9: | use Salient\Contract\Core\NormaliserFlag; |
10: | use Salient\Contract\Core\Providable; |
11: | use Salient\Contract\Core\Readable; |
12: | use Salient\Contract\Core\Relatable; |
13: | use Salient\Contract\Core\Temporal; |
14: | use Salient\Contract\Core\Treeable; |
15: | use Salient\Contract\Core\Writable; |
16: | use Salient\Utility\Reflect; |
17: | use Salient\Utility\Regex; |
18: | use Salient\Utility\Str; |
19: | use Closure; |
20: | use DateTimeInterface; |
21: | use ReflectionClass; |
22: | use ReflectionMethod; |
23: | use ReflectionNamedType; |
24: | use ReflectionProperty; |
25: | |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | class IntrospectionClass |
32: | { |
33: | public const ACTION_GET = 'get'; |
34: | public const ACTION_ISSET = 'isset'; |
35: | public const ACTION_SET = 'set'; |
36: | public const ACTION_UNSET = 'unset'; |
37: | |
38: | |
39: | |
40: | |
41: | |
42: | |
43: | public $Class; |
44: | |
45: | |
46: | |
47: | |
48: | |
49: | |
50: | public $IsReadable; |
51: | |
52: | |
53: | |
54: | |
55: | |
56: | |
57: | public $IsWritable; |
58: | |
59: | |
60: | |
61: | |
62: | |
63: | |
64: | public $IsExtensible; |
65: | |
66: | |
67: | |
68: | |
69: | |
70: | |
71: | public $IsProvidable; |
72: | |
73: | |
74: | |
75: | |
76: | |
77: | |
78: | public $IsRelatable; |
79: | |
80: | |
81: | |
82: | |
83: | |
84: | |
85: | public $IsTreeable; |
86: | |
87: | |
88: | |
89: | |
90: | |
91: | |
92: | public $HasDates; |
93: | |
94: | |
95: | |
96: | |
97: | |
98: | |
99: | |
100: | |
101: | |
102: | |
103: | public $Properties = []; |
104: | |
105: | |
106: | |
107: | |
108: | |
109: | |
110: | public $PublicProperties = []; |
111: | |
112: | |
113: | |
114: | |
115: | |
116: | |
117: | |
118: | |
119: | |
120: | |
121: | |
122: | |
123: | |
124: | public $ReadableProperties = []; |
125: | |
126: | |
127: | |
128: | |
129: | |
130: | |
131: | |
132: | |
133: | |
134: | |
135: | |
136: | |
137: | |
138: | public $WritableProperties = []; |
139: | |
140: | |
141: | |
142: | |
143: | |
144: | |
145: | public $Actions = []; |
146: | |
147: | |
148: | |
149: | |
150: | |
151: | |
152: | public $Parameters = []; |
153: | |
154: | |
155: | |
156: | |
157: | |
158: | |
159: | |
160: | public $RequiredParameters = []; |
161: | |
162: | |
163: | |
164: | |
165: | |
166: | |
167: | |
168: | public $NotNullableParameters = []; |
169: | |
170: | |
171: | |
172: | |
173: | |
174: | |
175: | |
176: | public $ServiceParameters = []; |
177: | |
178: | |
179: | |
180: | |
181: | |
182: | |
183: | public $PassByRefParameters = []; |
184: | |
185: | |
186: | |
187: | |
188: | |
189: | |
190: | |
191: | |
192: | |
193: | public $DateParameters = []; |
194: | |
195: | |
196: | |
197: | |
198: | |
199: | |
200: | public $DefaultArguments = []; |
201: | |
202: | |
203: | |
204: | |
205: | |
206: | |
207: | public $RequiredArguments = 0; |
208: | |
209: | |
210: | |
211: | |
212: | |
213: | |
214: | public $ParameterIndex = []; |
215: | |
216: | |
217: | |
218: | |
219: | |
220: | |
221: | public $SerializableProperties = []; |
222: | |
223: | |
224: | |
225: | |
226: | |
227: | |
228: | public $NormalisedKeys = []; |
229: | |
230: | |
231: | |
232: | |
233: | |
234: | |
235: | |
236: | |
237: | |
238: | public $ParentProperty; |
239: | |
240: | |
241: | |
242: | |
243: | |
244: | |
245: | |
246: | |
247: | |
248: | public $ChildrenProperty; |
249: | |
250: | |
251: | |
252: | |
253: | |
254: | |
255: | |
256: | public $OneToOneRelationships = []; |
257: | |
258: | |
259: | |
260: | |
261: | |
262: | |
263: | |
264: | public $OneToManyRelationships = []; |
265: | |
266: | |
267: | |
268: | |
269: | |
270: | |
271: | public $DateKeys = []; |
272: | |
273: | |
274: | |
275: | |
276: | |
277: | |
278: | public ?Closure $Normaliser = null; |
279: | |
280: | |
281: | |
282: | |
283: | |
284: | |
285: | public $GentleNormaliser; |
286: | |
287: | |
288: | |
289: | |
290: | |
291: | |
292: | public $CarefulNormaliser; |
293: | |
294: | |
295: | |
296: | |
297: | |
298: | |
299: | public $CreateFromSignatureClosures = []; |
300: | |
301: | |
302: | |
303: | |
304: | |
305: | |
306: | public $CreateProviderlessFromSignatureClosures = []; |
307: | |
308: | |
309: | |
310: | |
311: | |
312: | |
313: | public $CreateProvidableFromSignatureClosures = []; |
314: | |
315: | |
316: | |
317: | |
318: | |
319: | |
320: | public $CreateProviderlessFromClosures = []; |
321: | |
322: | |
323: | |
324: | |
325: | |
326: | |
327: | public $CreateProvidableFromClosures = []; |
328: | |
329: | |
330: | |
331: | |
332: | |
333: | |
334: | public $PropertyActionClosures = []; |
335: | |
336: | |
337: | public $GetNameClosure; |
338: | |
339: | |
340: | |
341: | |
342: | |
343: | |
344: | public $SerializeClosures = []; |
345: | |
346: | |
347: | protected $Reflector; |
348: | |
349: | |
350: | |
351: | |
352: | public function __construct(string $class) |
353: | { |
354: | $class = new ReflectionClass($class); |
355: | $className = $class->getName(); |
356: | $this->Reflector = $class; |
357: | $this->Class = $className; |
358: | $this->IsReadable = $class->implementsInterface(Readable::class); |
359: | $this->IsWritable = $class->implementsInterface(Writable::class); |
360: | $this->IsExtensible = $class->implementsInterface(Extensible::class); |
361: | $this->IsProvidable = $class->implementsInterface(Providable::class); |
362: | $this->IsTreeable = $class->implementsInterface(Treeable::class); |
363: | $this->IsRelatable = $this->IsTreeable || $class->implementsInterface(Relatable::class); |
364: | $this->HasDates = $class->implementsInterface(Temporal::class); |
365: | |
366: | if ($class->implementsInterface(NormaliserFactory::class)) { |
367: | |
368: | $this->Normaliser = $className::getNormaliser(); |
369: | } elseif ($class->implementsInterface(Normalisable::class)) { |
370: | $this->Normaliser = Closure::fromCallable([$className, 'normalise']); |
371: | } |
372: | |
373: | if ($this->Normaliser) { |
374: | $this->GentleNormaliser = fn(string $name): string => ($this->Normaliser)($name, false); |
375: | $this->CarefulNormaliser = fn(string $name): string => ($this->Normaliser)($name, true, ...$this->NormalisedKeys); |
376: | } |
377: | |
378: | $propertyFilter = ReflectionProperty::IS_PUBLIC; |
379: | $methodFilter = 0; |
380: | |
381: | |
382: | |
383: | if ($this->IsReadable || $this->IsWritable) { |
384: | $propertyFilter |= ReflectionProperty::IS_PROTECTED; |
385: | $methodFilter |= ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED; |
386: | } |
387: | |
388: | $parent = $class; |
389: | do { |
390: | $parents[] = $parent->getName(); |
391: | } while ($parent = $parent->getParentClass()); |
392: | $parents = array_flip($parents); |
393: | |
394: | |
395: | $properties = array_filter( |
396: | $class->getProperties($propertyFilter), |
397: | fn(ReflectionProperty $prop) => !$prop->isStatic() |
398: | ); |
399: | |
400: | uksort( |
401: | $properties, |
402: | function (int $a, int $b) use ($parents, $properties) { |
403: | $depthA = $parents[$properties[$a]->getDeclaringClass()->getName()]; |
404: | $depthB = $parents[$properties[$b]->getDeclaringClass()->getName()]; |
405: | |
406: | return $depthB <=> $depthA ?: $a <=> $b; |
407: | } |
408: | ); |
409: | $names = Reflect::getNames($properties); |
410: | $this->Properties = array_combine( |
411: | $this->maybeNormalise($names, NormaliserFlag::LAZY), |
412: | $names |
413: | ); |
414: | $this->PublicProperties = |
415: | $propertyFilter & ReflectionProperty::IS_PROTECTED |
416: | ? array_intersect( |
417: | $this->Properties, |
418: | Reflect::getNames(array_filter( |
419: | $properties, |
420: | fn(ReflectionProperty $prop) => $prop->isPublic() |
421: | )) |
422: | ) |
423: | : $this->Properties; |
424: | |
425: | if ($this->IsReadable) { |
426: | $readable = $class->getMethod('getReadableProperties')->invoke(null); |
427: | $readable = array_merge( |
428: | ['*'] === $readable |
429: | ? $this->Properties |
430: | : $readable, |
431: | $this->PublicProperties |
432: | ); |
433: | $this->ReadableProperties = array_intersect($this->Properties, $readable); |
434: | } |
435: | |
436: | if ($this->IsWritable) { |
437: | $writable = $class->getMethod('getWritableProperties')->invoke(null); |
438: | $writable = array_merge( |
439: | ['*'] === $writable |
440: | ? $this->Properties |
441: | : $writable, |
442: | $this->PublicProperties |
443: | ); |
444: | $this->WritableProperties = array_intersect($this->Properties, $writable); |
445: | } |
446: | |
447: | |
448: | if ($methodFilter) { |
449: | |
450: | $methods = array_filter( |
451: | $class->getMethods($methodFilter), |
452: | fn(ReflectionMethod $method) => !$method->isStatic() |
453: | ); |
454: | $regex = implode('|', [ |
455: | ...($this->IsReadable ? [self::ACTION_GET, self::ACTION_ISSET] : []), |
456: | ...($this->IsWritable ? [self::ACTION_SET, self::ACTION_UNSET] : []), |
457: | ]); |
458: | $regex = "/^_(?<action>{$regex})(?<property>.+)\$/i"; |
459: | foreach ($methods as $method) { |
460: | if (!Regex::match($regex, $name = $method->getName(), $match)) { |
461: | continue; |
462: | } |
463: | $action = Str::lower($match['action']); |
464: | $property = $this->maybeNormalise($match['property'], NormaliserFlag::LAZY); |
465: | $this->Actions[$action][$property] = $name; |
466: | } |
467: | } |
468: | |
469: | |
470: | |
471: | |
472: | |
473: | |
474: | |
475: | if (($constructor = $class->getConstructor()) && $constructor->isPublic()) { |
476: | $lastRequired = -1; |
477: | $index = -1; |
478: | foreach ($constructor->getParameters() as $param) { |
479: | $type = $param->getType(); |
480: | $type = $type instanceof ReflectionNamedType && !$type->isBuiltin() |
481: | ? $type->getName() |
482: | : null; |
483: | $normalised = $this->maybeNormalise($name = $param->getName(), NormaliserFlag::LAZY); |
484: | $defaultValue = null; |
485: | $isOptional = false; |
486: | if ($param->isOptional()) { |
487: | if ($param->isDefaultValueAvailable()) { |
488: | $defaultValue = $param->getDefaultValue(); |
489: | } |
490: | $isOptional = true; |
491: | if (!$param->allowsNull()) { |
492: | $this->NotNullableParameters[$normalised] = $name; |
493: | } |
494: | } elseif (!$param->allowsNull()) { |
495: | $this->RequiredParameters[$normalised] = $name; |
496: | if ($type) { |
497: | $this->ServiceParameters[$normalised] = $type; |
498: | } |
499: | } |
500: | $index++; |
501: | if (!$isOptional) { |
502: | $lastRequired = $index; |
503: | } |
504: | if ($param->isPassedByReference()) { |
505: | $this->PassByRefParameters[$normalised] = $name; |
506: | } |
507: | if ($this->HasDates && is_a($type, DateTimeInterface::class, true)) { |
508: | $this->DateParameters[$normalised] = $name; |
509: | } |
510: | $this->Parameters[$normalised] = $name; |
511: | $this->DefaultArguments[] = $defaultValue; |
512: | } |
513: | $this->RequiredArguments = $lastRequired + 1; |
514: | $this->ParameterIndex = array_flip(array_values($this->Parameters)); |
515: | } |
516: | |
517: | |
518: | $this->NormalisedKeys = array_keys( |
519: | $this->Properties |
520: | + ($this->Actions[self::ACTION_GET] ?? []) |
521: | + ($this->Actions[self::ACTION_ISSET] ?? []) |
522: | + ($this->Actions[self::ACTION_SET] ?? []) |
523: | + ($this->Actions[self::ACTION_UNSET] ?? []) |
524: | ); |
525: | |
526: | |
527: | |
528: | $this->SerializableProperties = |
529: | array_merge( |
530: | array_values( |
531: | array_intersect( |
532: | ($this->ReadableProperties ?: $this->PublicProperties), |
533: | ($this->WritableProperties ?: $this->PublicProperties), |
534: | ) |
535: | ), |
536: | array_keys( |
537: | array_intersect_key( |
538: | ($this->Actions[self::ACTION_GET] ?? []), |
539: | ($this->Actions[self::ACTION_SET] ?? []), |
540: | ) |
541: | ) |
542: | ); |
543: | |
544: | if ($this->IsRelatable) { |
545: | |
546: | $relationships = $class->getMethod('getRelationships')->invoke(null); |
547: | $relationships = array_combine( |
548: | $this->maybeNormalise(array_keys($relationships), NormaliserFlag::LAZY), |
549: | $relationships |
550: | ); |
551: | |
552: | |
553: | |
554: | |
555: | |
556: | |
557: | if ($this->IsTreeable) { |
558: | $parentMethod = $class->getMethod('getParentProperty'); |
559: | $parentClass = $parentMethod->getDeclaringClass(); |
560: | $childrenMethod = $class->getMethod('getChildrenProperty'); |
561: | $childrenClass = $childrenMethod->getDeclaringClass(); |
562: | |
563: | |
564: | |
565: | $service = $childrenClass->isSubclassOf($parentClass) |
566: | ? $childrenClass->getName() |
567: | : $parentClass->getName(); |
568: | |
569: | |
570: | $treeable = [ |
571: | $parentMethod->invoke(null), |
572: | $childrenMethod->invoke(null), |
573: | ]; |
574: | |
575: | $treeable = array_unique($this->maybeNormalise( |
576: | $treeable, NormaliserFlag::LAZY |
577: | )); |
578: | |
579: | |
580: | |
581: | |
582: | if (count(array_intersect($this->NormalisedKeys, $treeable)) === 2) { |
583: | $this->ParentProperty = $treeable[0]; |
584: | $this->ChildrenProperty = $treeable[1]; |
585: | $this->OneToOneRelationships[$this->ParentProperty] = $service; |
586: | $this->OneToManyRelationships[$this->ChildrenProperty] = $service; |
587: | } else { |
588: | $this->IsTreeable = false; |
589: | } |
590: | } |
591: | |
592: | foreach ($relationships as $property => $reference) { |
593: | $type = array_key_first($reference); |
594: | $target = $reference[$type]; |
595: | if (!in_array($property, $this->NormalisedKeys, true)) { |
596: | continue; |
597: | } |
598: | if (!is_a($target, Relatable::class, true)) { |
599: | continue; |
600: | } |
601: | switch ($type) { |
602: | case Cardinality::ONE_TO_ONE: |
603: | $this->OneToOneRelationships[$property] = $target; |
604: | break; |
605: | |
606: | case Cardinality::ONE_TO_MANY: |
607: | $this->OneToManyRelationships[$property] = $target; |
608: | break; |
609: | } |
610: | } |
611: | } |
612: | |
613: | if ($this->HasDates) { |
614: | |
615: | $dates = $class->getMethod('getDateProperties')->invoke(null); |
616: | |
617: | $nativeDates = []; |
618: | foreach ($properties as $property) { |
619: | if (!$property->hasType()) { |
620: | continue; |
621: | } |
622: | foreach (Reflect::getTypeNames($property->getType()) as $type) { |
623: | if (is_a($type, DateTimeInterface::class, true)) { |
624: | $nativeDates[] = $property->getName(); |
625: | continue 2; |
626: | } |
627: | } |
628: | } |
629: | |
630: | $this->DateKeys = |
631: | ['*'] === $dates |
632: | ? ($nativeDates |
633: | ? $this->maybeNormalise($nativeDates, NormaliserFlag::LAZY) |
634: | : $this->NormalisedKeys) |
635: | : array_intersect( |
636: | $this->NormalisedKeys, |
637: | $this->maybeNormalise(array_merge($dates, $nativeDates), NormaliserFlag::LAZY), |
638: | ); |
639: | } |
640: | } |
641: | |
642: | |
643: | |
644: | |
645: | |
646: | |
647: | |
648: | |
649: | |
650: | |
651: | |
652: | |
653: | |
654: | |
655: | final public function maybeNormalise($value, int $flags = NormaliserFlag::GREEDY) |
656: | { |
657: | if (!$this->Normaliser) { |
658: | return $value; |
659: | } |
660: | switch (true) { |
661: | case $flags & NormaliserFlag::LAZY: |
662: | $normaliser = $this->GentleNormaliser; |
663: | break; |
664: | case $flags & NormaliserFlag::CAREFUL: |
665: | $normaliser = $this->CarefulNormaliser; |
666: | break; |
667: | default: |
668: | $normaliser = $this->Normaliser; |
669: | } |
670: | if (is_array($value)) { |
671: | return array_map($normaliser, $value); |
672: | } |
673: | |
674: | return ($normaliser)($value); |
675: | } |
676: | |
677: | |
678: | |
679: | |
680: | |
681: | |
682: | final public function getReadableProperties(): array |
683: | { |
684: | return array_keys(( |
685: | $this->ReadableProperties |
686: | ?: $this->PublicProperties |
687: | ) + ($this->Actions[self::ACTION_GET] ?? [])); |
688: | } |
689: | |
690: | |
691: | |
692: | |
693: | |
694: | |
695: | final public function getWritableProperties(): array |
696: | { |
697: | return array_keys(( |
698: | $this->WritableProperties |
699: | ?: $this->PublicProperties |
700: | ) + ($this->Actions[self::ACTION_SET] ?? [])); |
701: | } |
702: | |
703: | |
704: | |
705: | |
706: | |
707: | |
708: | |
709: | final public function propertyActionIsAllowed(string $property, string $action): bool |
710: | { |
711: | switch ($action) { |
712: | case self::ACTION_GET: |
713: | case self::ACTION_ISSET: |
714: | return in_array( |
715: | $property, |
716: | $this->getReadableProperties() |
717: | ); |
718: | |
719: | case self::ACTION_SET: |
720: | case self::ACTION_UNSET: |
721: | return in_array( |
722: | $property, |
723: | $this->getWritableProperties() |
724: | ); |
725: | } |
726: | |
727: | return false; |
728: | } |
729: | } |
730: | |