| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Wandu\Service\Eloquent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Illuminate\Database\Capsule\Manager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Database\Schema\Blueprint; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Psr\Container\ContainerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Wandu\Migrator\Configuration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Wandu\Migrator\Contracts\Adapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Wandu\Migrator\Contracts\Migration; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class EloquentAdapter implements Adapter   | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** @var \Illuminate\Database\Capsule\Manager */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     protected $manager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** @var \Psr\Container\ContainerInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** @var \Wandu\Migrator\Configuration */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     public function __construct(Manager $manager, ContainerInterface $container, Configuration $config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $this->manager = $manager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $this->container = $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $this->config = $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function initialize() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         if (!$this->hasMigrateTable()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             $this->getConnection()->getSchemaBuilder()->create($this->config->getTable(), function (Blueprint $table) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                 $table->string('version', 30); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                 $table->index('version'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     public function getAppliedIds(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         if (!$this->hasMigrateTable()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         return $this->createMigrationQuery()->get()->map(function ($item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             return $item->version; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         })->toArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     public function isApplied($id): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         if (!$this->hasMigrateTable()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         foreach ($this->getAppliedIds() as $appliedId) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             if ($id == $appliedId) return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     public function getMigrationInstance(string $id, string $name): Migration | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         return $this->container->get($name); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     public function addToMigrationTable($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $this->createMigrationQuery()->insert([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             'version' => $id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     public function removeFromMigrationTable($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $this->createMigrationQuery()->where([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             'version' => $id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         ])->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     protected function hasMigrateTable() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             $this->createMigrationQuery()->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         } catch (\PDOException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @return \Illuminate\Database\Query\Builder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     protected function createMigrationQuery() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         return $this->getConnection()->table($this->config->getTable()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @return \Illuminate\Database\Connection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     protected function getConnection() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         return $this->manager->getConnection($this->config->getConnection()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 122 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 123 |  |  |  | 
            
                        
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: