| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace Thinkone\NovaPageSettings\QueryAdapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Database\Query\Builder as QueryBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Support\Str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use ReflectionClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Finder\Finder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Thinkone\NovaPageSettings\Templates\SettingsTemplate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class InternalSettingsQueryBuilder extends QueryBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     protected array $_templates         = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     protected array $_templatesCompiled = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 | 3 |  |     protected function runSelect(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 3 |  |         if (empty($this->_templatesCompiled)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 3 |  |             $this->_templatesCompiled = collect($this->getTemplates()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 3 |  |                 /** @var SettingsTemplate $template */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 3 |  |                 ->map(function ($template, $id) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 3 |  |                     return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 3 |  |                         InternalSettingsModel::ATTR_CLASS => $template, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 3 |  |                         InternalSettingsModel::ATTR_ID    => method_exists($template, 'getID')?$template::getID():$id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 3 |  |                         InternalSettingsModel::ATTR_NAME  => $template::getName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 3 |  |                     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 3 |  |                 })->toArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 3 |  |         return $this->_templatesCompiled; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 34 | 1 |  |     public function getCountForPagination($columns = [ '*' ]): int | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 1 |  |         $templates = $this->getTemplates(); | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 1 |  |         return count($templates); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 3 |  |     public function getTemplates() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 3 |  |         if (empty($this->_templates)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 3 |  |             $namespace = app()->getNamespace(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 3 |  |             $templates = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 3 |  |             if(Str::startsWith($this->from, DIRECTORY_SEPARATOR)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 3 |  |                 $from = $this->from; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 $from = base_path($this->from); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 3 |  |             foreach (( new Finder )->in($from)->files() as $template) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 3 |  |                 preg_match('/\s*namespace\s?([A-Za-z\\\]*)\s*;\s*/is', file_get_contents($template->getPathname()), $matches); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 3 |  |                 if(isset($matches[1])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 3 |  |                     $namespace = '\\'.$matches[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 3 |  |                 $template = $namespace . '\\' . Str::before($template->getBasename(), '.'.$template->getExtension()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 3 |  |                 if (class_implements($template, SettingsTemplate::class) && | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 3 |  |                      !( new ReflectionClass($template) )->isAbstract()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 3 |  |                     $templates[] = $template; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 3 |  |             $this->_templates = $templates; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 3 |  |         return $this->_templates; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 70 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |  |