[ Index ]

PHP Cross Reference of phpBB-3.3.12-deutsch

title

Body

[close]

/phpbb/di/ -> service_collection.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  namespace phpbb\di;
  15  
  16  use Symfony\Component\DependencyInjection\ContainerInterface;
  17  
  18  /**
  19  * Collection of services to be configured at container compile time.
  20  */
  21  class service_collection extends \ArrayObject
  22  {
  23      /**
  24      * @var ContainerInterface
  25      */
  26      protected $container;
  27  
  28      /**
  29      * @var array
  30      */
  31      protected $service_classes;
  32  
  33      /**
  34      * Constructor
  35      *
  36      * @param ContainerInterface $container Container object
  37      */
  38  	public function __construct(ContainerInterface $container)
  39      {
  40          $this->container = $container;
  41          $this->service_classes = array();
  42      }
  43  
  44      /**
  45      * {@inheritdoc}
  46      */
  47  	public function getIterator()
  48      {
  49          return new service_collection_iterator($this);
  50      }
  51  
  52      /**
  53      * {@inheritdoc}
  54      */
  55  	public function offsetGet($index)
  56      {
  57          return $this->container->get($index);
  58      }
  59  
  60      /**
  61      * Add a service to the collection
  62      *
  63      * @param string $name The service name
  64      * @return void
  65      */
  66  	public function add($name)
  67      {
  68          $this->offsetSet($name, false);
  69      }
  70  
  71      /**
  72      * Add a service's class to the collection
  73      *
  74      * @param string    $service_id
  75      * @param string    $class
  76      */
  77  	public function add_service_class($service_id, $class)
  78      {
  79          $this->service_classes[$service_id] = $class;
  80      }
  81  
  82      /**
  83      * Get services' classes
  84      *
  85      * @return array
  86      */
  87  	public function get_service_classes()
  88      {
  89          return $this->service_classes;
  90      }
  91  
  92      /**
  93       * Returns the service associated to a class
  94       *
  95       * @return mixed
  96       * @throw \RuntimeException if the
  97       */
  98  	public function get_by_class($class)
  99      {
 100          $service_id = null;
 101  
 102          foreach ($this->service_classes as $id => $service_class)
 103          {
 104              if ($service_class === $class)
 105              {
 106                  if ($service_id !== null)
 107                  {
 108                      throw new \RuntimeException('More than one service definitions found for class "'.$class.'" in collection.');
 109                  }
 110  
 111                  $service_id = $id;
 112              }
 113          }
 114  
 115          if ($service_id === null)
 116          {
 117              throw new \RuntimeException('No service found for class "'.$class.'" in collection.');
 118          }
 119  
 120          return $this->offsetGet($service_id);
 121      }
 122  }


Generated: Sun Jun 23 12:25:44 2024 Cross-referenced by PHPXref 0.7.1