File tree Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change @@ -106,6 +106,26 @@ public function getService($name)
106106 }
107107
108108
109+ /**
110+ * Gets the service type by name.
111+ * @param string
112+ * @return string
113+ * @throws MissingServiceException
114+ */
115+ public function getServiceType ($ name )
116+ {
117+ if (isset ($ this ->meta [self ::ALIASES ][$ name ])) {
118+ return $ this ->getServiceType ($ this ->meta [self ::ALIASES ][$ name ]);
119+
120+ } elseif (isset ($ this ->meta [self ::SERVICES ][$ name ])) {
121+ return $ this ->meta [self ::SERVICES ][$ name ];
122+
123+ } else {
124+ throw new MissingServiceException ("Service ' $ name' not found. " );
125+ }
126+ }
127+
128+
109129 /**
110130 * Does the service exist?
111131 * @param string service name
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ /**
4+ * Test: Nette\DI\Container getServiceType.
5+ */
6+
7+ use Nette \DI \Container ;
8+ use Tester \Assert ;
9+
10+
11+ require __DIR__ . '/../bootstrap.php ' ;
12+
13+
14+ class MyContainer extends Container
15+ {
16+
17+ protected $ meta = [
18+ 'services ' => [
19+ 'one ' => 'One ' ,
20+ 'two ' => 'Two ' ,
21+ ],
22+ 'aliases ' => [
23+ 'three ' => 'one ' ,
24+ ],
25+ ];
26+
27+ }
28+
29+
30+ $ container = new MyContainer ;
31+
32+ Assert::same ('One ' , $ container ->getServiceType ('one ' ));
33+ Assert::same ('Two ' , $ container ->getServiceType ('two ' ));
34+ Assert::same ('One ' , $ container ->getServiceType ('three ' ));
35+
36+ Assert::exception (function () use ($ container ) {
37+ $ container ->getServiceType ('four ' );
38+ }, Nette \DI \MissingServiceException::class, "Service 'four' not found. " );
You can’t perform that action at this time.
0 commit comments