From 820bc26b139b6b69b332551d87c461654adc635d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1chym=20Tou=C5=A1ek?= Date: Thu, 22 Sep 2016 23:08:53 +0200 Subject: [PATCH] Call checkRequirements for createComponent methods --- src/Application/UI/Component.php | 16 ++++++++++ tests/UI/Component.createComponent().phpt | 36 +++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 tests/UI/Component.createComponent().phpt 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());