commit 5c12fac58e30297ede5a563375d552d54f66827e
Author: jsacksick <jonathan.sacksick@gmail.com>
Date:   Sat Jun 8 10:34:52 2013 +0200

    Issue #1987826: Convert system_plugin_autocomplete() to a new style controller.

diff --git a/core/modules/system/lib/Drupal/system/Access/SystemPluginUiCheck.php b/core/modules/system/lib/Drupal/system/Access/SystemPluginUiCheck.php
new file mode 100644
index 0000000..022d5f3
--- /dev/null
+++ b/core/modules/system/lib/Drupal/system/Access/SystemPluginUiCheck.php
@@ -0,0 +1,53 @@
+<?php
+
+/**
+ * @file
+ * Contains Drupal\system\Access\SystemPluginUiCheck.
+ */
+
+namespace Drupal\system\Access;
+
+use Drupal\Component\Plugin\PluginManagerInterface;
+use Drupal\Core\Access\AccessCheckInterface;
+use Symfony\Component\Routing\Route;
+use Symfony\Component\HttpFoundation\Request;
+
+/**
+ * Access check for system routes.
+ */
+class SystemPluginUiCheck implements AccessCheckInterface {
+
+  /**
+   * The plugin UI manager.
+   *
+   * @var \Drupal\Component\Plugin\PluginManagerInterface
+   */
+  protected $pluginUiManager;
+
+  /**
+   * Constructs a SystemController object.
+   *
+   * @param \Drupal\Component\Plugin\PluginManagerInterface $plugin_ui_manager
+   *   The plugin UI manager.
+   */
+  public function __construct(PluginManagerInterface $plugin_ui_manager) {
+    $this->pluginUiManager = $plugin_ui_manager;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function applies(Route $route) {
+    return array_key_exists('_access_system_plugin_ui', $route->getRequirements());
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function access(Route $route, Request $request) {
+    // Checks access for a given plugin using the plugin's access() method.
+    $plugin_ui = $this->pluginUiManager->createInstance($request->attributes->get('plugin_id'), array());
+    return $plugin_ui->access(NULL);
+  }
+
+}
diff --git a/core/modules/system/lib/Drupal/system/Controller/SystemController.php b/core/modules/system/lib/Drupal/system/Controller/SystemController.php
new file mode 100644
index 0000000..dac198b
--- /dev/null
+++ b/core/modules/system/lib/Drupal/system/Controller/SystemController.php
@@ -0,0 +1,84 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\system\Controller\SystemController.
+ */
+
+namespace Drupal\system\Controller;
+
+use Drupal\Component\Utility\Tags;
+use Drupal\Component\Utility\Unicode;
+use Drupal\Core\Controller\ControllerInterface;
+use Drupal\Component\Plugin\PluginManagerInterface;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+use Symfony\Component\HttpFoundation\JsonResponse;
+use Symfony\Component\HttpFoundation\Request;
+
+
+/**
+ * Returns responses for System routes.
+ */
+class SystemController implements ControllerInterface {
+
+  /**
+   * The plugin UI manager.
+   *
+   * @var \Drupal\Component\Plugin\PluginManagerInterface
+   */
+  protected $pluginUiManager;
+
+  /**
+   * Constructs a SystemController object.
+   *
+   * @param \Drupal\Component\Plugin\PluginManagerInterface $plugin_ui_manager
+   *   The plugin UI manager.
+   */
+  public function __construct(PluginManagerInterface $plugin_ui_manager) {
+    $this->pluginUiManager = $plugin_ui_manager;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function create(ContainerInterface $container) {
+    return new static(
+      $container->get('plugin.manager.system.plugin_ui')
+    );
+  }
+
+  /**
+   * Autocompletes any plugin system tied to a plugin UI plugin.
+   *
+   * The passed plugin_id indicates the specific plugin_ui plugin that is in use
+   * here. The documentation within the annotation of that plugin will contain a
+   * manager for the plugins that need to be autocompleted allowing this
+   * function to autocomplete plugins for any plugin type.
+   *
+   * @param string $plugin_id
+   *   The plugin id for the calling plugin.
+   * @param Request $request
+   *   The request object that contains the typed tags.
+   *
+   * @return \Symfony\Component\HttpFoundation\JsonResponse
+   *   The matched plugins as json.
+   */
+  public function autocomplete($plugin_id, Request $request) {
+    $string_typed = $request->query->get('q');
+    $string_typed = Tags::explode($string_typed);
+    $string = Unicode::strtolower(array_pop($string_typed));
+    $matches = array();
+    if ($string) {
+      $plugin_ui = $this->pluginUiManager->getDefinition($plugin_id);
+      $manager = \Drupal::service($plugin_ui['manager']);
+      $titles = array();
+      foreach($manager->getDefinitions() as $plugin_id => $plugin) {
+        $titles[$plugin_id] = $plugin[$plugin_ui['title_attribute']];
+      }
+      $matches = preg_grep("/\b". $string . "/i", $titles);
+    }
+
+    return new JsonResponse($matches);
+  }
+
+}
diff --git a/core/modules/system/system.module b/core/modules/system/system.module
index 87fe50d..41d94ae 100644
--- a/core/modules/system/system.module
+++ b/core/modules/system/system.module
@@ -1023,11 +1023,7 @@ function system_menu() {
         }
       }
       $items['system/autocomplete/' . $plugin_id] = array(
-        'page callback' => 'system_plugin_autocomplete',
-        'page arguments' => array($plugin_id),
-        'access callback' => 'system_plugin_ui_access',
-        'access arguments' => array($plugin_id),
-        'type' => MENU_CALLBACK,
+        'route_name' => 'system_plugin_autocomplete',
       );
     }
   }
@@ -1050,37 +1046,6 @@ function system_plugin_ui_form($form, &$form_state, $plugin, $facet = NULL) {
 }
 
 /**
- * Page callback: Autocompletes any plugin system tied to a plugin UI plugin.
- *
- * The passed plugin_id indicates the specific plugin_ui plugin that is in use
- * here. The documentation within the annotation of that plugin will contain a
- * manager for the plugins that need to be autocompleted allowing this function
- * to autocomplete plugins for any plugin type.
- *
- * @param $plugin_id
- *   The plugin id for the calling plugin.
- *
- * @return object JsonResponse
- */
-function system_plugin_autocomplete($plugin_id) {
-  $string_typed = drupal_container()->get('request')->query->get('q');
-  $string_typed = drupal_explode_tags($string_typed);
-  $string = drupal_strtolower(array_pop($string_typed));
-  $matches = array();
-  if ($string) {
-    $plugin_ui = Drupal::service('plugin.manager.system.plugin_ui')->getDefinition($plugin_id);
-    $manager = Drupal::service($plugin_ui['manager']);
-    $titles = array();
-    foreach($manager->getDefinitions() as $plugin_id => $plugin) {
-      $titles[$plugin_id] = $plugin[$plugin_ui['title_attribute']];
-    }
-    $matches = preg_grep("/\b". $string . "/i", $titles);
-  }
-
-  return new JsonResponse($matches);
-}
-
-/**
  * Checks access for a given plugin using the plugin's access() method.
  *
  * @todo This needs more explanation, some @see, and parameter documentation.
diff --git a/core/modules/system/system.routing.yml b/core/modules/system/system.routing.yml
index 1e09f79..b277695 100644
--- a/core/modules/system/system.routing.yml
+++ b/core/modules/system/system.routing.yml
@@ -164,3 +164,10 @@ system_timezone:
     _controller: '\Drupal\system\Controller\TimezoneController::getTimezone'
   requirements:
     _access: 'TRUE'
+
+system_plugin_autocomplete:
+  pattern: '/system/autocomplete/{plugin_id}'
+  defaults:
+    _controller: 'Drupal\system\Controller\SystemController::autocomplete'
+  requirements:
+    _access_system_plugin_ui: 'TRUE'
diff --git a/core/modules/system/system.services.yml b/core/modules/system/system.services.yml
index 6aefa00..3b9eac7 100644
--- a/core/modules/system/system.services.yml
+++ b/core/modules/system/system.services.yml
@@ -3,6 +3,11 @@ services:
     class: Drupal\system\Access\CronAccessCheck
     tags:
       - { name: access_check }
+  access_check.system_plugin_ui:
+    class: Drupal\system\Access\SystemPluginUiCheck
+    tags:
+      - { name: access_check }
+    arguments: ['@plugin.manager.system.plugin_ui']
   plugin.manager.system.plugin_ui:
     class: Drupal\system\Plugin\Type\PluginUIManager
     arguments: ['@container.namespaces']
