123 lines
4.6 KiB
PHP
123 lines
4.6 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() {}
|
|
|
|
/**
|
|
* @return Application
|
|
*/
|
|
public static function getInstance() {
|
|
if (!Application::$instance instanceof self) {
|
|
Application::$instance = new self();
|
|
}
|
|
return Application::$instance;
|
|
}
|
|
|
|
/**
|
|
* Get the apps config
|
|
* @return array
|
|
*/
|
|
public function getConfig() {
|
|
return $this->config;
|
|
}
|
|
|
|
/**
|
|
* @return EntityManager
|
|
*/
|
|
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());
|
|
$em_config->setCustomDatetimeFunctions([
|
|
'DATE' => 'DoctrineExtensions\Query\Mysql\Date',
|
|
]);
|
|
$em_config->setCustomNumericFunctions(array(
|
|
'ACOS' => 'DoctrineExtensions\Query\Mysql\Acos',
|
|
'ASIN' => 'DoctrineExtensions\Query\Mysql\Asin',
|
|
'ATAN' => 'DoctrineExtensions\Query\Mysql\Atan',
|
|
'ATAN2' => 'DoctrineExtensions\Query\Mysql\Atan2',
|
|
'BIT_COUNT' => 'DoctrineExtensions\Query\Mysql\BitCount',
|
|
'BIT_XOR' => 'DoctrineExtensions\Query\Mysql\BitXor',
|
|
'COS' => 'DoctrineExtensions\Query\Mysql\Cos',
|
|
'COT' => 'DoctrineExtensions\Query\Mysql\Cot',
|
|
'DEGREES' => 'DoctrineExtensions\Query\Mysql\Degrees',
|
|
'LOG10' => 'DoctrineExtensions\Query\Mysql\Log10',
|
|
'RADIANS' => 'DoctrineExtensions\Query\Mysql\Radians',
|
|
'STDDEV' => 'DoctrineExtensions\Query\Mysql\StdDev',
|
|
'SIN' => 'DoctrineExtensions\Query\Mysql\Sin',
|
|
'TAN' => 'DoctrineExtensions\Query\Mysql\Tan',
|
|
'RAND' => 'DoctrineExtensions\Query\Mysql\Rand',
|
|
));
|
|
|
|
$connectionConfig = new Configuration();
|
|
$connectionParams = array(
|
|
'pdo' => new \PDO(
|
|
$config['db']['dsn'],
|
|
isSet($config['db']['username']) ? $config['db']['username'] : null,
|
|
isSet($config['db']['password']) ? $config['db']['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', 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;
|
|
}
|
|
}
|