| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Spatie\BladeX; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Support\Str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Support\Facades\File; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Finder\SplFileInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Spatie\BladeX\ComponentDirectory\RegularDirectory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Spatie\BladeX\Exceptions\CouldNotRegisterComponent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Spatie\BladeX\ComponentDirectory\NamespacedDirectory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class BladeX | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** @var array */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     public $registeredComponents = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $prefix = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @param string|string[] $view | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @param string $tag | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @return null|\Spatie\BladeX\Component | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public function component($view, string $tag = ''): ?Component | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         if (is_iterable($view)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             $this->registerViews($view); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         if ($view instanceof Component) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             $this->registeredComponents[] = $view; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             return $view; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         if (! is_string($view)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             throw CouldNotRegisterComponent::invalidArgument(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         if (Str::endsWith($view, '*')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             $this->registerComponents($view); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         if (! view()->exists($view)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             throw CouldNotRegisterComponent::viewNotFound($view, $tag); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $component = new Component($view, $tag); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $this->registeredComponents[] = $component; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         return $component; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param string|string[] $viewDirectory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @return \Spatie\BladeX\ComponentCollection|\Spatie\BladeX\Component[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     public function components($viewDirectory): ComponentCollection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         if (is_iterable($viewDirectory)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $components = new ComponentCollection(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             foreach($viewDirectory as $singleViewDirectory) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 if(Str::endsWith($singleViewDirectory, '*')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                     $components = $components->merge($this->registerComponents($singleViewDirectory)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                     $components->push($this->component($singleViewDirectory)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             return $components; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         return $this->registerComponents($viewDirectory); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @return \Spatie\BladeX\Component[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     public function registeredComponents(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         return collect($this->registeredComponents)->reverse()->unique(function (Component $component) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             return $component->getTag(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         })->reverse()->values()->all(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |     public function prefix(string $prefix = ''): self | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |         $this->prefix = $prefix; | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     public function getPrefix(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         return empty($this->prefix) ? '' : Str::finish($this->prefix, '-'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @internal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @param string $viewDirectory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @return \Spatie\BladeX\ComponentCollection|\Spatie\BladeX\Component[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     public function registerComponents(string $viewDirectory): ComponentCollection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         if (! Str::endsWith($viewDirectory, '*')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             throw CouldNotRegisterComponent::viewDirectoryWithoutWildcard($viewDirectory); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $componentDirectory = Str::contains($viewDirectory, '::') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             ? new NamespacedDirectory($viewDirectory) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             : new RegularDirectory($viewDirectory); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         return $this->registerViews( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             ComponentCollection::make(File::files($componentDirectory->getAbsoluteDirectory())) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                 ->filter(function (SplFileInfo $file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                     return Str::endsWith($file->getFilename(), '.blade.php'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                 ->map(function (SplFileInfo $file) use ($componentDirectory) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                     return $componentDirectory->getViewName($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                 }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * @param iterable|string[] $views | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * @return \Spatie\BladeX\ComponentCollection|\Spatie\BladeX\Component[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     protected function registerViews(iterable $views): ComponentCollection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         return ComponentCollection::make($views)->map(function (string $viewName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             return $this->component($viewName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 146 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 147 |  |  |  | 
            
                        
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.