You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
3.3 KiB
100 lines
3.3 KiB
<?php
|
|
|
|
namespace Common;
|
|
|
|
use Doctrine\Common\Proxy\AbstractProxyFactory;
|
|
use Doctrine\DBAL\Schema\AbstractNamedObject;
|
|
use Doctrine\ORM\ORMSetup;
|
|
use Doctrine\ORM\EntityManager;
|
|
use Doctrine\DBAL\DriverManager;
|
|
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
|
|
use Symfony\Component\Cache\Adapter\ArrayAdapter;
|
|
use RuntimeException;
|
|
|
|
/**
|
|
* Factory for creating Doctrine entity manager - designed to work with DI Containers
|
|
*
|
|
* @author Jan Pavlíček <jan.pavlicek@altekpro.cz>
|
|
* @since 1.0.0
|
|
*/
|
|
class EntityManagerFactory
|
|
{
|
|
public static function create(
|
|
array $params,
|
|
bool $dev_mode = false,
|
|
array $middlewares = [],
|
|
array $filters = [],
|
|
array $event_subscribers = [],
|
|
array $skip_names_in_schema_tool = []
|
|
) : EntityManager {
|
|
self::checkRequiredParams($params, ['paths', 'database', 'proxy_dir']);
|
|
|
|
// workaround for doctrine console commands requiring active database connection -
|
|
// specifying server version explicitly doesn't trigger this connection, which is not
|
|
// needed for proxy generation
|
|
if (defined('STDIN') && @$_SERVER['argv'][1] == 'orm:generate-proxies') {
|
|
$params['database']['serverVersion'] = '14';
|
|
}
|
|
|
|
$paths = $params['paths'];
|
|
$connection = $params['database'];
|
|
|
|
$cache_adapter = $dev_mode ? new ArrayAdapter : new FilesystemAdapter('', 0, $params['cache_dir']);
|
|
|
|
$config = ORMSetup::createAttributeMetadataConfiguration($paths, $dev_mode, $params['proxy_dir'], $cache_adapter);
|
|
$config->setAutoGenerateProxyClasses($dev_mode ? true : false);
|
|
|
|
if ($middlewares) $config->setMiddlewares($middlewares);
|
|
|
|
foreach ($filters as $name => $class) {
|
|
$config->addFilter($name, $class);
|
|
}
|
|
|
|
$connection = DriverManager::getConnection($params['database'], $config);
|
|
|
|
// add schema access filter, if passed - this allows for controlling which object in database are considered
|
|
// by doctrine when generating SQL scripts to sync with metadata
|
|
if ($skip_names_in_schema_tool) {
|
|
$connection->getConfiguration()->setSchemaAssetsFilter(function (AbstractNamedObject|string $object) use ($skip_names_in_schema_tool) {
|
|
$object_name = is_string($object) ? $object : $object->getObjectName()->toString();
|
|
foreach ($skip_names_in_schema_tool as $name) {
|
|
if (preg_match("/$name/", $object_name)) return false;
|
|
}
|
|
|
|
return true;
|
|
});
|
|
}
|
|
|
|
$em = new EntityManager($connection, $config);
|
|
|
|
if ($event_subscribers) {
|
|
$evm = $em->getEventManager();
|
|
foreach ($event_subscribers as $subscriber) {
|
|
$evm->addEventSubscriber($subscriber);
|
|
}
|
|
}
|
|
|
|
return $em;
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Validation method for checking parameters
|
|
*
|
|
* @throws RuntimeException
|
|
*/
|
|
private static function checkRequiredParams(array $params, array $required) : void
|
|
{
|
|
$errors = [];
|
|
foreach ($required as $name) {
|
|
if (!isset($params[$name])) {
|
|
$errors[] = "Required parameter '$name' not found in array.";
|
|
}
|
|
}
|
|
|
|
if ($errors) {
|
|
throw new RuntimeException(implode(' ', $errors));
|
|
}
|
|
}
|
|
}
|