Skip to content

Commit

Permalink
Changed the trigger_error by exceptions in ConnectionManager.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Jan 13, 2011
1 parent 166c776 commit d15ed32
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 25 deletions.
18 changes: 18 additions & 0 deletions cake/libs/error/exceptions.php
Expand Up @@ -370,6 +370,24 @@ class MissingShellFileException extends CakeException {
protected $_messageTemplate = "Shell file %s could not be loaded.";
}

/**
* Exception class to be thrown when a database file is not found
*
* @package cake.libs
*/
class MissingDatasourceConfigException extends CakeException {
protected $_messageTemplate = 'Database connection "%s" could not be loaded.';
}

/**
* Exception class to be thrown when a database file is not found
*
* @package cake.libs
*/
class MissingDatasourceFileException extends CakeException {
protected $_messageTemplate = 'Database connection "%s" could not be loaded.';
}

/**
* Exception class to be thrown when a database table is not found in the datasource
*
Expand Down
24 changes: 9 additions & 15 deletions cake/libs/model/connection_manager.php
Expand Up @@ -83,33 +83,27 @@ public static function &getInstance() {
*
* @param string $name The name of the DataSource, as defined in app/config/database.php
* @return object Instance
* @throws MissingDatasourceConfigException
* @throws MissingDatasourceFileException
*/
public static function &getDataSource($name) {
public static function getDataSource($name) {
$_this = ConnectionManager::getInstance();

if (!empty($_this->_dataSources[$name])) {
$return = $_this->_dataSources[$name];
return $return;
return $_this->_dataSources[$name];
}

if (empty($_this->_connectionsEnum[$name])) {
trigger_error(__("ConnectionManager::getDataSource - Non-existent data source %s", $name), E_USER_ERROR);
$null = null;
return $null;
throw new MissingDatasourceConfigException(array('config' => $name));
}
$conn = $_this->_connectionsEnum[$name];
$class = $conn['classname'];

if ($_this->loadDataSource($name) === null) {
trigger_error(__("ConnectionManager::getDataSource - Could not load class %s", $class), E_USER_ERROR);
$null = null;
return $null;
}
$_this->loadDataSource($name);
$_this->_dataSources[$name] = new $class($_this->config->{$name});
$_this->_dataSources[$name]->configKeyName = $name;

$return = $_this->_dataSources[$name];
return $return;
return $_this->_dataSources[$name];
}

/**
Expand Down Expand Up @@ -148,6 +142,7 @@ public static function getSourceName(&$source) {
* or an array containing the filename (without extension) and class name of the object,
* to be found in app/models/datasources/ or cake/libs/model/datasources/.
* @return boolean True on success, null on failure or false if the class is already loaded
* @throws MissingDatasourceFileException
*/
public static function loadDataSource($connName) {
$_this = ConnectionManager::getInstance();
Expand All @@ -170,8 +165,7 @@ public static function loadDataSource($connName) {
$class = "{$conn['plugin']}.{$conn['classname']}";

if (!App::import('Datasource', $class, !is_null($conn['plugin']))) {
trigger_error(__('ConnectionManager::loadDataSource - Unable to import DataSource class %s', $class), E_USER_ERROR);
return null;
throw new MissingDatasourceFileException(array('class' => $conn['classname'], 'plugin' => $conn['plugin']));
}
return true;
}
Expand Down
29 changes: 29 additions & 0 deletions cake/libs/view/errors/missing_datasource_config.ctp
@@ -0,0 +1,29 @@
<?php
/**
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package cake.libs.view.templates.errors
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
?>
<h2><?php echo __('Missing Datasource Configuration'); ?></h2>
<p class="error">
<strong><?php echo __('Error'); ?>: </strong>
<?php echo __('The datasource configuration %1$s was not found in databases.php.', '<em>' . $config . '</em>'); ?>
</p>
<p class="notice">
<strong><?php echo __('Notice'); ?>: </strong>
<?php echo __('If you want to customize this error message, create %s', APP_DIR . DS . 'views' . DS . 'errors' . DS . 'missing_datasource_config.ctp'); ?>
</p>

<?php echo $this->element('exception_stack_trace'); ?>
29 changes: 29 additions & 0 deletions cake/libs/view/errors/missing_datasource_file.ctp
@@ -0,0 +1,29 @@
<?php
/**
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package cake.libs.view.templates.errors
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
?>
<h2><?php echo __('Missing Datasource Class'); ?></h2>
<p class="error">
<strong><?php echo __('Error'); ?>: </strong>
<?php echo __('Datasource class %1$s was not found.', '<em>' . $class . '</em>'); ?>
</p>
<p class="notice">
<strong><?php echo __('Notice'); ?>: </strong>
<?php echo __('If you want to customize this error message, create %s', APP_DIR . DS . 'views' . DS . 'errors' . DS . 'missing_datasource_file.ctp'); ?>
</p>

<?php echo $this->element('exception_stack_trace'); ?>
8 changes: 6 additions & 2 deletions cake/libs/view/pages/home.ctp
Expand Up @@ -87,11 +87,15 @@ if (isset($filePresent)):
require LIBS . 'model' . DS . 'connection_manager.php';
}
$db = ConnectionManager::getInstance();
@$connected = $db->getDataSource('default');
try {
$connected = $db->getDataSource('default');
} catch (Exception $e) {
$connected = false;
}
?>
<p>
<?php
if ($connected->isConnected()):
if ($connected && $connected->isConnected()):
echo '<span class="notice success">';
echo __('Cake is able to connect to the database.');
echo '</span>';
Expand Down
25 changes: 17 additions & 8 deletions cake/tests/cases/libs/model/connection_manager.test.php
Expand Up @@ -81,12 +81,16 @@ function testGetDataSource() {

$source = ConnectionManager::getDataSource(key($connections));
$this->assertTrue(is_object($source));
}

$this->expectError();

$source = ConnectionManager::getDataSource('non_existent_source');
$this->assertEqual($source, null);

/**
* testGetDataSourceException() method
*
* @return void
* @expectedException MissingDatasourceConfigException
*/
public function testGetDataSourceException() {
ConnectionManager::getDataSource('non_existent_source');
}

/**
Expand Down Expand Up @@ -233,12 +237,17 @@ function testLoadDataSource() {
$loaded = ConnectionManager::loadDataSource($connection);
$this->assertEqual($loaded, !$exists, "%s Failed loading the {$connection['classname']} datasource");
}
}

/**
* testLoadDataSourceException() method
*
* @return void
* @expectedException MissingDatasourceFileException
*/
public function testLoadDataSourceException() {
$connection = array('classname' => 'NonExistentDataSource', 'filename' => 'non_existent');
$this->expectError();

$loaded = ConnectionManager::loadDataSource($connection);
$this->assertEqual($loaded, null);
}

/**
Expand Down

0 comments on commit d15ed32

Please sign in to comment.