Skip to content

Commit

Permalink
add PDO attributes service option
Browse files Browse the repository at this point in the history
  • Loading branch information
csanquer committed Jun 4, 2014
1 parent ae1e748 commit c32110d
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 11 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
)
);

Expand Down Expand Up @@ -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,
),
)
);

Expand All @@ -102,8 +111,6 @@ $app->register(
'driver' => 'sqlite',
'path' => 'var/db/db2.sqlite',
),
'pdo.db2.options' => array(
),
)
);

Expand Down
17 changes: 16 additions & 1 deletion src/Config/PdoConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}
4 changes: 4 additions & 0 deletions src/Provider/PDOServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
);

Expand All @@ -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);
}
Expand Down
23 changes: 23 additions & 0 deletions tests/Config/MySqlConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function dataProviderPrepareParameters()
'user' => 'fake-user',
'password' => 'fake-password',
'options' => array(),
'attributes' => array(),
),
),
array(
Expand All @@ -60,6 +61,7 @@ public function dataProviderPrepareParameters()
'user' => 'fake-user',
'password' => 'fake-password',
'options' => array(),
'attributes' => array(),
),
),
array(
Expand All @@ -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'"
),
),
),
);
Expand Down
3 changes: 3 additions & 0 deletions tests/Config/OracleConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function dataProviderPrepareParameters()
'user' => 'fake-user',
'password' => 'fake-password',
'options' => array(),
'attributes' => array(),
),
),
array(
Expand All @@ -60,6 +61,7 @@ public function dataProviderPrepareParameters()
'user' => 'fake-user',
'password' => 'fake-password',
'options' => array(),
'attributes' => array(),
),
),
array(
Expand All @@ -76,6 +78,7 @@ public function dataProviderPrepareParameters()
'user' => 'fake-user',
'password' => 'fake-password',
'options' => array(),
'attributes' => array(),
),
),
);
Expand Down
2 changes: 2 additions & 0 deletions tests/Config/PgSqlConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function dataProviderPrepareParameters()
'user' => 'fake-user',
'password' => 'fake-password',
'options' => array(),
'attributes' => array(),
),
),
array(
Expand All @@ -60,6 +61,7 @@ public function dataProviderPrepareParameters()
'user' => 'fake-user',
'password' => 'fake-password',
'options' => array(),
'attributes' => array(),
),
),
);
Expand Down
3 changes: 3 additions & 0 deletions tests/Config/SqlSrvConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function dataProviderPrepareParameters()
'user' => 'fake-user',
'password' => 'fake-password',
'options' => array(),
'attributes' => array(),
),
),
array(
Expand All @@ -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(),
),
),
);
Expand Down
3 changes: 3 additions & 0 deletions tests/Config/SqliteConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function dataProviderPrepareParameters()
array(
'dsn' => 'sqlite::memory:',
'options' => array(),
'attributes' => array(),
),
),
array(
Expand All @@ -49,6 +50,7 @@ public function dataProviderPrepareParameters()
array(
'dsn' => 'sqlite::memory:',
'options' => array(),
'attributes' => array(),
),
),
array(
Expand All @@ -58,6 +60,7 @@ public function dataProviderPrepareParameters()
array(
'dsn' => 'sqlite:var/db/db.sq3',
'options' => array(),
'attributes' => array(),
),
),
);
Expand Down
18 changes: 13 additions & 5 deletions tests/Provider/PDOServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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()
Expand All @@ -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,
),
),
);
}
Expand Down

0 comments on commit c32110d

Please sign in to comment.