|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Kirby\Form; |
| 4 | + |
| 5 | +use ReflectionClass; |
| 6 | +use ReflectionMethod; |
| 7 | +use ReflectionProperty; |
| 8 | + |
| 9 | +/** |
| 10 | + * Abstract field class to be used instead |
| 11 | + * of functional field components for more |
| 12 | + * control. |
| 13 | + * |
| 14 | + * @package Kirby Form |
| 15 | + * @author Bastian Allgeier <[email protected]> |
| 16 | + * @link https://getkirby.com |
| 17 | + * @copyright Bastian Allgeier |
| 18 | + * @license https://getkirby.com/license |
| 19 | + * |
| 20 | + * @use \Kirby\Cms\HasSiblings<\Kirby\Form\Fields> |
| 21 | + */ |
| 22 | +class FieldReflector |
| 23 | +{ |
| 24 | + protected array $properties = []; |
| 25 | + protected ReflectionClass $reflection; |
| 26 | + |
| 27 | + public function __construct( |
| 28 | + protected string $field |
| 29 | + ) { |
| 30 | + // get all arguments for the field class constructor |
| 31 | + $this->reflection = new ReflectionClass($field); |
| 32 | + $this->properties = $this->parseConstructor($this->reflection->getConstructor()); |
| 33 | + |
| 34 | + dump($this->properties); |
| 35 | + } |
| 36 | + |
| 37 | + protected function parseConstructor(ReflectionMethod|null $constructor): array |
| 38 | + { |
| 39 | + if ($constructor === null) { |
| 40 | + return []; |
| 41 | + } |
| 42 | + |
| 43 | + $properties = []; |
| 44 | + $internal = ['name', 'type', 'model', 'siblings']; |
| 45 | + |
| 46 | + // get the matching class property for each argument if it exists |
| 47 | + foreach ($constructor->getParameters() as $param) { |
| 48 | + |
| 49 | + if (in_array($param->name, $internal)) { |
| 50 | + continue; |
| 51 | + } |
| 52 | + |
| 53 | + $properties[$param->name] = [ |
| 54 | + 'name' => $param->name, |
| 55 | + 'type' => $param->getType()?->__toString(), |
| 56 | + 'required' => !$param->isOptional(), |
| 57 | + 'default' => $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null, |
| 58 | + ...$this->parseProperty($this->reflection->getProperty($param->name)) |
| 59 | + ]; |
| 60 | + } |
| 61 | + |
| 62 | + ksort($properties); |
| 63 | + |
| 64 | + return $properties; |
| 65 | + } |
| 66 | + |
| 67 | + protected function parseProperty(ReflectionProperty|null $property): array |
| 68 | + { |
| 69 | + if ($property === null) { |
| 70 | + return []; |
| 71 | + } |
| 72 | + |
| 73 | + $comment = $property->getDocComment(); |
| 74 | + |
| 75 | + if ($comment === false) { |
| 76 | + return []; |
| 77 | + } |
| 78 | + |
| 79 | + // parse the comment. extract the tags and description |
| 80 | + $tags = []; |
| 81 | + $description = ''; |
| 82 | + $lines = explode("\n", $comment); |
| 83 | + |
| 84 | + foreach ($lines as $line) { |
| 85 | + $line = trim($line); |
| 86 | + |
| 87 | + $line = str_replace('/**', '', $line); |
| 88 | + $line = str_replace('*/', '', $line); |
| 89 | + $line = preg_replace('/^\s*\*\s*/', '', $line); |
| 90 | + |
| 91 | + if (strpos($line, '@') === 0) { |
| 92 | + $tags[] = trim(str_replace('@', '', $line), ' '); |
| 93 | + } else { |
| 94 | + $description .= $line; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return [ |
| 99 | + 'tags' => $tags, |
| 100 | + 'description' => $description, |
| 101 | + ]; |
| 102 | + } |
| 103 | +} |
0 commit comments