1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Cycle\Schema\Renderer; |
||
6 | |||
7 | use Cycle\ORM\SchemaInterface; |
||
8 | use Cycle\Schema\Renderer\ConsoleRenderer\Formatter\PlainFormatter; |
||
9 | use Cycle\Schema\Renderer\ConsoleRenderer\Formatter\StyledFormatter; |
||
10 | use Cycle\Schema\Renderer\ConsoleRenderer\OutputRenderer; |
||
11 | use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\ColumnsRenderer; |
||
12 | use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\CustomPropertiesRenderer; |
||
13 | use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\KeysRenderer; |
||
14 | use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\ListenersRenderer; |
||
15 | use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\PropertyRenderer; |
||
16 | use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\RelationsRenderer; |
||
17 | use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\TitleRenderer; |
||
18 | |||
19 | /** |
||
20 | * The class is designed to prepare a human-readable representation of the Cycle ORM Schema for output to the console |
||
21 | */ |
||
22 | final class OutputSchemaRenderer extends OutputRenderer |
||
23 | { |
||
24 | public const FORMAT_PLAIN_TEXT = 0; |
||
25 | public const FORMAT_CONSOLE_COLOR = 1; |
||
26 | |||
27 | protected const DEFAULT_PROPERTY_LIST = [ |
||
28 | 'ROLE' => 'Role', |
||
29 | 'ENTITY' => 'Entity', |
||
30 | 'MAPPER' => 'Mapper', |
||
31 | 'SCOPE' => 'Scope', |
||
32 | 'REPOSITORY' => 'Repository', |
||
33 | ]; |
||
34 | |||
35 | public function __construct(int $format = self::FORMAT_CONSOLE_COLOR, ?ConstantsInterface $constants = null) |
||
36 | { |
||
37 | $formatter = $format === self::FORMAT_CONSOLE_COLOR |
||
38 | ? new StyledFormatter() |
||
39 | : new PlainFormatter(); |
||
40 | parent::__construct($formatter); |
||
41 | |||
42 | $constants = ($constants ?? new SchemaConstants())->all(); |
||
43 | $properties = $this->getOrmProperties($constants); |
||
44 | |||
45 | $this->addRenderer(...[ |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
46 | new TitleRenderer(), |
||
47 | |||
48 | // Default properties renderer (Without extra logic) |
||
49 | ...array_map(static function ($property, string $title) { |
||
50 | return new PropertyRenderer($property, $title); |
||
51 | }, array_keys($properties), $properties), |
||
52 | |||
53 | new KeysRenderer(SchemaInterface::PRIMARY_KEY, 'Primary key', true), |
||
54 | ]); |
||
55 | |||
56 | // JTI support |
||
57 | if (isset($constants['PARENT'], $constants['PARENT_KEY'])) { |
||
58 | $this->addRenderer(...[ |
||
59 | new PropertyRenderer($constants['PARENT'], 'Parent'), |
||
60 | new KeysRenderer($constants['PARENT_KEY'], 'Parent key', false), |
||
61 | ]); |
||
62 | } |
||
63 | |||
64 | // STI support |
||
65 | if (isset($constants['CHILDREN'])) { |
||
66 | $this->addRenderer(new PropertyRenderer($constants['CHILDREN'], 'Children')); |
||
67 | } |
||
68 | |||
69 | if (isset($constants['DISCRIMINATOR'])) { |
||
70 | $this->addRenderer(new KeysRenderer($constants['DISCRIMINATOR'], 'Discriminator', false)); |
||
71 | } |
||
72 | |||
73 | $this->addRenderer(new ColumnsRenderer()); |
||
74 | if (isset($constants['TYPECAST_HANDLER'])) { |
||
75 | $this->addRenderer(new PropertyRenderer($constants['TYPECAST_HANDLER'], 'Typecast')); |
||
76 | } |
||
77 | |||
78 | if (isset($constants['LISTENERS'])) { |
||
79 | $this->addRenderer(new ListenersRenderer($constants['LISTENERS'], 'Listeners')); |
||
80 | } |
||
81 | |||
82 | $this->addRenderer( |
||
83 | new RelationsRenderer(), |
||
84 | new CustomPropertiesRenderer(\array_values($constants)), |
||
85 | ); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param array<string, int> $constants |
||
90 | * |
||
91 | * @return array<int, string> |
||
92 | */ |
||
93 | private function getOrmProperties(array $constants): array |
||
94 | { |
||
95 | $result = []; |
||
96 | foreach ($constants as $name => $value) { |
||
97 | if (!array_key_exists($name, self::DEFAULT_PROPERTY_LIST)) { |
||
98 | continue; |
||
99 | } |
||
100 | $result[$value] = self::DEFAULT_PROPERTY_LIST[$name]; |
||
101 | } |
||
102 | return $result; |
||
103 | } |
||
104 | } |
||
105 |