|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Gedmo; |
| 4 | + |
| 5 | +use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver; |
| 6 | +use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain; |
| 7 | +use Gedmo\Mapping\Driver; |
| 8 | +use LaravelDoctrine\Fluent\Extensions\ExtensibleClassMetadata; |
| 9 | +use LaravelDoctrine\Fluent\FluentDriver; |
| 10 | + |
| 11 | +abstract class FluentExtension implements Driver |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var FluentDriver |
| 15 | + */ |
| 16 | + protected $originalDriver; |
| 17 | + |
| 18 | + /** |
| 19 | + * @return string |
| 20 | + */ |
| 21 | + abstract protected function getExtensionName(); |
| 22 | + |
| 23 | + /** |
| 24 | + * Read extended metadata configuration for |
| 25 | + * a single mapped class |
| 26 | + * |
| 27 | + * @param ExtensibleClassMetadata $meta |
| 28 | + * @param array $config |
| 29 | + * |
| 30 | + * @return void |
| 31 | + */ |
| 32 | + public function readExtendedMetadata($meta, array &$config) |
| 33 | + { |
| 34 | + if (! $meta instanceof ExtensibleClassMetadata) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + $config = array_merge_recursive($config, $meta->getExtension( |
| 39 | + $this->getExtensionName() |
| 40 | + )); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Make sure the original driver is Fluent. |
| 45 | + * |
| 46 | + * @param MappingDriver $driver |
| 47 | + * @return void |
| 48 | + */ |
| 49 | + public function setOriginalDriver($driver) |
| 50 | + { |
| 51 | + $this->originalDriver = $this->extractFluentDriver($driver); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @param MappingDriver $driver |
| 56 | + * |
| 57 | + * @return FluentDriver |
| 58 | + */ |
| 59 | + private function extractFluentDriver(MappingDriver $driver) |
| 60 | + { |
| 61 | + if ($driver instanceof FluentDriver) { |
| 62 | + return $driver; |
| 63 | + } |
| 64 | + |
| 65 | + if ($driver instanceof MappingDriverChain) { |
| 66 | + $default = $driver->getDefaultDriver(); |
| 67 | + if ($default instanceof FluentDriver) { |
| 68 | + return $default; |
| 69 | + } |
| 70 | + |
| 71 | + foreach ($driver->getDrivers() as $namespace => $driver) { |
| 72 | + if ($driver instanceof FluentDriver) { |
| 73 | + return $driver; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + throw new \UnexpectedValueException("Fluent driver not found in the driver chain."); |
| 79 | + } |
| 80 | +} |
0 commit comments