Refactor Configuration

This commit is contained in:
Romain Neutron
2012-02-22 18:05:35 +01:00
parent ecc8feb143
commit aa9d5171b0
12 changed files with 170 additions and 350 deletions

View File

@@ -79,11 +79,7 @@ class appbox extends base
$this->registry = $registry; $this->registry = $registry;
$this->session = Session_Handler::getInstance($this); $this->session = Session_Handler::getInstance($this);
$handler = new \Alchemy\Phrasea\Core\Configuration\Handler( $configuration = \Alchemy\Phrasea\Core\Configuration::build();
new \Alchemy\Phrasea\Core\Configuration\Application(),
new \Alchemy\Phrasea\Core\Configuration\Parser\Yaml()
);
$configuration = new \Alchemy\Phrasea\Core\Configuration($handler);
$choosenConnexion = $configuration->getPhraseanet()->get('database'); $choosenConnexion = $configuration->getPhraseanet()->get('database');
@@ -420,7 +416,7 @@ class appbox extends base
* @param type $write_file * @param type $write_file
* @return type * @return type
*/ */
public static function create(registryInterface &$registry, connection_interface $conn, $dbname, $write_file = false) public static function create(\Alchemy\Phrasea\Core $Core, registryInterface &$registry, connection_interface $conn, $dbname, $write_file = false)
{ {
$credentials = $conn->get_credentials(); $credentials = $conn->get_credentials();
@@ -441,6 +437,8 @@ class appbox extends base
$key = $key == 'hostname' ? 'host' : $key; $key = $key == 'hostname' ? 'host' : $key;
$connexionINI[$key] = (string) $value; $connexionINI[$key] = (string) $value;
} }
$Core->getConfiguration()->initialize();
$connexionINI['driver'] = 'pdo_mysql'; $connexionINI['driver'] = 'pdo_mysql';
$connexionINI['charset'] = 'UTF8'; $connexionINI['charset'] = 'UTF8';
@@ -448,42 +446,6 @@ class appbox extends base
$root = __DIR__ . '/../../'; $root = __DIR__ . '/../../';
//copy config sample
$configSampleFile = $root . "config/config.sample.yml";
$configFile = $root . "config/config.yml";
if (!copy($configSampleFile, $configFile))
{
throw new \Exception(sprintf("Unable to copy %s", $configSampleFile));
}
//copy service sample
$serviceSampleFile = $root . "config/services.sample.yml";
$serviceFile = $root . "config/services.yml";
if (!copy($serviceSampleFile, $serviceFile))
{
throw new \Exception(sprintf("Unable to copy %s", $serviceSampleFile));
}
//copy connexion sample
$connexionSampleFile = $root . "config/connexions.sample.yml";
$connexionFile = $root . "config/connexions.yml";
if (!copy($connexionSampleFile, $connexionFile))
{
throw new \Exception(sprintf("Unable to copy %s", $serviceSampleFile));
}
//get configuration object
$appConf = new \Alchemy\Phrasea\Core\Configuration\Application();
$parser = new \Alchemy\Phrasea\Core\Configuration\Parser\Yaml();
$handler = new \Alchemy\Phrasea\Core\Configuration\Handler($appConf, $parser);
$configuration = new \Alchemy\Phrasea\Core\Configuration($handler);
//write credentials to config file
$connexionFile = $appConf->getConnexionFile();
$connexion = array( $connexion = array(
'main_connexion' => $connexionINI, 'main_connexion' => $connexionINI,
'test_connexion' => array( 'test_connexion' => array(
@@ -503,16 +465,9 @@ class appbox extends base
$cacheService = "xcache_cache"; $cacheService = "xcache_cache";
} }
$yaml = $configuration->getConfigurationHandler()->getParser()->dump($connexion, 2); $Core->getConfiguration()->setConnexions($connexion);
if (!file_put_contents($connexionFile->getPathname(), $yaml) !== false) $services = $Core->getConfiguration()->getConfigurations();
{
throw new \Exception(sprintf(_('Impossible d\'ecrire dans le fichier %s'), $connexionFile->getPathname()));
}
//rewrite service file
$serviceFile = $appConf->getServiceFile();
$services = $configuration->getConfigurationHandler()->getParser()->parse($serviceFile);
foreach ($services as $serviceName => $service) foreach ($services as $serviceName => $service)
{ {
@@ -526,16 +481,9 @@ class appbox extends base
); );
} }
} }
$Core->getConfiguration()->setConfigurations($services);
$yaml = $configuration->getConfigurationHandler()->getParser()->dump($services, 5); $arrayConf = $Core->getConfiguration()->getConfigurations();
if (!file_put_contents($serviceFile->getPathname(), $yaml) !== false)
{
throw new \Exception(sprintf(_('Impossible d\'ecrire dans le fichier %s'), $serviceFile->getPathname()));
}
//rewrite servername in main config file
$arrayConf = $configuration->all();
foreach ($arrayConf as $key => $value) foreach ($arrayConf as $key => $value)
{ {
@@ -550,16 +498,7 @@ class appbox extends base
} }
} }
$configuration->write($arrayConf); $Core->getConfiguration()->setConfigurations($arrayConf);
if (function_exists('chmod'))
{
chmod($configuration->getFile()->getPathname(), 0700);
chmod($serviceFile->getPathname(), 0700);
chmod($connexionFile->getPathname(), 0700);
}
} }
try try
{ {
@@ -588,7 +527,7 @@ class appbox extends base
try try
{ {
$appbox = self::get_instance($registry); $appbox = self::get_instance($Core, $registry);
$appbox->insert_datas(); $appbox->insert_datas();
} }
catch (Exception $e) catch (Exception $e)

View File

@@ -136,13 +136,7 @@ class connection
} }
else else
{ {
$configuration = \Alchemy\Phrasea\Core\Configuration::build();
$handler = new \Alchemy\Phrasea\Core\Configuration\Handler(
new \Alchemy\Phrasea\Core\Configuration\Application(),
new \Alchemy\Phrasea\Core\Configuration\Parser\Yaml()
);
$configuration = new \Alchemy\Phrasea\Core\Configuration($handler);
$choosenConnexion = $configuration->getPhraseanet()->get('database'); $choosenConnexion = $configuration->getPhraseanet()->get('database');

View File

@@ -78,11 +78,7 @@ class module_console_fileConfigCheck extends Command
private function initTests(OutputInterface $output) private function initTests(OutputInterface $output)
{ {
$spec = new Core\Configuration\Application(); $this->configuration = Core\Configuration::build();
$parser = new Core\Configuration\Parser\Yaml();
$handler = new Core\Configuration\Handler($spec, $parser);
$this->configuration = new Core\Configuration($handler, $this->env);
if (!$this->configuration->isInstalled()) if (!$this->configuration->isInstalled())
{ {
@@ -146,39 +142,19 @@ class module_console_fileConfigCheck extends Command
private function checkParse(OutputInterface $output) private function checkParse(OutputInterface $output)
{ {
$parser = $this
->configuration
->getConfigurationHandler()
->getParser();
$fileConfig = $this
->configuration
->getConfigurationHandler()
->getSpecification()
->getConfigurationFile();
$fileService = $this
->configuration
->getConfigurationHandler()
->getSpecification()
->getServiceFile();
$fileConnexion = $this
->configuration
->getConfigurationHandler()
->getSpecification()
->getConnexionFile();
try if (!$this->configuration->getConfigurations())
{ {
$parser->parse($fileConfig); throw new \Exception("Unable to load configurations\n");
$parser->parse($fileService);
$parser->parse($fileConnexion);
} }
catch (\Exception $e) if (!$this->configuration->getConnexions())
{ {
$message = str_replace("\\", "", $e->getMessage()); throw new \Exception("Unable to load connexions\n");
$e = new \Exception($message); }
throw new \Exception(sprintf("Check parsing file\n"), null, $e); if (!$this->configuration->getServices())
{
throw new \Exception("Unable to load services\n");
} }
$output->writeln("<info>Parsing File OK</info>");
return; return;
} }
@@ -200,11 +176,7 @@ class module_console_fileConfigCheck extends Command
private function checkGetSelectedEnvironementFromFile(OutputInterface $output) private function checkGetSelectedEnvironementFromFile(OutputInterface $output)
{ {
$spec = new Core\Configuration\Application(); $configuration = Core\Configuration::build();
$parser = new Core\Configuration\Parser\Yaml();
$handler = new Core\Configuration\Handler($spec, $parser);
$configuration = new Core\Configuration($handler);
try try
{ {

View File

@@ -59,7 +59,6 @@ class module_console_fileEnsureProductionSetting extends Command
public function execute(InputInterface $input, OutputInterface $output) public function execute(InputInterface $input, OutputInterface $output)
{ {
$this->initTests($output); $this->initTests($output);
$this->prepareTests($output); $this->prepareTests($output);
@@ -72,11 +71,7 @@ class module_console_fileEnsureProductionSetting extends Command
private function initTests(OutputInterface $output) private function initTests(OutputInterface $output)
{ {
$spec = new Core\Configuration\Application(); $this->configuration = Core\Configuration::build();
$parser = new Core\Configuration\Parser\Yaml();
$handler = new Core\Configuration\Handler($spec, $parser);
$this->configuration = new Core\Configuration($handler);
if (!$this->configuration->isInstalled()) if (!$this->configuration->isInstalled())
{ {
@@ -175,35 +170,18 @@ class module_console_fileEnsureProductionSetting extends Command
private function checkParse(OutputInterface $output) private function checkParse(OutputInterface $output)
{ {
$parser = $this
->configuration
->getConfigurationHandler()
->getParser();
$fileConfig = $this
->configuration
->getConfigurationHandler()
->getSpecification()
->getConfigurationFile();
$fileService = $this
->configuration
->getConfigurationHandler()
->getSpecification()
->getServiceFile();
$fileConnexion = $this
->configuration
->getConfigurationHandler()
->getSpecification()
->getConnexionFile();
try if (!$this->configuration->getConfigurations())
{ {
$parser->parse($fileConfig); throw new \Exception("Unable to load configurations\n");
$parser->parse($fileService);
$parser->parse($fileConnexion);
} }
catch (\Exception $e) if (!$this->configuration->getConnexions())
{ {
throw new \Exception("Error parsing file\n", null, $e); throw new \Exception("Unable to load connexions\n");
}
if (!$this->configuration->getServices())
{
throw new \Exception("Unable to load services\n");
} }
return; return;
@@ -237,11 +215,7 @@ class module_console_fileEnsureProductionSetting extends Command
private function checkGetSelectedEnvironementFromFile(OutputInterface $output) private function checkGetSelectedEnvironementFromFile(OutputInterface $output)
{ {
$spec = new Core\Configuration\Application(); $configuration = Core\Configuration::build();
$parser = new Core\Configuration\Parser\Yaml();
$handler = new Core\Configuration\Handler($spec, $parser);
$configuration = new Core\Configuration($handler);
try try
{ {
@@ -262,26 +236,48 @@ class module_console_fileEnsureProductionSetting extends Command
{ {
$phraseanet = $this->configuration->getPhraseanet(); $phraseanet = $this->configuration->getPhraseanet();
$this->printConf($output, 'phraseanet', $phraseanet->all()); foreach($phraseanet->all() as $conf=>$value)
$url = $phraseanet->get("servername");
if (empty($url))
{ {
throw new \Exception("phraseanet:servername connot be empty"); switch($conf)
}
if (!filter_var($url, FILTER_VALIDATE_URL))
{ {
throw new \Exception(sprintf("%s url is not valid", $url)); default:
} $this->printConf($output, $conf, $value);
break;
case 'servername':
$url = $value;
$parseUrl = parse_url($url); $parseUrl = parse_url($url);
if ($parseUrl["scheme"] !== "https") if (empty($url))
{ {
$output->writeln(sprintf("<comment>/!\ %s url scheme should be https</comment>", $url)); $message = "<error>should not be empty</error>";
} }
elseif (!filter_var($url, FILTER_VALIDATE_URL))
{
$message = "<error>not valid</error>";
}
elseif ($parseUrl["scheme"] !== "https")
{
$message = "<comment>should be https</comment>";
}
else
{
$message = "<info>OK</info>";
}
$this->printConf($output, $conf, $value, false, $message);
break;
case 'maintenance':
case 'debug':
case 'display_errors':
$message = $value ? '<error>Should be false</error>' : '<info>OK</info>';
$this->printConf($output, $conf, $value, false, $message);
break;
}
}
// $this->printConf($output, 'phraseanet', $phraseanet->all());
// $url = $phraseanet->get("servername");
if (!$phraseanet->has("debug")) if (!$phraseanet->has("debug"))
@@ -624,7 +620,7 @@ class module_console_fileEnsureProductionSetting extends Command
} }
} }
private function printConf($output, $scope, $value, $scopage = false) private function printConf($output, $scope, $value, $scopage = false, $message = '')
{ {
if (is_array($value)) if (is_array($value))
{ {
@@ -632,24 +628,24 @@ class module_console_fileEnsureProductionSetting extends Command
{ {
if ($scopage) if ($scopage)
$key = $scope . ":" . $key; $key = $scope . ":" . $key;
$this->printConf($output, $key, $val, $scopage); $this->printConf($output, $key, $val, $scopage, '');
} }
} }
elseif (is_bool($value)) elseif (is_bool($value))
{ {
if ($value === false) if ($value === false)
{ {
$value = '0'; $value = 'false';
} }
elseif ($value === true) elseif ($value === true)
{ {
$value = '1'; $value = 'true';
} }
$output->writeln(sprintf("\t%s: %s", $scope, $value)); $output->writeln(sprintf("\t%s: %s %s", $scope, $value, $message));
} }
elseif (!empty($value)) elseif (!empty($value))
{ {
$output->writeln(sprintf("\t%s: %s", $scope, $value)); $output->writeln(sprintf("\t%s: %s %s", $scope, $value, $message));
} }
} }

View File

@@ -65,11 +65,7 @@ class module_console_systemUpgrade extends Command
$credentials = $conn->get_credentials(); $credentials = $conn->get_credentials();
$handler = new \Alchemy\Phrasea\Core\Configuration\Handler( $configuration = \Alchemy\Phrasea\Core\Configuration::build();
new \Alchemy\Phrasea\Core\Configuration\Application(),
new \Alchemy\Phrasea\Core\Configuration\Parser\Yaml()
);
$configuration = new \Alchemy\Phrasea\Core\Configuration($handler);
$connexionINI = array(); $connexionINI = array();

View File

@@ -105,11 +105,7 @@ class phrasea
public static function start() public static function start()
{ {
$handler = new \Alchemy\Phrasea\Core\Configuration\Handler( $configuration = \Alchemy\Phrasea\Core\Configuration::build();
new \Alchemy\Phrasea\Core\Configuration\Application(),
new \Alchemy\Phrasea\Core\Configuration\Parser\Yaml()
);
$configuration = new \Alchemy\Phrasea\Core\Configuration($handler);
$choosenConnexion = $configuration->getPhraseanet()->get('database'); $choosenConnexion = $configuration->getPhraseanet()->get('database');

View File

@@ -59,11 +59,7 @@ class registry implements registryInterface
{ {
$this->cache = new Alchemy\Phrasea\Cache\ArrayCache(); $this->cache = new Alchemy\Phrasea\Cache\ArrayCache();
$handler = new \Alchemy\Phrasea\Core\Configuration\Handler( $configuration = \Alchemy\Phrasea\Core\Configuration::build();
new \Alchemy\Phrasea\Core\Configuration\Application(),
new \Alchemy\Phrasea\Core\Configuration\Parser\Yaml()
);
$configuration = new \Alchemy\Phrasea\Core\Configuration($handler);
$this->cache->save('GV_RootPath', dirname(dirname(__DIR__)) . '/'); $this->cache->save('GV_RootPath', dirname(dirname(__DIR__)) . '/');
if ($configuration->isInstalled()) if ($configuration->isInstalled())

View File

@@ -70,21 +70,9 @@ class setup
public static function is_installed() public static function is_installed()
{ {
$appConf = new \Alchemy\Phrasea\Core\Configuration\Application(); $appConf = new \Alchemy\Phrasea\Core\Configuration\ApplicationSpecification();
$installed = false;
try
{
$appConf->getConfigurationFile();
$appConf->getConnexionFile();
$appConf->getServiceFile();
$installed = true;
}
catch (\Exception $e)
{
} return $appConf->isSetup();
return $installed;
} }
public static function needUpgradeConfigurationFile() public static function needUpgradeConfigurationFile()
@@ -817,37 +805,9 @@ class setup
} }
} }
$appConf = new \Alchemy\Phrasea\Core\Configuration\Application(); $appConf = new \Alchemy\Phrasea\Core\Configuration\ApplicationSpecification();
try $appConf->delete();
{
$configFile = $appConf->getConfigurationFile();
unlink($configFile->getPathname());
}
catch (\Exception $e)
{
}
try
{
$serviceFile = $appConf->getServiceFile();
unlink($serviceFile->getPathname());
}
catch (\Exception $e)
{
}
try
{
$connexionfFile = $appConf->getConnexionFile();
unlink($connexionfFile->getPathname());
}
catch (\Exception $e)
{
}
return; return;
} }

View File

@@ -61,37 +61,38 @@ class ConfigurationTest extends \PhraseanetPHPUnitAbstract
public function setUp() public function setUp()
{ {
$this->markTestSkipped('To rewrite');
parent::setUp(); parent::setUp();
$specNotInstalled = $this->getMock( $specNotInstalled = $this->getMock(
'\Alchemy\Phrasea\Core\Configuration\Application' '\Alchemy\Phrasea\Core\Configuration\Application'
, array('getConfigurationFile') , array('getConfigurationsFile')
); );
$specNotInstalled->expects($this->any()) $specNotInstalled->expects($this->any())
->method('getConfigurationFile') ->method('getConfigurationsFile')
->will( ->will(
$this->throwException(new Exception) $this->throwException(new Exception)
); );
$specExperience = $this->getMock( $specExperience = $this->getMock(
'\Alchemy\Phrasea\Core\Configuration\Application' '\Alchemy\Phrasea\Core\Configuration\Application'
, array('getConfigurationFile') , array('getConfigurationsFile')
); );
$specExperience->expects($this->any()) $specExperience->expects($this->any())
->method('getConfigurationFile') ->method('getConfigurationsFile')
->will( ->will(
$this->returnValue( $this->returnValue(
new \SplFileObject(__DIR__ . '/confTestFiles/config.yml') new \SplFileObject(__DIR__ . '/confTestFiles/config.yml')
) )
); );
$handler = new Configuration\Handler($specNotInstalled, new Configuration\Parser\Yaml()); $handler = new Configuration\Handler($specNotInstalled);
$this->confNotInstalled = new PhraseaCore\Configuration($handler); $this->confNotInstalled = new PhraseaCore\Configuration($handler);
$handler = new Configuration\Handler($specExperience, new Configuration\Parser\Yaml()); $handler = new Configuration\Handler($specExperience);
$this->object = new PhraseaCore\Configuration($handler); $this->object = new PhraseaCore\Configuration($handler);
} }
@@ -289,7 +290,7 @@ class ConfigurationTest extends \PhraseanetPHPUnitAbstract
$this->returnValue($file->getPathname()) $this->returnValue($file->getPathname())
); );
$handler = new Configuration\Handler($stub, new Configuration\Parser\Yaml()); $handler = new Configuration\Handler($stub);
$configuration = new PhraseaCore\Configuration($handler); $configuration = new PhraseaCore\Configuration($handler);
@@ -327,7 +328,7 @@ class ConfigurationTest extends \PhraseanetPHPUnitAbstract
$this->returnValue("unknow_path") $this->returnValue("unknow_path")
); );
$handler = new Configuration\Handler($stub, new Configuration\Parser\Yaml()); $handler = new Configuration\Handler($stub);
$configuration = new PhraseaCore\Configuration($handler); $configuration = new PhraseaCore\Configuration($handler);
@@ -367,7 +368,7 @@ class ConfigurationTest extends \PhraseanetPHPUnitAbstract
$this->returnValue($file->getPathname()) $this->returnValue($file->getPathname())
); );
$handler = new Configuration\Handler($stub, new Configuration\Parser\Yaml()); $handler = new Configuration\Handler($stub);
$configuration = new PhraseaCore\Configuration($handler); $configuration = new PhraseaCore\Configuration($handler);
@@ -393,7 +394,7 @@ class ConfigurationTest extends \PhraseanetPHPUnitAbstract
$this->returnValue("unknow_path") $this->returnValue("unknow_path")
); );
$handler = new Configuration\Handler($stub, new Configuration\Parser\Yaml()); $handler = new Configuration\Handler($stub);
$configuration = new PhraseaCore\Configuration($handler); $configuration = new PhraseaCore\Configuration($handler);
@@ -469,7 +470,7 @@ class ConfigurationTest extends \PhraseanetPHPUnitAbstract
$this->returnValue($file->getPathname()) $this->returnValue($file->getPathname())
); );
$handler = new Configuration\Handler($stub, new Configuration\Parser\Yaml()); $handler = new Configuration\Handler($stub);
$configuration = new PhraseaCore\Configuration($handler); $configuration = new PhraseaCore\Configuration($handler);
@@ -480,7 +481,7 @@ class ConfigurationTest extends \PhraseanetPHPUnitAbstract
try try
{ {
$configuration->getConfiguration();//it is not loaded $configuration->getConfiguration(); //it is not loaded
$this->fail("should raise an exception"); $this->fail("should raise an exception");
} }
catch (\Exception $e) catch (\Exception $e)
@@ -495,23 +496,5 @@ class ConfigurationTest extends \PhraseanetPHPUnitAbstract
unlink(__DIR__ . "/confTestFiles/yamlWriteTest.yml"); unlink(__DIR__ . "/confTestFiles/yamlWriteTest.yml");
} }
public function testSetHandler()
{
$handler = new Configuration\Handler(new Configuration\Application(), new Configuration\Parser\Yaml());
$this->object->setConfigurationHandler($handler);
$this->assertEquals($handler, $this->object->getConfigurationHandler());
}
public function testGetHandler()
{
$stub = $this->getMock(
'\Alchemy\Phrasea\Core\Configuration\Application'
, array('getConfigurationFile')
);
$handler = new Configuration\Handler($stub, new Configuration\Parser\Yaml());
$this->assertEquals($handler, $this->object->getConfigurationHandler());
}
} }

View File

@@ -64,12 +64,6 @@ no_display_errors:
cache: array_cache cache: array_cache
missing_phraseanet: missing_phraseanet:
##phraseanet:
##servername: 'http://dev.phrasea.net/'
##maintenance: false
##debug: true
##display_errors: true
##database: main_connexion
template_engine: twig_debug template_engine: twig_debug
orm: doctrine_test orm: doctrine_test
cache: array_cache cache: array_cache

View File

@@ -114,14 +114,12 @@ class CoreTest extends PhraseanetPHPUnitAbstract
set_new_overload(array($this, 'newCallback')); set_new_overload(array($this, 'newCallback'));
$handler = new \Alchemy\Phrasea\Core\Configuration\Handler( $specification = new \Alchemy\Phrasea\Core\Configuration\ApplicationSpecification();
new \Alchemy\Phrasea\Core\Configuration\Application()
, new \Alchemy\Phrasea\Core\Configuration\Parser\Yaml()
);
$class = $this->getMock( $class = $this->getMock(
'\Alchemy\Phrasea\Core\Configuration' '\Alchemy\Phrasea\Core\Configuration'
, array('isInstalled') , array('isInstalled')
, array($handler) , array($specification)
, 'ConfMock' , 'ConfMock'
); );

View File

@@ -814,11 +814,7 @@ abstract class PhraseanetPHPUnitAbstract extends WebTestCase
*/ */
protected function resetTwig() protected function resetTwig()
{ {
$handler = new \Alchemy\Phrasea\Core\Configuration\Handler( $configuration = \Alchemy\Phrasea\Core\Configuration::build();
new \Alchemy\Phrasea\Core\Configuration\Application(),
new \Alchemy\Phrasea\Core\Configuration\Parser\Yaml()
);
$configuration = new \Alchemy\Phrasea\Core\Configuration($handler);
$serviceName = $configuration->getTemplating(); $serviceName = $configuration->getTemplating();
$confService = $configuration->getService($serviceName); $confService = $configuration->getService($serviceName);