Skip to content

Commit

Permalink
l11
Browse files Browse the repository at this point in the history
  • Loading branch information
asanikovich committed Sep 29, 2024
1 parent 2f12381 commit abbea2e
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions src/LaravelSpatialServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,9 +28,7 @@ public function boot(): void

$this->validateConfig();

if (DB::connection()->isDoctrineAvailable()) {
$this->registerDoctrineTypes();
}
$this->registerDoctrineTypes();
}

/**
Expand All @@ -45,18 +43,6 @@ private function registerDoctrineTypes(): void
$this->registerDoctrineType(GeometryType::GEOMETRY_COLLECTION->getDoctrineClassName(), 'geomcollection');
}

/**
* @param class-string<Type> $class
*
* @throws Throwable
*/
private function registerDoctrineType(string $class, string $type): void
{
DB::registerDoctrineType($class, $type, $type);

DB::connection()->registerDoctrineType($class, $type, $type);
}

/**
* @throws LaravelSpatialException
*/
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit abbea2e

Please sign in to comment.