Skip to content

Commit

Permalink
Configurable methodName and containerClass
Browse files Browse the repository at this point in the history
  • Loading branch information
barton committed Nov 5, 2018
1 parent b227c4b commit 5884d6a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,19 +437,21 @@ public function addComponent(IComponent $component, $name, $insertBefore = null)

/**
* @param string $methodName
* @param string $containerClass
* @return void
*/
public static function register(string $methodName = 'addDynamic')
public static function register(string $methodName = 'addDynamic', string $containerClass = '\Nette\Forms\Container')
{
if (self::$registered) {
\Nette\Forms\Container::extensionMethod(self::$registered, function () {
throw new \Nette\MemberAccessException;
});
}

\Nette\Forms\Container::extensionMethod($methodName, function (\Nette\Forms\Container $_this, $name, $factory, $createDefault = 0, $forceDefault = false) {
\Nette\Forms\Container::extensionMethod($methodName, function (\Nette\Forms\Container $_this, $name, $factory, $createDefault = 0, $forceDefault = false) use ($containerClass) {
$control = new Container($factory, $createDefault, $forceDefault);
$control->currentGroup = $_this->currentGroup;
$control->containerClass = $containerClass;
return $_this[$name] = $control;
});

Expand Down
15 changes: 14 additions & 1 deletion src/DI/ReplicatorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Nette\Configurator;
use Nette\DI\Compiler;
use Nette\DI\CompilerExtension;
use Nette\Forms\Container;
use Nette\PhpGenerator\ClassType;

/**
Expand All @@ -13,6 +14,15 @@
*/
class ReplicatorExtension extends CompilerExtension
{
private const DEFAULTS = [
'methodName' => 'addDynamic',
'containerClass' => '\Nette\Forms\Container',
];

public function loadConfiguration()
{
$this->validateConfig(self::DEFAULTS);
}

/**
* @param ClassType $class
Expand All @@ -22,7 +32,10 @@ public function afterCompile(ClassType $class)
parent::afterCompile($class);

$init = $class->getMethod('initialize');
$init->addBody('\Webwings\Replicator\Container::register();');
$init->addBody("\Webwings\Replicator\Container::register(?, ?);", [
$this->config['methodName'],
$this->config['containerClass'],
]);
}


Expand Down

0 comments on commit 5884d6a

Please sign in to comment.