| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace LaravelDoctrine\Fluent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Doctrine\Common\Persistence\Mapping\ClassMetadata; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Doctrine\ORM\Mapping\MappingException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use LaravelDoctrine\Fluent\Builders\Builder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use LaravelDoctrine\Fluent\Mappers\MapperSet; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class FluentDriver implements MappingDriver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @var MapperSet | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $mappers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @type callable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     protected $fluentFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * Initializes a new FileDriver that looks in the given path(s) for mapping | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * documents and operates in the specified operating mode. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param string[] $mappings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function __construct(array $mappings = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 4 |  |         $this->fluentFactory = function (ClassMetadata $metadata) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 4 |  |             return new Builder(new ClassMetadataBuilder($metadata)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 14 |  |         $this->mappers = new MapperSet(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 14 |  |         $this->addMappings($mappings); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 14 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * Loads the metadata for the specified class into the provided container. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param string        $className | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param ClassMetadata $metadata | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 6 |  |     public function loadMetadataForClass($className, ClassMetadata $metadata) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 6 |  |         $this->mappers->getMapperFor($className)->map( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 5 |  |             $this->getFluent($metadata) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 5 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 5 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * Gets the names of all mapped classes known to this driver. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @throws MappingException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @return string[]         The names of all mapped classes known to this driver. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 2 |  |     public function getAllClassNames() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 2 |  |         return $this->mappers->getClassNames(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * Returns whether the class with the specified name should have its metadata loaded. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * This is only the case if it is either mapped as an Entity or a MappedSuperclass. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @param string $className | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 4 |  |     public function isTransient($className) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         return | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 4 |  |             ! $this->mappers->hasMapperFor($className) || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 4 |  |             $this->mappers->getMapperFor($className)->isTransient(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @param string[] $mappings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 14 |  |     public function addMappings(array $mappings = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 14 |  |         foreach ($mappings as $class) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 4 |  |             if (!class_exists($class)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |                 throw new InvalidArgumentException("Mapping class [{$class}] does not exist"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 3 |  |             $mapping = new $class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 3 |  |             if (!$mapping instanceof Mapping) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 1 |  |                 throw new InvalidArgumentException("Mapping class [{$class}] should implement " . Mapping::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 2 |  |             $this->addMapping($mapping); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 14 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 14 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @param Mapping $mapping | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @throws MappingException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 9 |  |     public function addMapping(Mapping $mapping) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 9 |  |         $this->mappers->add($mapping); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 9 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @return MapperSet | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 5 |  |     public function getMappers() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 5 |  |         return $this->mappers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * Override the default Fluent factory method with a custom one. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * Use this to implement your own Fluent builder. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * The method will receive a ClassMetadata object as its only argument. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * @param callable $factory | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 125 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 126 | 1 |  |     public function setFluentFactory(callable $factory) | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 128 | 1 |  |         $this->fluentFactory = $factory; | 
            
                                                                        
                            
            
                                    
            
            
                | 129 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @param  ClassMetadata $metadata | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @return Fluent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 5 |  |     protected function getFluent(ClassMetadata $metadata) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 5 |  |         return call_user_func($this->fluentFactory, $metadata); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 139 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 140 |  |  |  |