From 71aa460a4820ccd2a831f7b017a8c026274cf69c Mon Sep 17 00:00:00 2001 From: mark_story Date: Fri, 28 Aug 2009 01:06:21 -0400 Subject: [PATCH] Continuing work on getting plugin support for CakeSchema and SchemaShell. Applied patch from 'SkieDr', test cases added. --- cake/console/libs/schema.php | 7 +++-- cake/libs/model/cake_schema.php | 26 ++++++++++++++++--- .../cases/libs/model/cake_schema.test.php | 22 ++++++++++++++++ 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/cake/console/libs/schema.php b/cake/console/libs/schema.php index edeb25f3c..8fb8f21c0 100644 --- a/cake/console/libs/schema.php +++ b/cake/console/libs/schema.php @@ -58,7 +58,7 @@ class SchemaShell extends Shell { * @access public */ function startup() { - $name = $file = $path = $connection = null; + $name = $file = $path = $connection = $plugin = null; if (!empty($this->params['name'])) { $name = $this->params['name']; $this->params['file'] = Inflector::underscore($name); @@ -75,8 +75,11 @@ class SchemaShell extends Shell { if (!empty($this->params['connection'])) { $connection = $this->params['connection']; } + if (!empty($this->params['plugin'])) { + $plugin = $this->params['plugin']; + } - $this->Schema =& new CakeSchema(compact('name', 'path', 'file', 'connection')); + $this->Schema =& new CakeSchema(compact('name', 'path', 'file', 'connection', 'plugin')); } /** diff --git a/cake/libs/model/cake_schema.php b/cake/libs/model/cake_schema.php index ba37b329d..13fb36d47 100644 --- a/cake/libs/model/cake_schema.php +++ b/cake/libs/model/cake_schema.php @@ -65,6 +65,13 @@ class CakeSchema extends Object { */ var $connection = 'default'; +/** + * plugin name. + * + * @var string + **/ + var $plugin = null; + /** * Set of tables * @@ -84,6 +91,9 @@ class CakeSchema extends Object { if (empty($options['name'])) { $this->name = preg_replace('/schema$/i', '', get_class($this)); } + if (!empty($options['plugin'])) { + $this->plugin = $options['plugin']; + } if (strtolower($this->name) === 'cake') { $this->name = Inflector::camelize(Inflector::slug(Configure::read('App.dir'))); @@ -112,7 +122,7 @@ class CakeSchema extends Object { $file = null; foreach ($data as $key => $val) { if (!empty($val)) { - if (!in_array($key, array('name', 'path', 'file', 'connection', 'tables', '_log'))) { + if (!in_array($key, array('plugin', 'name', 'path', 'file', 'connection', 'tables', '_log'))) { $this->tables[$key] = $val; unset($this->{$key}); } elseif ($key !== 'tables') { @@ -207,6 +217,9 @@ class CakeSchema extends Object { $db =& ConnectionManager::getDataSource($connection); App::import('Model', 'AppModel'); + if (isset($this->plugin)) { + App::import('Model', Inflector::camelize($this->plugin) . 'AppModel'); + } $tables = array(); $currentTables = $db->listSources(); @@ -217,11 +230,18 @@ class CakeSchema extends Object { } if (!is_array($models) && $models !== false) { - $models = App::objects('model'); + if (isset($this->plugin)) { + $models = App::objects('model', App::pluginPath($this->plugin) . 'models' . DS, false); + } else { + $models = App::objects('model'); + } } if (is_array($models)) { foreach ($models as $model) { + if (isset($this->plugin)) { + $model = $this->plugin . '.' . $model; + } if (PHP5) { $Object = ClassRegistry::init(array('class' => $model, 'ds' => $connection)); } else { @@ -230,7 +250,7 @@ class CakeSchema extends Object { if (is_object($Object) && $Object->useTable !== false) { $Object->setDataSource($connection); - $table = $db->fullTableName($Object, false); + $table = $db->fullTableName($Object->useTable, false); if (in_array($table, $currentTables)) { $key = array_search($table, $currentTables); diff --git a/cake/tests/cases/libs/model/cake_schema.test.php b/cake/tests/cases/libs/model/cake_schema.test.php index ff40fe5a2..1cca3d93a 100644 --- a/cake/tests/cases/libs/model/cake_schema.test.php +++ b/cake/tests/cases/libs/model/cake_schema.test.php @@ -453,6 +453,28 @@ class CakeSchemaTest extends CakeTestCase { $this->assertTrue(empty($read['tables'])); } +/** + * test reading schema from plugins. + * + * @return void + **/ + function testSchemaReadWithPlugins() { + App::objects('model', null, false); + App::build(array( + 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS) + )); + $Schema =& new CakeSchema(); + $Schema->plugin = 'TestPlugin'; + $read = $Schema->read(array( + 'connection' => 'test_suite', + 'name' => 'TestApp', + 'models' => true + )); + $this->assertTrue(isset($read['tables']['posts'])); + + App::build(); + } + /** * testSchemaWrite method *