Upgrade Configuration

This commit is contained in:
Romain Neutron
2012-02-22 18:04:28 +01:00
parent 6c16b90e30
commit e3df263373
12 changed files with 269 additions and 644 deletions

View File

@@ -74,11 +74,7 @@ return call_user_func(function()
throw new \Exception(sprintf("Unable to copy %s", $connexionFile));
}
//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);
$configuration = \Alchemy\Phrasea\Core\Configuration::build();
//refactor credentials
$connexionINI = array();
@@ -127,7 +123,7 @@ return call_user_func(function()
}
//rewrite service file
$serviceFile = $appConf->getServiceFile();
$serviceFile = $appConf->getServicesFile();
$services = $configuration->getConfigurationHandler()->getParser()->parse($serviceFile);
foreach ($services as $serviceName => $service)
@@ -164,10 +160,7 @@ return call_user_func(function()
}
}
$configuration->write($arrayConf);
$app['install'] = true;
// $app->redirect("/setup/installer/");
}
else
{

View File

@@ -11,8 +11,7 @@
namespace Alchemy\Phrasea\Cache;
use Alchemy\Phrasea\Core\Configuration\Parser as FileParser,
\Alchemy\Phrasea\Core\Service\Builder,
use \Alchemy\Phrasea\Core\Service\Builder,
\Alchemy\Phrasea\Core;
/**
@@ -43,13 +42,13 @@ class Manager
*/
protected $registry = array();
public function __construct(Core $core, \SplFileObject $file, FileParser $parser)
public function __construct(Core $core, \SplFileObject $file)
{
$this->cacheFile = $file;
$this->parser = $parser;
$this->parser = new \Symfony\Component\Yaml\Yaml();
$this->core = $core;
$this->registry = $parser->parse($file);
$this->registry = $this->parser->parse($file);
}
protected function exists($name)

View File

@@ -176,13 +176,9 @@ class Installer implements ControllerProviderInterface
$setupRegistry = new \Setup_Registry();
$setupRegistry->set('GV_ServerName', $servername, \registry::TYPE_STRING);
$appbox = \appbox::create($setupRegistry, $conn, $appbox_name, true);
$appbox = \appbox::create($app['Core'], $setupRegistry, $conn, $appbox_name, true);
$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);
$configuration = \Alchemy\Phrasea\Core\Configuration::build();
if ($configuration->isInstalled())
{

View File

@@ -26,10 +26,7 @@ require_once __DIR__ . '/../../vendor/symfony/src/Symfony/Component/DependencyIn
require_once __DIR__ . '/Core/Configuration/Specification.php';
require_once __DIR__ . '/Core/Configuration.php';
require_once __DIR__ . '/Core/Configuration/Application.php';
require_once __DIR__ . '/Core/Configuration/Handler.php';
require_once __DIR__ . '/Core/Configuration/Parser.php';
require_once __DIR__ . '/Core/Configuration/Parser/Yaml.php';
require_once __DIR__ . '/Core/Configuration/ApplicationSpecification.php';
/**
*
@@ -57,12 +54,7 @@ class Core extends \Pimple
public function __construct($environement = null)
{
$appConf = new Core\Configuration\Application();
$parser = new Core\Configuration\Parser\Yaml();
$handler = new Core\Configuration\Handler($appConf, $parser);
$this->configuration = new Core\Configuration($handler, $environement);
$this->configuration = Core\Configuration::build(null, $environement);
$core = $this;
@@ -112,9 +104,7 @@ class Core extends \Pimple
$file = new \SplFileObject(__DIR__ . '/../../../tmp/cache_registry.yml');
$parser = new Core\Configuration\Parser\Yaml();
return new \Alchemy\Phrasea\Cache\Manager($core, $file, $parser);
return new \Alchemy\Phrasea\Cache\Manager($core, $file);
});
/**
* Set Entity Manager using configuration

View File

@@ -12,8 +12,7 @@
namespace Alchemy\Phrasea\Core;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Alchemy\Phrasea\Core\Configuration\Application;
use Alchemy\Phrasea\Core\Configuration\Parser as ConfigurationParser;
use Alchemy\Phrasea\Core\Configuration\ApplicationSpecification;
/**
* Handle configuration file mechanism of phraseanet
@@ -30,12 +29,7 @@ class Configuration
* @var ParameterBag\ParameterBag
*/
protected $configuration;
/**
* Class that take care of configuration process
* @var Configuration\Handler
*/
private $configurationHandler;
protected $specifications;
/**
* Return the current environnement
@@ -43,48 +37,25 @@ class Configuration
*/
private $environment;
/**
*
* @param type $envName the name of the loaded environnement
*/
public function __construct(Configuration\Handler $handler, $environment = null)
public static function build($specifications = null, $environment = null)
{
$this->configurationHandler = $handler;
$this->installed = false;
$this->environment = $environment;
try
if (!$specifications)
{
//if one of this files are missing consider phraseanet not installed
$handler->getSpecification()->getConfigurationFile();
$handler->getSpecification()->getServiceFile();
$handler->getSpecification()->getConnexionFile();
$this->installed = true;
}
catch (\Exception $e)
{
$specifications = new Configuration\ApplicationSpecification();
}
return new self($specifications, $environment);
}
/**
* Getter
* @return Configuration\Handler
*/
public function getConfigurationHandler()
public function __construct(Configuration\Specification $specifications, $environment = null)
{
return $this->configurationHandler;
}
$this->specifications = $specifications;
/**
* Setter
* @param Configuration\Handler $configurationHandler
*/
public function setConfigurationHandler(Configuration\Handler $configurationHandler)
{
$this->configurationHandler = $configurationHandler;
$configurations = $this->specifications->getConfigurations();
$environment = $environment ? : $configurations[self::KEYWORD_ENV];
$this->setEnvironnement($environment);
return $this;
}
/**
@@ -94,10 +65,6 @@ class Configuration
*/
public function getEnvironnement()
{
if (null === $this->environment && $this->isInstalled())
{
$this->refresh();
}
return $this->environment;
}
@@ -107,10 +74,21 @@ class Configuration
*
* @return string
*/
public function setEnvironnement($environement = null)
public function setEnvironnement($environment)
{
$this->environment = $environement;
$this->refresh();
$this->environment = $environment;
if ($this->specifications->isSetup())
{
$configurations = $this->specifications->getConfigurations();
$this->configuration = new ParameterBag($configurations[$this->environment]);
}
else
{
$this->configuration = new ParameterBag(array());
}
return $this;
}
/**
@@ -183,7 +161,7 @@ class Configuration
*/
public function getPhraseanet()
{
$phraseanetConf = $this->getConfiguration()->get('phraseanet');
$phraseanetConf = $this->configuration->get('phraseanet');
return new ParameterBag($phraseanetConf);
}
@@ -195,30 +173,49 @@ class Configuration
*/
public function isInstalled()
{
return $this->installed;
return $this->specifications->isSetup();
}
/**
* Return the configuration
*
* @return ParameterBag\ParameterBag
*/
public function getConfiguration()
public function initialize()
{
if ($this->installed && null === $this->configuration)
{
$configuration = $this->configurationHandler->handle($this->environment);
$this->environment = $this->configurationHandler->getSelectedEnvironnment();
$this->configuration = new ParameterBag($configuration);
}
elseif (!$this->installed)
{
$configuration = array();
$this->configuration = new ParameterBag($configuration);
return $this->specifications->initialize();
}
return $this->configuration;
public function setConfigurations($configurations)
{
return $this->specifications->setConfigurations($configurations);
}
public function setServices($services)
{
return $this->specifications->setServices($services);
}
public function setConnexions($connexions)
{
return $this->specifications->setConnexions($connexions);
}
public function getConfigurations()
{
return $this->specifications->getConfigurations();
}
public function getServices()
{
return $this->specifications->getServices();
}
public function getConnexions()
{
return $this->specifications->getConnexions();
}
const KEYWORD_ENV = 'environment';
public function getSelectedEnvironnment()
{
return $this->selectedEnvironnment;
}
/**
@@ -230,130 +227,12 @@ class Configuration
{
$connexions = $this->getConnexions();
try
if (!isset($connexions[$name]))
{
$conn = $connexions->get($name);
}
catch (\Exception $e)
{
throw new \Exception(sprintf('Unknow connexion name %s declared in %s'
, $name
, $this->configurationHandler
->getSpecification()
->getConnexionFile()
->getFileName()
)
);
throw new \Exception(sprintf('Unknown connexion name %s', $name));
}
return new Parameterbag($conn);
}
/**
* Return all connexions defined in connexions.yml
* @return ParameterBag
*/
public function getConnexions()
{
return new ParameterBag($this->configurationHandler->getParser()->parse(
$this->configurationHandler->getSpecification()->getConnexionFile()
)
);
}
/**
* Return a the configuration file as an SplFileObject
*
* @return \SplFileObject
*/
public function getFile()
{
return $this->configurationHandler->getSpecification()->getConfigurationFile();
}
/**
* Return the full configuration file as an Array
*
* @return Array
*/
public function all()
{
$allConf = $this->configurationHandler->getParser()->parse($this->getFile());
return $allConf;
}
/**
* Return all services defined in services.yml
* @return ParameterBag
*/
public function getServices()
{
return new ParameterBag($this->configurationHandler->getParser()->parse(
$this->getServiceFile()
)
);
}
/**
* Write datas in config file
*
* @param array $data
* @param type $flag
* @return Configuration
*/
public function write(Array $data, $flag = 0, $delete = false)
{
if ($delete)
{
$this->delete();
}
$yaml = $this->configurationHandler->getParser()->dump($data, 5);
$filePathName = $this->configurationHandler
->getSpecification()
->getConfigurationPathName();
if (false === file_put_contents($filePathName, $yaml, $flag))
{
$filePath = $this->configurationHandler
->getSpecification()
->getConfigurationFilePath();
throw new \Exception(sprintf(_('Impossible d\'ecrire dans le dossier %s'), $filePath));
}
return $this;
}
/**
* Delete configuration file
* @return Configuration
*/
public function delete()
{
$deleted = false;
try
{
$filePathName = $this
->configurationHandler
->getSpecification()
->getConfigurationPathName();
$deleted = unlink($filePathName);
}
catch (\Exception $e)
{
}
if (!$deleted)
{
throw new \Exception(sprintf(_('Impossible d\'effacer le fichier %s'), $filePathName));
}
return $this;
return new Parameterbag($connexions[$name]);
}
/**
@@ -362,17 +241,17 @@ class Configuration
*/
public function getTemplating()
{
return 'TemplateEngine\\' . $this->getConfiguration()->get('template_engine');
return 'TemplateEngine\\' . $this->configuration->get('template_engine');
}
public function getCache()
{
return 'Cache\\' . $this->getConfiguration()->get('cache');
return 'Cache\\' . $this->configuration->get('cache');
}
public function getOpcodeCache()
{
return 'Cache\\' . $this->getConfiguration()->get('opcodecache');
return 'Cache\\' . $this->configuration->get('opcodecache');
}
/**
@@ -381,7 +260,7 @@ class Configuration
*/
public function getOrm()
{
return 'Orm\\' . $this->getConfiguration()->get('orm');
return 'Orm\\' . $this->configuration->get('orm');
}
/**
@@ -393,7 +272,7 @@ class Configuration
public function getService($name)
{
$scopes = explode('\\', $name);
$services = $this->getServices();
$services = new ParameterBag($this->getServices());
$service = null;
while ($scopes)
@@ -407,65 +286,11 @@ class Configuration
}
catch (\Exception $e)
{
throw new \Exception(sprintf('Unknow service name %s declared in %s'
, $scope
, $this->configurationHandler
->getSpecification()
->getServiceFile()
->getFileName()
)
);
throw new \Exception(sprintf('Unknow service name %s', $name));
}
}
return $service;
}
/**
* return the service file
* @return \SplFileObject
*/
public function getServiceFile()
{
return $this->configurationHandler->getSpecification()->getServiceFile();
}
/**
* Return the connexion file
* @return \SplFileObject
*/
public function getConnexionFile()
{
return $this->configurationHandler->getSpecification()->getConnexionFile();
}
/**
* Refresh the configuration
* @return Configuration
*/
public function refresh()
{
try
{
$this->configurationHandler->getSpecification()->getConfigurationFile();
$this->configurationHandler->getSpecification()->getServiceFile();
$this->configurationHandler->getSpecification()->getConnexionFile();
$this->installed = true;
}
catch (\Exception $e)
{
$this->installed = false;
}
if ($this->installed)
{
$configuration = $this->configurationHandler->handle($this->environment);
$this->environment = $this->configurationHandler->getSelectedEnvironnment();
$this->configuration = new ParameterBag($configuration);
}
return $this;
}
}

View File

@@ -1,114 +0,0 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2010 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Core\Configuration;
/**
* Precise some informations about phraseanet configuration mechanism
*
* @package
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link www.phraseanet.com
*/
class Application implements Specification
{
const DEFAULT_ENV = 'prod';
const KEYWORD_ENV = 'environment';
/**
*
* {@inheritdoc}
*/
public function getConfigurationFilePath()
{
return realpath(__DIR__ . '/../../../../../config');
}
/**
*
* {@inheritdoc}
*/
public function getConfigurationPathName()
{
$path = sprintf(
'%s/%s.%s'
, $this->getConfigurationFilePath()
, $this->getConfigurationFileName()
, $this->getConfigurationFileExtension()
);
return $path;
}
/**
*
* {@inheritdoc}
*/
public function getConfigurationFileName()
{
return 'config';
}
/**
*
* {@inheritdoc}
*/
public function getConfigurationFileExtension()
{
return 'yml';
}
/**
* Return the selected environnment from configuration file
*
* @return string
*/
public function getSelectedEnv(Array $config)
{
if (!isset($config[self::KEYWORD_ENV]))
{
return self::DEFAULT_ENV;
}
return $config[self::KEYWORD_ENV];
}
/**
* Return the main configuration file
*
* @return \SplFileObject
*/
public function getConfigurationFile()
{
return new \SplFileObject($this->getConfigurationPathName());
}
/**
* Return the main configuration file
*
* @return \SplFileObject
*/
public function getServiceFile()
{
return new \SplFileObject(realpath(__DIR__ . '/../../../../../config/services.yml'));
}
/**
* Return the main configuration file
*
* @return \SplFileObject
*/
public function getConnexionFile()
{
return new \SplFileObject(realpath(__DIR__ . '/../../../../../config/connexions.yml'));
}
}

View File

@@ -0,0 +1,163 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2010 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Core\Configuration;
/**
* Precise some informations about phraseanet configuration mechanism
*
* @package
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link www.phraseanet.com
*/
class ApplicationSpecification implements Specification
{
protected $parser;
public function __construct()
{
$this->parser = new \Symfony\Component\Yaml\Yaml();
}
public function setConfigurations($configurations)
{
return file_put_contents(
$this->getConfigurationsPathFile(), $this->parser->dump($configurations, 5)
);
}
public function setConnexions($connexions)
{
return file_put_contents(
$this->getConnexionsPathFile(), $this->parser->dump($connexions, 5)
);
}
public function setServices($services)
{
return file_put_contents(
$this->getServicesPathFile(), $this->parser->dump($services, 5)
);
}
public function getConfigurations()
{
return $this->parser->parse(
file_get_contents($this->getConfigurationsPathFile())
);
}
public function getConnexions()
{
return $this->parser->parse(
file_get_contents($this->getConnexionsPathFile())
);
}
public function getServices()
{
return $this->parser->parse(
file_get_contents($this->getServicesPathFile())
);
}
protected function getConfigurationsFile()
{
return new \SplFileObject($this->getConfigurationsPathFile());
}
protected function getConnexionsFile()
{
return new \SplFileObject($this->getConnexionsPathFile());
}
protected function getServicesFile()
{
return new \SplFileObject($this->getServicesPathFile());
}
public function delete()
{
$files = array(
$this->getConnexionsPathFile(),
$this->getConfigurationsPathFile(),
$this->getServicesPathFile()
);
foreach ($files as $file)
{
if (file_exists($file))
unlink($file);
}
}
public function initialize()
{
touch($this->getConnexionsPathFile());
touch($this->getConfigurationsPathFile());
touch($this->getServicesPathFile());
copy(
$this->getRealRootPath() . "/config/config.sample.yml"
, $this->getRealRootPath() . "/config/config.yml"
);
copy(
$this->getRealRootPath() . "/config/services.sample.yml"
, $this->getRealRootPath() . "/config/services.yml"
);
if (function_exists('chmod'))
{
chmod($this->getConnexionsPathFile(), 0700);
chmod($this->getConfigurationsPathFile(), 0700);
chmod($this->getServicesPathFile(), 0700);
}
}
public function isSetup()
{
try
{
$this->getConfigurationsFile();
$this->getConnexionsFile();
$this->getServicesFile();
return true;
}
catch (\Exception $e)
{
}
return false;
}
protected function getConfigurationsPathFile()
{
return $this->getRealRootPath() . '/config/config.yml';
}
protected function getConnexionsPathFile()
{
return $this->getRealRootPath() . '/config/connexions.yml';
}
protected function getServicesPathFile()
{
return $this->getRealRootPath() . '/config/services.yml';
}
protected function getRealRootPath()
{
return realpath(__DIR__ . '/../../../../../');
}
}

View File

@@ -1,108 +0,0 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2010 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Core\Configuration;
use \Symfony\Component\Yaml\Yaml;
/**
* Handle configuration mechanism
*
* @package
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link www.phraseanet.com
*/
class Handler
{
/**
* Configuration file specification interface
* @var ConfigurationSpecification
*/
protected $confSpecification;
/**
* A file parser interface
* @var Parser\ParserInterface
*/
protected $parser;
/**
* The environnment selected
* @var string
*/
private $selectedEnvironnment;
/**
* Tell handler the configuration specification ans which parser to use
*
* @param ConfigurationSpecification $configSpec
* @param Parser\ParserInterface $parser
*/
public function __construct(Specification $configSpec, Parser $parser)
{
$this->confSpecification = $configSpec;
$this->parser = $parser;
}
/**
* Getter
* @return Specification
*/
public function getSpecification()
{
return $this->confSpecification;
}
/**
* Getter
* @return Parser
*/
public function getParser()
{
return $this->parser;
}
/**
* Handle the configuration process and return the final configuration
*
* @param strinig $name the name of the loaded environnement
* @return Array
*/
public function handle($selectedEnv = null)
{
//get the correspondant file
$file = $this->confSpecification->getConfigurationFile();
//parse
$env = $this->parser->parse($file);
//get selected env
if (null === $selectedEnv)
{
$selectedEnv = $this->confSpecification->getSelectedEnv($env);
}
$this->selectedEnvironnment = $selectedEnv;
if (!isset($env[$selectedEnv]))
{
throw new \Exception(sprintf("The choosen development environment '%s' is not declared in %s", $selectedEnv, $file->getFileName()));
}
return $env[$selectedEnv];
}
public function getSelectedEnvironnment()
{
return $this->selectedEnvironnment;
}
}

View File

@@ -1,40 +0,0 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2010 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Core\Configuration;
/**
* A interface to parse configuration file
*
* @package
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link www.phraseanet.com
*/
interface Parser
{
/**
* Parse the configuration file $file to an array
*
* @param \SplFileObject $file the file to parse
* @return Array
*/
public function parse(\SplFileObject $file);
/**
* Dump into string from array
*
* @param $conf
* @return string
*/
public function dump(Array $conf, $level);
}

View File

@@ -1,49 +0,0 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2010 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Core\Configuration\Parser;
use Symfony\Component\Yaml\Yaml as SfYaml;
/**
* Parse a configuration file in yaml format and return an array of values
*
* @package
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link www.phraseanet.com
*/
class Yaml implements \Alchemy\Phrasea\Core\Configuration\Parser
{
/**
*
* @Override
*/
public function parse(\SplFileObject $file)
{
try
{
return SfYaml::parse($file->getPathname());
}
catch(\Exception $e)
{
throw new \Exception(sprintf('Failed to parse the configuration file %s', $e->getMessage()));
}
}
/**
*
* @Override
*/
public function dump(Array $conf, $level = 1)
{
return SfYaml::dump($conf, $level);
}
}

View File

@@ -21,32 +21,22 @@ namespace Alchemy\Phrasea\Core\Configuration;
interface Specification
{
/**
* Return the pathname of the configuration file
*
* @return string
*/
public function getConfigurationPathName();
public function setConfigurations($configurations);
/**
* Return the path to the configuration file
*
* @return string
*/
public function getConfigurationFilePath();
public function setConnexions($connexions);
/**
* Return the configurationFile extension
*
* @return string
*/
public function getConfigurationFileExtension();
public function setServices($services);
/**
* Return the name of the configuration file
*
* @return string
*/
public function getConfigurationFileName();
public function getConfigurations();
public function getConnexions();
public function getServices();
public function initialize();
public function delete();
public function isSetup();
}

View File

@@ -29,15 +29,6 @@ use Doctrine\ORM\Events as DoctrineEvents;
class Doctrine extends ServiceAbstract implements ServiceInterface
{
const ARRAYCACHE = 'array';
const MEMCACHE = 'memcache';
const XCACHE = 'xcache';
const REDIS = 'redis';
const APC = 'apc';
protected $caches = array(
self::MEMCACHE, self::APC, self::ARRAYCACHE, self::XCACHE, self::REDIS
);
protected $outputs = array(
'json', 'yaml', 'vdump'
);
@@ -50,7 +41,7 @@ class Doctrine extends ServiceAbstract implements ServiceInterface
public function __construct(Core $core, $name, Array $options)
{
parent::__construct( $core, $name, $options);
parent::__construct($core, $name, $options);
$config = new \Doctrine\ORM\Configuration();
@@ -126,27 +117,16 @@ class Doctrine extends ServiceAbstract implements ServiceInterface
}
catch (\Exception $e)
{
$connexionFile = $this
->core->getConfiguration()
->getConfigurationHandler()
->getSpecification()
->getConnexionFile();
throw new \Exception(sprintf(
"Connexion '%s' is not declared in %s"
, $connexion
, $connexionFile->getFileName()
)
);
throw new \Exception("Connexion '%s' is not declared");
}
$evm = new \Doctrine\Common\EventManager();
$evm->addEventSubscriber(new \Gedmo\Timestampable\TimestampableListener());
// $evm->addEventListener(DoctrineEvents::postUpdate, new ClearCacheListener());
// $evm->addEventListener(DoctrineEvents::postRemove, new ClearCacheListener());
// $evm->addEventListener(DoctrineEvents::postPersist, new ClearCacheListener());
$evm->addEventListener(DoctrineEvents::postUpdate, new ClearCacheListener());
$evm->addEventListener(DoctrineEvents::postRemove, new ClearCacheListener());
$evm->addEventListener(DoctrineEvents::postPersist, new ClearCacheListener());
try