File tree Expand file tree Collapse file tree 2 files changed +48
-0
lines changed Expand file tree Collapse file tree 2 files changed +48
-0
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ use Nette \Application ;
4+ use Tester \Assert ;
5+
6+ require __DIR__ . '/../bootstrap.php ' ;
7+
8+
9+ class TestSubComponent extends Application \UI \Component
10+ {
11+ }
12+
13+ class TestComponent extends Application \UI \Component
14+ {
15+ public $ calledWith ;
16+
17+ public function checkRequirements ($ element )
18+ {
19+ $ this ->calledWith = $ element ;
20+ }
21+
22+ public function createComponentTest ()
23+ {
24+ return new TestSubComponent ;
25+ }
26+ }
27+
28+ $ component = new TestComponent ;
29+ Assert::true ($ component ->getComponent ('test ' ) instanceof TestSubComponent);
30+ Assert::true ($ component ->calledWith instanceof ReflectionMethod);
31+ Assert::same ('createComponentTest ' , $ component ->calledWith ->getName ());
32+ Assert::same (TestComponent::class, $ component ->calledWith ->getDeclaringClass ()->getName ());
You can’t perform that action at this time.
0 commit comments