|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the Nette Framework (https://nette.org) |
| 5 | + * Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace Nette\Forms; |
| 11 | + |
| 12 | + |
| 13 | +/** |
| 14 | + * Generates blueprints for forms. |
| 15 | + */ |
| 16 | +final class Blueprint |
| 17 | +{ |
| 18 | + private const ClassNameSuffix = 'FormData'; |
| 19 | + |
| 20 | + |
| 21 | + /** |
| 22 | + * Generates blueprint of form Latte template. |
| 23 | + */ |
| 24 | + public static function latte(Form $form, bool $exit = true): void |
| 25 | + { |
| 26 | + $bp = new self; |
| 27 | + $bp->printBegin(); |
| 28 | + $bp->printHeader('Form ' . $form->getName()); |
| 29 | + $bp->printCode($bp->generateLatte($form), 'latte'); |
| 30 | + $bp->printEnd(); |
| 31 | + if ($exit) { |
| 32 | + exit; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + /** |
| 38 | + * Generates blueprint of form data class. |
| 39 | + */ |
| 40 | + public static function dataClass(Form $form, bool $exit = true): void |
| 41 | + { |
| 42 | + $bp = new self; |
| 43 | + $bp->printBegin(); |
| 44 | + $bp->printHeader('Form Data Class ' . $form->getName()); |
| 45 | + $bp->printCode($bp->generateDataClass($form), 'php'); |
| 46 | + if (PHP_VERSION_ID >= 80000) { |
| 47 | + $bp->printCode($bp->generateDataClass($form, true), 'php'); |
| 48 | + } |
| 49 | + $bp->printEnd(); |
| 50 | + if ($exit) { |
| 51 | + exit; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + private function printBegin(): void |
| 57 | + { |
| 58 | + echo '<script src="https://nette.github.io/resources/prism/prism.js"></script>'; |
| 59 | + echo '<link rel="stylesheet" href="https://nette.github.io/resources/prism/prism.css">'; |
| 60 | + echo "<div style='all:initial; position:fixed; overflow:auto; z-index:1000; left:0; right:0; top:0; bottom:0; color:black; background:white; padding:1em'>\n"; |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + private function printEnd(): void |
| 65 | + { |
| 66 | + echo "</div>\n"; |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + private function printHeader(string $string): void |
| 71 | + { |
| 72 | + echo "<h1 style='all:initial; display:block; font-size:2em; margin:1em 0'>", |
| 73 | + htmlspecialchars($string), |
| 74 | + "</h1>\n"; |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + private function printCode(string $code, string $lang): void |
| 79 | + { |
| 80 | + echo '<pre><code class="language-', htmlspecialchars($lang), '">', |
| 81 | + htmlspecialchars($code), |
| 82 | + "</code></pre>\n"; |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + public function generateLatte(Form $form): string |
| 87 | + { |
| 88 | + $dict = new \SplObjectStorage; |
| 89 | + $dummyForm = new class extends Form { |
| 90 | + protected function receiveHttpData(): ?array |
| 91 | + { |
| 92 | + return []; |
| 93 | + } |
| 94 | + }; |
| 95 | + |
| 96 | + foreach ($form->getControls() as $input) { |
| 97 | + $dict[$input] = $dummyInput = new class extends Controls\BaseControl { |
| 98 | + public $inner; |
| 99 | + |
| 100 | + |
| 101 | + public function getLabel($caption = null) |
| 102 | + { |
| 103 | + return $this->inner->getLabel() |
| 104 | + ? '{label ' . $this->inner->lookupPath(Form::class) . '/}' |
| 105 | + : null; |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + public function getControl() |
| 110 | + { |
| 111 | + return '{input ' . $this->inner->lookupPath(Form::class) . '}'; |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | + public function isRequired(): bool |
| 116 | + { |
| 117 | + return $this->inner->isRequired(); |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + public function getOption($key) |
| 122 | + { |
| 123 | + return $key === 'rendered' |
| 124 | + ? parent::getOption($key) |
| 125 | + : $this->inner->getOption($key); |
| 126 | + } |
| 127 | + }; |
| 128 | + $dummyInput->inner = $input; |
| 129 | + $dummyForm->addComponent($dummyInput, (string) $dict->count()); |
| 130 | + $dummyInput->addError('{inputError ' . $input->lookupPath(Form::class) . '}'); |
| 131 | + } |
| 132 | + |
| 133 | + foreach ($form->getGroups() as $group) { |
| 134 | + $dummyGroup = $dummyForm->addGroup(); |
| 135 | + foreach ($group->getOptions() as $k => $v) { |
| 136 | + $dummyGroup->setOption($k, $v); |
| 137 | + } |
| 138 | + |
| 139 | + foreach ($group->getControls() as $control) { |
| 140 | + if ($dict[$control]) { |
| 141 | + $dummyGroup->add($dict[$control]); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + $renderer = clone $form->getRenderer(); |
| 147 | + $dummyForm->setRenderer($renderer); |
| 148 | + $dummyForm->onRender = $form->onRender; |
| 149 | + $dummyForm->fireRenderEvents(); |
| 150 | + |
| 151 | + if ($renderer instanceof Rendering\DefaultFormRenderer) { |
| 152 | + $renderer->wrappers['error']['container'] = $renderer->getWrapper('error container')->setAttribute('n:ifcontent', true); |
| 153 | + $renderer->wrappers['error']['item'] = $renderer->getWrapper('error item')->setAttribute('n:foreach', '$form->getOwnErrors() as $error'); |
| 154 | + $renderer->wrappers['control']['errorcontainer'] = $renderer->getWrapper('control errorcontainer')->setAttribute('n:ifcontent', true); |
| 155 | + $dummyForm->addError('{$error}'); |
| 156 | + |
| 157 | + ob_start(); |
| 158 | + $dummyForm->render('end'); |
| 159 | + $end = ob_get_clean(); |
| 160 | + } |
| 161 | + |
| 162 | + ob_start(); |
| 163 | + $dummyForm->render(); |
| 164 | + $body = ob_get_clean(); |
| 165 | + |
| 166 | + $body = str_replace($dummyForm->getElementPrototype()->startTag(), '<form n:name="' . $form->getName() . '">', $body); |
| 167 | + $body = str_replace($end ?? '', '</form>', $body); |
| 168 | + return $body; |
| 169 | + } |
| 170 | + |
| 171 | + |
| 172 | + public function generateDataClass( |
| 173 | + Container $container, |
| 174 | + ?bool $propertyPromotion = false, |
| 175 | + ?string $baseName = null |
| 176 | + ): string |
| 177 | + { |
| 178 | + $baseName = $baseName ?? preg_replace('~Form$~', '', ucwords((string) $container->getName())); |
| 179 | + $nextCode = ''; |
| 180 | + $props = []; |
| 181 | + foreach ($container->getComponents() as $name => $input) { |
| 182 | + if ($input instanceof Controls\BaseControl && $input->isOmitted()) { |
| 183 | + continue; |
| 184 | + } elseif ($input instanceof Controls\Checkbox) { |
| 185 | + $type = 'bool'; |
| 186 | + } elseif ($input instanceof Controls\MultiChoiceControl) { |
| 187 | + $type = 'array'; |
| 188 | + } elseif ($input instanceof Controls\ChoiceControl) { |
| 189 | + $type = 'string|int'; |
| 190 | + if (!$input->isRequired()) { |
| 191 | + $type .= '|null'; |
| 192 | + } |
| 193 | + } elseif ($input instanceof Controls\HiddenField || $input instanceof Controls\TextBase) { |
| 194 | + $type = 'string'; |
| 195 | + foreach ($input->getRules() as $rule) { |
| 196 | + if ($rule->validator === Form::Integer) { |
| 197 | + $type = 'int'; |
| 198 | + break; |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + if (!$input->isRequired()) { |
| 203 | + $type = '?' . $type; |
| 204 | + } |
| 205 | + } elseif ($input instanceof Controls\UploadControl) { |
| 206 | + $type = '\Nette\Http\FileUpload'; |
| 207 | + if (!$input->isRequired()) { |
| 208 | + $type = '?' . $type; |
| 209 | + } |
| 210 | + } elseif ($input instanceof Container) { |
| 211 | + $type = $baseName . ucwords($name); |
| 212 | + $nextCode .= $this->generateDataClass($input, $propertyPromotion, $type); |
| 213 | + $type .= self::ClassNameSuffix; |
| 214 | + } else { |
| 215 | + $type = ''; |
| 216 | + } |
| 217 | + |
| 218 | + $props[] = 'public ' . ($type ? $type . ' ' : '') . '$' . $name; |
| 219 | + } |
| 220 | + |
| 221 | + $class = $baseName . self::ClassNameSuffix; |
| 222 | + return "class $class\n" |
| 223 | + . "{\n" |
| 224 | + . ($propertyPromotion |
| 225 | + ? "\tpublic function __construct(\n" |
| 226 | + . ($props ? "\t\t" . implode(",\n\t\t", $props) . ",\n" : '') |
| 227 | + . "\t) {\n\t}\n" |
| 228 | + : ($props ? "\t" . implode(";\n\t", $props) . ";\n" : '') |
| 229 | + ) |
| 230 | + . "}\n\n" |
| 231 | + . $nextCode; |
| 232 | + } |
| 233 | +} |
0 commit comments