*/ private $freeServices = []; /** @var array */ private $premiumServices = []; /** @var \MailPoetVendor\Symfony\Component\DependencyInjection\Definition[] */ private $definitions = []; /** @var string[][] */ private $argumentsFlattened = []; public function getTab() { $this->loadServices(); $img = ''; return $img . '' . count($this->freeServices) . ' + ' . count($this->premiumServices) . ' services'; } public function getPanel() { ob_start(); require __DIR__ . '/di-panel.phtml'; return ob_get_clean(); } public static function init() { Debugger::getBar()->addPanel(new DIPanel()); } private function loadServices() { list($services, $definitions) = $this->getServicesFromContainer('freeContainer'); $this->freeServices = array_keys($services); sort($this->freeServices); $this->definitions = $definitions; $this->argumentsFlattened = $this->flattenArguments($this->freeServices); if (array_key_exists('premium_container', $services)) { list($services, $definitions) = $this->getServicesFromContainer('premiumContainer'); $this->premiumServices = array_keys($services); sort($this->premiumServices); $this->definitions = array_merge($this->definitions, $definitions); $this->argumentsFlattened = array_merge($this->flattenArguments($this->premiumServices), $this->argumentsFlattened); } } private function getServicesFromContainer($name) { $containerWrapper = ContainerWrapper::getInstance(); $reflection = new \ReflectionProperty(ContainerWrapper::class, $name); $reflection->setAccessible(true); $container = $reflection->getValue($containerWrapper); $reflection = new \ReflectionProperty(get_class($container), 'services'); $reflection->setAccessible(true); return [$reflection->getValue($container), $container->getDefinitions()]; } /** * For each service finds all of its arguments recursively and makes them an array * @param array $services * @return array */ private function flattenArguments($services) { $result = []; foreach ($services as $service) { $result[$service] = []; $this->getAllArguments($service, $result[$service]); } return $result; } /** * Find all argument of each service and adds them to $results array, repeats recursively * @param int|string $service * @param string[] $results */ private function getAllArguments($service, &$results) { if (array_key_exists($service, $this->definitions)) { $arguments = $this->definitions[$service]->getArguments(); if (!empty($arguments)) { foreach ($arguments as $argument) { if (is_null($argument)) continue; if ($argument instanceof TypedReference) { $argumentName = $argument->getType(); } elseif ($argument instanceof Reference) { $argumentName = (string)$argument; } else { continue; } $results[$argumentName] = $argumentName; if ($argumentName !== 'MailPoet\DI\ContainerWrapper') { $this->getAllArguments($argumentName, $results); } } } } } /** * @param string $item */ public function printItem($item) { echo $item; if (array_key_exists($item, $this->definitions)) { $arguments = $this->definitions[$item]->getArguments(); if (!empty($arguments)) { echo '...'; echo '
'; foreach ($arguments as $argument) { if (is_null($argument)) { echo 'NULL
'; } elseif ($argument instanceof TypedReference) { $this->printItem($argument->getType()); echo '
'; } elseif ($argument instanceof Reference) { $this->printItem((string)$argument); echo '
'; } elseif (is_string($argument)) { echo $argument; echo '
'; } } echo '
'; } } } /** * @param string $item */ public function printUsages($item) { $usedIn = []; foreach ($this->argumentsFlattened as $service => $arguments) { if (array_key_exists($item, $arguments)) { $usedIn[] = $service; } } if (count($usedIn)) { $label = 'Used in ' . count($usedIn) . ' services'; echo '' . $label . '...'; echo '
'; echo join('
', $usedIn); echo '
'; } } }