File tree Expand file tree Collapse file tree 3 files changed +60
-46
lines changed
src/Symfony/Component/Messenger/Handler/Locator Expand file tree Collapse file tree 3 files changed +60
-46
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ /*
4+ * This file is part of the Symfony package.
5+ *
6+ * (c) Fabien Potencier <[email protected] > 7+ *
8+ * For the full copyright and license information, please view the LICENSE
9+ * file that was distributed with this source code.
10+ */
11+
12+ namespace Symfony \Component \Messenger \Handler \Locator ;
13+
14+ use Symfony \Component \Messenger \Exception \NoHandlerForMessageException ;
15+
16+ /**
17+ * @author Miha Vrhovnik <[email protected] > 18+ * @author Samuel Roze <[email protected] > 19+ */
20+ abstract class AbstractHandlerLocator implements HandlerLocatorInterface
21+ {
22+ public function resolve ($ message ): callable
23+ {
24+ $ messageClass = \get_class ($ message );
25+
26+ if (null === $ handler = $ this ->resolveFromClass ($ messageClass )) {
27+ throw new NoHandlerForMessageException (sprintf ('No handler for message "%s". ' , $ messageClass ));
28+ }
29+
30+ return $ handler ;
31+ }
32+
33+ private function resolveFromClass (string $ class ): ?callable
34+ {
35+ if ($ handler = $ this ->getHandler ($ class )) {
36+ return $ handler ;
37+ }
38+
39+ foreach (class_implements ($ class , false ) as $ interface ) {
40+ if ($ handler = $ this ->getHandler ($ interface )) {
41+ return $ handler ;
42+ }
43+ }
44+
45+ foreach (class_parents ($ class , false ) as $ parent ) {
46+ if ($ handler = $ this ->getHandler ($ parent )) {
47+ return $ handler ;
48+ }
49+ }
50+
51+ return null ;
52+ }
53+
54+ abstract protected function getHandler (string $ class );
55+ }
Original file line number Diff line number Diff line change 1212namespace Symfony \Component \Messenger \Handler \Locator ;
1313
1414use Psr \Container \ContainerInterface ;
15- use Symfony \Component \Messenger \Exception \NoHandlerForMessageException ;
1615
1716/**
1817 * @author Miha Vrhovnik <[email protected] > 1918 * @author Samuel Roze <[email protected] > 2019 */
21- class ContainerHandlerLocator implements HandlerLocatorInterface
20+ class ContainerHandlerLocator extends AbstractHandlerLocator
2221{
2322 private $ container ;
2423
@@ -27,39 +26,7 @@ public function __construct(ContainerInterface $container)
2726 $ this ->container = $ container ;
2827 }
2928
30- public function resolve ($ message ): callable
31- {
32- $ messageClass = \get_class ($ message );
33-
34- if (null === $ handler = $ this ->resolveFromClass ($ messageClass )) {
35- throw new NoHandlerForMessageException (sprintf ('No handler for message "%s". ' , $ messageClass ));
36- }
37-
38- return $ handler ;
39- }
40-
41- private function resolveFromClass ($ class ): ?callable
42- {
43- if ($ handler = $ this ->getHandler ($ class )) {
44- return $ handler ;
45- }
46-
47- foreach (class_implements ($ class , false ) as $ interface ) {
48- if ($ handler = $ this ->getHandler ($ interface )) {
49- return $ handler ;
50- }
51- }
52-
53- foreach (class_parents ($ class , false ) as $ parent ) {
54- if ($ handler = $ this ->getHandler ($ parent )) {
55- return $ handler ;
56- }
57- }
58-
59- return null ;
60- }
61-
62- private function getHandler ($ class )
29+ protected function getHandler (string $ class )
6330 {
6431 $ handlerKey = 'handler. ' .$ class ;
6532
Original file line number Diff line number Diff line change 1111
1212namespace Symfony \Component \Messenger \Handler \Locator ;
1313
14- use Symfony \Component \Messenger \Exception \NoHandlerForMessageException ;
15-
1614/**
1715 * @author Samuel Roze <[email protected] > 1816 */
19- class HandlerLocator implements HandlerLocatorInterface
17+ class HandlerLocator extends AbstractHandlerLocator
2018{
2119 /**
2220 * Maps a message (its class) to a given handler.
@@ -28,14 +26,8 @@ public function __construct(array $messageToHandlerMapping = array())
2826 $ this ->messageToHandlerMapping = $ messageToHandlerMapping ;
2927 }
3028
31- public function resolve ( $ message ): callable
29+ protected function getHandler ( string $ class )
3230 {
33- $ messageKey = \get_class ($ message );
34-
35- if (!isset ($ this ->messageToHandlerMapping [$ messageKey ])) {
36- throw new NoHandlerForMessageException (sprintf ('No handler for message "%s". ' , $ messageKey ));
37- }
38-
39- return $ this ->messageToHandlerMapping [$ messageKey ];
31+ return $ this ->messageToHandlerMapping [$ class ] ?? null ;
4032 }
4133}
You can’t perform that action at this time.
0 commit comments