diff --git a/src/LaravelSpatialServiceProvider.php b/src/LaravelSpatialServiceProvider.php index 684c3e9..7bccb2f 100644 --- a/src/LaravelSpatialServiceProvider.php +++ b/src/LaravelSpatialServiceProvider.php @@ -8,7 +8,7 @@ use ASanikovich\LaravelSpatial\Exceptions\LaravelSpatialException; use Doctrine\DBAL\Types\Type; use Illuminate\Database\DatabaseServiceProvider; -use Illuminate\Support\Facades\DB; +use RuntimeException; use Throwable; final class LaravelSpatialServiceProvider extends DatabaseServiceProvider @@ -28,9 +28,7 @@ public function boot(): void $this->validateConfig(); - if (DB::connection()->isDoctrineAvailable()) { - $this->registerDoctrineTypes(); - } + $this->registerDoctrineTypes(); } /** @@ -45,18 +43,6 @@ private function registerDoctrineTypes(): void $this->registerDoctrineType(GeometryType::GEOMETRY_COLLECTION->getDoctrineClassName(), 'geomcollection'); } - /** - * @param class-string $class - * - * @throws Throwable - */ - private function registerDoctrineType(string $class, string $type): void - { - DB::registerDoctrineType($class, $type, $type); - - DB::connection()->registerDoctrineType($class, $type, $type); - } - /** * @throws LaravelSpatialException */ @@ -85,4 +71,24 @@ private function validateConfig(): void } } } + + private function isDoctrineAvailable(): bool + { + return class_exists('Doctrine\DBAL\Connection'); + } + + private function registerDoctrineType(Type|string $class, string $name): void + { + if (! $this->isDoctrineAvailable()) { + throw new RuntimeException( + 'Registering a custom Doctrine type requires Doctrine DBAL (doctrine/dbal).' + ); + } + + if (! Type::hasType($name)) { + /** @var Type $type */ + $type = is_string($class) ? new $class : $class; + Type::getTypeRegistry()->register($name, $type); + } + } }