Better configuration handling and abstraction

This commit is contained in:
Klaus Silveira
2012-07-15 01:50:38 -03:00
parent c85d32f15c
commit 6a0baffc9c
2 changed files with 68 additions and 22 deletions

View File

@@ -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(),

52
lib/GitList/Config.php Normal file
View File

@@ -0,0 +1,52 @@
<?php
namespace GitList;
class Config
{
protected $data;
public function __construct($file)
{
if (!file_exists($file)) {
die("Please, create the config.ini file.");
}
$this->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");
}
}
}