DemoServiceProvider.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace Drupal\demo;
  3. use Drupal\Component\Utility\Unicode;
  4. use Drupal\Core\DependencyInjection\ContainerBuilder;
  5. use Drupal\Core\DependencyInjection\ServiceProviderInterface;
  6. use Symfony\Component\DependencyInjection\ContainerInterface;
  7. use Symfony\Component\DependencyInjection\Definition;
  8. use Symfony\Component\DependencyInjection\Reference;
  9. class DemoServiceProvider implements ServiceProviderInterface {
  10. public function boolRow(Definition $def, $check) {
  11. $method = 'is' . Unicode::ucwords($check);
  12. $ret = [$check];
  13. $ret[] = $def->{$method}() ? 'Yes' : 'No';
  14. return $ret;
  15. }
  16. /**
  17. * Provide a textual representation of an argument or method call parameter.
  18. *
  19. * @see http://symfony.com/doc/current/service_container/optional_dependencies.html
  20. *
  21. * @param mixed $param
  22. *
  23. * @return string
  24. */
  25. public function formatParameter($param) {
  26. if ($param instanceof Reference) {
  27. $prefix = '@';
  28. $behavior = $param->getInvalidBehavior();
  29. switch ($behavior) {
  30. case ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE:
  31. // Default handling.
  32. break;
  33. case ContainerInterface::NULL_ON_INVALID_REFERENCE:
  34. // No YAML representation.
  35. $prefix .= '(Null)';
  36. break;
  37. case ContainerInterface::IGNORE_ON_INVALID_REFERENCE:
  38. $prefix .= '?';
  39. break;
  40. default:
  41. $prefix .= "($behavior)";
  42. break;
  43. }
  44. $result = "{$prefix}{$param}";
  45. }
  46. else {
  47. $result = $param;
  48. }
  49. return $result;
  50. }
  51. /**
  52. * Registers services to the container.
  53. *
  54. * @param ContainerBuilder $container
  55. * The ContainerBuilder to register services to.
  56. */
  57. public function register(ContainerBuilder $container) {
  58. $id = 'url_generator';
  59. $id = 'http_middleware.session';
  60. $def = $container->getDefinition($id);
  61. $rows = [];
  62. $rows[] = ['Option', 'Value'];
  63. $rows[] = [str_repeat('-', 20), str_repeat('-', 80)];
  64. $rows[] = ['Service ID', $id];
  65. $rows[] = ['Class', $def->getClass()];
  66. $rows[] = ['Factory', $def->getFactory()];
  67. $calls = $def->getMethodCalls();
  68. $call_rows = [];
  69. foreach ($calls as $call) {
  70. list($method, $params) = $call;
  71. $params = implode(', ', array_map([$this, 'formatParameter'], $params));
  72. $call_rows[] = [NULL, "$method($params)"];
  73. }
  74. if (empty($call_rows)) {
  75. $call_rows[] = [NULL, '-'];
  76. }
  77. $call_rows[0][0] = 'Calls';
  78. $rows = array_merge($rows, $call_rows);
  79. $tags = $def->getTags();
  80. $first = TRUE;
  81. $tag_rows = [];
  82. foreach ($tags as $tag => $info) {
  83. if (strpos($tag, '_') === 0) {
  84. continue;
  85. }
  86. foreach ($info as $info_row) {
  87. foreach ($info_row as $name => $value) {
  88. $row = [NULL, $tag, $name, $value];
  89. }
  90. if ($first) {
  91. $row[0] = 'Tags';
  92. $first = FALSE;
  93. }
  94. $tag_rows[] = $row;
  95. }
  96. }
  97. if (empty($tag_rows)) {
  98. $tag_rows[] = ['Tags', '-'];
  99. }
  100. $rows = array_merge($rows, $tag_rows);
  101. $rows[] = $this->boolRow($def, 'Public');
  102. $rows[] = $this->boolRow($def, 'Synthetic');
  103. $rows[] = $this->boolRow($def, 'Lazy');
  104. $rows[] = $this->boolRow($def, 'Shared');
  105. $rows[] = $this->boolRow($def, 'Abstract');
  106. $rows[] = $this->boolRow($def, 'Autowired');
  107. foreach ($rows as $row) {
  108. $left = $row[0];
  109. $right = $row[1];
  110. $three = $row[2] ?? NULL;
  111. $four = $row[3] ?? NULL;
  112. printf("%-20s %-20s %-20s %s\n", $left, $right, $three, $four);
  113. }
  114. echo "\n";
  115. }
  116. }