load doctrine extensions during kernel bootstrap #84 (#92)

This commit is contained in:
Kevin Papst
2018-01-16 21:35:23 +01:00
committed by GitHub
parent 3f221fcdb8
commit 0a65965187
11 changed files with 126 additions and 172 deletions

View File

@@ -24,37 +24,107 @@ use Symfony\Component\Yaml\Yaml;
class DoctrineCompilerPass implements CompilerPassInterface
{
/**
* @var string[]
*/
protected $allowedEngines = [
'mysql',
'oracle',
'postgres',
'sqlite'
];
/**
* @param ContainerBuilder $container
* @return array|false|null|string
* @throws \Exception
*/
protected function findEngine(ContainerBuilder $container)
{
$engine = null;
// TODO - this does return the wrong connection. it used to be mysql, even if
// TODO - getenv('DATABASE_URL') returned an sqlite:// connection string
/*
$dbConfig = $container->getExtensionConfig('doctrine');
if (isset($dbConfig[0]['dbal']['driver'])) {
$engine = str_replace('pdo_', '', $dbConfig[0]['dbal']['driver']);
}
*/
if ($engine === null) {
$dbConfig = explode('://', getenv('DATABASE_URL'));
$engine = $dbConfig['0'] ?: null;
}
if ($engine === null) {
$engine = getenv('DATABASE_ENGINE');
}
if ($engine === null) {
throw new \Exception(
'Could not detect database engine. Please set the environment config DATABASE_ENGINE ' .
'to one ' . implode(', ', $this->allowedEngines) . ', e.g. in your .env file: DATABASE_ENGINE=sqlite'
);
}
if (!in_array($engine, $this->allowedEngines)) {
throw new \Exception(
'Unsupported database engine: ' . $engine . '. Kimai only supports one of: ' .
implode(', ', $this->allowedEngines)
);
}
return $engine;
}
/**
* @param ContainerBuilder $container
* @return string
* @throws \Exception
*/
protected function getConfigFile(ContainerBuilder $container)
{
$engine = $this->findEngine($container);
$configDir = realpath(
$container->getParameter('kernel.project_dir') . '/vendor/beberlei/DoctrineExtensions/config/'
);
$configFile = $configDir . '/' . $engine . '.yml';
if (!file_exists($configFile)) {
throw new \Exception('Could not find config file for database engine. Looked at ' . $configFile);
}
return $configFile;
}
/**
* @param ContainerBuilder $container
* @throws \Exception
*/
public function process(ContainerBuilder $container)
{
$engine = $container->getParameter('database_engine');
if (null === $engine) {
throw new ParameterNotFoundException('database_engine');
}
$configFile = $this->getConfigFile($container);
$config = Yaml::parse(file_get_contents($configFile));
$ormConfigDef = $container->getDefinition('doctrine.orm.default_configuration');
$configDir = realpath($container->getParameter('kernel.root_dir') . '/config/');
$config = Yaml::parse(file_get_contents($configDir . '/' . $engine . '.yml'));
if (!isset($config['doctrine']['orm']['dql'])) {
return;
if (!isset($config['doctrine']['orm']['dql']) || empty($config['doctrine']['orm']['dql'])) {
throw new \Exception('could not load custom Doctrine functions from: ' . $configFile);
}
$sql = $config['doctrine']['orm']['dql'];
if (!empty($sql)) {
foreach ($sql['string_functions'] as $name => $function) {
$ormConfigDef->addMethodCall('addCustomStringFunction', array($name, $function));
}
foreach ($sql['numeric_functions'] as $name => $function) {
$ormConfigDef->addMethodCall('addCustomNumericFunction', array($name, $function));
}
foreach ($sql['datetime_functions'] as $name => $function) {
$ormConfigDef->addMethodCall('addCustomDatetimeFunction', array($name, $function));
}
$ormConfig = $container->getDefinition('doctrine.orm.default_configuration');
foreach ($sql['string_functions'] as $name => $function) {
$ormConfig->addMethodCall('addCustomStringFunction', array($name, $function));
}
foreach ($sql['numeric_functions'] as $name => $function) {
$ormConfig->addMethodCall('addCustomNumericFunction', array($name, $function));
}
foreach ($sql['datetime_functions'] as $name => $function) {
$ormConfig->addMethodCall('addCustomDatetimeFunction', array($name, $function));
}
}
}

View File

@@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/
namespace App\EventListener;
namespace App\Doctrine;
use App\Entity\UserPreference;
use Doctrine\Common\EventSubscriber;
@@ -22,7 +22,7 @@ use App\Entity\Timesheet;
*
* @author Kevin Papst <kevin@kevinpapst.de>
*/
class TimesheetListener implements EventSubscriber
class TimesheetSubscriber implements EventSubscriber
{
/**

View File

@@ -48,10 +48,7 @@ class Kernel extends BaseKernel
$loader->load($confDir.'/services'.self::CONFIG_EXTS, 'glob');
$loader->load($confDir.'/services_'.$this->environment.self::CONFIG_EXTS, 'glob');
// TODO active doctrine-extensions
// composer require beberlei/DoctrineExtensions
// $container->addCompilerPass(new DoctrineCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -1000);
$container->addCompilerPass(new DoctrineCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -1000);
}
protected function configureRoutes(RouteCollectionBuilder $routes)

View File

@@ -25,9 +25,10 @@ use App\Entity\Project;
class TimesheetQuery extends BaseQuery
{
const STATE_ALL = 0;
const STATE_RUNNING = 1;
const STATE_STOPPED = 2;
const STATE_ALL = 1;
const STATE_RUNNING = 2;
const STATE_STOPPED = 3;
/**
* Overwritten for different default order
* @var string
@@ -153,9 +154,15 @@ class TimesheetQuery extends BaseQuery
*/
public function setState($state)
{
if (!is_int($state) && $state != (int) $state) {
return $this;
}
$state = (int) $state;
if (in_array($state, [self::STATE_ALL, self::STATE_RUNNING, self::STATE_STOPPED], true)) {
$this->state = $state;
}
return $this;
}
}

View File

@@ -14,13 +14,14 @@ namespace App\Repository;
use App\Entity\User;
use App\Model\UserStatistic;
use App\Repository\Query\UserQuery;
use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface;
/**
* Class UserRepository
*
* @author Kevin Papst <kevin@kevinpapst.de>
*/
class UserRepository extends AbstractRepository
class UserRepository extends AbstractRepository implements UserLoaderInterface
{
/**
@@ -65,4 +66,20 @@ class UserRepository extends AbstractRepository
return $this->getPager($qb->getQuery(), $query->getPage(), $query->getPageSize());
}
/**
* @param string $username
* @return mixed|null|\Symfony\Component\Security\Core\User\UserInterface
* @throws \Doctrine\ORM\NoResultException
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function loadUserByUsername($username)
{
return $this->createQueryBuilder('u')
->where('u.username = :username')
->orWhere('u.email = :username')
->setParameter('username', $username)
->getQuery()
->getSingleResult();
}
}