From 0b7fe7424dedd2461e6fd274a10eb6e9d16fc116 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 11 Feb 2021 02:04:46 +0100 Subject: [PATCH] added FormFactory & DI service --- src/Bridges/FormsDI/FormsExtension.php | 11 ++++++++ src/Forms/Form.php | 7 +++++ src/Forms/FormFactory.php | 38 ++++++++++++++++++++++++++ tests/Forms.DI/FormsExtension.phpt | 15 ++++++++++ tests/Forms/FormFactory.phpt | 20 ++++++++++++++ 5 files changed, 91 insertions(+) create mode 100644 src/Forms/FormFactory.php create mode 100644 tests/Forms/FormFactory.phpt diff --git a/src/Bridges/FormsDI/FormsExtension.php b/src/Bridges/FormsDI/FormsExtension.php index 58281781b..17b052b05 100644 --- a/src/Bridges/FormsDI/FormsExtension.php +++ b/src/Bridges/FormsDI/FormsExtension.php @@ -26,6 +26,17 @@ public function __construct() } + public function beforeCompile() + { + $builder = $this->getContainerBuilder(); + + if ($builder->findByType(Nette\Http\IRequest::class)) { + $builder->addDefinition($this->prefix('factory')) + ->setFactory(Nette\Forms\FormFactory::class); + } + } + + public function afterCompile(Nette\PhpGenerator\ClassType $class) { $initialize = $this->initialization ?? $class->getMethod('initialize'); diff --git a/src/Forms/Form.php b/src/Forms/Form.php index 6b2e66146..ba685ed0d 100644 --- a/src/Forms/Form.php +++ b/src/Forms/Form.php @@ -666,6 +666,13 @@ public function getToggles(): array /********************* backend ****************d*g**/ + /** @internal */ + public function setHttpRequest(Nette\Http\IRequest $request) + { + $this->httpRequest = $request; + } + + private function getHttpRequest(): Nette\Http\IRequest { if (!$this->httpRequest) { diff --git a/src/Forms/FormFactory.php b/src/Forms/FormFactory.php new file mode 100644 index 000000000..8bbb8a7cf --- /dev/null +++ b/src/Forms/FormFactory.php @@ -0,0 +1,38 @@ +httpRequest = $httpRequest; + } + + + public function createForm(string $name = null): Form + { + $form = new Form($name); + $form->setHttpRequest($this->httpRequest); + return $form; + } +} diff --git a/tests/Forms.DI/FormsExtension.phpt b/tests/Forms.DI/FormsExtension.phpt index e96c8d978..17dc487f0 100644 --- a/tests/Forms.DI/FormsExtension.phpt +++ b/tests/Forms.DI/FormsExtension.phpt @@ -52,3 +52,18 @@ Assert::exception(function () { eval($compiler->addConfig($config)->setClassName('Container2')->compile()); }, Nette\InvalidArgumentException::class, 'Constant Nette\Forms\Form::Foo\Bar or constant Foo\Bar does not exist.'); + + +test('form factory', function () { + $compiler = new DI\Compiler; + $compiler->addExtension('http', new Nette\Bridges\HttpDI\HttpExtension); + $compiler->addExtension('forms', new FormsExtension); + + eval($compiler->setClassName('Container3')->compile()); + + $container = new Container3; + $container->initialize(); + $factory = $container->getByType(Nette\Forms\FormFactory::class); + $form = $factory->createForm(); + Assert::type(Form::class, $form); +}); diff --git a/tests/Forms/FormFactory.phpt b/tests/Forms/FormFactory.phpt new file mode 100644 index 000000000..8c3862c00 --- /dev/null +++ b/tests/Forms/FormFactory.phpt @@ -0,0 +1,20 @@ +fromGlobals(); +$factory = new FormFactory($request); +$form = $factory->createForm(); +Assert::type(Form::class, $form); + + +$form = $factory->createForm('foo'); +Assert::same('foo', $form->getName());