| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace A17\Twill; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use A17\Twill\Exceptions\NoCapsuleFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use A17\Twill\Helpers\Capsule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Support\Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Support\Str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class TwillCapsules | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * @var \A17\Twill\Helpers\Capsule[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     public $registeredCapsules = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     public function registerPackageCapsule( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         string $name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         string $namespace, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         string $path, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         string $singular = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         bool $enabled = true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     ): Capsule { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $capsule = new Capsule($name, $namespace, $path, $singular, $enabled, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $this->registerCapsule($capsule); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         return $this->registeredCapsules[$name]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function registerCapsule(Capsule $capsule): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->registeredCapsules[$capsule->name] = $capsule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * Generates a non package capsule object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     public function makeProjectCapsule(string $name): Capsule | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         return new Capsule($name, $this->capsuleNamespace($name), config("twill.capsules.path") . '/' . $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @throws \A17\Twill\Exceptions\NoCapsuleFoundException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     public function getCapsuleForModule(string $module): Capsule | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $capsule = $this->getRegisteredCapsules()->first(function (Capsule $capsule) use ($module) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             return $capsule->getModule() === $module; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         if (!$capsule) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             throw new NoCapsuleFoundException($module); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         return $capsule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @throws \A17\Twill\Exceptions\NoCapsuleFoundException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 62 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     public function getCapsuleForModel(string $model): Capsule | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         $capsule = $this->getRegisteredCapsules()->first(function (Capsule $capsule) use ($model) { | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |             return $capsule->getSingular() === $model; | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         }); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         if (!$capsule) { | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |             throw new NoCapsuleFoundException($model); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         return $capsule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @return Collection<Capsule> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     public function getRegisteredCapsules(): Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $this->loadProjectCapsules(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         return collect($this->registeredCapsules); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     public function loadProjectCapsules(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $path = config('twill.capsules.path'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $list = collect(config('twill.capsules.list')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $list | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             ->where('enabled', true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             ->map(function ($capsule) use ($path) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 $this->registerCapsule( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                     new Capsule( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                         $capsule['name'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                         $this->capsuleNamespace($capsule['name']), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                         $path . '/' . $capsule['name'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                         $capsule['singular'] ?? null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                         $capsule['enabled'] ?? true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     public function capsuleNamespace($capsuleName, $type = null): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         // @todo: Read from capsules to get this data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $base = config('twill.capsules.namespaces.base'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $type = config("twill.capsules.namespaces.$type"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         return "$base\\$capsuleName" . (filled($type) ? "\\$type" : ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     public function capsuleNamespaceToPath( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $namespace, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $capsuleNamespace, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $rootPath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     ): string { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $namespace = Str::after($namespace, $capsuleNamespace . '\\'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         return "$rootPath/{$this->getProjectCapsulesSubdirectory()}" . str_replace('\\', '/', $namespace); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     public function getProjectCapsulesPath() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         return config("twill.capsules.path") . $this->getProjectCapsulesSubdirectory(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     private function getProjectCapsulesSubdirectory(): string { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         $subdirectory = config('twill.capsules.namespaces.subdir'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         return filled($subdirectory) ? "$subdirectory/" : ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     public function getAutoloader() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         return app()->bound('autoloader') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             ? app('autoloader') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             : require base_path('vendor/autoload.php'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 142 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 143 |  |  |  |