Skip to content

Instantly share code, notes, and snippets.

@avtehnik
Created April 5, 2018 15:04
Show Gist options
  • Save avtehnik/a6adb6c6de41d09b1db8344ece6850d8 to your computer and use it in GitHub Desktop.
Save avtehnik/a6adb6c6de41d09b1db8344ece6850d8 to your computer and use it in GitHub Desktop.
include doctrine without symfony
<?php
use Doctrine\ORM\Tools\Console\ConsoleRunner;
define('ABSPATH', __DIR__ . '/../');
require_once('vendor/autoload.php');
require_once('doctrine-loader.php');
DoctrineOrm::init(
[
'applicationMode' => 'development',
'db' => [
'driver' => 'pdo_mysql',
'user' => DB_USER,
'host' => DB_HOST,
'password' => DB_PASSWORD,
'dbname' => DB_NAME
]
]
);
return ConsoleRunner::createHelperSet(DoctrineOrm::getEntityManager());
{
"name": "name",
"type": "project",
"require": {
"bugsnag/bugsnag": "^3.0",
"doctrine/orm": "^2.6",
"doctrine/dbal": "^2.6",
"stof/doctrine-extensions-bundle": "^1.3"
},
"require-dev": {
"phpunit/phpunit": "^7",
"guzzlehttp/guzzle": "^6.3"
},
"autoload": {
"psr-0": {
"": "src/"
}
}
}
<?php
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Proxy\ProxyFactory;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\Common\Annotations\AnnotationReader;
class DoctrineOrm
{
private static $instance = null;
private $em = null;
private static $params = [];
public static function init($params)
{
self::$params = $params;
}
private function __construct($params)
{
$applicationMode = $params['applicationMode'];
$connectionOptions = $params['db'];
if ($applicationMode === 'development') {
$isDevMode = true;
$cache = new \Doctrine\Common\Cache\ArrayCache();
} else {
$isDevMode = false;
$cache = new \Doctrine\Common\Cache\ApcuCache();
}
$paths = [ABSPATH . "src/App/Entity"];
$annotationReader = new Doctrine\Common\Annotations\AnnotationReader();
$cachedAnnotationReader = new Doctrine\Common\Annotations\CachedReader(
$annotationReader,
$cache
);
$driverChain = new Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain();
$driverChain->addDriver(new AnnotationDriver(new AnnotationReader(), $paths), 'App\Entity');
Gedmo\DoctrineExtensions::registerAbstractMappingIntoDriverChainORM(
$driverChain,
$cachedAnnotationReader
);
$evm = new Doctrine\Common\EventManager();
$timestampableListener = new Gedmo\Timestampable\TimestampableListener();
$timestampableListener->setAnnotationReader($cachedAnnotationReader);
$evm->addEventSubscriber($timestampableListener);
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode, null, null, false);
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);
$config->setMetadataDriverImpl($driverChain);
$config->setProxyDir(sys_get_temp_dir());
$config->setProxyDir(ABSPATH . 'cache/orm');
$config->setProxyNamespace('Proxy');
if ($applicationMode === 'development') {
$config->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_ALWAYS);
} else {
$config->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS);
}
$this->em = EntityManager::create($connectionOptions, $config, $evm);
$conn = $this->em->getConnection();
$conn->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
$conn->getDatabasePlatform()->registerDoctrineTypeMapping('set', 'string');
}
public static function getInstance()
{
if (self::$instance == null) {
self::$instance = new DoctrineOrm(self::$params);
}
return self::$instance;
}
/**
* @return EntityManager
*/
public function getEm()
{
return $this->em;
}
/**
* @return EntityManager
*/
public static function getEntityManager()
{
return DoctrineOrm::getInstance()->getEm();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment