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); } }