alexlcdee há 8 anos atrás
pai
commit
8d079620b3
1 ficheiros alterados com 22 adições e 11 exclusões
  1. 22 11
      src/Registry.php

+ 22 - 11
src/Registry.php

@@ -5,20 +5,31 @@ namespace AlexLcDee\RegistryContainer;
 
 class Registry implements RegistryInterface
 {
+    /**
+     * @var Registry
+     */
+    private static $instance;
+
     /**
      * @var CategoriesContainer
      */
-    private static $container;
+    private $container;
 
-    public static function init($typesMap)
+    private function __construct($typesMap)
     {
-        if (static::$container !== null) {
-            throw new RegistryException('Registry must be initialized once');
-        }
-        static::$container = new CategoriesContainer();
+        $this->container = new CategoriesContainer();
         foreach ($typesMap as $category => $type) {
-            static::$container->set($category, new ItemsContainer($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;
     }
 
     /**
@@ -28,12 +39,12 @@ class Registry implements RegistryInterface
      */
     public static function add($category, RegistrableItem $item)
     {
-        if (!static::$container->contains($category)) {
+        if (!static::$instance->container->contains($category)) {
             throw new RegistryException(strtr('Registry does not contain category "{category}"', [
                 '{category}' => $category
             ]));
         }
-        static::$container->get($category)->add($item);
+        static::$instance->container->get($category)->add($item);
     }
 
     /**
@@ -43,11 +54,11 @@ class Registry implements RegistryInterface
      */
     public static function get($category)
     {
-        if (!static::$container->contains($category)) {
+        if (!static::$instance->container->contains($category)) {
             throw new RegistryException(strtr('Registry does not contain category "{category}"', [
                 '{category}' => $category
             ]));
         }
-        return static::$container->get($category);
+        return static::$instance->container->get($category);
     }
 }