Continuing work on getting plugin support for CakeSchema and SchemaShell. Applied patch from 'SkieDr', test cases added.

This commit is contained in:
mark_story 2009-08-28 01:06:21 -04:00
parent 58e9c0e328
commit 71aa460a48
3 changed files with 50 additions and 5 deletions

View file

@ -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'));
}
/**

View file

@ -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);

View file

@ -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
*