| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace uuf6429\ElderBrother; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Psr\Log\LoggerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Psr\Log\NullLogger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use uuf6429\ElderBrother\Action\ActionAbstract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class Config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * @var string[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     protected $paths; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @var LoggerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     protected $logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @param string[]             $paths | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param LoggerInterface|null $logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function __construct($paths, LoggerInterface $logger = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->paths = $paths; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->logger = $logger ?: new NullLogger(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @return self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     protected function load() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->config = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         foreach ($this->paths as $path) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             if (file_exists($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 $this->logger->debug('Loading config file: ' . $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 $config = include $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 foreach ($config as $event => $prioritizedActions) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                     // merge config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                     $this->config[$event] = array_merge( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                         isset($this->config[$event]) ? $this->config[$event] : [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                         $prioritizedActions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                     // reorder actions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                     ksort($this->config[$event]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 $this->logger->debug('Config file does not exist: ' . $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $this->logger->debug('Configuration loaded.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public function getAll() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         if (is_null($this->config)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $this->load(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         return (array) $this->config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @param string $event | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @param bool   $supportedOnly | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @return ActionAbstract[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     public function get($event, $supportedOnly = true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         if (is_null($this->config)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             $this->load(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $config = isset($this->config[$event]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             ? array_values($this->config[$event]) : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         if ($supportedOnly) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             $config = array_filter( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 $config, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 function (ActionAbstract $action) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                     try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                         $action->checkSupport(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                     } catch (\Exception $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                         $this->logger->warning( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                             sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                                 '%s is not supported: %s.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                                 $action->getName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                                 $ex->getMessage() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         return $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @return LoggerInterface | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 122 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 123 |  |  |     public function getLog() | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 125 |  |  |         return $this->logger; | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 127 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 128 |  |  |  |