diff --git a/index.php b/index.php index 6618e3e..ce98b4e 100644 --- a/index.php +++ b/index.php @@ -5,34 +5,27 @@ * https://github.com/klaussilveira/gitlist */ -if (!file_exists('config.ini')) { - die("Please, create the config.ini file."); -} - -$config = parse_ini_file('config.ini', true); - -if (empty($config['git']['repositories']) || !is_dir($config['git']['repositories'])) { - die("Please, edit the config.ini file and provide your repositories directory"); -} - require 'vendor/autoload.php'; +// Load configuration +$config = new GitList\Config('config.ini'); +$config->set('git', 'repositories', rtrim($config->get('git', 'repositories'), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR); + +// Startup and configure Silex application $app = new Silex\Application(); -$app['debug'] = isset($config['app']['debug']) && $config['app']['debug']; -$app['filetypes'] = $config['filetypes']; -$app['hidden'] = isset($config['git']['hidden']) ? $config['git']['hidden'] : array(); -$config['git']['repositories'] = rtrim($config['git']['repositories'], DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; +$app['debug'] = $config->get('app', 'debug'); +$app['filetypes'] = $config->getSection('filetypes'); +$app['hidden'] = $config->get('git', 'hidden') ? $config->get('git', 'hidden') : array(); +$app['cache.archives'] = __DIR__ . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'archives'; -$app['cache.archives'] = __DIR__.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR.'archives'; - -// Register Git and Twig service providersclass_path +// Register services $app->register(new Silex\Provider\TwigServiceProvider(), array( - 'twig.path' => __DIR__.'/views', - 'twig.options' => array('cache' => __DIR__.'/cache'), + 'twig.path' => __DIR__ . '/views', + 'twig.options' => array('cache' => __DIR__ . '/cache'), )); $app->register(new GitList\Provider\GitServiceProvider(), array( - 'git.client' => $config['git']['client'], - 'git.repos' => $config['git']['repositories'], + 'git.client' => $config->get('git', 'client'), + 'git.repos' => $config->get('git', 'repositories'), )); $app->register(new GitList\Provider\ViewUtilServiceProvider()); $app->register(new GitList\Provider\RepositoryUtilServiceProvider()); @@ -43,12 +36,13 @@ $app['twig'] = $app->share($app->extend('twig', function($twig, $app) { return $twig; })); +// Mount the controllers $app->mount('', new GitList\Controller\MainController()); $app->mount('', new GitList\Controller\BlobController()); $app->mount('', new GitList\Controller\CommitController()); $app->mount('', new GitList\Controller\TreeController()); -// Error handling +// Handle errors $app->error(function (\Exception $e, $code) use ($app) { return $app['twig']->render('error.twig', array( 'message' => $e->getMessage(), diff --git a/lib/GitList/Config.php b/lib/GitList/Config.php new file mode 100644 index 0000000..e214340 --- /dev/null +++ b/lib/GitList/Config.php @@ -0,0 +1,52 @@ +data = parse_ini_file('config.ini', true); + $this->validateOptions(); + } + + 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() + { + if (!$this->get('git', 'repositories') || !is_dir($this->get('git', 'repositories'))) { + die("Please, edit the config.ini file and provide your repositories directory"); + } + } +} \ No newline at end of file