|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace uuf6429\ExpressionLanguage; |
| 4 | + |
| 5 | +use JetBrains\PhpStorm\Language; |
| 6 | +use ReflectionException; |
| 7 | +use ReflectionMethod; |
| 8 | + |
| 9 | +/** |
| 10 | + * @codeCoverageIgnore |
| 11 | + */ |
| 12 | +class ClassBuilder |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @var string |
| 16 | + */ |
| 17 | + private $parent; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var string |
| 21 | + */ |
| 22 | + private $child; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + private $usedTraits = []; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var string |
| 31 | + */ |
| 32 | + private $usedImports = []; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var array<string, string> |
| 36 | + */ |
| 37 | + private $overriddenMethods = []; |
| 38 | + |
| 39 | + public static function create(): ClassBuilder |
| 40 | + { |
| 41 | + return new self(); |
| 42 | + } |
| 43 | + |
| 44 | + public function class(string $fqn): ClassBuilder |
| 45 | + { |
| 46 | + $this->child = '\\' . ltrim($fqn, '\\'); |
| 47 | + return $this; |
| 48 | + } |
| 49 | + |
| 50 | + public function extend(string $fqn): ClassBuilder |
| 51 | + { |
| 52 | + $this->parent = '\\' . ltrim($fqn, '\\'); |
| 53 | + return $this; |
| 54 | + } |
| 55 | + |
| 56 | + public function use(string $fqn): ClassBuilder |
| 57 | + { |
| 58 | + $this->usedTraits[] = '\\' . ltrim($fqn, '\\'); |
| 59 | + return $this; |
| 60 | + } |
| 61 | + |
| 62 | + public function import(string $fqn): ClassBuilder |
| 63 | + { |
| 64 | + $this->usedImports[] = '\\' . ltrim($fqn, '\\'); |
| 65 | + return $this; |
| 66 | + } |
| 67 | + |
| 68 | + public function override( |
| 69 | + string $method, |
| 70 | + #[Language('InjectablePHP')] |
| 71 | + string $body |
| 72 | + ): ClassBuilder { |
| 73 | + $this->overriddenMethods[$method] = $body; |
| 74 | + return $this; |
| 75 | + } |
| 76 | + |
| 77 | + public function buildClass(): string |
| 78 | + { |
| 79 | + |
| 80 | + list($class, $namespace) = array_map('strrev', explode('\\', strrev($this->child), 2)) + ['']; |
| 81 | + |
| 82 | + $codeLines = [ |
| 83 | + '', |
| 84 | + sprintf('namespace %s;', ltrim($namespace, '\\')), |
| 85 | + '', |
| 86 | + ]; |
| 87 | + |
| 88 | + foreach ($this->usedImports as $import) { |
| 89 | + $codeLines[] = "use $import;"; |
| 90 | + } |
| 91 | + |
| 92 | + $codeLines[] = ''; |
| 93 | + |
| 94 | + $codeLines[] = "class $class extends $this->parent"; |
| 95 | + $codeLines[] = '{'; |
| 96 | + |
| 97 | + foreach ($this->usedTraits as $trait) { |
| 98 | + $codeLines[] = " use $trait;"; |
| 99 | + } |
| 100 | + |
| 101 | + foreach ($this->overriddenMethods as $method => $body) { |
| 102 | + if (($sig = $this->extractSignature($this->parent, $method)) !== null) { |
| 103 | + $codeLines[] = ''; |
| 104 | + array_push($codeLines, ...$sig); |
| 105 | + $codeLines[] = ' {'; |
| 106 | + $codeLines[] = " $body"; |
| 107 | + $codeLines[] = ' }'; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + $codeLines[] = '}'; |
| 112 | + |
| 113 | + return implode("\n", $codeLines); |
| 114 | + } |
| 115 | + |
| 116 | + public function createClass() |
| 117 | + { |
| 118 | + eval($this->buildClass()); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @param string $class |
| 123 | + * @param string $method |
| 124 | + * @return string[]|null |
| 125 | + */ |
| 126 | + private function extractSignature(string $class, string $method) |
| 127 | + { |
| 128 | + try { |
| 129 | + $code = file_get_contents( |
| 130 | + (new ReflectionMethod($class, $method))->getFileName() |
| 131 | + ); |
| 132 | + |
| 133 | + return preg_match("/([^}]+function {$method}[^{]+?)\\n\\s+?{/", $code, $matches) |
| 134 | + ? array_filter(explode("\n", $matches[1])) |
| 135 | + : null; |
| 136 | + } catch (ReflectionException $ex) { |
| 137 | + return null; |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments