loadServices(); $img = ''; return $img . '' . count($this->free_services) . ' + ' . count($this->premium_services) . ' services'; } function getPanel() { ob_start(); require __DIR__ . '/di-panel.phtml'; return ob_get_clean(); } static function init() { Debugger::getBar()->addPanel(new static()); } private function loadServices() { list($services, $definitions) = $this->getServicesFromContainer('free_container'); $this->free_services = array_keys($services); sort($this->free_services); $this->definitions = $definitions; $this->arguments_flattened = $this->flattenArguments($this->free_services); if (array_key_exists('premium_container', $services)) { list($services, $definitions) = $this->getServicesFromContainer('premium_container'); $this->premium_services = array_keys($services); sort($this->premium_services); $this->definitions = array_merge($this->definitions, $definitions); $this->arguments_flattened = array_merge($this->flattenArguments($this->premium_services), $this->arguments_flattened); } } private function getServicesFromContainer($name) { $container_wrapper = ContainerWrapper::getInstance(); $reflection = new \ReflectionProperty(ContainerWrapper::class, $name); $reflection->setAccessible(true); $container = $reflection->getValue($container_wrapper); $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 string[] $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 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) { $used_in = []; foreach ($this->arguments_flattened as $service => $arguments) { if (array_key_exists($item, $arguments)) { $used_in[] = $service; } } if (count($used_in)) { $label = 'Used in ' . count($used_in) . ' services'; echo '' . $label . '...'; echo '
'; echo join('
', $used_in); echo '
'; } } }