| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace A17\Twill; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use A17\Twill\Services\Capsules\HasCapsules; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use A17\Twill\Services\Capsules\Manager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use A17\Twill\Services\Routing\HasRoutes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Foundation\Support\Providers\RouteServiceProvider; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class CapsulesServiceProvider extends RouteServiceProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     use HasRoutes, HasCapsules; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     public static $capsulesBootstrapped = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     protected $manager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected function mergeTwillConfig() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $this->mergeConfigFrom( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |             __DIR__ . '/../config/capsules.php', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |             'twill.capsules' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $this->app | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             ->make('config') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             ->set('twill.capsules.list', $this->getCapsuleList()->toArray()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->app->make('config')->set('twill.capsules.loaded', true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function register() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->registerManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->mergeTwillConfig(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->bootCapsules(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     public function boot() | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         $this->registerCapsules(); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         $this->registerViewPaths(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     public function registerCapsules() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $this->manager->getCapsuleList()->map(function ($capsule) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $this->registerCapsule($capsule); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * Boot the capsules so their psr, config and service providers are booted. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @see HasCapsules::bootstrapCapsule | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     public function bootCapsules() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         if (!self::$capsulesBootstrapped) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $this->getCapsuleList() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 ->where('enabled', true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 ->each(function ($capsule) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                     $this->bootstrapCapsule($capsule); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             self::$capsulesBootstrapped = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     protected function registerCapsule($capsule) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $this->loadMigrationsFrom($capsule['migrations_dir']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $this->loadTranslationsFrom($capsule['lang_dir'], 'twill:capsules:' . $capsule['module']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     public function registerViewPaths() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         if (file_exists(config('twill.capsules.path'))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $callback = function ($view) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 $view->addLocation(config('twill.capsules.path')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $this->app->afterResolving('view', $callback); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             if ($this->app->resolved('view')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 $callback($this->app->make('view'), $this->app); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     public function registerManager() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $this->app->instance( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             'twill.capsules.manager', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             $this->manager = new Manager() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     public function testCanMakeCapsule() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $this->assertExitCodeIsGood( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             $this->artisan('twill:make:capsule', [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 'moduleName' => 'Cars', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 '--hasBlocks' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 '--hasTranslation' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 '--hasSlug' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 '--hasMedias' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 '--hasFiles' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 '--hasPosition' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                 '--hasRevisions' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             ])->run() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $this->assertFileExists( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             twill_path('Twill/Capsules/Cars/app/Models/Car.php') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $this->assertIsObject( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             $this->app->make('App\Twill\Capsules\Cars\Models\Car') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 121 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |  | 
            
                        
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: