Skip to content

Commit

Permalink
add routeCollector to show the current route config.
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa.seef-aldeen committed Sep 7, 2023
1 parent 9a06f87 commit b61bc8f
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use DebugBar\Storage\PdoStorage;
use Mezzio\DebugBar\DataCollector\ConfigCollectorFactory;
use Mezzio\DebugBar\DataCollector\DoctrineCollectorFactory;
use Mezzio\DebugBar\DataCollector\RouteCollector;
use Mezzio\DebugBar\DataCollector\RouteCollectorFactory;
use Mezzio\DebugBar\Storage\DoctrineStorage;
use Mezzio\DebugBar\Storage\DoctrineStorageFactory;
use Mezzio\DebugBar\Storage\FileStorageFactory;
Expand All @@ -19,6 +21,7 @@
final class ConfigProvider
{
public const OPEN_HANDLER_URL = 'debugbarOpen';

/**
* Returns the configuration array
*/
Expand Down Expand Up @@ -66,6 +69,7 @@ public function getDependencies(): array
OpenHandler::class => OpenHandlerFactory::class,
DoctrineStorage::class => DoctrineStorageFactory::class,
PdoStorage::class => PdoStorageFactory::class,
RouteCollector::class => RouteCollectorFactory::class,
],
];
}
Expand Down
128 changes: 128 additions & 0 deletions src/DataCollector/RouteCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

declare(strict_types=1);

namespace Mezzio\DebugBar\DataCollector;

use DebugBar\DataCollector\AssetProvider;
use DebugBar\DataCollector\DataCollector;
use DebugBar\DataCollector\Renderable;
use Laminas\Diactoros\ServerRequestFactory;
use Mezzio\Router\RouterInterface;

use function is_string;

class RouteCollector extends DataCollector implements Renderable, AssetProvider
{
protected string $name;

protected array $config;

protected RouterInterface $router;

// The HTML var dumper requires debug bar users to support the new inline assets, which not all
// may support yet - so return false by default for now.
protected bool $useHtmlVarDumper = false;

public function __construct(RouterInterface $router, array $config)
{
$this->router = $router;
$this->config = $config;
$this->name = 'Route';
}


/**
* @return array
*/
public function collect(): array
{
$data = $this->getRouteInformation();
foreach ($data as $k => $v) {
if ($this->isHtmlVarDumperUsed()) {
$v = $this->getVarDumper()->renderVar($v);
} elseif (! is_string($v)) {
$v = $this->getDataFormatter()->formatVar($v);
}
$data[$k] = $v;
}
return $data;
}

/**
* @return string[]
*/
protected function getRouteInformation(): array
{
$request = ServerRequestFactory::fromGlobals(
$_SERVER,
$_GET,
$_POST,
$_COOKIE,
$_FILES
);

$match = $this->router->match($request);

return $this->config[ 'routes' ][ $match->getMatchedRouteName() ] ?? ['no data'];
}

/**
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* @return array
*/
public function getAssets()
{
return $this->isHtmlVarDumperUsed() ? $this->getVarDumper()->getAssets() : [];
}

/**
* @return array
*/
public function getWidgets()
{
$name = $this->getName();
$widget = $this->isHtmlVarDumperUsed()
? "PhpDebugBar.Widgets.HtmlVariableListWidget"
: "PhpDebugBar.Widgets.VariableListWidget";
return [
"$name" => [
"icon" => "gear",
"widget" => $widget,
"map" => "$name",
"default" => "{}",
],
];
}

/**
* Sets a flag indicating whether the Symfony HtmlDumper will be used to dump variables for
* rich variable rendering.
*
* @param bool $value
* @return $this
*/
public function useHtmlVarDumper($value = true): RouteCollector
{
$this->useHtmlVarDumper = $value;
return $this;
}

/**
* Indicates whether the Symfony HtmlDumper will be used to dump variables for rich variable
* rendering.
*
* @return mixed
*/
public function isHtmlVarDumperUsed()
{
return $this->useHtmlVarDumper;
}
}
19 changes: 19 additions & 0 deletions src/DataCollector/RouteCollectorFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Mezzio\DebugBar\DataCollector;

use Mezzio\Router\RouterInterface;
use Psr\Container\ContainerInterface;

class RouteCollectorFactory
{
public function __invoke(ContainerInterface $container): RouteCollector
{
$config = $container->get('config');
$router = $container->get(RouterInterface::class);

return new RouteCollector($router, $config);
}
}

0 comments on commit b61bc8f

Please sign in to comment.