Skip to content

Commit 27b5f35

Browse files
mabarMilan Felix Šulc
authored andcommitted
ApplicationFormFactory
1 parent ae2e288 commit 27b5f35

File tree

7 files changed

+159
-0
lines changed

7 files changed

+159
-0
lines changed

.docs/README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,86 @@
22

33
## Content
44

5+
- [ApplicationFormFactoryExtension - provides Nette\Application\UI\Form factory](#application-form-factory)
56
- [StandaloneFormFactoryExtension - provides Nette\Forms\Form factory](#standalone-form-factory)
67

8+
## Application Form Factory
9+
10+
ApplicationFormFactory returns instance of Nette\Application\UI\Form. It should be used instead of StandaloneFormFactory if [nette/application](https://github.com/nette/application) is installed.
11+
12+
```yaml
13+
extensions:
14+
forms.application: Contributte\Forms\DI\ApplicationFormFactoryExtension
15+
```
16+
17+
You can override it by your implementation.
18+
19+
```yaml
20+
services:
21+
forms.application.factory: My\FormFactory
22+
```
23+
24+
Straightforward is to **inject** factory to presenter.
25+
26+
```php
27+
namespace App\Presenters;
28+
29+
use Contributte\Forms\IApplicationFormFactory;
30+
use Nette\Application\UI\Form;
31+
32+
final class UserPresenter extends BasePresenter
33+
{
34+
35+
/** @var IApplicationFormFactory @inject */
36+
public $factory;
37+
38+
protected function createComponentUserForm(): Form
39+
{
40+
$form = $this->factory->create();
41+
42+
// Add inputs here!
43+
44+
return $form;
45+
}
46+
47+
}
48+
```
49+
50+
Even better is to use factory in your custom form factory.
51+
52+
```php
53+
namespace App\Forms;
54+
55+
use Contributte\Forms\IApplicationFormFactory;
56+
use Nette\Application\UI\Form;
57+
58+
final class UserFormFactory
59+
{
60+
61+
/** @var IApplicationFormFactory */
62+
private $factory;
63+
64+
public function __construct(IApplicationFormFactory $factory)
65+
{
66+
$this->factory = $factory;
67+
}
68+
69+
public function create(): Form
70+
{
71+
$form = $this->factory->create();
72+
73+
// Add inputs here!
74+
75+
return $form;
76+
}
77+
78+
}
79+
```
80+
781
## Standalone Form Factory
882

83+
StandaloneFormFactory returns instance of Nette\Forms\Form. It should be used only if [nette/application](https://github.com/nette/application) is not installed.
84+
985
```yaml
1086
extensions:
1187
forms.standalone: Contributte\Forms\DI\StandaloneFormFactoryExtension

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ composer require contributte/forms
3131

3232
## Overview
3333

34+
- [Nette\Forms\Form factory (ApplicationFormFactoryExtension)](https://github.com/contributte/forms/blob/master/.docs/README.md#application-form-factory)
3435
- [Nette\Forms\Form factory (StandaloneFormFactoryExtension)](https://github.com/contributte/forms/blob/master/.docs/README.md#standalone-form-factory)
3536

3637
## Maintainers

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@
2626
"require-dev": {
2727
"ninjify/qa": "^0.8.0",
2828
"ninjify/nunjuck": "^0.2",
29+
"nette/application": "~2.4.12",
2930
"nette/di": "~2.4.11",
3031
"nette/utils": "~2.5.2"
3132
},
3233
"suggest": {
3334
"nette/di": "to use FormFactoryExtension[CompilerExtension]"
3435
},
36+
"conflict": {
37+
"nette/application": "<2.4.12"
38+
},
3539
"autoload": {
3640
"psr-4": {
3741
"Contributte\\Forms\\": "src"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Contributte\Forms\DI;
4+
5+
use Contributte\Forms\Exception\LogicalException;
6+
use Contributte\Forms\IApplicationFormFactory;
7+
use Nette\Application\UI\Form;
8+
use Nette\DI\CompilerExtension;
9+
10+
class ApplicationFormFactoryExtension extends CompilerExtension
11+
{
12+
13+
/**
14+
* Register services
15+
*/
16+
public function loadConfiguration(): void
17+
{
18+
if (!class_exists(Form::class)) {
19+
throw new LogicalException(sprintf('Install nette/application to use %s factory', Form::class));
20+
}
21+
22+
$builder = $this->getContainerBuilder();
23+
24+
$builder->addDefinition($this->prefix('factory'))
25+
->setImplement(IApplicationFormFactory::class);
26+
}
27+
28+
}

src/Exception/LogicalException.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Contributte\Forms\Exception;
4+
5+
use LogicException;
6+
7+
class LogicalException extends LogicException
8+
{
9+
10+
}

src/IApplicationFormFactory.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Contributte\Forms;
4+
5+
use Nette\Application\UI\Form;
6+
7+
interface IApplicationFormFactory
8+
{
9+
10+
public function create(): Form;
11+
12+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types = 1);
2+
3+
/**
4+
* Test: DI\ApplicationFormFactoryExtension
5+
*/
6+
7+
use Contributte\Forms\DI\ApplicationFormFactoryExtension;
8+
use Contributte\Forms\IApplicationFormFactory;
9+
use Nette\Application\UI\Form;
10+
use Nette\DI\Compiler;
11+
use Nette\DI\Container;
12+
use Nette\DI\ContainerLoader;
13+
use Tester\Assert;
14+
15+
require_once __DIR__ . '/../../bootstrap.php';
16+
17+
test(function (): void {
18+
$loader = new ContainerLoader(TEMP_DIR, true);
19+
$class = $loader->load(function (Compiler $compiler): void {
20+
$compiler->addExtension('formFactory', new ApplicationFormFactoryExtension());
21+
}, 1);
22+
23+
/** @var Container $container */
24+
$container = new $class();
25+
26+
Assert::type(IApplicationFormFactory::class, $container->getByType(IApplicationFormFactory::class));
27+
Assert::type(Form::class, $container->getByType(IApplicationFormFactory::class)->create());
28+
});

0 commit comments

Comments
 (0)