| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace MichaelRubel\AutoBinder\Traits; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Support\Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Support\Facades\File; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Illuminate\Support\LazyCollection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Illuminate\Support\Str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\Finder\SplFileInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | trait BindsToContainer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * Run the directory scanning & bind the results. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 12 |  |     protected function scan(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 12 |  |         $this->getFolderFiles()->each(function (SplFileInfo $file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 11 |  |             $relativePath             = $file->getRelativePathname(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 11 |  |             $filenameWithoutExtension = $file->getFilenameWithoutExtension(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 11 |  |             $filenameWithRelativePath = $this->prepareFilename($relativePath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 11 |  |             $interface = $this->interfaceFrom($filenameWithoutExtension); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 11 |  |             $concrete  = $this->concreteFrom($filenameWithRelativePath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 11 |  |             if (! interface_exists($interface) || ! class_exists($concrete)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 9 |  |                 return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 11 |  |             $dependencies = collect($this->dependencies); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 11 |  |             $concrete = match (true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 11 |  |                 $dependencies->has($interface) => $dependencies->get($interface), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 10 |  |                 $dependencies->has($concrete)  => $dependencies->get($concrete), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 9 |  |                 default                        => $concrete, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 11 |  |             app()->{$this->bindingType}($interface, $concrete); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * Get the folder files except for ignored ones. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @return LazyCollection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 12 |  |     protected function getFolderFiles(): LazyCollection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 12 |  |         return LazyCollection::make(File::directories(base_path($this->basePath . DIRECTORY_SEPARATOR . $this->classFolder))) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 11 |  |             ->reject(fn ($folder) => in_array(basename($folder), $this->excludesFolders)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 11 |  |             ->map(fn ($folder) => File::allFiles($folder)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 11 |  |             ->flatten(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * Prepare the filename. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param string $filename | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 65 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 66 | 11 |  |     protected function prepareFilename(string $filename): string | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 11 |  |         return (string) Str::of($filename) | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 11 |  |             ->replace('/', '\\') | 
            
                                                                        
                            
            
                                    
            
            
                | 70 | 11 |  |             ->substr(0, (int) strrpos($filename, '.')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * Get the namespace from a given path. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 11 |  |     protected function namespaceFrom(string $path): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 11 |  |         return (string) Str::of($path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 11 |  |             ->replace('/', '\\') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 11 |  |             ->ucfirst(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * Get the concrete from filename. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @param string $filenameWithRelativePath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 11 |  |     protected function concreteFrom(string $filenameWithRelativePath): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 11 |  |         return $this->classNamespace . '\\' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 11 |  |             . $this->classFolder . '\\' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 11 |  |             . $this->prepareNamingFor($filenameWithRelativePath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * Get the interface from filename. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @param string $filenameWithoutExtension | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 11 |  |     protected function interfaceFrom(string $filenameWithoutExtension): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 11 |  |         $guessedInterface = $this->guessInterfaceBy($filenameWithoutExtension); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 11 |  |         if (is_null($guessedInterface)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 4 |  |             return $this->buildInterfaceBy($filenameWithoutExtension); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 7 |  |         return $guessedInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * Guess the interface with a given filename. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @param string $filenameWithoutExtension | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * @return string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 11 |  |     protected function guessInterfaceBy(string $filenameWithoutExtension): ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 11 |  |         if (! Str::contains($this->interfaceNamespace, '\\')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 7 |  |             return $this->buildInterfaceFromClassBy($filenameWithoutExtension); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 4 |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * Build the interface class-string. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * @param string $filename | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 4 |  |     protected function buildInterfaceBy(string $filename): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 4 |  |         return $this->interfaceNamespace . '\\' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 4 |  |             . $this->prepareNamingFor($filename) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 4 |  |             . ($this->interfaceNaming); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * Build the interface class-string based on the class folder. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * @param string $filename | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 7 |  |     protected function buildInterfaceFromClassBy(string $filename): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 7 |  |         return $this->classNamespace . '\\' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 7 |  |             . $this->classFolder . '\\' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 7 |  |             . $this->interfaceNamespace . '\\' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 7 |  |             . $this->prepareNamingFor($filename) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 7 |  |             . ($this->interfaceNaming); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * Cleans up filename to append the desired interface name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      * @param string $filename | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 | 11 |  |     protected function prepareNamingFor(string $filename): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 11 |  |         return Str::replace($this->interfaceNaming, '', $filename); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 176 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 177 |  |  |  |