HummingBirdAnimeClient/src/Aviat/Ion/Di/Container.php

134 lines
2.5 KiB
PHP
Raw Normal View History

2015-09-17 23:11:18 -04:00
<?php
2015-11-16 11:40:01 -05:00
/**
* Ion
*
* Building blocks for web development
*
* @package Ion
* @author Timothy J. Warren
* @copyright Copyright (c) 2015 - 2016
2015-11-16 11:40:01 -05:00
* @license MIT
*/
2015-09-17 23:11:18 -04:00
namespace Aviat\Ion\Di;
2015-11-13 16:31:01 -05:00
use Psr\Log\LoggerInterface;
2015-09-17 23:11:18 -04:00
/**
* Dependency container
*/
class Container implements ContainerInterface {
/**
* Array with class instances
*
2015-12-09 14:54:11 -05:00
* @var array
2015-09-17 23:11:18 -04:00
*/
protected $container = [];
2015-11-13 16:31:01 -05:00
/**
* Map of logger instances
*
2015-12-09 14:54:11 -05:00
* @var array
2015-11-13 16:31:01 -05:00
*/
protected $loggers = [];
2015-09-17 23:11:18 -04:00
/**
* Constructor
*
* @param array $values (optional)
*/
public function __construct(array $values = [])
{
2015-12-08 16:39:49 -05:00
$this->container = $values;
$this->loggers = [];
2015-09-17 23:11:18 -04:00
}
/**
* Finds an entry of the container by its identifier and returns it.
*
* @param string $id Identifier of the entry to look for.
*
* @throws NotFoundException No entry was found for this identifier.
* @throws ContainerException Error while retrieving the entry.
*
* @return mixed Entry.
*/
public function get($id)
{
if ( ! is_string($id))
{
throw new Exception\ContainerException("Id must be a string");
}
if ($this->has($id))
{
return $this->container[$id];
}
2015-09-18 22:55:40 -04:00
throw new Exception\NotFoundException("Item '{$id}' does not exist in container.");
2015-09-17 23:11:18 -04:00
}
/**
* Add a value to the container
*
* @param string $id
* @param mixed $value
2015-11-13 16:31:01 -05:00
* @return ContainerInterface
2015-09-17 23:11:18 -04:00
*/
public function set($id, $value)
{
$this->container[$id] = $value;
return $this;
}
/**
* Returns true if the container can return an entry for the given identifier.
* Returns false otherwise.
*
* @param string $id Identifier of the entry to look for.
*
* @return boolean
*/
public function has($id)
{
2015-12-08 16:39:49 -05:00
return array_key_exists($id, $this->container);
2015-09-17 23:11:18 -04:00
}
2015-11-13 16:31:01 -05:00
/**
* Determine whether a logger channel is registered
* @param string $key The logger channel
* @return boolean
*/
public function hasLogger($key = 'default')
{
2015-12-08 16:39:49 -05:00
return array_key_exists($key, $this->loggers);
2015-11-13 16:31:01 -05:00
}
/**
* Add a logger to the Container
*
* @param LoggerInterface $logger
* @param string $key The logger 'channel'
* @return ContainerInterface
*/
public function setLogger(LoggerInterface $logger, $key = 'default')
{
$this->loggers[$key] = $logger;
return $this;
}
/**
* Retrieve a logger for the selected channel
*
* @param string $key The logger to retreive
* @return LoggerInterface|null
*/
public function getLogger($key = 'default')
{
return ($this->hasLogger($key))
? $this->loggers[$key]
: NULL;
}
2015-09-17 23:11:18 -04:00
}
// End of Container.php