Commit d4624b66 authored by Avris's avatar Avris

v4.0

parent 83706831
orm:
class: Avris\Micrus\Doctrine\Doctrine
Avris\Micrus\Doctrine\Doctrine:
params:
- '@config.database'
- '%APP_DEBUG%'
......@@ -7,8 +6,7 @@ orm:
- '#module'
- '@Psr\Log\LoggerInterface'
- '@?doctrineCache'
#events: ['cacheClear', 'cacheWarmup', 'console']
userProvider:
class: Avris\Micrus\Doctrine\DoctrineUserProvider
params: ['@orm']
params: ['@Avris\Micrus\Doctrine\Doctrine']
......@@ -2,9 +2,9 @@
namespace Avris\Micrus\Doctrine;
use Avris\Bag\Bag;
use Avris\Micrus\Bootstrap\EventSubscriberInterface;
use Avris\Micrus\Bootstrap\ModuleInterface;
use Avris\Micrus\Console\ConsoleEvent;
use Avris\Micrus\Model\ORM;
use Avris\Micrus\Tool\Config\ParametersProvider;
use Doctrine\Common\Cache\ClearableCache;
use Doctrine\Common\Proxy\Autoloader;
......@@ -15,7 +15,7 @@ use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
use Doctrine\ORM\Tools\Setup;
use Psr\Log\LoggerInterface;
class Doctrine implements ORM, ParametersProvider
class Doctrine implements ParametersProvider, EventSubscriberInterface
{
/** @var EntityManager */
protected $entityManager;
......@@ -53,26 +53,6 @@ class Doctrine implements ORM, ParametersProvider
return $this->entityManager;
}
public function find($type, $id)
{
return $this->entityManager->getRepository($type)->find($id);
}
public function findAll($type)
{
return $this->entityManager->getRepository($type)->findAll();
}
public function findBy($type, $attribute, $value)
{
return $this->entityManager->getRepository($type)->findBy([$attribute => $value]);
}
public function findOneBy($type, $attribute, $value)
{
return $this->entityManager->getRepository($type)->findOneBy([$attribute => $value]);
}
// TODO re-think those three events
public function onCacheClear()
......@@ -113,13 +93,13 @@ class Doctrine implements ORM, ParametersProvider
public function onConsole(ConsoleEvent $event)
{
$proxyDir = $this->entityManager->getConfiguration()->getProxyDir();
Autoloader::register($proxyDir, 'DoctrineProxies');
if (!$this->entityManager) {
return;
}
$proxyDir = $this->entityManager->getConfiguration()->getProxyDir();
Autoloader::register($proxyDir, 'DoctrineProxies');
$app = $event->getApp();
$helperSet = $app->getHelperSet();
$helperSet->set(new ConnectionHelper($this->entityManager->getConnection()), 'db');
......@@ -149,4 +129,9 @@ class Doctrine implements ORM, ParametersProvider
],
];
}
public function getSubscribedEvents(): iterable
{
yield 'console' => [$this, 'onConsole'];
}
}
<?php
namespace Avris\Micrus\Doctrine;
use Avris\Micrus\Exception\NotFoundException;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\MappingException;
class DoctrineAutomatcher // implements Something
{
/** @var EntityManager */
private $em;
public function __construct(EntityManagerInterface $entityManager)
{
$this->em = $entityManager;
}
public function supports(string $className)
{
try {
$this->em->getClassMetadata($className);
return true;
} catch (MappingException $e) {
return false;
}
}
public function fetch(string $className, string $key, $value)
{
$repo = $this->em->getRepository($className);
$object = $repo->findOneBy([$key => $value]);
if (!$object) {
throw new NotFoundException(sprintf(
'Cannot find an object of type "%s" with %s=%s',
$className,
$key,
$value
));
}
return $object;
}
}
<?php
namespace Avris\Micrus\Doctrine;
use Avris\Micrus\Console\Task\AbstractFixturesTask;
use Avris\Micrus\Console\Task\AbstractFixturesCommand;
abstract class DoctrineFixturesTask extends AbstractFixturesTask
abstract class DoctrineFixturesCommand extends AbstractFixturesCommand
{
protected function truncateDatabase()
{
......
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