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 routeCollector to show the current route config.
- Loading branch information
mostafa.seef-aldeen
committed
Sep 7, 2023
1 parent
9a06f87
commit b61bc8f
Showing
3 changed files
with
151 additions
and
0 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
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; | ||
} | ||
} |
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,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); | ||
} | ||
} |