api.emotionhero.com/src/Application.php

91 lines
3.2 KiB
PHP

<?php
namespace EmotionHero;
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Logging\DebugStack;
use Doctrine\DBAL\Types\Type;
class Application {
// object instance
private static $instance;
private $config;
private $em;
private function __construct() {
$default_config = require __DIR__ . "/../config/_default.php";
$env_config = require __DIR__ . "/../config/".APPLICATION_ENV.".php";
$this->config = array_merge($default_config, $env_config);
}
private function __clone() {}
public static function getInstance() {
if (!Application::$instance instanceof self) {
Application::$instance = new self();
}
return Application::$instance;
}
public function getConfig() {
return $this->config;
}
public function getEm() {
if(!$this->em) {
$config = $this->getConfig();
$em_config = Setup::createAnnotationMetadataConfiguration([__DIR__.'/../src/'], $config['debug'], __DIR__ . '/../cache/proxy',null,false);
$em_config->addFilter('soft-deleteable', \Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter::class);
// TODO: make sure APCu is enabled (in test AND live!!)
// $em_config->setQueryCacheImpl(new \Doctrine\Common\Cache\ApcuCache());
// $em_config->setResultCacheImpl(new \Doctrine\Common\Cache\ApcuCache());
// $em_config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ApcuCache());
$connectionConfig = new Configuration();
$connectionParams = array(
'pdo' => new \PDO(
$config['db']['dsn'],
isSet($config['username']) ? $config['username'] : null,
isSet($config['password']) ? $config['password'] : null
)
);
if($config['debug'])
{
$doctrine_debug_stack = new DebugStack();
$connectionConfig->setSQLLogger($doctrine_debug_stack);
}
$conn = DriverManager::getConnection($connectionParams, $connectionConfig);
$platform = $conn->getDatabasePlatform();
$platform->registerDoctrineTypeMapping('enum', 'string');
Type::addType('utctime', EmotionHero\Tools\UTCDateTimeType::class);
$platform->registerDoctrineTypeMapping('datetime', 'utctime');
// AnnotationRegistry::registerAutoloadNamespace("Hateoas\Configuration", __DIR__."/vendor/willdurand/hateoas/src/");
AnnotationRegistry::registerAutoloadNamespace("JMS\Serializer", __DIR__."/../vendor/jms/serializer/src/");
AnnotationRegistry::registerAutoloadNamespace("Gedmo\Mapping", __DIR__."/../vendor/gedmo/doctrine-extensions/lib/");
// obtaining the entity manager
$_em = EntityManager::create($conn, $em_config);
// Need to enable explicitly: https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/softdeleteable.md
$_em->getFilters()->enable('soft-deleteable');
$_em->getEventManager()->addEventSubscriber(new \Gedmo\SoftDeleteable\SoftDeleteableListener());
$_em->getEventManager()->addEventSubscriber(new \Gedmo\Timestampable\TimestampableListener());
$this->em = $_em;
}
return $this->em;
}
}