mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Changed the trigger_error by exceptions in ConnectionManager.
This commit is contained in:
parent
166c776099
commit
d15ed329d9
6 changed files with 108 additions and 25 deletions
|
@ -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
|
||||
*
|
||||
|
|
|
@ -83,33 +83,27 @@ class ConnectionManager {
|
|||
*
|
||||
* @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];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -148,6 +142,7 @@ class ConnectionManager {
|
|||
* 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();
|
||||
|
@ -170,8 +165,7 @@ class ConnectionManager {
|
|||
$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;
|
||||
}
|
||||
|
|
29
cake/libs/view/errors/missing_datasource_config.ctp
Normal file
29
cake/libs/view/errors/missing_datasource_config.ctp
Normal file
|
@ -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
cake/libs/view/errors/missing_datasource_file.ctp
Normal file
29
cake/libs/view/errors/missing_datasource_file.ctp
Normal file
|
@ -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'); ?>
|
|
@ -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>';
|
||||
|
|
|
@ -81,12 +81,16 @@ class ConnectionManagerTest extends CakeTestCase {
|
|||
|
||||
$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');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -233,12 +237,17 @@ class ConnectionManagerTest extends CakeTestCase {
|
|||
$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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue