Skip to content
Snippets Groups Projects
Commit b1eab704 authored by Laurent Lecluse's avatar Laurent Lecluse
Browse files

Possibilité de n'installer que la BDD sans les données

[Fix] Nouvelle méthode d'instanciation des classes par transmission du container : pb de cycle
[Fix] Gestion du false en retour pour un select, renvoie null
parent 262ee39f
No related branches found
No related tags found
No related merge requests found
Pipeline #32080 passed
......@@ -144,6 +144,9 @@ class Bdd
protected bool $inCopy = false;
/** @var \Psr\Container\ContainerInterface|null */
public $__container = null;
private DataManager $data;
......@@ -151,7 +154,7 @@ class Bdd
public function __construct(array $options = [], ?string $connection = null)
{
if (!empty($options)) {
// Compatibilité avec l'ancienne configuration des connections
// Compatibilité avec l'ancienne configuration des connexions
if (!isset($options[self::OPTION_CONNECTION]) && isset($options['host'])) {
$options = [self::OPTION_CONNECTION => [$connection => $options]];
}
......@@ -173,6 +176,33 @@ class Bdd
/**
* Hack pour initialiser une classe à partir d'un container respectant Psr\Container\ContainerInterface
* Si la classe n'est pas connue du container, alors elle est instanciée à l'aide de new
*
* @param string $classname
* @return object
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*
*/
public function __newClass(string $classname): mixed
{
if ($this->__container && method_exists($this->__container, 'has') && method_exists($this->__container, 'get')){
if ($this->__container->has($classname)){
return $this->__container->get($classname);
}
}
if (!class_exists($classname)){
throw new \Exception('La classe '.$classname.' n\'existe pas');
}
return new $classname;
}
public function hasConnection(string $connection): bool
{
$connections = $this->options[self::OPTION_CONNECTION];
......@@ -297,7 +327,11 @@ class Bdd
public function select(string $sql, array $params = [], array $options = []): array|null|SelectParser
{
return $this->driver->select($sql, $params, $options);
$result = $this->driver->select($sql, $params, $options);
if (false === $result){
return null;
}
return $result;
}
......@@ -787,7 +821,7 @@ class Bdd
public function install(): self
public function install(bool $withData=true): self
{
$this->logTitle('Installation de la base de données');
......@@ -800,7 +834,7 @@ class Bdd
$this->logError($e);
}
if (!empty($this->data()->getSources())) {
if ($withData && !empty($this->data()->getSources())) {
// Installation des données
$this->logTitle('Insertion du jeu de données données');
try {
......
......@@ -29,35 +29,9 @@ class BddFactory
}
$config = $container->get('config')['unicaen-bddadmin'];
$bdd = new Bdd($this->prepareConfig($config, $container));
$bdd = new Bdd($config);
$bdd->__container = $container;
return $bdd;
}
protected function prepareConfig(array $config, ContainerInterface $container): array
{
// Chargement des sources de données depuis le ServiceManager si nécessaire
if (isset($config[Bdd::OPTION_DATA][DataManager::OPTION_SOURCES])){
$sources = &$config[Bdd::OPTION_DATA][DataManager::OPTION_SOURCES];
foreach( $sources as $index => $source){
if ($container->has($source)){
$sources[$index] = $container->get($source);
}
}
}
// Chargement des scripts de migration depuis le ServiceManager si nécessaire
if (isset($config[Bdd::OPTION_MIGRATION])){
$sources = &$config[Bdd::OPTION_MIGRATION];
foreach( $sources as $index => $source){
if ($container->has($source)){
$sources[$index] = $container->get($source);
}
}
}
return $config;
}
}
\ No newline at end of file
......@@ -72,6 +72,10 @@ class DataManager
break;
}
$methodName = str_replace('.', '_', $table);
if (is_string($source) && class_exists($source)){
$source = $this->getBdd()->__newClass($source);
}
if (is_object($source) && method_exists($source, $methodName)) {
$data = $source->$methodName($action);
break;
......
......@@ -164,7 +164,7 @@ class MigrationManager
foreach ($scripts as $index => $script) {
if (is_string($script) && class_exists($script)) {
$script = new $script();
$script = $this->getBdd()->__newClass($script);
$scripts[$index] = $script;
}
if (!$script instanceof MigrationAction) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment