You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.4 KiB
64 lines
1.4 KiB
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Silex\DI;
|
|
|
|
use Silex\Gateway\NewsGateway;
|
|
use PDO;
|
|
use Silex\Gateway\UserGateway;
|
|
use Silex\Router\Router;
|
|
use Silex\Security\Security;
|
|
|
|
class DI
|
|
{
|
|
private Router $router;
|
|
private ?PDO $pdo = null;
|
|
private ?NewsGateway $newsGateway = null;
|
|
private ?UserGateway $userGateway = null;
|
|
private ?Security $security = null;
|
|
|
|
public function __construct(Router $router)
|
|
{
|
|
$this->router = $router;
|
|
}
|
|
|
|
public function getRouter(): Router
|
|
{
|
|
return $this->router;
|
|
}
|
|
|
|
public function getNewsGateway(): NewsGateway
|
|
{
|
|
if ($this->newsGateway === null) {
|
|
$this->newsGateway = new NewsGateway($this->getPDO());
|
|
}
|
|
return $this->newsGateway;
|
|
}
|
|
|
|
public function getUserGateway(): UserGateway
|
|
{
|
|
if ($this->userGateway === null) {
|
|
$this->userGateway = new UserGateway($this->getPDO());
|
|
}
|
|
return $this->userGateway;
|
|
}
|
|
|
|
public function getSecurity(): Security
|
|
{
|
|
if ($this->security === null) {
|
|
session_start();
|
|
$this->security = new Security($this->getUserGateway(), $_SESSION);
|
|
}
|
|
return $this->security;
|
|
}
|
|
|
|
private function getPDO(): PDO
|
|
{
|
|
if ($this->pdo === null) {
|
|
return new PDO(sprintf('mysql:host=%s;dbname=%s', DB_HOST, DB_DATABASE), DB_USER, DB_PASSWORD);
|
|
}
|
|
return $this->pdo;
|
|
}
|
|
}
|