annotate core/lib/Drupal/Component/Bridge/ZfExtensionManagerSfContainer.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Component\Bridge;
Chris@0 4
Chris@0 5 use Symfony\Component\DependencyInjection\ContainerAwareInterface;
Chris@0 6 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@17 7 use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
Chris@0 8 use Zend\Feed\Reader\ExtensionManagerInterface as ReaderManagerInterface;
Chris@0 9 use Zend\Feed\Writer\ExtensionManagerInterface as WriterManagerInterface;
Chris@0 10
Chris@0 11 /**
Chris@0 12 * Defines a bridge between the ZF2 service manager to Symfony container.
Chris@0 13 */
Chris@0 14 class ZfExtensionManagerSfContainer implements ReaderManagerInterface, WriterManagerInterface, ContainerAwareInterface {
Chris@0 15
Chris@0 16 /**
Chris@0 17 * This property was based from Zend Framework (http://framework.zend.com/)
Chris@0 18 *
Chris@0 19 * @link http://github.com/zendframework/zf2 for the canonical source repository
Chris@0 20 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
Chris@0 21 * @license http://framework.zend.com/license/new-bsd New BSD License
Chris@0 22 *
Chris@0 23 * A map of characters to be replaced through strtr.
Chris@0 24 *
Chris@0 25 * @var array
Chris@0 26 *
Chris@0 27 * @see \Drupal\Component\Bridge\ZfExtensionManagerSfContainer::canonicalizeName().
Chris@0 28 */
Chris@0 29 protected $canonicalNamesReplacements = ['-' => '', '_' => '', ' ' => '', '\\' => '', '/' => ''];
Chris@0 30
Chris@0 31 /**
Chris@0 32 * The prefix to be used when retrieving plugins from the container.
Chris@0 33 *
Chris@0 34 * @var string
Chris@0 35 */
Chris@0 36 protected $prefix = '';
Chris@0 37
Chris@0 38 /**
Chris@0 39 * The service container.
Chris@0 40 *
Chris@0 41 * @var \Symfony\Component\DependencyInjection\ContainerInterface
Chris@0 42 */
Chris@0 43 protected $container;
Chris@0 44
Chris@0 45 /**
Chris@0 46 * A local cache of computed canonical names.
Chris@0 47 *
Chris@0 48 * @var string[]
Chris@0 49 */
Chris@0 50 protected $canonicalNames;
Chris@0 51
Chris@0 52 /**
Chris@17 53 * @var \Zend\Feed\Reader\ExtensionManagerInterface|\Zend\Feed\Writer\ExtensionManagerInterface
Chris@17 54 */
Chris@17 55 protected $standalone;
Chris@17 56
Chris@17 57 /**
Chris@0 58 * Constructs a ZfExtensionManagerSfContainer object.
Chris@0 59 *
Chris@0 60 * @param string $prefix
Chris@0 61 * The prefix to be used when retrieving plugins from the container.
Chris@0 62 */
Chris@0 63 public function __construct($prefix = '') {
Chris@0 64 $this->prefix = $prefix;
Chris@0 65 }
Chris@0 66
Chris@0 67 /**
Chris@0 68 * {@inheritdoc}
Chris@0 69 */
Chris@0 70 public function get($extension) {
Chris@17 71 try {
Chris@17 72 return $this->container->get($this->prefix . $this->canonicalizeName($extension));
Chris@17 73 }
Chris@17 74 catch (ServiceNotFoundException $e) {
Chris@17 75 if ($this->standalone && $this->standalone->has($extension)) {
Chris@17 76 return $this->standalone->get($extension);
Chris@17 77 }
Chris@17 78 throw $e;
Chris@17 79 }
Chris@0 80 }
Chris@0 81
Chris@0 82 /**
Chris@0 83 * {@inheritdoc}
Chris@0 84 */
Chris@0 85 public function has($extension) {
Chris@17 86 if ($this->container->has($this->prefix . $this->canonicalizeName($extension))) {
Chris@17 87 return TRUE;
Chris@17 88 }
Chris@17 89 return $this->standalone && $this->standalone->has($extension);
Chris@0 90 }
Chris@0 91
Chris@0 92 /**
Chris@0 93 * This method was based from Zend Framework (http://framework.zend.com/)
Chris@0 94 *
Chris@0 95 * @link http://github.com/zendframework/zf2 for the canonical source repository
Chris@0 96 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
Chris@0 97 * @license http://framework.zend.com/license/new-bsd New BSD License
Chris@0 98 *
Chris@0 99 * Canonicalize the extension name to a service name.
Chris@0 100 *
Chris@0 101 * @param string $name
Chris@0 102 * The extension name.
Chris@0 103 *
Chris@0 104 * @return string
Chris@0 105 * The service name, without the prefix.
Chris@0 106 */
Chris@0 107 protected function canonicalizeName($name) {
Chris@0 108 if (isset($this->canonicalNames[$name])) {
Chris@0 109 return $this->canonicalNames[$name];
Chris@0 110 }
Chris@0 111 // This is just for performance instead of using str_replace().
Chris@0 112 return $this->canonicalNames[$name] = strtolower(strtr($name, $this->canonicalNamesReplacements));
Chris@0 113 }
Chris@0 114
Chris@0 115 /**
Chris@0 116 * {@inheritdoc}
Chris@0 117 */
Chris@0 118 public function setContainer(ContainerInterface $container = NULL) {
Chris@0 119 $this->container = $container;
Chris@0 120 }
Chris@0 121
Chris@17 122 /**
Chris@17 123 * @param $class
Chris@17 124 */
Chris@17 125 public function setStandalone($class) {
Chris@17 126 if (!is_subclass_of($class, ReaderManagerInterface::class) && !is_subclass_of($class, WriterManagerInterface::class)) {
Chris@17 127 throw new \RuntimeException("$class must implement Zend\Feed\Reader\ExtensionManagerInterface or Zend\Feed\Writer\ExtensionManagerInterface");
Chris@17 128 }
Chris@17 129 $this->standalone = new $class();
Chris@17 130 }
Chris@17 131
Chris@0 132 }