diff --git a/composer.json b/composer.json index 1f6f887..07b7333 100644 --- a/composer.json +++ b/composer.json @@ -25,6 +25,7 @@ "cycle/orm": "^2.0.2", "cycle/schema-migrations-generator": "^2.1", "cycle/schema-renderer": "^1.2", + "cycle/schema-builder": "^2.5", "doctrine/inflector": "^1.4 || ^2.0", "spiral/attributes": "^2.10 || ^3.0", "spiral/framework": "^3.3", diff --git a/src/Bootloader/SchemaBootloader.php b/src/Bootloader/SchemaBootloader.php index e7d3f7f..8ed0b71 100644 --- a/src/Bootloader/SchemaBootloader.php +++ b/src/Bootloader/SchemaBootloader.php @@ -5,12 +5,14 @@ namespace Spiral\Cycle\Bootloader; use Cycle\ORM\SchemaInterface; +use Cycle\Schema\Defaults; use Cycle\Schema\Generator; use Cycle\Schema\GeneratorInterface; use Cycle\Schema\Registry; use Spiral\Boot\Bootloader\Bootloader; use Spiral\Boot\MemoryInterface; use Spiral\Core\Container; +use Spiral\Core\FactoryInterface; use Spiral\Cycle\Config\CycleConfig; use Spiral\Cycle\Schema\Compiler; use Spiral\Tokenizer\Bootloader\TokenizerBootloader; @@ -28,6 +30,7 @@ final class SchemaBootloader extends Bootloader implements Container\SingletonIn protected const BINDINGS = [ SchemaInterface::class => [self::class, 'schema'], + Registry::class => [self::class, 'initRegistry'] ]; /** @var string[][]|GeneratorInterface[][] */ @@ -108,5 +111,13 @@ protected function schema(MemoryInterface $memory, CycleConfig $config): SchemaI return $schemaCompiler->toSchema(); } + + private function initRegistry(FactoryInterface $factory, CycleConfig $config): Registry + { + $defaults = new Defaults(); + $defaults->merge($config->getSchemaDefaults()); + + return $factory->make(Registry::class, ['defaults' => $defaults]); + } } diff --git a/tests/src/Bootloader/SchemaBootloaderTest.php b/tests/src/Bootloader/SchemaBootloaderTest.php index f3ff68f..2e7fb7c 100644 --- a/tests/src/Bootloader/SchemaBootloaderTest.php +++ b/tests/src/Bootloader/SchemaBootloaderTest.php @@ -5,8 +5,12 @@ namespace Spiral\Tests\Bootloader; use Cycle\Annotated\Entities; +use Cycle\ORM\Mapper\Mapper; use Cycle\ORM\SchemaInterface; +use Cycle\ORM\Select\Repository; +use Cycle\ORM\Select\Source; use Cycle\Schema\GeneratorInterface; +use Cycle\Schema\Registry; use Spiral\Cycle\Bootloader\SchemaBootloader; use Spiral\Cycle\Config\CycleConfig; use Spiral\Tests\BaseTest; @@ -51,4 +55,27 @@ public function testGetsSchemaGeneratorsOverrideByConfigWithEmptyArray(): void $this->assertCount(0, $generators); } + + public function testRegistryWithDefaultConfig(): void + { + $defaults = $this->getContainer()->get(Registry::class)->getDefaults(); + + $this->assertSame(Mapper::class, $defaults[SchemaInterface::MAPPER]); + $this->assertSame(Repository::class, $defaults[SchemaInterface::REPOSITORY]); + $this->assertSame(Source::class, $defaults[SchemaInterface::SOURCE]); + $this->assertNull($defaults[SchemaInterface::SCOPE]); + $this->assertNull($defaults[SchemaInterface::TYPECAST_HANDLER]); + } + + #[ConfigAttribute(path: 'cycle.schema.defaults', value: [SchemaInterface::TYPECAST_HANDLER => ['foo', 'bar']])] + public function testRegistryWithModifiedConfig(): void + { + $defaults = $this->getContainer()->get(Registry::class)->getDefaults(); + + $this->assertSame(Mapper::class, $defaults[SchemaInterface::MAPPER]); + $this->assertSame(Repository::class, $defaults[SchemaInterface::REPOSITORY]); + $this->assertSame(Source::class, $defaults[SchemaInterface::SOURCE]); + $this->assertNull($defaults[SchemaInterface::SCOPE]); + $this->assertSame(['foo', 'bar'], $defaults[SchemaInterface::TYPECAST_HANDLER]); + } } diff --git a/tests/src/LoggerFactoryTest.php b/tests/src/LoggerFactoryTest.php index f74bd4b..4a9b92f 100644 --- a/tests/src/LoggerFactoryTest.php +++ b/tests/src/LoggerFactoryTest.php @@ -16,6 +16,7 @@ final class LoggerFactoryTest extends BaseTest { protected ConfigsInterface $config; + private LogsInterface $logger; protected function setUp(): void {