From c32110d78410850c283363ce1feeb73233d4e152 Mon Sep 17 00:00:00 2001 From: Charles Sanquer Date: Wed, 4 Jun 2014 17:13:12 +0200 Subject: [PATCH] add PDO attributes service option --- README.md | 17 ++++++++++++----- src/Config/PdoConfig.php | 17 ++++++++++++++++- src/Provider/PDOServiceProvider.php | 4 ++++ tests/Config/MySqlConfigTest.php | 23 +++++++++++++++++++++++ tests/Config/OracleConfigTest.php | 3 +++ tests/Config/PgSqlConfigTest.php | 2 ++ tests/Config/SqlSrvConfigTest.php | 3 +++ tests/Config/SqliteConfigTest.php | 3 +++ tests/Provider/PDOServiceProviderTest.php | 18 +++++++++++++----- 9 files changed, 79 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 327f1cd..0db3147 100644 --- a/README.md +++ b/README.md @@ -52,11 +52,17 @@ $app->register( 'user' => 'ger', 'password' => 'GER', ), - // optional PDO options used in PDO constructor 4th argument driver_options + // optional PDO attributes used in PDO constructor 4th argument driver_options + // some PDO attributes can be used only as PDO driver_options // see http://www.php.net/manual/fr/pdo.construct.php 'pdo.options' => array( \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'" ), + // optional PDO attributes set with PDO::setAttribute + // see http://www.php.net/manual/fr/pdo.setattribute.php + 'pdo.attributes' => array( + \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, + ), ) ); @@ -86,11 +92,14 @@ $app->register( 'user' => 'username', 'password' => 'password', ), + // optional PDO attributes used in PDO constructor 4th argument driver_options 'pdo.db1.options' => array( - // optional PDO options used in PDO constructor 4th argument driver_options - // see http://www.php.net/manual/fr/pdo.construct.php \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'" ), + // optional PDO attributes set with PDO::setAttribute + 'pdo.db1.attributes' => array( + \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, + ), ) ); @@ -102,8 +111,6 @@ $app->register( 'driver' => 'sqlite', 'path' => 'var/db/db2.sqlite', ), - 'pdo.db2.options' => array( - ), ) ); diff --git a/src/Config/PdoConfig.php b/src/Config/PdoConfig.php index 20c54f3..98af2e8 100644 --- a/src/Config/PdoConfig.php +++ b/src/Config/PdoConfig.php @@ -75,6 +75,7 @@ public function __construct() $this->allowedTypes = array_merge(array( 'driver' => array('string'), 'options' => array('array', 'null'), + 'attributes' => array('array'), ), $this->allowedTypes); $this->allowedValues = array_merge(array( @@ -84,6 +85,7 @@ public function __construct() $this->defaults = array_merge(array( 'driver' => $this->driver, 'options' => array(), + 'attributes' => array(), ), $this->defaults); $this->resolver = new OptionsResolver(); @@ -147,6 +149,11 @@ public function prepareParameters(array $params) unset($params['password']); } + if (isset($params['attributes'])) { + $preparedParams['attributes'] = $params['attributes']; + unset($params['attributes']); + } + $preparedParams['dsn'] = $this->constructDSN($params); return $preparedParams; @@ -162,11 +169,19 @@ public function connect(array $params) { $params = $this->prepareParameters($params); - return new \PDO( + $pdo = new \PDO( $params['dsn'], isset($params['user']) ? (string) $params['user'] : null, isset($params['password']) ? (string) $params['password'] : null, isset($params['options']) ? (array) $params['options'] : array() ); + + if (is_array($params['attributes'])) { + foreach ($params['attributes'] as $attr => $value) { + $pdo->setAttribute($attr, $value); + } + } + + return $pdo; } } diff --git a/src/Provider/PDOServiceProvider.php b/src/Provider/PDOServiceProvider.php index 2a1bfec..8620b3b 100644 --- a/src/Provider/PDOServiceProvider.php +++ b/src/Provider/PDOServiceProvider.php @@ -52,6 +52,9 @@ protected function getPdo(Application $app, $prefix) $app[$prefix.'.server'], array( 'options' => isset($app[$prefix.'.options']) ? (array) $app[$prefix.'.options'] : array(), + ), + array( + 'attributes' => isset($app[$prefix.'.attributes']) ? (array) $app[$prefix.'.attributes'] : array(), ) ); @@ -67,6 +70,7 @@ public function register(Application $app) $app[$prefix.'.server'] = array(); $app[$prefix.'.options'] = array(); + $app[$prefix.'.attributes'] = array(); $app[$prefix] = $this->getPdo($app, $prefix); } diff --git a/tests/Config/MySqlConfigTest.php b/tests/Config/MySqlConfigTest.php index 74b1dc5..dab8865 100644 --- a/tests/Config/MySqlConfigTest.php +++ b/tests/Config/MySqlConfigTest.php @@ -45,6 +45,7 @@ public function dataProviderPrepareParameters() 'user' => 'fake-user', 'password' => 'fake-password', 'options' => array(), + 'attributes' => array(), ), ), array( @@ -60,6 +61,7 @@ public function dataProviderPrepareParameters() 'user' => 'fake-user', 'password' => 'fake-password', 'options' => array(), + 'attributes' => array(), ), ), array( @@ -74,6 +76,27 @@ public function dataProviderPrepareParameters() 'user' => 'fake-user', 'password' => 'fake-password', 'options' => array(), + 'attributes' => array(), + ), + ), + array( + array( + 'unix_socket' => '/var/run/mysqld/mysqld.sock', + 'dbname' => 'fake-db', + 'user' => 'fake-user', + 'password' => 'fake-password', + 'attributes' => array( + \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'" + ), + ), + array( + 'dsn' => 'mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=fake-db', + 'user' => 'fake-user', + 'password' => 'fake-password', + 'options' => array(), + 'attributes' => array( + \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'" + ), ), ), ); diff --git a/tests/Config/OracleConfigTest.php b/tests/Config/OracleConfigTest.php index 5c36509..167680f 100644 --- a/tests/Config/OracleConfigTest.php +++ b/tests/Config/OracleConfigTest.php @@ -45,6 +45,7 @@ public function dataProviderPrepareParameters() 'user' => 'fake-user', 'password' => 'fake-password', 'options' => array(), + 'attributes' => array(), ), ), array( @@ -60,6 +61,7 @@ public function dataProviderPrepareParameters() 'user' => 'fake-user', 'password' => 'fake-password', 'options' => array(), + 'attributes' => array(), ), ), array( @@ -76,6 +78,7 @@ public function dataProviderPrepareParameters() 'user' => 'fake-user', 'password' => 'fake-password', 'options' => array(), + 'attributes' => array(), ), ), ); diff --git a/tests/Config/PgSqlConfigTest.php b/tests/Config/PgSqlConfigTest.php index 0a87cbf..30248e9 100644 --- a/tests/Config/PgSqlConfigTest.php +++ b/tests/Config/PgSqlConfigTest.php @@ -45,6 +45,7 @@ public function dataProviderPrepareParameters() 'user' => 'fake-user', 'password' => 'fake-password', 'options' => array(), + 'attributes' => array(), ), ), array( @@ -60,6 +61,7 @@ public function dataProviderPrepareParameters() 'user' => 'fake-user', 'password' => 'fake-password', 'options' => array(), + 'attributes' => array(), ), ), ); diff --git a/tests/Config/SqlSrvConfigTest.php b/tests/Config/SqlSrvConfigTest.php index 09a7e8c..888f670 100644 --- a/tests/Config/SqlSrvConfigTest.php +++ b/tests/Config/SqlSrvConfigTest.php @@ -45,6 +45,7 @@ public function dataProviderPrepareParameters() 'user' => 'fake-user', 'password' => 'fake-password', 'options' => array(), + 'attributes' => array(), ), ), array( @@ -54,12 +55,14 @@ public function dataProviderPrepareParameters() 'dbname' => 'fake-db', 'user' => 'fake-user', 'password' => 'fake-password', + 'attributes' => array(), ), array( 'dsn' => 'sqlsrv:dbname=fake-db;server=127.0.0.1', 'user' => 'fake-user', 'password' => 'fake-password', 'options' => array(), + 'attributes' => array(), ), ), ); diff --git a/tests/Config/SqliteConfigTest.php b/tests/Config/SqliteConfigTest.php index f08b0c7..b360be6 100644 --- a/tests/Config/SqliteConfigTest.php +++ b/tests/Config/SqliteConfigTest.php @@ -40,6 +40,7 @@ public function dataProviderPrepareParameters() array( 'dsn' => 'sqlite::memory:', 'options' => array(), + 'attributes' => array(), ), ), array( @@ -49,6 +50,7 @@ public function dataProviderPrepareParameters() array( 'dsn' => 'sqlite::memory:', 'options' => array(), + 'attributes' => array(), ), ), array( @@ -58,6 +60,7 @@ public function dataProviderPrepareParameters() array( 'dsn' => 'sqlite:var/db/db.sq3', 'options' => array(), + 'attributes' => array(), ), ), ); diff --git a/tests/Provider/PDOServiceProviderTest.php b/tests/Provider/PDOServiceProviderTest.php index 879ab64..53cfc96 100644 --- a/tests/Provider/PDOServiceProviderTest.php +++ b/tests/Provider/PDOServiceProviderTest.php @@ -18,7 +18,7 @@ class PDOServiceProviderTest extends \PHPUnit_Framework_TestCase /** * @dataProvider providerConnection */ - public function testConnection($prefix, $server = array(), $options = array(), $expectedServer = array(), $expectedOptions = array()) + public function testConnection($prefix, $server = array(), $options = array(), $attributes = array(), $expectedServer = array(), $expectedOptions = array(), $expectedAttributes = array()) { if (!in_array('sqlite', \PDO::getAvailableDrivers())) { $this->markTestSkipped('pdo_sqlite is not available'); @@ -32,11 +32,13 @@ public function testConnection($prefix, $server = array(), $options = array(), $ $app->register(new PDOServiceProvider($prefix), array( $prefix.'.server' => $server, $prefix.'.options' => $options, + $prefix.'.attributes' => $attributes, )); $this->assertInstanceOf('\PDO', $app[$prefix]); $this->assertEquals($expectedServer, $app[$prefix.'.server']); $this->assertEquals($expectedOptions, $app[$prefix.'.options']); + $this->assertEquals($expectedAttributes, $app[$prefix.'.attributes']); } public function providerConnection() @@ -47,17 +49,23 @@ public function providerConnection() ), array( 'pdo', - array( - ), - array( - ), + array(), + array(), + array(), array('driver' => 'sqlite'), ), array( 'foo', array('driver' => 'sqlite', 'path' => 'memory'), array(), + array( + \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, + ), array('driver' => 'sqlite', 'path' => 'memory'), + array(), + array( + \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, + ), ), ); }