forked from middlewares/debugbar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add OpenHandler to load the data after an ajax request.
- Loading branch information
mostafa.seef-aldeen
committed
Aug 18, 2023
1 parent
397afb5
commit fa996dc
Showing
4 changed files
with
134 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Mezzio\DebugBar; | ||
|
||
use DebugBar\DebugBar; | ||
use DebugBar\DebugBarException; | ||
use Laminas\Diactoros\Response\JsonResponse; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
use function in_array; | ||
|
||
class OpenHandler implements RequestHandlerInterface | ||
{ | ||
protected const ALLOW_OPERATION = ['find', 'get', 'clear']; | ||
protected const FILTER_KEYS = ['utime', 'datetime', 'ip', 'uri', 'method']; | ||
|
||
protected DebugBar $debugBar; | ||
|
||
public function __construct(DebugBar $debugBar) | ||
{ | ||
$this->debugBar = $debugBar; | ||
} | ||
|
||
/** | ||
* @throws DebugBarException | ||
*/ | ||
public function handle(ServerRequestInterface $request): ResponseInterface | ||
{ | ||
$queryParams = $request->getQueryParams(); | ||
$op = $queryParams['op'] ?? 'find'; | ||
if (! in_array($op, self::ALLOW_OPERATION)) { | ||
throw new DebugBarException('Invalid operation:' . $op); | ||
} | ||
switch ($op) { | ||
case 'get': | ||
$response = $this->get($queryParams); | ||
break; | ||
case 'clear': | ||
$response = $this->clear(); | ||
break; | ||
default: | ||
$response = $this->find($queryParams); | ||
} | ||
return new JsonResponse($response); | ||
} | ||
|
||
protected function find(array $queryParams): array | ||
{ | ||
$max = $queryParams['max'] ?? 20; | ||
|
||
$offset = $queryParams['offset'] ?? 0; | ||
|
||
$filters = []; | ||
foreach (self::FILTER_KEYS as $key) { | ||
if (isset($queryParams[$key])) { | ||
$filters[$key] = $queryParams[$key]; | ||
} | ||
} | ||
|
||
return $this->debugBar->getStorage()->find($filters, $max, $offset); | ||
} | ||
|
||
/** | ||
* @throws DebugBarException | ||
*/ | ||
protected function get(array $queryParams): array | ||
{ | ||
$id = $queryParams['id'] ?? null; | ||
if ($id === null) { | ||
throw new DebugBarException("Missing 'id' parameter in 'get' operation"); | ||
} | ||
return $this->debugBar->getStorage()->get($id); | ||
} | ||
|
||
protected function clear(): array | ||
{ | ||
$this->debugBar->getStorage()->clear(); | ||
return ['success' => true]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Mezzio\DebugBar; | ||
|
||
use DebugBar\DebugBar; | ||
use DebugBar\DebugBarException; | ||
use Psr\Container\ContainerInterface; | ||
|
||
class OpenHandlerFactory | ||
{ | ||
/** | ||
* @throws DebugBarException | ||
*/ | ||
public function __invoke(ContainerInterface $container): OpenHandler | ||
{ | ||
$debugBar = $container->get(DebugBar::class); | ||
if ($debugBar->getStorage() === null) { | ||
throw new DebugBarException("DebugBar must have a storage to use OpenHandler"); | ||
} | ||
return new OpenHandler($debugBar); | ||
} | ||
} |