diff --git a/src/Application/UI/Component.php b/src/Application/UI/Component.php index 21203abf9..418c2c55c 100644 --- a/src/Application/UI/Component.php +++ b/src/Application/UI/Component.php @@ -106,6 +106,22 @@ public function checkRequirements($element) } + /** + * Component factory. Delegates the creation of components to a createComponent 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 diff --git a/tests/UI/Component.createComponent().phpt b/tests/UI/Component.createComponent().phpt new file mode 100644 index 000000000..d785f2334 --- /dev/null +++ b/tests/UI/Component.createComponent().phpt @@ -0,0 +1,36 @@ +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());