| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- <?php
- namespace AlexLcDee\RegistryContainer;
- class Registry implements RegistryInterface
- {
- /**
- * @var Registry
- */
- private static $instance;
- /**
- * @var CategoriesContainer
- */
- private $container;
- private function __construct($typesMap)
- {
- $this->container = new CategoriesContainer();
- foreach ($typesMap as $category => $type) {
- $this->container->set($category, new ItemsContainer($type));
- }
- }
- public static function init($typesMap)
- {
- if(static::$instance !== null) {
- throw new RegistryException('Registry can be initialized only once.');
- }
- static::$instance = new static($typesMap);
- return static::$instance;
- }
- /**
- * @param string $category
- * @param RegistrableItem $item
- * @throws RegistryException
- */
- public static function add($category, RegistrableItem $item)
- {
- if (!static::$instance->container->contains($category)) {
- throw new RegistryException(strtr('Registry does not contain category "{category}"', [
- '{category}' => $category
- ]));
- }
- static::$instance->container->get($category)->add($item);
- }
- /**
- * @param string $category
- * @return ItemsContainer
- * @throws RegistryException
- */
- public static function get($category)
- {
- if (!static::$instance->container->contains($category)) {
- throw new RegistryException(strtr('Registry does not contain category "{category}"', [
- '{category}' => $category
- ]));
- }
- return static::$instance->container->get($category);
- }
- }
|