Skip to content

Commit 820bc26

Browse files
committed
Call checkRequirements for createComponent methods
1 parent 21f129c commit 820bc26

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/Application/UI/Component.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,22 @@ public function checkRequirements($element)
106106
}
107107

108108

109+
/**
110+
* Component factory. Delegates the creation of components to a createComponent<Name> method.
111+
* @param string $name
112+
* @return Nette\ComponentModel\IComponent the created component (optionally)
113+
*/
114+
protected function createComponent($name)
115+
{
116+
$method = 'createComponent' . ucfirst($name);
117+
if (method_exists($this, $method)) {
118+
$this->checkRequirements($this->getReflection()->getMethod($method));
119+
}
120+
121+
return parent::createComponent($name);
122+
}
123+
124+
109125
/**
110126
* Access to reflection.
111127
* @return ComponentReflection
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Application\UI\Component::createComponent()
5+
*/
6+
7+
use Nette\Application;
8+
use Tester\Assert;
9+
10+
require __DIR__ . '/../bootstrap.php';
11+
12+
13+
class TestSubComponent extends Application\UI\Component
14+
{
15+
}
16+
17+
class TestComponent extends Application\UI\Component
18+
{
19+
public $calledWith;
20+
21+
public function checkRequirements($element)
22+
{
23+
$this->calledWith = $element;
24+
}
25+
26+
public function createComponentTest()
27+
{
28+
return new TestSubComponent;
29+
}
30+
}
31+
32+
$component = new TestComponent;
33+
Assert::true($component->getComponent('test') instanceof TestSubComponent);
34+
Assert::true($component->calledWith instanceof ReflectionMethod);
35+
Assert::same('createComponentTest', $component->calledWith->getName());
36+
Assert::same(TestComponent::class, $component->calledWith->getDeclaringClass()->getName());

0 commit comments

Comments
 (0)