Last active
December 20, 2015 22:59
-
-
Save pavelkucera/6208889 to your computer and use it in GitHub Desktop.
How to add a factory taking parameter into Nette DIC. Both ways, using config.neon and compiler extension, have the same result.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
factories: | |
formMapperFactory: | |
create: Namespace\FormMapper(..., %form%) | |
implement: Namespace\IFactory | |
parameters: [Namespace\Form form] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Extension extends \Nette\DI\CompilerExtension | |
{ | |
public function loadConfiguration() | |
{ | |
$container = $this->getContainerBuilder(); | |
$container->addDefinition($this->prefix('formMapperFactory')) | |
->setImplement('Namespace\IFactory') | |
->setParameters(['Namespace\Form form']) | |
->setFactory('Namespace\FormMapper', [ | |
'form' => new \Nette\PhpGenerator\PhpLiteral('$form'), | |
]) | |
->setAutowired(TRUE); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Namespace; | |
interface IFactory | |
{ | |
/** | |
* @param \Namespace\Form $form | |
* @return \Namespace\FormMapper | |
*/ | |
function create(\Namespace\Form $form); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$factory->create(new Namespace\Form()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thx, helped me today!