File tree Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Original file line number Diff line number Diff line change @@ -106,6 +106,22 @@ public function checkRequirements($element)
106
106
}
107
107
108
108
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
+
109
125
/**
110
126
* Access to reflection.
111
127
* @return ComponentReflection
Original file line number Diff line number Diff line change
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 ());
You can’t perform that action at this time.
0 commit comments