浏览代码

Adds TableLocator::addNamespace method.

Robert Pustułka 7 年之前
父节点
当前提交
c7f09e04e9
共有 2 个文件被更改,包括 40 次插入2 次删除
  1. 18 2
      src/ORM/Locator/TableLocator.php
  2. 22 0
      tests/TestCase/ORM/Locator/TableLocatorTest.php

+ 18 - 2
src/ORM/Locator/TableLocator.php

@@ -66,7 +66,7 @@ class TableLocator implements LocatorInterface
     /**
      * Constructor.
      *
-     * @param array|null $namespaces Namespaces where tables should be located located.
+     * @param array|null $namespaces Namespaces where tables should be located.
      *   If none provided, the default `Model\Table` under your app's namespace is used.
      */
     public function __construct(array $namespaces = null)
@@ -77,7 +77,9 @@ class TableLocator implements LocatorInterface
             ];
         }
 
-        $this->_namespaces = $namespaces;
+        foreach ($namespaces as $namespace) {
+            $this->addNamespace($namespace);
+        }
     }
 
     /**
@@ -342,4 +344,18 @@ class TableLocator implements LocatorInterface
             $this->_fallbacked[$alias]
         );
     }
+
+    /**
+     * Adds a namespace where tables should be looked for.
+     *
+     * @param string $namespace Namespace to add.
+     * @return $this
+     */
+    public function addNamespace($namespace)
+    {
+        $namespace = str_replace('\\', '/', $namespace);
+        $this->_namespaces[] = trim($namespace, '/');
+
+        return $this;
+    }
 }

+ 22 - 0
tests/TestCase/ORM/Locator/TableLocatorTest.php

@@ -689,4 +689,26 @@ class TableLocatorTest extends TestCase
         $table = $locator->get('Articles');
         $this->assertInstanceOf(Table::class, $table);
     }
+
+    /**
+     * testAddNamespace
+     *
+     * Tests that adding a namespace takes effect.
+     *
+     * @return void
+     */
+    public function testAddNamespace()
+    {
+        $locator = new TableLocator([]);
+
+        $table = $locator->get('Addresses');
+        $this->assertInstanceOf(Table::class, $table);
+
+        $locator->clear();
+        $locator->addNamespace('Infrastructure/Table');
+
+        $table = $locator->get('Addresses');
+        $this->assertInstanceOf(AddressesTable::class, $table);
+
+    }
 }