Skip to content

Instantly share code, notes, and snippets.

@Nicofuma
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nicofuma/dd575a0b38dfaf41325b to your computer and use it in GitHub Desktop.
Save Nicofuma/dd575a0b38dfaf41325b to your computer and use it in GitHub Desktop.
<?php
/**
*
* @package phpBB3
* @copyright (c) 2011 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
namespace phpbb\di;
use Symfony\Component\DependencyInjection\ContainerInterface;
class service_collection_iterator extends \ArrayIterator
{
protected $container;
public function __construct($container, $array = array() , $flags = 0)
{
parent::__construct($array, $flags);
$this->container = $container;
}
public function offsetGet($index)
{
if (($task = parent::offsetGet($index)) == null)
{
$task = $this->container->get($index);
$this->offsetSet($index, $task);
}
return $task;
}
public function current()
{
if (($task = parent::current()) == null)
{
$name = $this->key();
$task = $this->container->get($name);
$this->offsetSet($name, $task);
}
return $task;
}
}
/**
* Collection of services to be configured at container compile time.
*
* @package phpBB3
*/
class service_collection extends \ArrayObject
{
/**
* Constructor
*
* @param ContainerInterface $container Container object
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function getIterator()
{
return new service_collection_iterator($this->container, $this);
}
public function offsetGet($index)
{
if (($task = parent::offsetGet($index)) == null)
{
$task = $this->container->get($index);
$this->offsetSet($index, $task);
}
return $task;
}
/**
* Add a service to the collection
*
* @param string $name The service name
* @return null
*/
public function add($name)
{
$this->offsetSet($name, null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment