1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69:
<?php
namespace Faulancer\Service\Factory;
use Faulancer\Service\DbService;
use ORM\DbConfig;
use ORM\EntityManager;
use Faulancer\Service\Config;
use Faulancer\ServiceLocator\FactoryInterface;
use Faulancer\ServiceLocator\ServiceLocatorInterface;
class DbServiceFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
$config = $serviceLocator->get(Config::class);
$type = $config->get('db:type');
$name = $config->get('db:name');
$user = $config->get('db:username');
$pass = $config->get('db:password');
$host = $config->get('db:host') ?: 'localhost';
$port = '';
$attributes = [];
if ($type === 'mysql') {
$attributes = [
\PDO::MYSQL_ATTR_INIT_COMMAND => "SET sql_mode ='ANSI_QUOTES', NAMES utf8",
\PDO::ATTR_EMULATE_PREPARES => false,
];
$port = $config->get('db:port') ?: 3306;
}
$dbConf = new DbConfig(
$type,
$name,
$user,
$pass,
$host,
$port,
$attributes
);
$entityManager = new EntityManager([EntityManager::OPT_CONNECTION => $dbConf]);
return new DbService($entityManager);
}
}