├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml.dist └── src ├── lib └── Herrera │ └── Pdo │ ├── PdoServiceProvider.php │ └── PdoTrait.php └── tests ├── Herrera └── Pdo │ ├── Test │ ├── Application.php │ └── Pdo.php │ └── Tests │ ├── PdoServiceProviderTest.php │ └── PdoTraitTest.php └── bootstrap.php /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea/ 2 | /bin/ 3 | /coverage/ 4 | /src/vendors/ 5 | 6 | /composer.lock 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.3 5 | - 5.4 6 | - 5.5 7 | 8 | before_script: 9 | - composer self-update 10 | - composer install --dev --no-interaction --prefer-source 11 | 12 | script: bin/phpunit 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Kevin Herrera 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 7 | of the Software, and to permit persons to whom the Software is furnished to do 8 | so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 15 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 16 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | PDO Service Provider 2 | ==================== 3 | 4 | [![Build Status]](https://travis-ci.org/herrera-io/php-silex-pdo) 5 | 6 | A simple PDO service provider. 7 | 8 | Summary 9 | ------- 10 | 11 | A stupid simple PDO service provider. I cannot get any more clear than this. 12 | 13 | Installation 14 | ------------ 15 | 16 | Add it to your list of Composer dependencies: 17 | 18 | ```sh 19 | $ composer require "herrera-io/silex-pdo=1.*" 20 | ``` 21 | 22 | Usage 23 | ----- 24 | 25 | ```php 26 | use Herrera\Pdo\PdoServiceProvider; 27 | use Silex\Application; 28 | 29 | $app = new Application(); 30 | $app->register( 31 | new PdoServiceProvider(), 32 | array( 33 | 'pdo.dsn' => 'pdo_mysql:dbname=test;host=127.0.0.1', 34 | 'pdo.username' => 'username', // optional 35 | 'pdo.password' => 'password', // optional 36 | 'pdo.options' => array( // optional 37 | PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'" 38 | ) 39 | ) 40 | ); 41 | 42 | $pdo = $app['pdo']; 43 | ``` 44 | 45 | You can also use the `PdoTrait` to add a `prepare` method to your application: 46 | 47 | ```php 48 | use Herrera\Pdo\PdoTrait; 49 | use Silex\Application; 50 | 51 | class MyApp extends Application 52 | { 53 | use PdoTrait; 54 | } 55 | ``` 56 | 57 | > You will still need to register the service provider. 58 | 59 | If debugging is enabled and Monolog is available, a [logging version][] 60 | of `PDO` will be used to log queries and their execution time. 61 | 62 | [Build Status]: https://travis-ci.org/herrera-io/php-silex-pdo.png?branch=master 63 | [logging version]: https://github.com/herrera-io/php-pdo-log 64 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "herrera-io/silex-pdo", 3 | "description": "A simple PDO service provider.", 4 | "keywords": ["silex", "pdo", "service", "provider"], 5 | "homepage": "http://github.com/herrera-io/php-silex-pdo", 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Kevin Herrera", 10 | "email": "kevin@herrera.io", 11 | "homepage": "http://kevin.herrera.io" 12 | } 13 | ], 14 | "support": { 15 | "issues": "https://github.com/herrera-io/php-silex-pdo/issues" 16 | }, 17 | "require": { 18 | "php": ">=5.3.3", 19 | "silex/silex": "1.*", 20 | "herrera-io/pdo-log": "~1.0" 21 | }, 22 | "require-dev": { 23 | "herrera-io/phpunit-test-case": "~1.0", 24 | "monolog/monolog": "~1.1", 25 | "phpunit/phpunit": "~3.7" 26 | }, 27 | "autoload": { 28 | "psr-0": { 29 | "Herrera\\Pdo": "src/lib" 30 | } 31 | }, 32 | "config": { 33 | "bin-dir": "bin", 34 | "vendor-dir": "src/vendors" 35 | }, 36 | "extra": { 37 | "branch-alias": { 38 | "dev-master": "1.0-dev" 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | src/lib/ 15 | 16 | 17 | 18 | 19 | src/tests/ 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/lib/Herrera/Pdo/PdoServiceProvider.php: -------------------------------------------------------------------------------- 1 | 14 | */ 15 | class PdoServiceProvider implements ServiceProviderInterface 16 | { 17 | /** 18 | * {@inheritDoc} 19 | */ 20 | // @codeCoverageIgnoreStart 21 | public function boot(Application $app) 22 | { 23 | } 24 | // @codeCoverageIgnoreEnd 25 | 26 | /** 27 | * {@inheritDoc} 28 | */ 29 | public function register(Application $app) 30 | { 31 | $app['pdo.factory'] = $app->protect( 32 | function ( 33 | $dsn, 34 | $username = null, 35 | $password = null, 36 | array $options = array() 37 | ) use ($app) { 38 | if ($app['debug'] && isset($app['monolog'])) { 39 | $pdo = new PdoLog($dsn, $username, $password, $options); 40 | 41 | $pdo->onLog( 42 | function (array $entry) use ($app) { 43 | $app['monolog']->addDebug( 44 | sprintf( 45 | 'PDO query: %s, values :%s', 46 | $entry['query'], 47 | var_export($entry['values'], true) 48 | ) 49 | ); 50 | } 51 | ); 52 | 53 | return $pdo; 54 | } 55 | 56 | return new PDO($dsn, $username, $password, $options); 57 | } 58 | ); 59 | 60 | $app['pdo'] = $app->share( 61 | function (Application $app) { 62 | foreach ($app['pdo.defaults'] as $name => $value) { 63 | if (!isset($app[$name])) { 64 | $app[$name] = $value; 65 | } 66 | } 67 | 68 | return $app['pdo.factory']( 69 | $app['pdo.dsn'], 70 | $app['pdo.username'], 71 | $app['pdo.password'], 72 | $app['pdo.options'] 73 | ); 74 | } 75 | ); 76 | 77 | $app['pdo.defaults'] = array( 78 | 'pdo.username' => null, 79 | 'pdo.password' => null, 80 | 'pdo.options' => array() 81 | ); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/lib/Herrera/Pdo/PdoTrait.php: -------------------------------------------------------------------------------- 1 | 11 | */ 12 | trait PdoTrait 13 | { 14 | /** 15 | * Creates a new prepared statement. 16 | * 17 | * @param string $statement The statement. 18 | * @param array $options The driver options. 19 | * 20 | * @return PDOStatement The new statement. 21 | */ 22 | public function prepare($statement, array $options = array()) 23 | { 24 | return $this['pdo']->prepare($statement, $options); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/tests/Herrera/Pdo/Test/Application.php: -------------------------------------------------------------------------------- 1 | calls[] = array('prepare', $a, $b); 12 | 13 | return 123; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/tests/Herrera/Pdo/Tests/PdoServiceProviderTest.php: -------------------------------------------------------------------------------- 1 | provider->register($this->app); 26 | 27 | $this->app['pdo.dsn'] = 'sqlite::memory:'; 28 | 29 | $this->assertInstanceOf('Closure', $this->app['pdo.factory']); 30 | $this->assertInstanceOf('PDO', $this->app['pdo']); 31 | $this->assertSame( 32 | array( 33 | 'pdo.username' => null, 34 | 'pdo.password' => null, 35 | 'pdo.options' => array() 36 | ), 37 | $this->app['pdo.defaults'] 38 | ); 39 | } 40 | 41 | /** 42 | * @depends testRegister 43 | */ 44 | public function testPdo() 45 | { 46 | $this->provider->register($this->app); 47 | 48 | $_this = $this; 49 | 50 | $this->app['pdo.factory'] = $this->app->protect( 51 | function ($a, $b, $c, array $d) use ($_this) { 52 | $_this->assertEquals('a', $a); 53 | $_this->assertEquals('b', $b); 54 | $_this->assertEquals('c', $c); 55 | $_this->assertEquals(array('d'), $d); 56 | 57 | return true; 58 | } 59 | ); 60 | 61 | $this->app['pdo.defaults'] = array( 62 | 'pdo.dsn' => 'a', 63 | 'pdo.username' => 'b', 64 | 'pdo.password' => 'c', 65 | 'pdo.options' => array('d') 66 | ); 67 | 68 | $this->assertTrue($this->app['pdo']); 69 | } 70 | 71 | /** 72 | * @depends testRegister 73 | */ 74 | public function testPdoDebug() 75 | { 76 | $file = $this->createFile(); 77 | 78 | $this->app['debug'] = true; 79 | $this->app->register( 80 | new MonologServiceProvider(), 81 | array( 82 | 'monolog.level' => Logger::DEBUG, 83 | 'monolog.logfile' => $file, 84 | ) 85 | ); 86 | 87 | $this->app['pdo.dsn'] = 'sqlite::memory:'; 88 | 89 | $this->provider->register($this->app); 90 | 91 | $this->app['pdo']->exec('CREATE TABLE test(id INTEGER PRIMARY KEY)'); 92 | 93 | $this->assertContains( 94 | 'CREATE TABLE test(id INTEGER PRIMARY KEY)', 95 | file_get_contents($file) 96 | ); 97 | } 98 | 99 | protected function setUp() 100 | { 101 | $this->app = new Application(); 102 | $this->provider = new PdoServiceProvider(); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /src/tests/Herrera/Pdo/Tests/PdoTraitTest.php: -------------------------------------------------------------------------------- 1 | rand()); 21 | 22 | $this->assertSame(123, $this->app->prepare($a, $b)); 23 | } 24 | 25 | protected function setUp() 26 | { 27 | if (!version_compare(PHP_VERSION, '5.4', '>=')) { 28 | $this->markTestSkipped('PHP 5.4 or greater required.'); 29 | } 30 | 31 | $this->app = new Application(); 32 | $this->app['pdo'] = new Pdo(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/tests/bootstrap.php: -------------------------------------------------------------------------------- 1 | add(null, __DIR__); 5 | --------------------------------------------------------------------------------