Skip to content

Commit

Permalink
Ajout de la timezone pour MySQL (#1612)
Browse files Browse the repository at this point in the history
  • Loading branch information
stakovicz authored Feb 9, 2025
1 parent cba6dbc commit 669a64b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 34 deletions.
30 changes: 0 additions & 30 deletions app/config/config.php

This file was deleted.

6 changes: 5 additions & 1 deletion app/config/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
imports:
- { resource: config.php }
- { resource: security.yml }
- { resource: services.yml }

Expand Down Expand Up @@ -357,6 +356,11 @@ ting:
port: "%database_port%"
user: "%database_user%"
password: "%database_password%"

databases_options:
main:
timezone: "%database_timezone%"

repositories:
event:
namespace : AppBundle\Event\Model\Repository
Expand Down
3 changes: 2 additions & 1 deletion sources/Afup/Corporate/_Site_Base_De_Donnees.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public function __construct()
$conf->obtenir('database_name'),
$conf->obtenir('database_user'),
$conf->obtenir('database_password'),
$conf->obtenir('database_port')
$conf->obtenir('database_port'),
$conf->obtenir('database_timezone'),
);
}
}
9 changes: 7 additions & 2 deletions sources/Afup/Utils/Base_De_Donnees.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,28 @@ class Base_De_Donnees
* @param string $database Nom de la base
* @param string $user Nom de l'utilisateur
* @param string $password Mot de passe
* @param string $timezone Timezone de la base de données
* @return void
*/
public function __construct($host, $database, $user, $password, $port = null)
public function __construct($host, $database, $user, $password, $port = null, $timezone = null)
{
$this->config = [
'host' => $host,
'database' => $database,
'user' => $user,
'password' => $password,
'port' => $port,
'timezone' => $timezone,
];
}

public function getDbLink()
{
if ($this->link === null) {
$this->link = mysqli_connect($this->config['host'], $this->config['user'], $this->config['password'], null, (int) $this->config['port']) or die('Connexion à la base de données impossible');
$this->link = mysqli_connect($this->config['host'], $this->config['user'], $this->config['password'], null, $this->config['port']) or die('Connexion à la base de données impossible');
if ($this->config['timezone']) {
mysqli_query($this->link, "SET time_zone = '" . $this->config['timezone'] . "'");
}
mysqli_set_charset($this->link, "utf8mb4");
$this->selectionnerBase($this->config['database']);
}
Expand Down
30 changes: 30 additions & 0 deletions sources/AppBundle/AppBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,38 @@

namespace AppBundle;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class AppBundle extends Bundle
{
public function build(ContainerBuilder $container): void
{
$path = dirname(__FILE__, 3);
$envFile = is_file($path . '/.env') ? $path . '/.env' : $path . '/.env.dist';
$lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
if (strpos(trim($line), '#') === 0) {
continue;
}

[$name, $default] = explode('=', $line, 2);
// On va chercher dans les variables d'env en premier
if (!$value = getenv($name)) {
$value = $default;
}
$name = strtolower(trim($name));
$value = trim($value);
if (is_numeric($value)) {
$value = (int) $value;
}

$container->setParameter($name, $value);
}
// On calcul l'offset de la timezone pour MySQL
// car la base de données est en UTC et la base n'accepte que les offset.
$container->setParameter('database_timezone', date('P'));

parent::build($container);
}
}

0 comments on commit 669a64b

Please sign in to comment.