Commit 957cda94 authored by Avris's avatar Avris

v4.0

parent 6f9aa235
orm:
class: Avris\Micrus\Doctrine\Doctrine
params:
- '@logger'
- '@env'
- '@rootDir'
- '@config.database'
- '#modelDirectory'
- '%APP_DEBUG%'
- '{@rootDir}/var/cache/{@env}/doctrine'
- '#module'
- '@logger'
- '@?doctrineCache'
events: ['cacheClear', 'cacheWarmup', 'console']
tags: ['defaultParameters']
......
<?php
namespace Avris\Micrus\Doctrine;
use Avris\Bag\Bag;
use Avris\Micrus\Bootstrap\ModuleInterface;
use Avris\Micrus\Console\ConsoleEvent;
use Avris\Micrus\Model\ModelDirectory;
use Avris\Micrus\Model\ORM;
use Avris\Micrus\Tool\Config\ParametersProvider;
use Doctrine\Common\Cache\ClearableCache;
......@@ -19,42 +20,28 @@ class Doctrine implements ORM, ParametersProvider
/** @var EntityManager */
protected $entityManager;
/** @var string */
protected $cacheDir;
public function __construct(LoggerInterface $logger, $env, $rootDir, $db, array $modelDirectories, Cache $cache = null)
{
if (empty($db)) {
$logger->warning('No ORM configuration');
$db = ['driver' => 'pdo_sqlite', 'memory' => true];
}
$logger->debug(sprintf('Doctrine ORM, database driver: %s', $db['driver']));
$this->cacheDir = $rootDir . '/var/cache/' . $env . '/doctrine'; // TODO inject
$dirs = [$rootDir . '/src/Model']; // TODO unify
foreach ($modelDirectories as $dir) {
$dirs[] = $dir->getDirectory();
public function __construct(
Bag $connection,
bool $debug,
string $proxyDir,
array $modules,
LoggerInterface $logger,
Cache $cache = null
) {
$dirs = [];
/** @var ModuleInterface $module */
foreach (array_reverse($modules) as $module) {
if (is_dir($dir = $module->getDir() . '/src/Entity')) {
$dirs[] = $dir;
}
}
$config = Setup::createAnnotationMetadataConfiguration(
$dirs,
$env == 'dev',
$this->cacheDir,
$cache,
false
);
$config->setRepositoryFactory(new MicrusRepositoryFactory());
$config = Setup::createAnnotationMetadataConfiguration($dirs, $debug, $proxyDir, $cache, false);
$config->setSQLLogger(new DoctrineLogger($logger));
if (empty($db['charset'])) {
$db['charset'] = 'utf8mb4';
}
$this->entityManager = EntityManager::create($db, $config);
$this->entityManager = EntityManager::create($connection->all(), $config);
// TODO
$warmup = $this->onCacheWarmup();
if ($warmup) {
$logger->notice($warmup);
......@@ -105,24 +92,27 @@ class Doctrine implements ORM, ParametersProvider
public function onCacheWarmup()
{
if (file_exists($this->cacheDir)) {
$proxyDir = $this->entityManager->getConfiguration()->getProxyDir();
if (file_exists($proxyDir)) {
return false;
}
mkdir($this->cacheDir, 0777, true);
mkdir($proxyDir, 0777, true);
$metadatas = $this->entityManager->getMetadataFactory()->getAllMetadata();
$this->entityManager->getProxyFactory()->generateProxyClasses($metadatas, $this->cacheDir);
$this->entityManager->getProxyFactory()->generateProxyClasses($metadatas, $proxyDir);
return sprintf(
'Metadata for %s generated in %s',
implode(', ', array_map(function($el) { return $el->name; }, $metadatas)),
$this->cacheDir
$proxyDir
);
}
public function onConsole(ConsoleEvent $event)
{
Autoloader::register($this->cacheDir, 'DoctrineProxies');
$proxyDir = $this->entityManager->getConfiguration()->getProxyDir();
Autoloader::register($proxyDir, 'DoctrineProxies');
if (!$this->entityManager) {
return;
......
<?php
namespace Avris\Micrus\Doctrine;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Repository\DefaultRepositoryFactory;
use Doctrine\ORM\Repository\RepositoryFactory;
class MicrusRepositoryFactory implements RepositoryFactory
{
/** @var DefaultRepositoryFactory */
protected $defaultFactory;
public function __construct()
{
$this->defaultFactory = new DefaultRepositoryFactory();
}
public function getRepository(EntityManagerInterface $entityManager, $entityName)
{
return $this->defaultFactory->getRepository(
$entityManager,
strpos($entityName, '\\') === false
? 'App\\Model\\' . ucfirst($entityName)
: $entityName
);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment