0 follower

Final Class Yiisoft\Yii\Debug\Api\Debug\Controller\DebugController

InheritanceYiisoft\Yii\Debug\Api\Debug\Controller\DebugController

Debug controller provides endpoints that expose information about requests processed that debugger collected.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Yii\Debug\Api\Debug\Controller\DebugController
dump() Dump information about a processed request identified by ID. Yiisoft\Yii\Debug\Api\Debug\Controller\DebugController
eventStream() Yiisoft\Yii\Debug\Api\Debug\Controller\DebugController
index() List of requests processed. Yiisoft\Yii\Debug\Api\Debug\Controller\DebugController
object() Object information about a processed request identified by ID. Yiisoft\Yii\Debug\Api\Debug\Controller\DebugController
summary() Summary about a processed request identified by ID specified. Yiisoft\Yii\Debug\Api\Debug\Controller\DebugController
view() Detail information about a processed request identified by ID. Yiisoft\Yii\Debug\Api\Debug\Controller\DebugController

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Yiisoft\DataResponse\DataResponseFactoryInterface $responseFactory, Yiisoft\Yii\Debug\Api\Debug\Repository\CollectorRepositoryInterface $collectorRepository )
$responseFactory \Yiisoft\DataResponse\DataResponseFactoryInterface
$collectorRepository Yiisoft\Yii\Debug\Api\Debug\Repository\CollectorRepositoryInterface

                public function __construct(
    private DataResponseFactoryInterface $responseFactory,
    private CollectorRepositoryInterface $collectorRepository
) {
}

            
dump() public method

Dump information about a processed request identified by ID.

public \Psr\Http\Message\ResponseInterface dump ( \Yiisoft\Router\CurrentRoute $currentRoute )
$currentRoute \Yiisoft\Router\CurrentRoute
return \Psr\Http\Message\ResponseInterface

Response.

throws Yiisoft\Yii\Debug\Api\Debug\Exception\NotFoundException

                #[OA\Get(
    path: '/debug/api/dump/{id}?collector={collector}',
    description: 'Dump information about a processed request identified by ID',
    tags: ['yii-debug-api'],
    responses: [
        new OA\Response(
            response: '200',
            description: 'Success',
            content: new OA\JsonContent(
                allOf: [
                    new OA\Schema(ref: '#/components/schemas/DebugSuccessResponse'),
                ]
            )
        ),
        new OA\Response(
            response: '404',
            description: 'Not found',
            content: new OA\JsonContent(
                allOf: [
                    new OA\Schema(ref: '#/components/schemas/DebugNotFoundResponse'),
                ]
            )
        ),
    ],
    parameters: [
        new OA\Parameter(
            name: 'id',
            required: true,
            in: 'path',
            parameter: 'id',
            description: 'Request ID for getting the dump information',
            schema: new OA\Schema(type: 'string')
        ),
        new OA\Parameter(
            name: 'collector',
            required: false,
            in: 'path',
            parameter: 'collector',
            description: 'Collector for getting the dump information',
            schema: new OA\Schema(type: 'string'),
            allowEmptyValue: true
        ),
    ]
)]
public function dump(CurrentRoute $currentRoute): ResponseInterface
{
    $data = $this->collectorRepository->getDumpObject(
        $currentRoute->getArgument('id')
    );
    if ($currentRoute->getArgument('collector') !== null) {
        if (isset($data[$currentRoute->getArgument('collector')])) {
            $data = $data[$currentRoute->getArgument('collector')];
        } else {
            throw new NotFoundException('Requested collector doesn\'t exists.');
        }
    }
    return $this->responseFactory->createResponse($data);
}

            
eventStream() public method

public \Psr\Http\Message\ResponseInterface eventStream ( \Yiisoft\Yii\Debug\Storage\StorageInterface $storage, \Psr\Http\Message\ResponseFactoryInterface $responseFactory )
$storage \Yiisoft\Yii\Debug\Storage\StorageInterface
$responseFactory \Psr\Http\Message\ResponseFactoryInterface

                public function eventStream(
    StorageInterface $storage,
    ResponseFactoryInterface $responseFactory
): ResponseInterface {
    // TODO implement OS signal handling
    $compareFunction = static function () use ($storage) {
        $read = $storage->read(StorageInterface::TYPE_SUMMARY, null);
        return md5(json_encode($read, JSON_THROW_ON_ERROR));
    };
    $hash = $compareFunction();
    $maxRetries = 10;
    $retries = 0;
    return $responseFactory->createResponse()
        ->withHeader('Content-Type', 'text/event-stream')
        ->withHeader('Cache-Control', 'no-cache')
        ->withHeader('Connection', 'keep-alive')
        ->withBody(
            new ServerSentEventsStream(function (array &$buffer) use (
                $compareFunction,
                &$hash,
                &$retries,
                $maxRetries
            ) {
                $newHash = $compareFunction();
                if ($hash !== $newHash) {
                    $response = [
                        'type' => 'debug-updated',
                        'payload' => [],
                    ];
                    $buffer[] = json_encode($response);
                    $hash = $newHash;
                }
                // break the loop if the client aborted the connection (closed the page)
                if (connection_aborted()) {
                    return false;
                }
                if ($retries++ > $maxRetries) {
                    return false;
                }
                sleep(1);
                return true;
            })
        );
}

            
index() public method

List of requests processed.

public \Psr\Http\Message\ResponseInterface index ( )

                #[OA\Get(
    path: '/debug/api',
    description: 'List of requests processed',
    tags: ['yii-debug-api'],
    responses: [
        new OA\Response(
            response: '200',
            description: 'Success',
            content: new OA\JsonContent(
                allOf: [
                    new OA\Schema(ref: '#/components/schemas/DebugSuccessResponse'),
                ]
            )
        ),
    ]
)]
public function index(): ResponseInterface
{
    return $this->responseFactory->createResponse($this->collectorRepository->getSummary());
}

            
object() public method

Object information about a processed request identified by ID.

public \Psr\Http\Message\ResponseInterface object ( \Yiisoft\Router\CurrentRoute $currentRoute )
$currentRoute \Yiisoft\Router\CurrentRoute
return \Psr\Http\Message\ResponseInterface

Response.

                #[OA\Get(
    path: '/debug/api/object/{id}/{objectId}',
    description: 'Object information about a processed request identified by ID',
    tags: ['yii-debug-api'],
    responses: [
        new OA\Response(
            response: '200',
            description: 'Success',
            content: new OA\JsonContent(
                allOf: [
                    new OA\Schema(ref: '#/components/schemas/DebugSuccessResponse'),
                ]
            )
        ),
        new OA\Response(
            response: '404',
            description: 'Not found',
            content: new OA\JsonContent(
                allOf: [
                    new OA\Schema(ref: '#/components/schemas/DebugNotFoundResponse'),
                ]
            )
        ),
    ],
    parameters: [
        new OA\Parameter(
            name: 'id',
            required: true,
            in: 'path',
            parameter: 'id',
            description: 'Request ID for getting the object information',
            schema: new OA\Schema(type: 'string')
        ),
        new OA\Parameter(
            name: 'objectId',
            required: true,
            in: 'path',
            parameter: 'objectId',
            description: 'ID for getting the object information',
            schema: new OA\Schema(type: 'string')
        ),
    ]
)]
public function object(CurrentRoute $currentRoute): ResponseInterface
{
    $data = $this->collectorRepository->getObject(
        $currentRoute->getArgument('id'),
        $currentRoute->getArgument('objectId')
    );
    if (null === $data) {
        throw new NotFoundException('Requested objectId doesn\'t exists.');
    }
    return $this->responseFactory->createResponse([
        'class' => $data[0],
        'value' => $data[1],
    ]);
}

            
summary() public method

Summary about a processed request identified by ID specified.

public \Psr\Http\Message\ResponseInterface summary ( \Yiisoft\Router\CurrentRoute $currentRoute )
$currentRoute \Yiisoft\Router\CurrentRoute

                #[OA\Get(
    path: '/debug/api/summary/{id}',
    description: 'Summary about a processed request identified by ID specified',
    tags: ['yii-debug-api'],
    responses: [
        new OA\Response(
            response: '200',
            description: 'Success',
            content: new OA\JsonContent(
                allOf: [
                    new OA\Schema(ref: '#/components/schemas/DebugSuccessResponse'),
                ]
            )
        ),
        new OA\Response(
            response: '404',
            description: 'Not found',
            content: new OA\JsonContent(
                allOf: [
                    new OA\Schema(ref: '#/components/schemas/DebugNotFoundResponse'),
                ]
            )
        ),
    ],
    parameters: [
        new OA\Parameter(
            name: 'id',
            required: true,
            in: 'path',
            parameter: 'id',
            description: 'Request ID for getting the summary',
            schema: new OA\Schema(type: 'string')
        ),
    ]
)]
public function summary(CurrentRoute $currentRoute): ResponseInterface
{
    $data = $this->collectorRepository->getSummary($currentRoute->getArgument('id'));
    return $this->responseFactory->createResponse($data);
}

            
view() public method

Detail information about a processed request identified by ID.

public \Psr\Http\Message\ResponseInterface view ( \Yiisoft\Router\CurrentRoute $currentRoute, \Psr\Http\Message\ServerRequestInterface $serverRequest, \Psr\Container\ContainerInterface $container )
$currentRoute \Yiisoft\Router\CurrentRoute
$serverRequest \Psr\Http\Message\ServerRequestInterface
$container \Psr\Container\ContainerInterface

                #[OA\Get(
    path: '/debug/api/view/{id}?collector={collector}',
    description: 'Detail information about a processed request identified by ID',
    tags: ['yii-debug-api'],
    responses: [
        new OA\Response(
            response: '200',
            description: 'Success',
            content: new OA\JsonContent(
                allOf: [
                    new OA\Schema(ref: '#/components/schemas/DebugSuccessResponse'),
                ]
            )
        ),
        new OA\Response(
            response: '404',
            description: 'Not found',
            content: new OA\JsonContent(
                allOf: [
                    new OA\Schema(ref: '#/components/schemas/DebugNotFoundResponse'),
                ]
            )
        ),
    ],
    parameters: [
        new OA\Parameter(
            name: 'id',
            required: true,
            in: 'path',
            parameter: 'id',
            description: 'Request ID for getting the summary',
            schema: new OA\Schema(type: 'string')
        ),
        new OA\Parameter(
            name: 'collector',
            in: 'query',
            parameter: 'collector',
            description: 'Collector for getting the detail information',
            schema: new OA\Schema(type: 'string'),
            allowEmptyValue: true
        ),
    ]
)]
public function view(
    CurrentRoute $currentRoute,
    ServerRequestInterface $serverRequest,
    ContainerInterface $container,
): ResponseInterface {
    $data = $this->collectorRepository->getDetail(
        $currentRoute->getArgument('id')
    );
    $collectorClass = $serverRequest->getQueryParams()['collector'] ?? null;
    if ($collectorClass !== null) {
        $data = $data[$collectorClass] ?? throw new NotFoundException(
            sprintf("Requested collector doesn't exist: %s.", $collectorClass)
        );
    }
    if (is_subclass_of($collectorClass, HtmlViewProviderInterface::class)) {
        return $this->createHtmlPanelResponse($container, $collectorClass, $data);
    }
    if (is_subclass_of($collectorClass, ModuleFederationProviderInterface::class)) {
        return $this->createJsPanelResponse($container, $collectorClass, $data);
    }
    return $this->responseFactory->createResponse($data);
}