src/Config.php
6a0baffc
 <?php
 
 namespace GitList;
 
 class Config
 {
     protected $data;
 
d5f2ae5a
     public function __construct($data = array())
cc1dc5af
     {
         $this->data = $data;
     }
 
132ba426
     public static function fromFile($file)
     {
6a0baffc
         if (!file_exists($file)) {
a83fbfdb
             die(sprintf('Please, create the %1$s file.', $file));
6a0baffc
         }
132ba426
 
e64e24e8
         $data = parse_ini_file($file, true);
5122e91b
         $config = new static($data);
         $config->validateOptions();
fc32fd68
 
5122e91b
         return $config;
87d6391b
     }
6a0baffc
 
     public function get($section, $option)
     {
         if (!array_key_exists($section, $this->data)) {
             return false;
         }
 
         if (!array_key_exists($option, $this->data[$section])) {
             return false;
         }
 
         return $this->data[$section][$option];
     }
 
     public function getSection($section)
     {
         if (!array_key_exists($section, $this->data)) {
             return false;
         }
 
         return $this->data[$section];
     }
 
     public function set($section, $option, $value)
     {
         $this->data[$section][$option] = $value;
     }
 
     protected function validateOptions()
     {
e64e24e8
         $repositories = $this->get('git', 'repositories');
 
         $atLeastOneOk = false;
         $atLeastOneWrong = false;
ccdee643
 
e64e24e8
         foreach ($repositories as $directory) {
             if (!$directory || !is_dir($directory)) {
                 $atLeastOneWrong = true;
a22760d5
             } else {
e64e24e8
                 $atLeastOneOk = true;
a22760d5
             }
6a0baffc
         }
ccdee643
 
e64e24e8
         if (!$atLeastOneOk) {
cc1dc5af
             die('Please, edit the config file and provide your repositories directory');
a22760d5
         }
ccdee643
 
e64e24e8
         if ($atLeastOneWrong) {
cc1dc5af
             die('One or more of the supplied repository paths appears to be wrong. Please, check the config file');
a22760d5
         }
6a0baffc
     }
90d9fd49
 }