Registry.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace AlexLcDee\RegistryContainer;
  3. class Registry implements RegistryInterface
  4. {
  5. /**
  6. * @var CategoriesContainer
  7. */
  8. private static $container;
  9. public static function init($typesMap)
  10. {
  11. if (static::$container !== null) {
  12. throw new RegistryException('Registry must be initialized once');
  13. }
  14. static::$container = new CategoriesContainer();
  15. foreach ($typesMap as $category => $type) {
  16. static::$container->set($category, $type);
  17. }
  18. }
  19. /**
  20. * @param string $category
  21. * @param RegistrableItem $item
  22. * @throws RegistryException
  23. */
  24. public static function add($category, RegistrableItem $item)
  25. {
  26. if (!static::$container->contains($category)) {
  27. throw new RegistryException(strtr('Registry does not contain category "{category}"', [
  28. '{category}' => $category
  29. ]));
  30. }
  31. static::$container->get($category)->add($item);
  32. }
  33. /**
  34. * @param string $category
  35. * @return ItemsContainer
  36. * @throws RegistryException
  37. */
  38. public static function get($category)
  39. {
  40. if (!static::$container->contains($category)) {
  41. throw new RegistryException(strtr('Registry does not contain category "{category}"', [
  42. '{category}' => $category
  43. ]));
  44. }
  45. return static::$container->get($category);
  46. }
  47. }