| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace JeroenNoten\LaravelAdminLte\Console\PackageResources; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Support\Facades\File; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use JeroenNoten\LaravelAdminLte\Helpers\CommandHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class BladeComponentsResource extends PackageResource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * Create a new package resource instance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 14 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 15 | 30 |  |     public function __construct() | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |         // Fill the resource data. | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 19 | 30 |  |         $this->description = 'The set of blade components provided by this package'; | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 | 30 |  |         $this->source = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 22 | 30 |  |             'classes' => CommandHelper::getPackagePath('src/View/Components'), | 
            
                                                                        
                            
            
                                    
            
            
                | 23 | 30 |  |             'views' => CommandHelper::getPackagePath('resources/views/components'), | 
            
                                                                        
                            
            
                                    
            
            
                | 24 | 30 |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 | 30 |  |         $this->target = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 27 | 30 |  |             'classes' => app_path('View/Components/Adminlte'), | 
            
                                                                        
                            
            
                                    
            
            
                | 28 | 30 |  |             'views' => CommandHelper::getViewPath('vendor/adminlte/components'), | 
            
                                                                        
                            
            
                                    
            
            
                | 29 | 30 |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 | 30 |  |         $this->required = false; | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         // Fill the set of installation messages. | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 | 30 |  |         $this->messages = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 30 |  |             'install' => 'Do you want to publish the blade component files?', | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 30 |  |             'overwrite' => 'Blade components were already published. Want to replace?', | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 30 |  |             'success' => 'Blade components files published successfully', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 30 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * Installs or publishes the resource. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 7 |  |     public function install() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         // Copy the component classes to the publishing destination. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 7 |  |         CommandHelper::CopyDirectory( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 7 |  |             $this->source['classes'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 7 |  |             $this->target['classes'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 7 |  |             true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 7 |  |             true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 7 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         // Copy the component views to the publishing destination. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 7 |  |         CommandHelper::CopyDirectory( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 7 |  |             $this->source['views'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 7 |  |             $this->target['views'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 7 |  |             true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 7 |  |             true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 7 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         // Adapt published components classes to the Laravel framework, this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         // will mainly change the component classes namespace. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 7 |  |         $this->adaptPublishedComponentClasses(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * Uninstalls the resource. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 7 |  |     public function uninstall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         // Remove the component classes from the target folder. When | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         // component classes does not exists, we consider they as uninstalled. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 7 |  |         if (File::isDirectory($this->target['classes'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 7 |  |             File::deleteDirectory($this->target['classes']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         // Remove the component views from the target folder. When | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         // component views does not exists, we consider they as uninstalled. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 7 |  |         if (File::isDirectory($this->target['views'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 7 |  |             File::deleteDirectory($this->target['views']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * Checks whether the resource already exists in the target location. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 5 |  |     public function exists() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 5 |  |         return File::isDirectory($this->target['classes']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 5 |  |             || File::isDirectory($this->target['views']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * Checks whether the resource is correctly installed, i.e. if the source | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * items matches with the items available at the target location. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 8 |  |     public function installed() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         // Note we can't expect the published component classes to match | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         // exactly with the default package ones, since the namespace of the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         // classes are changed during the installation process. So, we'll just | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         // control that the number of published component classes matches with | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         // the packages default ones. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 8 |  |         $srcClassesNum = count(File::allFiles($this->source['classes'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 8 |  |         $tgtClassesNum = File::isDirectory($this->target['classes']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 5 |  |             ? count(File::allFiles($this->target['classes'])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 7 |  |             : 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 8 |  |         return $srcClassesNum === $tgtClassesNum | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 8 |  |             && CommandHelper::compareDirectories( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 8 |  |                 $this->source['views'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 8 |  |                 $this->target['views'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 8 |  |                 true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 8 |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * Adapt the published blade component classes by changing their namespace. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 7 |  |     protected function adaptPublishedComponentClasses() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         // Get an array of all published component classes files. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 7 |  |         $files = File::allFiles($this->target['classes']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         // Make replacements on each of the collected files. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 7 |  |         foreach ($files as $file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 | 7 |  |             $content = File::get($file->getPathname()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             // Replace the namespace. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 7 |  |             $content = str_replace( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 7 |  |                 'namespace JeroenNoten\LaravelAdminLte\View\Components', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 7 |  |                 'namespace App\View\Components\Adminlte', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 7 |  |                 $content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 7 |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |             // Put the new content in the file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 7 |  |             File::put($file->getPathname(), $content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 163 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 164 |  |  |  |