Skip to content

Commit

Permalink
ContainerPanel: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 15, 2023
1 parent 8be501f commit 8229ef7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 23 deletions.
34 changes: 13 additions & 21 deletions src/Bridges/DITracy/ContainerPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,37 +57,29 @@ public function getTab(): string
*/
public function getPanel(): string
{
$rc = new \ReflectionClass($this->container);
$tags = [];
$types = [];
foreach ($rc->getMethods() as $method) {
if (preg_match('#^createService(.+)#', $method->name, $m) && $method->getReturnType()) {
$types[lcfirst(str_replace('__', '.', $m[1]))] = $method->getReturnType()->getName();
}
$methods = (function () { return $this->methods; })->bindTo($this->container, Container::class)();
$services = [];
foreach ($methods as $name => $foo) {
$name = lcfirst(str_replace('__', '.', substr($name, 13)));
$services[$name] = $this->container->getServiceType($name);
}
ksort($services, SORT_NATURAL);

$types = $this->getContainerProperty('types') + $types;
ksort($types, SORT_NATURAL);
foreach ($this->getContainerProperty('tags') as $tag => $tmp) {
$propertyTags = (function () { return $this->tags; })->bindTo($this->container, $this->container)();
$tags = [];
foreach ($propertyTags as $tag => $tmp) {
foreach ($tmp as $service => $val) {
$tags[$service][$tag] = $val;
}
}

return Nette\Utils\Helpers::capture(function () use ($tags, $types, $rc) {
return Nette\Utils\Helpers::capture(function () use ($tags, $services) {
$container = $this->container;
$rc = (new \ReflectionClass($this->container));
$file = $rc->getFileName();
$instances = $this->getContainerProperty('instances');
$wiring = $this->getContainerProperty('wiring');
$instances = (function () { return $this->instances; })->bindTo($this->container, Container::class)();
$wiring = (function () { return $this->wiring; })->bindTo($this->container, $this->container)();
require __DIR__ . '/templates/ContainerPanel.panel.phtml';
});
}


private function getContainerProperty(string $name)
{
$prop = (new \ReflectionClass(Nette\DI\Container::class))->getProperty($name);
$prop->setAccessible(true);
return $prop->getValue($this->container);
}
}
2 changes: 1 addition & 1 deletion src/Bridges/DITracy/templates/ContainerPanel.panel.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use Tracy\Helpers;
</tr>
</thead>
<tbody>
<?php foreach ($types as $name => $type): ?>
<?php foreach ($services as $name => $type): ?>
<?php $name = (string) $name ?>
<?php $autowired = in_array($name, array_merge($wiring[$type][0] ?? [], $wiring[$type][1] ?? []), true) ?>
<tr>
Expand Down
2 changes: 1 addition & 1 deletion src/DI/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Container
/** @var array circular reference detector */
private $creating;

/** @var array */
/** @var array<string, string|\Closure> */
private $methods;


Expand Down

0 comments on commit 8229ef7

Please sign in to comment.