Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions src/GedmoExtensionsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

namespace LaravelDoctrine\Extensions;

use Gedmo\DoctrineExtensions;
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Support\ServiceProvider;
use LaravelDoctrine\Fluent\Extensions\GedmoExtensions;
use LaravelDoctrine\Fluent\FluentDriver;

class GedmoExtensionsServiceProvider extends ServiceProvider
{
Expand All @@ -20,16 +23,38 @@ public function register(): void
foreach ($registry->getManagers() as $manager) {
$chain = $manager->getConfiguration()->getMetadataDriverImpl();

if ($this->needsAllMappings()) {
DoctrineExtensions::registerMappingIntoDriverChainORM($chain);
} else {
DoctrineExtensions::registerAbstractMappingIntoDriverChainORM($chain);
if (! $this->hasFluentDriver($chain)) {
continue;
}

$this->registerGedmoForFluent($chain);
}
});
}

private function needsAllMappings(): mixed
private function hasFluentDriver(MappingDriverChain $driver): bool
{
foreach ($driver->getDrivers() as $driver) {
if ($driver instanceof FluentDriver) {
return true;
}
}

return false;
}

/** @throws BindingResolutionException */
private function registerGedmoForFluent(MappingDriverChain $chain): void
{
if ($this->needsAllMappings()) {
GedmoExtensions::registerAll($chain);
} else {
GedmoExtensions::registerAbstract($chain);
}
}

/** @throws BindingResolutionException */
private function needsAllMappings(): bool
{
return $this->app->make('config')->get('doctrine.gedmo.all_mappings', false) === true;
}
Expand Down