Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added compiler extension
DatabaseTypeProviderExtension
- added test - updated README
- Loading branch information
Showing
6 changed files
with
143 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SixtyEightPublishers\DoctrineBridge\Bridge\Nette\DI\Config; | ||
|
||
final class DatabaseTypeConfig | ||
{ | ||
/** @var class-string */ | ||
public string $class; | ||
|
||
public ?string $mappingType = null; | ||
|
||
/** @var array<string, mixed> */ | ||
public array $context = []; | ||
} |
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,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SixtyEightPublishers\DoctrineBridge\Bridge\Nette\DI; | ||
|
||
use Nette\Schema\Expect; | ||
use Nette\Schema\Schema; | ||
use Nette\DI\CompilerExtension; | ||
use SixtyEightPublishers\DoctrineBridge\Bridge\Nette\DI\Config\DatabaseTypeConfig; | ||
use function assert; | ||
use function is_array; | ||
use function is_string; | ||
|
||
final class DatabaseTypeProviderExtension extends CompilerExtension implements DatabaseTypeProviderInterface | ||
{ | ||
public function getConfigSchema(): Schema | ||
{ | ||
return Expect::arrayOf( | ||
Expect::anyOf( | ||
Expect::string(), | ||
Expect::structure([ | ||
'class' => Expect::string()->required(), | ||
'mapping_type' => Expect::string()->nullable(), | ||
'context' => Expect::arrayOf('mixed', 'string'), | ||
]), | ||
)->before( | ||
static fn (string|array $databaseType): array => is_array($databaseType) | ||
? $databaseType | ||
: ['class' => $databaseType, 'mapping_type' => null, 'context' => []] | ||
)->castTo(DatabaseTypeConfig::class), | ||
'string', | ||
); | ||
} | ||
|
||
public function getDatabaseTypes(): array | ||
{ | ||
$databaseTypes = []; | ||
$config = $this->getConfig(); | ||
assert(is_array($config)); | ||
|
||
foreach ($config as $databaseTypeName => $databaseTypeConfig) { | ||
assert(is_string($databaseTypeName) && $databaseTypeConfig instanceof DatabaseTypeConfig); | ||
|
||
$databaseTypes[] = new DatabaseType( | ||
name: $databaseTypeName, | ||
class: $databaseTypeConfig->class, | ||
mappingType: $databaseTypeConfig->mappingType, | ||
context: $databaseTypeConfig->context, | ||
); | ||
} | ||
|
||
return $databaseTypes; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
tests/Bridge/Nette/DI/Nettrine/DatabaseType/config.withDatabaseTypeProviderExtension.neon
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,15 @@ | ||
includes: | ||
- ../base.neon | ||
|
||
extensions: | ||
database_type_provider: SixtyEightPublishers\DoctrineBridge\Bridge\Nette\DI\DatabaseTypeProviderExtension | ||
|
||
database_type_provider: | ||
custom_type_inline: SixtyEightPublishers\DoctrineBridge\Tests\Fixtures\DatabaseType\CustomTypeWithContainer | ||
custom_type_extended: | ||
class: SixtyEightPublishers\DoctrineBridge\Tests\Fixtures\DatabaseType\CustomTypeWithContainer | ||
custom_type_extended_2: | ||
class: SixtyEightPublishers\DoctrineBridge\Tests\Fixtures\DatabaseType\CustomTypeWithContainer | ||
mapping_type: integer | ||
context: | ||
default_value: 3 |
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