singleton pour la connection PDO

This commit is contained in:
Rudy Masson 2022-05-03 16:25:38 +02:00
parent 98733f3a01
commit 28f6b96951
4 changed files with 59 additions and 7 deletions

45
src/Pdo/PdoConnect.php Normal file
View File

@ -0,0 +1,45 @@
<?php
namespace App\Pdo;
use PDO;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class PdoConnect extends AbstractController
{
/**
* @var Singleton
* @access private
* @static
*/
private static $_instance = null;
/**
* Constructeur de la classe
*
* @param void
* @return void
*/
private function __construct() {
}
/**
* Méthode qui crée l'unique instance de la classe
* si elle n'existe pas encore puis la retourne.
*
* @param void
* @return PdoConnect
*/
public static function getInstance() {
if(is_null(self::$_instance)) {
self::$_instance = new PdoConnect();
}
return self::$_instance;
}
public function connect($urlDatabase, $dbUser, $dbPassword)
{
return new PDO($urlDatabase, $dbUser, $dbPassword);
}
}

View File

@ -2,6 +2,7 @@
namespace App\Services;
use App\Pdo\PdoConnect;
use PDO;
use PDOException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@ -16,15 +17,11 @@ class PdoServices extends AbstractController
$this->params = $params;
}
public function connection()
{
return new PDO($this->params->get('urlDatabase'), $this->params->get('dbUser'), $this->params->get('dbPassword'));
}
public function fetchDatas($email)
{
try {
$dbh = $this->connection();
$pdo = PdoConnect::getInstance();
$dbh = $pdo->connect($this->params->get('urlDatabase'), $this->params->get('dbUser'), $this->params->get('dbPassword'));
$query = $dbh->prepare($this->getParameter('queryFetchDatas'));
$query->execute(['email'=> $email]);
$datas = $query->fetch(PDO::FETCH_ASSOC);

View File

@ -4300,3 +4300,13 @@
2022-05-03 14:32:13,009 INFO success: php-fpm entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2022-05-03 14:32:13,011 INFO spawned: 'rsyslog' with pid 77
2022-05-03 14:32:14,019 INFO success: rsyslog entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2022-05-03 16:04:28,347 CRIT Supervisor is running as root. Privileges were not dropped because no user is specified in the config file. If you intend to run as root, you can set user=root in the config file to avoid this message.
2022-05-03 16:04:28,349 INFO RPC interface 'supervisor' initialized
2022-05-03 16:04:28,349 CRIT Server 'unix_http_server' running without any HTTP authentication checking
2022-05-03 16:04:28,349 INFO supervisord started with pid 26
2022-05-03 16:04:29,352 INFO spawned: 'apache2' with pid 27
2022-05-03 16:04:29,353 INFO spawned: 'php-fpm' with pid 28
2022-05-03 16:04:29,353 INFO spawned: 'rsyslog' with pid 29
2022-05-03 16:04:30,374 INFO success: apache2 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2022-05-03 16:04:30,374 INFO success: php-fpm entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2022-05-03 16:04:30,374 INFO success: rsyslog entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)

View File

@ -1 +1 @@
12
26