You are here

class ZfExtensionManagerSfContainer in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Component/Bridge/ZfExtensionManagerSfContainer.php \Drupal\Component\Bridge\ZfExtensionManagerSfContainer

Defines a bridge between the Laminas service manager to Symfony container.

Hierarchy

  • class \Drupal\Component\Bridge\ZfExtensionManagerSfContainer implements \Laminas\Feed\Reader\ExtensionManagerInterface, \Laminas\Feed\Writer\ExtensionManagerInterface, \Symfony\Component\DependencyInjection\ContainerAwareInterface

Expanded class hierarchy of ZfExtensionManagerSfContainer

1 file declares its use of ZfExtensionManagerSfContainer
ZfExtensionManagerSfContainerTest.php in core/tests/Drupal/Tests/Component/Bridge/ZfExtensionManagerSfContainerTest.php
1 string reference to 'ZfExtensionManagerSfContainer'
core.services.yml in core/core.services.yml
core/core.services.yml
2 services use ZfExtensionManagerSfContainer
feed.bridge.reader in core/core.services.yml
Drupal\Component\Bridge\ZfExtensionManagerSfContainer
feed.bridge.writer in core/core.services.yml
Drupal\Component\Bridge\ZfExtensionManagerSfContainer

File

core/lib/Drupal/Component/Bridge/ZfExtensionManagerSfContainer.php, line 14

Namespace

Drupal\Component\Bridge
View source
class ZfExtensionManagerSfContainer implements ReaderManagerInterface, WriterManagerInterface, ContainerAwareInterface {

  /**
   * A map of characters to be replaced through strtr.
   *
   * This property is based on Laminas service manager.
   *
   * @link https://github.com/laminas/laminas-servicemanager for the canonical source repository
   * @copyright Copyright (c) 2019, Laminas Foundation. (https://getlaminas.org/)
   * @license https://github.com/laminas/laminas-servicemanager/blob/master/LICENSE.md
   *
   * @var array
   *
   * @see \Drupal\Component\Bridge\ZfExtensionManagerSfContainer::canonicalizeName().
   * @see https://github.com/laminas/laminas-servicemanager/blob/2.7.11/src/ServiceManager.php#L114
   */
  protected $canonicalNamesReplacements = [
    '-' => '',
    '_' => '',
    ' ' => '',
    '\\' => '',
    '/' => '',
  ];

  /**
   * The prefix to be used when retrieving plugins from the container.
   *
   * @var string
   */
  protected $prefix = '';

  /**
   * The service container.
   *
   * @var \Symfony\Component\DependencyInjection\ContainerInterface
   */
  protected $container;

  /**
   * A local cache of computed canonical names.
   *
   * @var string[]
   */
  protected $canonicalNames;

  /**
   * @var \Laminas\Feed\Reader\ExtensionManagerInterface|\Laminas\Feed\Writer\ExtensionManagerInterface
   */
  protected $standalone;

  /**
   * Constructs a ZfExtensionManagerSfContainer object.
   *
   * @param string $prefix
   *   The prefix to be used when retrieving plugins from the container.
   */
  public function __construct($prefix = '') {
    $this->prefix = $prefix;
  }

  /**
   * {@inheritdoc}
   */
  public function get($extension) {
    try {
      return $this->container
        ->get($this->prefix . $this
        ->canonicalizeName($extension));
    } catch (ServiceNotFoundException $e) {
      if ($this->standalone && $this->standalone
        ->has($extension)) {
        return $this->standalone
          ->get($extension);
      }
      throw $e;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function has($extension) {
    if ($this->container
      ->has($this->prefix . $this
      ->canonicalizeName($extension))) {
      return TRUE;
    }
    return $this->standalone && $this->standalone
      ->has($extension);
  }

  /**
   * Canonicalize the extension name to a service name.
   *
   * This method is based on Laminas service manager.
   *
   * @link https://github.com/laminas/laminas-servicemanager for the canonical source repository
   * @copyright Copyright (c) 2019, Laminas Foundation. (https://getlaminas.org/)
   * @license https://github.com/laminas/laminas-servicemanager/blob/master/LICENSE.md
   *
   * @param string $name
   *   The extension name.
   *
   * @return string
   *   The service name, without the prefix.
   *
   * @see https://github.com/laminas/laminas-servicemanager/blob/2.7.11/src/ServiceManager.php#L900
   */
  protected function canonicalizeName($name) {
    if (isset($this->canonicalNames[$name])) {
      return $this->canonicalNames[$name];
    }

    // This is just for performance instead of using str_replace().
    return $this->canonicalNames[$name] = strtolower(strtr($name, $this->canonicalNamesReplacements));
  }

  /**
   * {@inheritdoc}
   */
  public function setContainer(ContainerInterface $container = NULL) {
    $this->container = $container;
  }

  /**
   * @param $class
   */
  public function setStandalone($class) {
    if (!is_subclass_of($class, ReaderManagerInterface::class) && !is_subclass_of($class, WriterManagerInterface::class)) {
      throw new \RuntimeException("{$class} must implement Laminas\\Feed\\Reader\\ExtensionManagerInterface or Laminas\\Feed\\Writer\\ExtensionManagerInterface");
    }
    $this->standalone = new $class();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ZfExtensionManagerSfContainer::$canonicalNames protected property A local cache of computed canonical names.
ZfExtensionManagerSfContainer::$canonicalNamesReplacements protected property A map of characters to be replaced through strtr.
ZfExtensionManagerSfContainer::$container protected property The service container.
ZfExtensionManagerSfContainer::$prefix protected property The prefix to be used when retrieving plugins from the container.
ZfExtensionManagerSfContainer::$standalone protected property
ZfExtensionManagerSfContainer::canonicalizeName protected function Canonicalize the extension name to a service name.
ZfExtensionManagerSfContainer::get public function
ZfExtensionManagerSfContainer::has public function
ZfExtensionManagerSfContainer::setContainer public function
ZfExtensionManagerSfContainer::setStandalone public function
ZfExtensionManagerSfContainer::__construct public function Constructs a ZfExtensionManagerSfContainer object.