Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
120 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Cycle\Injector; | ||
|
||
use Cycle\Database\DatabaseInterface; | ||
use Cycle\Database\DatabaseManager; | ||
use Cycle\Database\Exception\DBALException; | ||
use Spiral\Core\Container\InjectorInterface; | ||
|
||
final class DatabaseInjector implements InjectorInterface | ||
{ | ||
public function __construct( | ||
private DatabaseManager $dm | ||
) { | ||
} | ||
|
||
public function createInjection(\ReflectionClass $class, string $context = null): DatabaseInterface | ||
{ | ||
// if context is empty default database will be returned | ||
try { | ||
return $this->dm->database($context); | ||
} catch (DBALException $e) { | ||
if ($context === null || !str_contains($e->getMessage(), ' no presets for ')) { | ||
throw $e; | ||
} | ||
// get default database | ||
return $this->dm->database(); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
tests/app/Injector/DatabaseInjectorAliasResolverTester.php
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,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\App\Injector; | ||
|
||
use Cycle\Database\Database; | ||
use Cycle\Database\DatabaseInterface; | ||
|
||
final class DatabaseInjectorAliasResolverTester | ||
{ | ||
public function __construct( | ||
public DatabaseInterface $dbRuntime, | ||
public DatabaseInterface $dbOther, | ||
public Database $dbSqlite, | ||
public Database $dbPostgres, | ||
) { | ||
} | ||
|
||
public static function getDefaultDatabase(DatabaseInterface $usingUndefinedAlias) | ||
{ | ||
return $usingUndefinedAlias; | ||
} | ||
} |
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,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Tests\Injector; | ||
|
||
use Cycle\Database\Database; | ||
use Cycle\Database\DatabaseInterface; | ||
use Spiral\App\Injector\DatabaseInjectorAliasResolverTester; | ||
use Spiral\Core\Container; | ||
use Spiral\Tests\BaseTest; | ||
|
||
final class DatabaseInjectorTest extends BaseTest | ||
{ | ||
public function testInjectRepository(): void | ||
{ | ||
$this->assertInstanceOf(DatabaseInterface::class, $this->getContainer()->get(DatabaseInterface::class)); | ||
$this->assertInstanceOf(Database::class, $this->getContainer()->get(Database::class)); | ||
} | ||
|
||
public function testAlias(): void | ||
{ | ||
$obj = $this->getContainer()->get(DatabaseInjectorAliasResolverTester::class); | ||
$this->assertInstanceOf(Database::class, $obj->dbRuntime); | ||
$this->assertInstanceOf(Database::class, $obj->dbOther); | ||
$this->assertSame($obj->dbRuntime, $obj->dbSqlite); | ||
$this->assertSame($obj->dbOther, $obj->dbPostgres); | ||
} | ||
|
||
public function testUseUndeclaredContext(): void | ||
{ | ||
$c = $this->getContainer(); | ||
|
||
$database1 = $c->invoke([DatabaseInjectorAliasResolverTester::class, 'getDefaultDatabase']); | ||
$database2 = $c->get(DatabaseInterface::class); | ||
|
||
$this->assertSame($database2, $database1); | ||
} | ||
} |