Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutex #119

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft

Mutex #119

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"yiisoft/di": "^1.0",
"yiisoft/friendly-exception": "^1.1",
"yiisoft/http": "^1.2",
"yiisoft/mutex": "^1.1",
"yiisoft/router": "^3.0",
"yiisoft/translator": "^3.0",
"yiisoft/var-dumper": "^1.4",
Expand Down
61 changes: 32 additions & 29 deletions src/Controller/DebugController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@

namespace Yiisoft\Yii\Debug\Api\Controller;

use OpenApi\Annotations as OA;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Yiisoft\Assets\AssetManager;
use Yiisoft\Assets\AssetPublisherInterface;
use Yiisoft\DataResponse\DataResponse;
use Yiisoft\DataResponse\DataResponseFactoryInterface;
use Yiisoft\Mutex\Synchronizer;
use Yiisoft\Router\CurrentRoute;
use Yiisoft\Yii\Debug\Api\Exception\NotFoundException;
use Yiisoft\Yii\Debug\Api\Exception\PackageNotInstalledException;
use Yiisoft\Yii\Debug\Api\HtmlViewProviderInterface;
use Yiisoft\Yii\Debug\Api\ModuleFederationProviderInterface;
use Yiisoft\Yii\Debug\Api\Repository\CollectorRepositoryInterface;
use Yiisoft\Yii\Debug\Debugger;
use Yiisoft\Yii\View\ViewRenderer;

/**
Expand Down Expand Up @@ -48,7 +49,6 @@ public function __construct(private DataResponseFactoryInterface $responseFactor
*
* @OA\JsonContent(
* allOf={
*
* @OA\Schema(ref="#/components/schemas/DebugSuccessResponse")
* }
* )
Expand Down Expand Up @@ -78,13 +78,22 @@ public function index(): ResponseInterface
* description="Request ID for getting the summary"
* ),
*
* @OA\Parameter(
* name="timeout",
* required=false,
*
* @OA\Schema(type="integer"),
* in="query",
* parameter="timeout",
* description="Timeout to wait for the debug entry saving. 0 means do not wait."
* ),
*
* @OA\Response(
* response="200",
* description="Success",
*
* @OA\JsonContent(
* allOf={
*
* @OA\Schema(ref="#/components/schemas/DebugSuccessResponse")
* }
* )
Expand All @@ -96,16 +105,21 @@ public function index(): ResponseInterface
*
* @OA\JsonContent(
* allOf={
*
* @OA\Schema(ref="#/components/schemas/DebugNotFoundResponse")
* }
* )
* )
* )
*/
public function summary(CurrentRoute $currentRoute): ResponseInterface
{
$data = $this->collectorRepository->getSummary($currentRoute->getArgument('id'));
public function summary(
CurrentRoute $currentRoute,
ServerRequestInterface $serverRequest,
Synchronizer $synchronizer,
): ResponseInterface {
$id = $currentRoute->getArgument('id');
$timeout = max(0, (int)($serverRequest->getQueryParams()['timeout'] ?? 0));

$data = $synchronizer->execute(Debugger::SAVING_MUTEX_NAME . $id, fn () => $this->collectorRepository->getSummary($id), $timeout);
return $this->responseFactory->createResponse($data);
}

Expand Down Expand Up @@ -143,7 +157,6 @@ public function summary(CurrentRoute $currentRoute): ResponseInterface
*
* @OA\JsonContent(
* allOf={
*
* @OA\Schema(ref="#/components/schemas/DebugSuccessResponse")
* }
* )
Expand All @@ -155,7 +168,6 @@ public function summary(CurrentRoute $currentRoute): ResponseInterface
*
* @OA\JsonContent(
* allOf={
*
* @OA\Schema(ref="#/components/schemas/DebugNotFoundResponse")
* }
* )
Expand All @@ -167,9 +179,8 @@ public function view(
ServerRequestInterface $serverRequest,
ContainerInterface $container,
): ResponseInterface {
$data = $this->collectorRepository->getDetail(
$currentRoute->getArgument('id')
);
$id = $currentRoute->getArgument('id');
$data = $this->collectorRepository->getDetail($id);

$collectorClass = $serverRequest->getQueryParams()['collector'] ?? null;
if ($collectorClass !== null) {
Expand Down Expand Up @@ -222,7 +233,6 @@ public function view(
*
* @OA\JsonContent(
* allOf={
*
* @OA\Schema(ref="#/components/schemas/DebugSuccessResponse")
* }
* )
Expand All @@ -234,7 +244,6 @@ public function view(
*
* @OA\JsonContent(
* allOf={
*
* @OA\Schema(ref="#/components/schemas/DebugNotFoundResponse")
* }
* )
Expand All @@ -247,16 +256,12 @@ public function view(
*/
public function dump(CurrentRoute $currentRoute): ResponseInterface
{
$data = $this->collectorRepository->getDumpObject(
$currentRoute->getArgument('id')
);
$id = $currentRoute->getArgument('id');
$data = $this->collectorRepository->getDumpObject($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.');
}
$collector = $currentRoute->getArgument('collector');
if ($collector !== null) {
$data = $data[$collector] ?? throw new NotFoundException('Requested collector does not exist.');
}

return $this->responseFactory->createResponse($data);
Expand Down Expand Up @@ -296,7 +301,6 @@ public function dump(CurrentRoute $currentRoute): ResponseInterface
*
* @OA\JsonContent(
* allOf={
*
* @OA\Schema(ref="#/components/schemas/DebugSuccessResponse")
* }
* )
Expand All @@ -308,7 +312,6 @@ public function dump(CurrentRoute $currentRoute): ResponseInterface
*
* @OA\JsonContent(
* allOf={
*
* @OA\Schema(ref="#/components/schemas/DebugNotFoundResponse")
* }
* )
Expand All @@ -319,10 +322,10 @@ public function dump(CurrentRoute $currentRoute): ResponseInterface
*/
public function object(CurrentRoute $currentRoute): ResponseInterface
{
$data = $this->collectorRepository->getObject(
$currentRoute->getArgument('id'),
$currentRoute->getArgument('objectId')
);
$id = $currentRoute->getArgument('id');
$objectId = $currentRoute->getArgument('objectId');

$data = $this->collectorRepository->getObject($id, $objectId);

return $this->responseFactory->createResponse($data);
}
Expand Down