function EntityResolverManager::getControllerClass

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Entity/EntityResolverManager.php \Drupal\Core\Entity\EntityResolverManager::getControllerClass()
  2. 8.9.x core/lib/Drupal/Core/Entity/EntityResolverManager.php \Drupal\Core\Entity\EntityResolverManager::getControllerClass()
  3. 10 core/lib/Drupal/Core/Entity/EntityResolverManager.php \Drupal\Core\Entity\EntityResolverManager::getControllerClass()

Gets the controller class using route defaults.

By design we cannot support all possible routes, but just the ones which use the defaults provided by core, which are _controller and _form.

Rather than creating an instance of every controller determine the class and method that would be used. This is not possible for the service:method notation as the runtime container does not allow static introspection.

Parameters

array $defaults: The default values provided by the route.

Return value

string|null Returns the controller class, otherwise NULL.

See also

\Drupal\Core\Controller\ControllerResolver::getControllerFromDefinition()

\Drupal\Core\Controller\ClassResolver::getInstanceFromDefinition()

1 call to EntityResolverManager::getControllerClass()
EntityResolverManager::setRouteOptions in core/lib/Drupal/Core/Entity/EntityResolverManager.php
Set the upcasting route objects.

File

core/lib/Drupal/Core/Entity/EntityResolverManager.php, line 70

Class

EntityResolverManager
Sets the entity route parameter converter options automatically.

Namespace

Drupal\Core\Entity

Code

protected function getControllerClass(array $defaults) {
    $controller = NULL;
    if (isset($defaults['_controller'])) {
        $controller = $defaults['_controller'];
    }
    if (isset($defaults['_form'])) {
        $controller = $defaults['_form'];
        // Check if the class exists and if so use the buildForm() method from the
        // interface.
        if (class_exists($controller)) {
            return [
                $controller,
                'buildForm',
            ];
        }
    }
    if ($controller === NULL) {
        return NULL;
    }
    if (!str_contains($controller, ':')) {
        if (method_exists($controller, '__invoke')) {
            return [
                $controller,
                '__invoke',
            ];
        }
        if (function_exists($controller)) {
            return $controller;
        }
        return NULL;
    }
    $count = substr_count($controller, ':');
    if ($count == 1) {
        // Controller in the service:method notation. Get the information from the
        // service. This is dangerous as the controller could depend on services
        // that could not exist at this point. There is however no other way to
        // do it, as the container does not allow static introspection.
        [
            $class_or_service,
            $method,
        ] = explode(':', $controller, 2);
        return [
            $this->classResolver
                ->getInstanceFromDefinition($class_or_service),
            $method,
        ];
    }
    elseif (str_contains($controller, '::')) {
        // Controller in the class::method notation.
        return explode('::', $controller, 2);
    }
    return NULL;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.