Skip to content

Commit

Permalink
added FormFactory & DI service
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 11, 2021
1 parent 7927223 commit 0b7fe74
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Bridges/FormsDI/FormsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
7 changes: 7 additions & 0 deletions src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
38 changes: 38 additions & 0 deletions src/Forms/FormFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Nette\Forms;

use Nette;


/**
* Creates form.
*/
final class FormFactory
{
use Nette\StaticClass;

/** @var Nette\Http\IRequest */
private $httpRequest;


public function __construct(Nette\Http\IRequest $httpRequest)
{
$this->httpRequest = $httpRequest;
}


public function createForm(string $name = null): Form
{
$form = new Form($name);
$form->setHttpRequest($this->httpRequest);
return $form;
}
}
15 changes: 15 additions & 0 deletions tests/Forms.DI/FormsExtension.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
20 changes: 20 additions & 0 deletions tests/Forms/FormFactory.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

use Nette\Forms\Form;
use Nette\Forms\FormFactory;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


$request = (new Nette\Http\RequestFactory)->fromGlobals();
$factory = new FormFactory($request);
$form = $factory->createForm();
Assert::type(Form::class, $form);


$form = $factory->createForm('foo');
Assert::same('foo', $form->getName());

0 comments on commit 0b7fe74

Please sign in to comment.