123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- namespace Drupal\demo;
- use Drupal\Component\Utility\Unicode;
- use Drupal\Core\DependencyInjection\ContainerBuilder;
- use Drupal\Core\DependencyInjection\ServiceProviderInterface;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- use Symfony\Component\DependencyInjection\Definition;
- use Symfony\Component\DependencyInjection\Reference;
- class DemoServiceProvider implements ServiceProviderInterface {
- public function boolRow(Definition $def, $check) {
- $method = 'is' . Unicode::ucwords($check);
- $ret = [$check];
- $ret[] = $def->{$method}() ? 'Yes' : 'No';
- return $ret;
- }
- /**
- * Provide a textual representation of an argument or method call parameter.
- *
- * @see http://symfony.com/doc/current/service_container/optional_dependencies.html
- *
- * @param mixed $param
- *
- * @return string
- */
- public function formatParameter($param) {
- if ($param instanceof Reference) {
- $prefix = '@';
- $behavior = $param->getInvalidBehavior();
- switch ($behavior) {
- case ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE:
- // Default handling.
- break;
- case ContainerInterface::NULL_ON_INVALID_REFERENCE:
- // No YAML representation.
- $prefix .= '(Null)';
- break;
- case ContainerInterface::IGNORE_ON_INVALID_REFERENCE:
- $prefix .= '?';
- break;
- default:
- $prefix .= "($behavior)";
- break;
- }
- $result = "{$prefix}{$param}";
- }
- else {
- $result = $param;
- }
- return $result;
- }
- /**
- * Registers services to the container.
- *
- * @param ContainerBuilder $container
- * The ContainerBuilder to register services to.
- */
- public function register(ContainerBuilder $container) {
- $id = 'url_generator';
- $id = 'http_middleware.session';
- $def = $container->getDefinition($id);
- $rows = [];
- $rows[] = ['Option', 'Value'];
- $rows[] = [str_repeat('-', 20), str_repeat('-', 80)];
- $rows[] = ['Service ID', $id];
- $rows[] = ['Class', $def->getClass()];
- $rows[] = ['Factory', $def->getFactory()];
- $calls = $def->getMethodCalls();
- $call_rows = [];
- foreach ($calls as $call) {
- list($method, $params) = $call;
- $params = implode(', ', array_map([$this, 'formatParameter'], $params));
- $call_rows[] = [NULL, "$method($params)"];
- }
- if (empty($call_rows)) {
- $call_rows[] = [NULL, '-'];
- }
- $call_rows[0][0] = 'Calls';
- $rows = array_merge($rows, $call_rows);
- $tags = $def->getTags();
- $first = TRUE;
- $tag_rows = [];
- foreach ($tags as $tag => $info) {
- if (strpos($tag, '_') === 0) {
- continue;
- }
- foreach ($info as $info_row) {
- foreach ($info_row as $name => $value) {
- $row = [NULL, $tag, $name, $value];
- }
- if ($first) {
- $row[0] = 'Tags';
- $first = FALSE;
- }
- $tag_rows[] = $row;
- }
- }
- if (empty($tag_rows)) {
- $tag_rows[] = ['Tags', '-'];
- }
- $rows = array_merge($rows, $tag_rows);
- $rows[] = $this->boolRow($def, 'Public');
- $rows[] = $this->boolRow($def, 'Synthetic');
- $rows[] = $this->boolRow($def, 'Lazy');
- $rows[] = $this->boolRow($def, 'Shared');
- $rows[] = $this->boolRow($def, 'Abstract');
- $rows[] = $this->boolRow($def, 'Autowired');
- foreach ($rows as $row) {
- $left = $row[0];
- $right = $row[1];
- $three = $row[2] ?? NULL;
- $four = $row[3] ?? NULL;
- printf("%-20s %-20s %-20s %s\n", $left, $right, $three, $four);
- }
- echo "\n";
- }
- }
|