function ContainerInfoController::serviceDetail

Same name in other branches
  1. 8.x-1.x src/Controller/ContainerInfoController.php \Drupal\devel\Controller\ContainerInfoController::serviceDetail()
  2. 4.x src/Controller/ContainerInfoController.php \Drupal\devel\Controller\ContainerInfoController::serviceDetail()

Returns a render array representation of the service.

Parameters

string $service_id: The ID of the service to retrieve.

Return value

array A render array containing the service detail.

Throws

\Symfony\Component\HttpKernel\Exception\NotFoundHttpException If the requested service is not defined.

1 string reference to 'ContainerInfoController::serviceDetail'
devel.routing.yml in ./devel.routing.yml
devel.routing.yml

File

src/Controller/ContainerInfoController.php, line 126

Class

ContainerInfoController
Provides route responses for the container info pages.

Namespace

Drupal\devel\Controller

Code

public function serviceDetail(string $service_id) : array {
    $container = $this->kernel
        ->getContainer();
    
    /** @var object|null $instance */
    $instance = $container->get($service_id, ContainerInterface::NULL_ON_INVALID_REFERENCE);
    if ($instance === NULL) {
        throw new NotFoundHttpException();
    }
    $output = [];
    // Tries to retrieve the service definition from the kernel's cached
    // container definition.
    $cached_definitions = $this->kernel
        ->getCachedContainerDefinition();
    if ($cached_definitions && isset($cached_definitions['services'][$service_id])) {
        $definition = unserialize($cached_definitions['services'][$service_id]);
        // If the service has an alias add it to the definition.
        if ($alias = array_search($service_id, $cached_definitions['aliases'], TRUE)) {
            $definition['alias'] = $alias;
        }
        $output['definition'] = $this->dumper
            ->exportAsRenderable($definition, $this->t('Computed Definition'));
    }
    $output['instance'] = $this->dumper
        ->exportAsRenderable($instance, $this->t('Instance'));
    return $output;
}