From 013b2462b1e6503e73c9c38ddf92b4cf0acc238f Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Sat, 27 Apr 2013 14:53:58 +0200 Subject: [PATCH] Small refactoring and beginnings of a flyweight for table objects that will replace ClassRegistry --- lib/Cake/ORM/Table.php | 59 +++++++++++++++++++++--- lib/Cake/Test/TestCase/ORM/TableTest.php | 30 ++++++++++-- 2 files changed, 80 insertions(+), 9 deletions(-) diff --git a/lib/Cake/ORM/Table.php b/lib/Cake/ORM/Table.php index 1cdd3ec674a..41dcc7d6658 100644 --- a/lib/Cake/ORM/Table.php +++ b/lib/Cake/ORM/Table.php @@ -18,7 +18,11 @@ class Table { - protected $_name; + protected static $_instances = []; + + protected static $_tablesMap = []; + + protected $_table; protected $_alias; @@ -27,14 +31,14 @@ class Table { protected $_schema; public function __construct($config = array()) { - if (!empty($config['name'])) { - $this->_name = $config['name']; + if (!empty($config['table'])) { + $this->_table = $config['table']; } if (!empty($config['alias'])) { $this->alias($config['alias']); } else { - $this->alias($this->_name); + $this->alias($this->_table); } if (!empty($config['connection'])) { @@ -45,6 +49,49 @@ public function __construct($config = array()) { } } + public static function build($alias, array $options = []) { + if (isset(static::$_instances[$alias])) { + return static::$_instances[$alias]; + } + if (!empty($options['table']) && isset(static::$_tablesMap[$options['table']])) { + $options = array_merge(static::$_tablesMap[$options['table']], $options); + } + + $options = ['alias' => $alias] + $options; + if (empty($options['className'])) { + $options['className'] = get_called_class(); + } + + return static::$_instances[$alias] = new $options['className']($options); + } + + public static function map($alias = null, array $options = null) { + if ($alias === null) { + return static::$_tablesMap; + } + if (!is_string($alias)) { + static::$_tablesMap = $alias; + return; + } + if ($options === null) { + return isset(static::$_tablesMap[$alias]) ? static::$_tablesMap[$alias] : null; + } + static::$_tablesMap[$alias] = $options; + } + + public function clearRegistry() { + static::$_instances = []; + static::$_tablesMap = []; + } + + public function table($table = null) { + if ($table !== null) { + $this->_table = $table; + } + return $this->_table; + } + + public function alias($alias = null) { if ($alias !== null) { $this->_alias = $alias; @@ -62,7 +109,7 @@ public function connection($conn = null) { public function schema($schema = null) { if ($schema === null) { if ($this->_schema === null) { - $this->_schema = $this->connection()->describe($this->_name); + $this->_schema = $this->connection()->describe($this->_table); } return $this->_schema; } @@ -86,7 +133,7 @@ protected function buildQuery() { return $query ->repository($this) ->select() - ->from([$this->_alias => $this->_name]); + ->from([$this->_alias => $this->_table]); } } diff --git a/lib/Cake/Test/TestCase/ORM/TableTest.php b/lib/Cake/Test/TestCase/ORM/TableTest.php index 746758260d3..fcf58520b84 100644 --- a/lib/Cake/Test/TestCase/ORM/TableTest.php +++ b/lib/Cake/Test/TestCase/ORM/TableTest.php @@ -91,9 +91,32 @@ protected function _createDatesTable() { return $result; } +/** + * Tests that table options can be pre-configured for the factory method + * + * @return void + */ + public function testMapAndBuild() { + $map = Table::map(); + $this->assertEquals([], $map); + + $options = ['connection' => $this->connection]; + Table::map('things', $options); + $map = Table::map(); + $this->assertEquals(['things' => $options], $map); + $this->assertEquals($options, Table::map('things')); + + $options += ['schema' => ['id' => ['rubbish']]]; + + $table = Table::build('foo', ['table' => 'things']); + $this->assertInstanceOf('Cake\ORM\Table', $table); + $this->assertEquals('things', $table->table()); + $this->assertEquals('foo', $table->alias()); + } + public function testFindAllNoFields() { $this->_createThingsTable(); - $table = new Table(['name' => 'things', 'connection' => $this->connection]); + $table = new Table(['table' => 'things', 'connection' => $this->connection]); $results = $table->find('all')->toArray(); $expected = [ ['things' => ['id' => 1, 'title' => 'a title', 'body' => 'a body']], @@ -104,7 +127,7 @@ public function testFindAllNoFields() { public function testFindAllSomeFields() { $this->_createThingsTable(); - $table = new Table(['name' => 'things', 'connection' => $this->connection]); + $table = new Table(['table' => 'things', 'connection' => $this->connection]); $results = $table->find('all')->select(['id', 'title'])->toArray(); $expected = [ ['things' => ['id' => 1, 'title' => 'a title']], @@ -122,7 +145,7 @@ public function testFindAllSomeFields() { public function testFindAllConditionAutoTypes() { $this->_createDatesTable(); - $table = new Table(['name' => 'dates', 'connection' => $this->connection]); + $table = new Table(['table' => 'dates', 'connection' => $this->connection]); $query = $table->find('all') ->select(['id', 'name']) ->where(['posted >=' => new \DateTime('2012-12-22 12:01')]); @@ -138,4 +161,5 @@ public function testFindAllConditionAutoTypes() { ]; $this->assertSame($expected, $query->toArray()); } + }