Skip to content

Commit

Permalink
Merge pull request #2561 from cakephp/3.0-registry-exception
Browse files Browse the repository at this point in the history
3.0 - Make TableRegistry methods throw exceptions.
  • Loading branch information
lorenzo committed Dec 27, 2013
2 parents 27ad458 + a2a3b02 commit dbe58d7
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 15 deletions.
5 changes: 2 additions & 3 deletions Cake/Network/Session/DatabaseSession.php
Expand Up @@ -51,9 +51,8 @@ public function __construct() {
$modelAlias = Configure::read('Session.handler.model');

if (empty($modelAlias)) {
$this->_table = TableRegistry::get('Sessions', [
'table' => 'cake_sessions',
]);
$config = TableRegistry::exists('Sessions') ? [] : ['table' => 'cake_sessions'];
$this->_table = TableRegistry::get('Sessions', $config);
} else {
$this->_table = TableRegistry::get($modelAlias);
}
Expand Down
8 changes: 5 additions & 3 deletions Cake/ORM/Association.php
Expand Up @@ -237,9 +237,11 @@ public function target(Table $table = null) {
}

if ($table === null) {
$this->_targetTable = TableRegistry::get($this->_name, [
'className' => $this->_className,
]);
$config = [];
if (!TableRegistry::exists($this->_name)) {
$config = ['className' => $this->_className];
}
$this->_targetTable = TableRegistry::get($this->_name, $config);
}
return $this->_targetTable;
}
Expand Down
9 changes: 6 additions & 3 deletions Cake/ORM/Association/BelongsToMany.php
Expand Up @@ -124,9 +124,12 @@ public function junction($table = null) {
if (empty($this->_junctionTable)) {
$tableName = $this->_junctionTableName();
$tableAlias = Inflector::camelize($tableName);
$table = TableRegistry::get($tableAlias, [
'table' => $tableName
]);

$config = [];
if (!TableRegistry::exists($tableAlias)) {
$config = ['table' => $tableName];
}
$table = TableRegistry::get($tableAlias, $config);
} else {
return $this->_junctionTable;
}
Expand Down
32 changes: 30 additions & 2 deletions Cake/ORM/TableRegistry.php
Expand Up @@ -19,6 +19,7 @@
use Cake\Core\App;
use Cake\Database\ConnectionManager;
use Cake\Utility\Inflector;
use RuntimeException;

/**
* Provides a registry/factory for Table objects.
Expand Down Expand Up @@ -72,7 +73,7 @@ class TableRegistry {
* Stores a list of options to be used when instantiating an object
* with a matching alias.
*
* The options that can be stored are those that are recognized by `build()`
* The options that can be stored are those that are recognized by `get()`
* If second argument is omitted, it will return the current settings
* for $alias.
*
Expand All @@ -82,6 +83,7 @@ class TableRegistry {
* @param string $alias Name of the alias
* @param null|array $options list of options for the alias
* @return array The config data.
* @throws RuntimeException When you attempt to configure an existing table instance.
*/
public static function config($alias = null, $options = null) {
if ($alias === null) {
Expand All @@ -93,6 +95,13 @@ public static function config($alias = null, $options = null) {
if ($options === null) {
return isset(static::$_config[$alias]) ? static::$_config[$alias] : [];
}
if (isset(static::$_instances[$alias])) {
throw new RuntimeException(__d(
'cake_dev',
'You cannot configure "%s", it has already been constructed.',
$alias
));
}
return static::$_config[$alias] = $options;
}

Expand Down Expand Up @@ -124,9 +133,18 @@ public static function config($alias = null, $options = null) {
* @param array $options The options you want to build the table with.
* If a table has already been loaded the options will be ignored.
* @return Cake\Database\Table
* @throws RuntimeException When you try to configure an alias that already exists.
*/
public static function get($alias, $options = []) {
if (isset(static::$_instances[$alias])) {
$exists = isset(static::$_instances[$alias]);
if ($exists && !empty($options)) {
throw new RuntimeException(__d(
'cake_dev',
'You cannot configure "%s", it already exists in the registry.',
$alias
));
}
if ($exists) {
return static::$_instances[$alias];
}

Expand All @@ -149,6 +167,16 @@ public static function get($alias, $options = []) {
return static::$_instances[$alias] = new $options['className']($options);
}

/**
* Check to see if an instance exists in the registry.
*
* @param string $alias The alias to check for.
* @return boolean
*/
public static function exists($alias) {
return isset(static::$_instances[$alias]);
}

/**
* Set an instance.
*
Expand Down
2 changes: 1 addition & 1 deletion Cake/Test/TestCase/ORM/QueryTest.php
Expand Up @@ -211,7 +211,7 @@ public function testContainToFieldsPredefined() {
]
];

$table = TableRegistry::get('foo', ['schema' => ['id' => ['type' => 'integer']]]);
$table = TableRegistry::get('foo');
$query = new Query($this->connection, $table);

$query->select('foo.id')->contain($contains)->sql();
Expand Down
43 changes: 40 additions & 3 deletions Cake/Test/TestCase/ORM/TableRegistryTest.php
Expand Up @@ -82,6 +82,33 @@ public function testConfig() {
$this->assertEquals($expected, $result);
}

/**
* Test calling config() on existing instances throws an error.
*
* @expectedException RuntimeException
* @expectedExceptionMessage You cannot configure "Users", it has already been constructed.
* @return void
*/
public function testConfigOnDefinedInstance() {
$users = TableRegistry::get('Users');
TableRegistry::config('Users', ['table' => 'my_users']);
}

/**
* Test the exists() method.
*
* @return void
*/
public function testExists() {
$this->assertFalse(TableRegistry::exists('Articles'));

TableRegistry::config('Articles', ['table' => 'articles']);
$this->assertFalse(TableRegistry::exists('Articles'));

TableRegistry::get('Articles', ['table' => 'articles']);
$this->assertTrue(TableRegistry::exists('Articles'));
}

/**
* Test getting instances from the registry.
*
Expand All @@ -94,9 +121,7 @@ public function testGet() {
$this->assertInstanceOf('Cake\ORM\Table', $result);
$this->assertEquals('my_articles', $result->table());

$result2 = TableRegistry::get('Articles', [
'table' => 'herp_derp',
]);
$result2 = TableRegistry::get('Articles');
$this->assertSame($result, $result2);
$this->assertEquals('my_articles', $result->table());
}
Expand All @@ -114,6 +139,18 @@ public function testGetWithConfig() {
$this->assertEquals('my_articles', $result->table(), 'Should use config() data.');
}

/**
* Test get with config throws an exception if the alias exists already.
*
* @expectedException RuntimeException
* @expectedExceptionMessage You cannot configure "Users", it already exists in the registry.
* @return void
*/
public function testGetExistingWithConfigData() {
$users = TableRegistry::get('Users');
TableRegistry::get('Users', ['table' => 'my_users']);
}

/**
* Tests that tables can be instantiated based on conventions
* and using plugin notation
Expand Down

0 comments on commit dbe58d7

Please sign in to comment.