Last active
August 29, 2015 14:01
-
-
Save Nicofuma/dd575a0b38dfaf41325b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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