Skip to content

Commit

Permalink
Allow usage of invokable controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
MalteWunsch committed Oct 30, 2024
1 parent cf8eea2 commit f29494d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/Handler/EmbeddedShortcodeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ public function __construct(
RequestStack $requestStack,
?LoggerInterface $logger = null
) {
$callableFragments = explode('::', $controllerName);
if (!\is_array($callableFragments) || !isset($callableFragments[1]) || !method_exists($callableFragments[0], $callableFragments[1])) {
throw new InvalidArgumentException('The controller method: "'.$controllerName.'" does not exist.');
}
$this->validateControllerName($controllerName);

$this->fragmentHandler = $fragmentHandler;
$this->controllerName = $controllerName;
Expand Down Expand Up @@ -92,4 +89,22 @@ public function getControllerName(): string
{
return $this->controllerName;
}

private function validateControllerName(string $controllerName): void
{
if (class_exists($controllerName)) {
// Check with method_exists instead of is_callable, because is_callable would need an object instance to
// positively test an invokable classes
if (method_exists($controllerName, '__invoke')) {
return;
}

throw new InvalidArgumentException('The configured controller "'.$controllerName.'" does not refer a method. Although a class "'.$controllerName.'" exists, but has no __invoke method.');
}

$callableFragments = explode('::', $controllerName);
if (!\is_array($callableFragments) || !isset($callableFragments[1]) || !method_exists($callableFragments[0], $callableFragments[1])) {
throw new InvalidArgumentException('The controller method: "'.$controllerName.'" does not exist.');
}
}
}
1 change: 1 addition & 0 deletions tests/Functional/EmbeddedShortcodeHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public static function provideControllerNames(): Generator
yield 'Not existing controller' => ['Foo\Bar::baz'];
yield 'Missing method name' => [ShortcodeTestController::class];
yield 'Not existing method' => [ShortcodeTestController::class.'_notExistingMethod'];
yield 'Missing class' => ['ThisClassDoesNotExist'];
}

private function processShortcodes(string $content, ?Request $request = null): string
Expand Down

0 comments on commit f29494d

Please sign in to comment.