|
2 | 2 |
|
3 | 3 | namespace Webfactory\ShortcodeBundle\Handler; |
4 | 4 |
|
| 5 | +use InvalidArgumentException; |
5 | 6 | use Psr\Log\LoggerInterface; |
6 | 7 | use Psr\Log\NullLogger; |
7 | 8 | use Symfony\Component\HttpFoundation\RequestStack; |
@@ -33,17 +34,15 @@ class EmbeddedShortcodeHandler |
33 | 34 | /** @var RequestStack */ |
34 | 35 | private $requestStack; |
35 | 36 |
|
36 | | - /** |
37 | | - * @param string $controllerName |
38 | | - * @param string $renderer |
39 | | - */ |
40 | 37 | public function __construct( |
41 | 38 | FragmentHandler $fragmentHandler, |
42 | | - $controllerName, |
43 | | - $renderer, |
| 39 | + string $controllerName, |
| 40 | + string $renderer, |
44 | 41 | RequestStack $requestStack, |
45 | 42 | ?LoggerInterface $logger = null |
46 | 43 | ) { |
| 44 | + $this->validateControllerName($controllerName); |
| 45 | + |
47 | 46 | $this->fragmentHandler = $fragmentHandler; |
48 | 47 | $this->controllerName = $controllerName; |
49 | 48 | $this->renderer = $renderer; |
@@ -90,4 +89,22 @@ public function getControllerName(): string |
90 | 89 | { |
91 | 90 | return $this->controllerName; |
92 | 91 | } |
| 92 | + |
| 93 | + private function validateControllerName(string $controllerName): void |
| 94 | + { |
| 95 | + if (class_exists($controllerName)) { |
| 96 | + // Check with method_exists instead of is_callable, because is_callable would need an object instance to |
| 97 | + // positively test an invokable classes |
| 98 | + if (method_exists($controllerName, '__invoke')) { |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + throw new InvalidArgumentException('The configured controller "'.$controllerName.'" does not refer a method. Although a class "'.$controllerName.'" exists, but has no __invoke method.'); |
| 103 | + } |
| 104 | + |
| 105 | + $callableFragments = explode('::', $controllerName); |
| 106 | + if (!\is_array($callableFragments) || 2 !== \count($callableFragments) || !method_exists($callableFragments[0], $callableFragments[1])) { |
| 107 | + throw new InvalidArgumentException('The controller method: "'.$controllerName.'" does not exist.'); |
| 108 | + } |
| 109 | + } |
93 | 110 | } |
0 commit comments