From c3cd98af42c3e30ec3ba56bb5a781239cfd3aa66 Mon Sep 17 00:00:00 2001 From: jperras Date: Thu, 24 Sep 2009 22:56:02 -0400 Subject: [PATCH] Implementing loading datasources from plugins. --- cake/libs/model/connection_manager.php | 57 +++++++++++++------ .../libs/model/connection_manager.test.php | 24 +++++++- 2 files changed, 62 insertions(+), 19 deletions(-) diff --git a/cake/libs/model/connection_manager.php b/cake/libs/model/connection_manager.php index 8a9ea58b1fc..a3628056b36 100644 --- a/cake/libs/model/connection_manager.php +++ b/cake/libs/model/connection_manager.php @@ -106,18 +106,32 @@ function &getDataSource($name) { } $connections = $_this->enumConnectionObjects(); - if (!empty($connections[$name])) { - $conn = $connections[$name]; - $class = $conn['classname']; - $_this->loadDataSource($name); - $_this->_dataSources[$name] =& new $class($_this->config->{$name}); - $_this->_dataSources[$name]->configKeyName = $name; - } else { + + if (empty($connections[$name])) { trigger_error(sprintf(__("ConnectionManager::getDataSource - Non-existent data source %s", true), $name), E_USER_ERROR); $null = null; return $null; } + $conn = $connections[$name]; + + if (strpos($conn['classname'], '.') !== false) { + list($plugin, $class) = explode('.', $conn['classname']); + } else { + $class = $conn['classname']; + } + + $class = Inflector::classify($class); + + if ($_this->loadDataSource($name) === null) { + trigger_error(sprintf(__("ConnectionManager::getDataSource - Could not load class %s", true), $class), E_USER_ERROR); + $null = null; + return $null; + } + + $_this->_dataSources[$name] =& new $class($_this->config->{$name}); + $_this->_dataSources[$name]->configKeyName = $name; + $return =& $_this->_dataSources[$name]; return $return; } @@ -181,13 +195,12 @@ function loadDataSource($connName) { return false; } - if (file_exists(MODELS . 'datasources' . DS . $conn['filename'] . '.php')) { - require (MODELS . 'datasources' . DS . $conn['filename'] . '.php'); - } elseif (fileExistsInPath(LIBS . 'model' . DS . 'datasources' . DS . $conn['filename'] . '.php')) { - require (LIBS . 'model' . DS . 'datasources' . DS . $conn['filename'] . '.php'); - } else { - $error = __('Unable to load DataSource file %s.php', true); - trigger_error(sprintf($error, $conn['filename']), E_USER_ERROR); + $conn = array_merge(array('plugin' => null, 'classname' => null, 'parent' => null), $conn); + $class = "{$conn['plugin']}.{$conn['classname']}"; + + if (!App::import('Datasource', $class)) { + $error = __('ConnectionManager::loadDataSource - Unable to import DataSource class %s', true); + trigger_error(sprintf($error, $class), E_USER_ERROR); return null; } @@ -254,16 +267,24 @@ function __getDriver($config) { $config['datasource'] = 'dbo'; } + $filename = $classname = $parent = $plugin = null; + if (isset($config['driver']) && $config['driver'] != null && !empty($config['driver'])) { $filename = $config['datasource'] . DS . $config['datasource'] . '_' . $config['driver']; $classname = Inflector::camelize(strtolower($config['datasource'] . '_' . $config['driver'])); $parent = $this->__getDriver(array('datasource' => $config['datasource'])); } else { - $filename = $config['datasource'] . '_source'; - $classname = Inflector::camelize(strtolower($config['datasource'] . '_source')); - $parent = null; + if (strpos($config['datasource'], '.') !== false) { + list($plugin, $classname) = explode('.', $config['datasource']); + $filename = Inflector::underscore($classname); + } else { + $filename = $config['datasource'] . '_source'; + $classname = Inflector::camelize(strtolower($config['datasource'] . '_source')); + } } - return array('filename' => $filename, 'classname' => $classname, 'parent' => $parent); + + $driver = compact('filename', 'classname', 'parent', 'plugin'); + return $driver; } /** diff --git a/cake/tests/cases/libs/model/connection_manager.test.php b/cake/tests/cases/libs/model/connection_manager.test.php index 4bc22685484..de9cc0b4f66 100644 --- a/cake/tests/cases/libs/model/connection_manager.test.php +++ b/cake/tests/cases/libs/model/connection_manager.test.php @@ -88,6 +88,28 @@ function testGetDataSource() { } +/** + * testGetPluginDataSource method + * + * @access public + * @return void + */ + function testGetPluginDataSource() { + App::build(array( + 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS) + )); + + $name = 'test_source'; + $config = array('datasource' => 'TestPlugin.TestSource'); + $connection = ConnectionManager::create($name, $config); + + $this->assertTrue(class_exists('TestSource')); + $this->assertEqual($connection->configKeyName, $name); + $this->assertEqual($connection->config, $config); + + App::build(); + } + /** * testSourceList method * @@ -138,7 +160,7 @@ function testLoadDataSource() { } $connection = array('classname' => 'NonExistentDataSource', 'filename' => 'non_existent'); - $this->expectError(new PatternExpectation('/Unable to load DataSource file/i')); + $this->expectError(new PatternExpectation('/Unable to import DataSource class/i')); $loaded = ConnectionManager::loadDataSource($connection); $this->assertEqual($loaded, null);