Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Application/UI/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,22 @@ public function checkRequirements($element)
}


/**
* Component factory. Delegates the creation of components to a createComponent<Name> method.
* @param string $name
* @return Nette\ComponentModel\IComponent the created component (optionally)
*/
protected function createComponent($name)
{
$method = 'createComponent' . ucfirst($name);
if (method_exists($this, $method)) {
$this->checkRequirements($this->getReflection()->getMethod($method));
}

return parent::createComponent($name);
}


/**
* Access to reflection.
* @return ComponentReflection
Expand Down
36 changes: 36 additions & 0 deletions tests/UI/Component.createComponent().phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* Test: Nette\Application\UI\Component::createComponent()
*/

use Nette\Application;
use Tester\Assert;

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


class TestSubComponent extends Application\UI\Component
{
}

class TestComponent extends Application\UI\Component
{
public $calledWith;

public function checkRequirements($element)
{
$this->calledWith = $element;
}

public function createComponentTest()
{
return new TestSubComponent;
}
}

$component = new TestComponent;
Assert::true($component->getComponent('test') instanceof TestSubComponent);
Assert::true($component->calledWith instanceof ReflectionMethod);
Assert::same('createComponentTest', $component->calledWith->getName());
Assert::same(TestComponent::class, $component->calledWith->getDeclaringClass()->getName());