Simplify connection access

This commit is contained in:
Romain Neutron
2012-10-03 13:56:26 +02:00
parent a67327c139
commit d283054e0e
10 changed files with 24 additions and 23 deletions

View File

@@ -139,14 +139,14 @@ class Install extends Command
$abName = $dialog->ask($output, "DB name (phraseanet) : ", 'phraseanet'); $abName = $dialog->ask($output, "DB name (phraseanet) : ", 'phraseanet');
try { try {
$abConn = new \connection_pdo('appbox', $hostname, $port, $dbUser, $dbPassword, $abName, array(), $setupRegistry); $abConn = new \connection_pdo('appbox', $hostname, $port, $dbUser, $dbPassword, $abName, array(), $this->container['debug']);
$output->writeln("\n\t<info>Application-Box : Connection successful !</info>\n"); $output->writeln("\n\t<info>Application-Box : Connection successful !</info>\n");
} catch (\Exception $e) { } catch (\Exception $e) {
$output->writeln("\n\t<error>Invalid connection parameters</error>\n"); $output->writeln("\n\t<error>Invalid connection parameters</error>\n");
} }
} while (!$abConn); } while (!$abConn);
} else { } else {
$abConn = new \connection_pdo('appbox', $input->getOption('db-host'), $input->getOption('db-port'), $input->getOption('db-user'), $input->getOption('db-password'), $input->getOption('appbox'), array(), $setupRegistry); $abConn = new \connection_pdo('appbox', $input->getOption('db-host'), $input->getOption('db-port'), $input->getOption('db-user'), $input->getOption('db-password'), $input->getOption('appbox'), array(), $this->container['debug']);
$output->writeln("\n\t<info>Application-Box : Connection successful !</info>\n"); $output->writeln("\n\t<info>Application-Box : Connection successful !</info>\n");
} }
@@ -155,6 +155,9 @@ class Install extends Command
private function getDBConn(InputInterface $input, OutputInterface $output, DialogHelper $dialog) private function getDBConn(InputInterface $input, OutputInterface $output, DialogHelper $dialog)
{ {
/**
* TODO NEUTRON, can not work
*/
$dbConn = null; $dbConn = null;
if (!$input->getOption('databox')) { if (!$input->getOption('databox')) {
do { do {
@@ -163,7 +166,7 @@ class Install extends Command
if ($dbName) { if ($dbName) {
try { try {
$dbConn = new \connection_pdo('databox', $hostname, $port, $dbUser, $dbPassword, $dbName, array(), $setupRegistry); $dbConn = new \connection_pdo('databox', $hostname, $port, $dbUser, $dbPassword, $dbName, array(), $this->container['debug']);
$output->writeln("\n\t<info>Data-Box : Connection successful !</info>\n"); $output->writeln("\n\t<info>Data-Box : Connection successful !</info>\n");
do { do {
@@ -179,7 +182,7 @@ class Install extends Command
} }
} while ($retry); } while ($retry);
} else { } else {
$dbConn = new \connection_pdo('databox', $input->getOption('db-host'), $input->getOption('db-port'), $input->getOption('db-user'), $input->getOption('db-password'), $input->getOption('databox'), array(), $setupRegistry); $dbConn = new \connection_pdo('databox', $input->getOption('db-host'), $input->getOption('db-port'), $input->getOption('db-user'), $input->getOption('db-password'), $input->getOption('databox'), array(), $this->container['debug']);
$output->writeln("\n\t<info>Data-Box : Connection successful !</info>\n"); $output->writeln("\n\t<info>Data-Box : Connection successful !</info>\n");
$template = $input->getOption('db-template') ? : 'en'; $template = $input->getOption('db-template') ? : 'en';
} }

View File

@@ -226,7 +226,7 @@ class Databoxes implements ControllerProviderInterface
$dataTemplate = new \SplFileInfo($app['phraseanet.registry']->get('GV_RootPath') . 'lib/conf.d/data_templates/' . $dataTemplate . '.xml'); $dataTemplate = new \SplFileInfo($app['phraseanet.registry']->get('GV_RootPath') . 'lib/conf.d/data_templates/' . $dataTemplate . '.xml');
try { try {
$connbas = new \connection_pdo('databox_creation', $hostname, $port, $user, $password, $dbName, array(), $app['phraseanet.registry']); $connbas = new \connection_pdo('databox_creation', $hostname, $port, $user, $password, $dbName, array(), $app['debug']);
} catch (\PDOException $e) { } catch (\PDOException $e) {
return $app->redirect('/admin/databoxes/?success=0&error=database-failed'); return $app->redirect('/admin/databoxes/?success=0&error=database-failed');
} }
@@ -252,7 +252,7 @@ class Databoxes implements ControllerProviderInterface
try { try {
$data_template = new \SplFileInfo($app['phraseanet.registry']->get('GV_RootPath') . 'lib/conf.d/data_templates/' . $dataTemplate . '.xml'); $data_template = new \SplFileInfo($app['phraseanet.registry']->get('GV_RootPath') . 'lib/conf.d/data_templates/' . $dataTemplate . '.xml');
$connbas = new \connection_pdo('databox_creation', $hostname, $port, $userDb, $passwordDb, $dbName, array(), $app['phraseanet.registry']); $connbas = new \connection_pdo('databox_creation', $hostname, $port, $userDb, $passwordDb, $dbName, array(), $app['debug']);
try { try {
$base = \databox::create($app, $connbas, $data_template, $app['phraseanet.registry']); $base = \databox::create($app, $connbas, $data_template, $app['phraseanet.registry']);
$base->registerAdmin($app['phraseanet.user']); $base->registerAdmin($app['phraseanet.user']);

View File

@@ -165,14 +165,14 @@ class Installer implements ControllerProviderInterface
$setupRegistry = new \Setup_Registry(); $setupRegistry = new \Setup_Registry();
try { try {
$abConn = new \connection_pdo('appbox', $hostname, $port, $user_ab, $ab_password, $appbox_name, array(), $setupRegistry); $abConn = new \connection_pdo('appbox', $hostname, $port, $user_ab, $ab_password, $appbox_name, array(), $app['debug']);
} catch (\Exception $e) { } catch (\Exception $e) {
return $app->redirect('/setup/installer/step2/?error=' . _('Appbox is unreachable')); return $app->redirect('/setup/installer/step2/?error=' . _('Appbox is unreachable'));
} }
try { try {
if ($databox_name) { if ($databox_name) {
$dbConn = new \connection_pdo('databox', $hostname, $port, $user_ab, $ab_password, $databox_name, array(), $setupRegistry); $dbConn = new \connection_pdo('databox', $hostname, $port, $user_ab, $ab_password, $databox_name, array(), $app['debug']);
} }
} catch (\Exception $e) { } catch (\Exception $e) {
return $app->redirect('/setup/installer/step2/?error=' . _('Databox is unreachable')); return $app->redirect('/setup/installer/step2/?error=' . _('Databox is unreachable'));

View File

@@ -39,7 +39,7 @@ class ConnectionTest implements ControllerProviderInterface
$connection_ok = $db_ok = $is_databox = $is_appbox = $empty = false; $connection_ok = $db_ok = $is_databox = $is_appbox = $empty = false;
try { try {
$conn = new \connection_pdo('test', $hostname, $port, $user, $password, null, array(), $app['phraseanet.registry']); $conn = new \connection_pdo('test', $hostname, $port, $user, $password, null, array(), false);
$connection_ok = true; $connection_ok = true;
} catch (\Exception $e) { } catch (\Exception $e) {
@@ -47,7 +47,7 @@ class ConnectionTest implements ControllerProviderInterface
if ($dbname && $connection_ok === true) { if ($dbname && $connection_ok === true) {
try { try {
$conn = new \connection_pdo('test', $hostname, $port, $user, $password, $dbname, array(), $app['phraseanet.registry']); $conn = new \connection_pdo('test', $hostname, $port, $user, $password, $dbname, array(), false);
$db_ok = true; $db_ok = true;
$sql = "SHOW TABLE STATUS"; $sql = "SHOW TABLE STATUS";

View File

@@ -62,7 +62,7 @@ class appbox extends base
public function __construct(Application $app) public function __construct(Application $app)
{ {
$this->app = $app; $this->app = $app;
$this->connection = connection::getPDOConnection($app, null, $app['phraseanet.registry']); $this->connection = connection::getPDOConnection($app);
$choosenConnexion = $app['phraseanet.configuration']->getPhraseanet()->get('database'); $choosenConnexion = $app['phraseanet.configuration']->getPhraseanet()->get('database');
$connexion = $app['phraseanet.configuration']->getConnexion($choosenConnexion); $connexion = $app['phraseanet.configuration']->getConnexion($choosenConnexion);

View File

@@ -108,7 +108,7 @@ class connection
* @param string $name * @param string $name
* @return connection_pdo * @return connection_pdo
*/ */
public static function getPDOConnection(Application $app, $name = null, registryInterface $registry = null) public static function getPDOConnection(Application $app, $name = null)
{ {
self::instantiate($app); self::instantiate($app);
if (trim($name) == '') { if (trim($name) == '') {
@@ -147,9 +147,7 @@ class connection
} }
try { try {
$registry = $registry ? : $app['phraseanet.registry']; self::$_PDO_instance[$name] = new connection_pdo($name, $hostname, $port, $user, $password, $dbname, array(), $app['debug']);
self::$_PDO_instance[$name] = new connection_pdo($name, $hostname, $port, $user, $password, $dbname, array(), $registry);
self::$_PDO_instance[$name]->query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");
} catch (Exception $e) { } catch (Exception $e) {
throw new Exception('Connection not available'); throw new Exception('Connection not available');
} }

View File

@@ -17,7 +17,7 @@
*/ */
class connection_pdo extends connection_abstract implements connection_interface class connection_pdo extends connection_abstract implements connection_interface
{ {
protected $registry; protected $debug;
/** /**
* *
@@ -30,9 +30,9 @@ class connection_pdo extends connection_abstract implements connection_interface
* @param array $options * @param array $options
* @return connection_pdo * @return connection_pdo
*/ */
public function __construct($name, $hostname, $port, $user, $passwd, $dbname = false, $options = array(), registryInterface $registry) public function __construct($name, $hostname, $port, $user, $passwd, $dbname = false, $options = array(), $debug)
{ {
$this->registry = $registry; $this->debug = $debug;
$this->name = $name; $this->name = $name;
if ($dbname) if ($dbname)
$dsn = 'mysql:dbname=' . $dbname . ';host=' . $hostname . ';port=' . $port . ';'; $dsn = 'mysql:dbname=' . $dbname . ';host=' . $hostname . ';port=' . $port . ';';
@@ -65,7 +65,7 @@ class connection_pdo extends connection_abstract implements connection_interface
*/ */
public function prepare($statement, $driver_options = array()) public function prepare($statement, $driver_options = array())
{ {
if ($this->registry->get('GV_debug')) { if ($this->debug) {
return new connection_pdoStatementDebugger(parent::prepare($statement, $driver_options)); return new connection_pdoStatementDebugger(parent::prepare($statement, $driver_options));
} else { } else {
return parent::prepare($statement, $driver_options); return parent::prepare($statement, $driver_options);

View File

@@ -103,7 +103,7 @@ class databox extends base
assert($sbas_id > 0); assert($sbas_id > 0);
$this->app = $app; $this->app = $app;
$this->connection = connection::getPDOConnection($app, $sbas_id, null); $this->connection = connection::getPDOConnection($app, $sbas_id);
$this->id = $sbas_id; $this->id = $sbas_id;
$connection_params = phrasea::sbas_params($this->app); $connection_params = phrasea::sbas_params($this->app);
@@ -469,7 +469,7 @@ class databox extends base
*/ */
public static function mount(Application $app, $host, $port, $user, $password, $dbname, registry $registry) public static function mount(Application $app, $host, $port, $user, $password, $dbname, registry $registry)
{ {
$connection = new connection_pdo('test', $host, $port, $user, $password, $dbname, array(), $registry); $connection = new connection_pdo('test', $host, $port, $user, $password, $dbname, array(), $app['debug']);
$conn = $app['phraseanet.appbox']->get_connection(); $conn = $app['phraseanet.appbox']->get_connection();
$sql = 'SELECT MAX(ord) as ord FROM sbas'; $sql = 'SELECT MAX(ord) as ord FROM sbas';

View File

@@ -119,7 +119,7 @@ class ApplicationSetupTest extends PhraseanetWebTestCaseAbstract
$dbName = isset($settings['dataBox']) ? $settings['dataBox'] : null; $dbName = isset($settings['dataBox']) ? $settings['dataBox'] : null;
$connection = new connection_pdo('unitTestsAB', $host, $port, $MySQLuser, $MySQLpassword, $abName, array(), self::$DI['app']['phraseanet.registry']); $connection = new connection_pdo('unitTestsAB', $host, $port, $MySQLuser, $MySQLpassword, $abName, array(), false);
self::$DI['app']['phraseanet.appbox']->set_connection($connection); self::$DI['app']['phraseanet.appbox']->set_connection($connection);

View File

@@ -100,7 +100,7 @@ abstract class PhraseanetWebTestCaseAuthenticatedAbstract extends PhraseanetPHPU
$connexion = $configuration->getConnexion($choosenConnexion); $connexion = $configuration->getConnexion($choosenConnexion);
try { try {
$conn = new \connection_pdo('databox_creation', $connexion->get('host'), $connexion->get('port'), $connexion->get('user'), $connexion->get('password'), 'unit_test_db', array(), self::$DI['app']['phraseanet.registry']); $conn = new \connection_pdo('databox_creation', $connexion->get('host'), $connexion->get('port'), $connexion->get('user'), $connexion->get('password'), 'unit_test_db', array(), false);
} catch (\PDOException $e) { } catch (\PDOException $e) {
$this->markTestSkipped('Could not reach DB'); $this->markTestSkipped('Could not reach DB');