瀏覽代碼

Service providers have access to the ContainerBuilder.

Frederic G. MARAND 8 年之前
父節點
當前提交
26496fafec
共有 4 個文件被更改,包括 177 次插入0 次删除
  1. 14 0
      composer.json
  2. 5 0
      demo.info.yml
  3. 24 0
      demo.module
  4. 134 0
      src/DemoServiceProvider.php

+ 14 - 0
composer.json

@@ -0,0 +1,14 @@
+{
+  "name": "drupal/demo",
+  "type": "drupal-module",
+  "description": "Demonstrate various features",
+  "keywords": ["Drupal"],
+  "license": "GPL-2.0+",
+  "homepage": "https://www.drupal.org/project/demo",
+  "minimum-stability": "dev",
+  "support": {
+    "issues": "http://drupal.org/project/issues/demo",
+    "source": "http://cgit.drupalcode.org/demo"
+  },
+  "require": { }
+}

+ 5 - 0
demo.info.yml

@@ -0,0 +1,5 @@
+name: demo
+type: module
+description: Demonstrate various features
+core: 8.x
+package: OSInet

+ 24 - 0
demo.module

@@ -0,0 +1,24 @@
+<?php
+
+/**
+ * @file
+ * Contains demo.module..
+ */
+
+use Drupal\Core\Routing\RouteMatchInterface;
+
+/**
+ * Implements hook_help().
+ */
+function demo_help($route_name, RouteMatchInterface $route_match) {
+  switch ($route_name) {
+    // Main module help for the demo module.
+    case 'help.page.demo':
+      $output = '';
+      $output .= '<h3>' . t('About') . '</h3>';
+      $output .= '<p>' . t('Demonstrate various features') . '</p>';
+      return $output;
+
+    default:
+  }
+}

+ 134 - 0
src/DemoServiceProvider.php

@@ -0,0 +1,134 @@
+<?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";
+
+  }
+}