| 3241 | rexy | 1 | <?php
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 | namespace mbolli\nfsen_ng\common;
 | 
        
           |  |  | 4 |   | 
        
           |  |  | 5 | use mbolli\nfsen_ng\datasources\Datasource;
 | 
        
           |  |  | 6 | use mbolli\nfsen_ng\processor\Processor;
 | 
        
           |  |  | 7 |   | 
        
           |  |  | 8 | abstract class Config {
 | 
        
           |  |  | 9 |     public const VERSION = 'v0.3';
 | 
        
           |  |  | 10 |     /**
 | 
        
           |  |  | 11 |      * @var array{
 | 
        
           |  |  | 12 |      *     general: array{ports: int[], sources: string[], db: string, processor: string},
 | 
        
           |  |  | 13 |      *     frontend: array{reload_interval: int, defaults: array<string, array>},
 | 
        
           |  |  | 14 |      *     nfdump: array{binary: string, profiles-data: string, profile: string, max-processes: int},
 | 
        
           |  |  | 15 |      *     db: array<string, array>,
 | 
        
           |  |  | 16 |      *     log: array{priority: int}
 | 
        
           |  |  | 17 |      *     }|array{}
 | 
        
           |  |  | 18 |      */
 | 
        
           |  |  | 19 |     public static array $cfg = [];
 | 
        
           |  |  | 20 |     public static string $path;
 | 
        
           |  |  | 21 |     public static Datasource $db;
 | 
        
           |  |  | 22 |     public static Processor $processorClass;
 | 
        
           |  |  | 23 |     private static bool $initialized = false;
 | 
        
           |  |  | 24 |   | 
        
           |  |  | 25 |     private function __construct() {}
 | 
        
           |  |  | 26 |   | 
        
           |  |  | 27 |     public static function initialize(bool $initProcessor = false): void {
 | 
        
           |  |  | 28 |         global $nfsen_config;
 | 
        
           |  |  | 29 |         if (self::$initialized === true) {
 | 
        
           |  |  | 30 |             return;
 | 
        
           |  |  | 31 |         }
 | 
        
           |  |  | 32 |   | 
        
           |  |  | 33 |         $settingsFile = \dirname(__DIR__) . \DIRECTORY_SEPARATOR . 'settings' . \DIRECTORY_SEPARATOR . 'settings.php';
 | 
        
           |  |  | 34 |         if (!file_exists($settingsFile)) {
 | 
        
           |  |  | 35 |             throw new \Exception('No settings.php found. Did you rename the distributed settings correctly?');
 | 
        
           |  |  | 36 |         }
 | 
        
           |  |  | 37 |   | 
        
           |  |  | 38 |         include $settingsFile;
 | 
        
           |  |  | 39 |   | 
        
           |  |  | 40 |         self::$cfg = $nfsen_config;
 | 
        
           |  |  | 41 |         self::$path = \dirname(__DIR__);
 | 
        
           |  |  | 42 |         self::$initialized = true;
 | 
        
           |  |  | 43 |   | 
        
           |  |  | 44 |         // find data source
 | 
        
           |  |  | 45 |         $dbClass = 'mbolli\\nfsen_ng\\datasources\\' . ucfirst(mb_strtolower(self::$cfg['general']['db']));
 | 
        
           |  |  | 46 |         if (class_exists($dbClass)) {
 | 
        
           |  |  | 47 |             self::$db = new $dbClass();
 | 
        
           |  |  | 48 |         } else {
 | 
        
           |  |  | 49 |             throw new \Exception('Failed loading class ' . self::$cfg['general']['db'] . '. The class doesn\'t exist.');
 | 
        
           |  |  | 50 |         }
 | 
        
           |  |  | 51 |   | 
        
           |  |  | 52 |         // find processor
 | 
        
           |  |  | 53 |         $processorClass = \array_key_exists('processor', self::$cfg['general']) ? ucfirst(mb_strtolower(self::$cfg['general']['processor'])) : 'Nfdump';
 | 
        
           |  |  | 54 |         $processorClass = 'mbolli\\nfsen_ng\\processor\\' . $processorClass;
 | 
        
           |  |  | 55 |         if (!class_exists($processorClass)) {
 | 
        
           |  |  | 56 |             throw new \Exception('Failed loading class ' . $processorClass . '. The class doesn\'t exist.');
 | 
        
           |  |  | 57 |         }
 | 
        
           |  |  | 58 |   | 
        
           |  |  | 59 |         if (!\in_array(Processor::class, class_implements($processorClass), true)) {
 | 
        
           |  |  | 60 |             throw new \Exception('Processor class ' . $processorClass . ' doesn\'t implement ' . Processor::class . '.');
 | 
        
           |  |  | 61 |         }
 | 
        
           |  |  | 62 |   | 
        
           |  |  | 63 |         if ($initProcessor === true) {
 | 
        
           |  |  | 64 |             self::$processorClass = new $processorClass();
 | 
        
           |  |  | 65 |         }
 | 
        
           |  |  | 66 |     }
 | 
        
           |  |  | 67 | }
 |