ソースを参照

Merge pull request #4434 from eryw/bugfix/patch-tableregistry

Fix TableRegistry::get() behavior when called several times with the same option
José Lorenzo Rodríguez 11 年 前
コミット
919b740521
2 ファイル変更27 行追加4 行削除
  1. 15 4
      src/ORM/TableRegistry.php
  2. 12 0
      tests/TestCase/ORM/TableRegistryTest.php

+ 15 - 4
src/ORM/TableRegistry.php

@@ -76,6 +76,13 @@ class TableRegistry {
 	protected static $_fallbacked = [];
 
 /**
+ * Contains a list of options that were passed to get() method.
+ *
+ * @var array
+ */
+	protected static $_options = [];
+
+/**
  * Stores a list of options to be used when instantiating an object
  * with a matching alias.
  *
@@ -147,15 +154,19 @@ class TableRegistry {
 	public static function get($name, array $options = []) {
 		list($plugin, $alias) = pluginSplit($name);
 		$exists = isset(static::$_instances[$alias]);
+
 		if ($exists && !empty($options)) {
-			throw new RuntimeException(sprintf(
-				'You cannot configure "%s", it already exists in the registry.',
-				$alias
-			));
+			if (static::$_options[$alias] !== $options) {
+				throw new RuntimeException(sprintf(
+					'You cannot configure "%s", it already exists in the registry.',
+					$alias
+				));
+			}
 		}
 		if ($exists) {
 			return static::$_instances[$alias];
 		}
+		static::$_options[$alias] = $options;
 		$options = ['alias' => $alias] + $options;
 
 		if (empty($options['className'])) {

+ 12 - 0
tests/TestCase/ORM/TableRegistryTest.php

@@ -151,6 +151,18 @@ class TableRegistryTest extends TestCase {
 	}
 
 /**
+ * Test get() can be called several times with the same option without
+ * throwing an exception.
+ *
+ * @return void
+ */
+	public function testGetWithSameOption() {
+		$result = TableRegistry::get('Users', ['className' => 'Cake\Test\TestCase\ORM\MyUsersTable']);
+		$result2 = TableRegistry::get('Users', ['className' => 'Cake\Test\TestCase\ORM\MyUsersTable']);
+		$this->assertEquals($result, $result2);
+	}
+
+/**
  * Tests that tables can be instantiated based on conventions
  * and using plugin notation
  *