Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from cadrephp/1.x-connection-factory
Introducing ConnectionFactory
- Loading branch information
Showing
7 changed files
with
227 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
namespace Cadre\AtlasOrmDebugBarBridge; | ||
|
||
use Aura\Sql\ExtendedPdo as BaseExtendedPdo; | ||
use DebugBar\DataCollector\PDO\TraceablePDO; | ||
use PDO; | ||
|
||
class ConnectionFactory | ||
{ | ||
private $dsn; | ||
private $username; | ||
private $password; | ||
private $options = array(); | ||
private $attributes = array(); | ||
private $pdo; | ||
|
||
public function __construct( | ||
$dsn, | ||
$username = null, | ||
$password = null, | ||
array $options = array(), | ||
array $attributes = array() | ||
) { | ||
if ($dsn instanceof ExtendedPdo) { | ||
$this->pdo = $dsn; | ||
} elseif ($dsn instanceof BaseExtendedPdo) { | ||
$this->pdo = new ExtendedPdo(new TraceablePDO($dsn->getPdo())); | ||
} elseif ($dsn instanceof TraceablePDO) { | ||
$this->pdo = new ExtendedPdo($dsn); | ||
} elseif ($dsn instanceof PDO) { | ||
$this->pdo = new ExtendedPdo(new TraceablePDO($dsn)); | ||
} else { | ||
$this->dsn = $dsn; | ||
$this->username = $username; | ||
$this->password = $password; | ||
$this->options = $options; | ||
$this->attributes = array_replace($this->attributes, $attributes); | ||
} | ||
} | ||
|
||
public function __invoke() | ||
{ | ||
if (!isset($this->pdo)) { | ||
$this->pdo = new ExtendedPdo( | ||
$this->dsn, | ||
$this->username, | ||
$this->password, | ||
$this->options, | ||
$this->attributes | ||
); | ||
} | ||
|
||
if (isset($this->collector)) { | ||
$this->collector->addConnection($this->pdo->getPdo(), $this->name); | ||
} | ||
|
||
return $this->pdo; | ||
} | ||
|
||
public function setCollector(AtlasOrmCollector $collector, $name = null) | ||
{ | ||
$this->collector = $collector; | ||
$this->name = $name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
namespace Cadre\AtlasOrmDebugBarBridge; | ||
|
||
use Aura\Sql\ExtendedPdo as BaseExtendedPdo; | ||
use DebugBar\DataCollector\PDO\TraceablePDO; | ||
use PDO; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ConnectionFactoryTest extends TestCase | ||
{ | ||
public function testConnectionFactoryWithConfig() | ||
{ | ||
$factory = new ConnectionFactory('sqlite::memory:'); | ||
$extended = ($factory)(); | ||
|
||
$this->assertInstanceOf(ExtendedPdo::class, $extended); | ||
} | ||
|
||
public function testConnectionFactoryWithExtendedPdo() | ||
{ | ||
$pdo = new ExtendedPdo('sqlite::memory:'); | ||
|
||
$factory = new ConnectionFactory($pdo); | ||
$extended = ($factory)(); | ||
|
||
$this->assertInstanceOf(ExtendedPdo::class, $extended); | ||
$this->assertSame($pdo, $extended); | ||
} | ||
|
||
public function testConnectionFactoryWithTraceablePdo() | ||
{ | ||
$pdo = new TraceablePDO(new PDO('sqlite::memory:')); | ||
|
||
$factory = new ConnectionFactory($pdo); | ||
$extended = ($factory)(); | ||
|
||
$this->assertInstanceOf(ExtendedPdo::class, $extended); | ||
$this->assertSame($pdo, $extended->getPdo()); | ||
} | ||
|
||
public function testConnectionFactoryWithPdo() | ||
{ | ||
$pdo = new PDO('sqlite::memory:'); | ||
|
||
$factory = new ConnectionFactory($pdo); | ||
$extended = ($factory)(); | ||
$traceable = $extended->getPdo(); | ||
|
||
$this->assertInstanceOf(ExtendedPdo::class, $extended); | ||
$this->assertInstanceOf(TraceablePDO::class, $traceable); | ||
} | ||
|
||
public function testConnectionFactoryWithBaseExtendedPdo() | ||
{ | ||
$pdo = new BaseExtendedPdo('sqlite::memory:'); | ||
|
||
$factory = new ConnectionFactory($pdo); | ||
$extended = ($factory)(); | ||
$traceable = $extended->getPdo(); | ||
|
||
$this->assertInstanceOf(ExtendedPdo::class, $extended); | ||
$this->assertInstanceOf(TraceablePDO::class, $traceable); | ||
} | ||
} |