|
|
@ -0,0 +1,58 @@ |
|
|
|
|
|
<?php |
|
|
|
|
|
|
|
|
|
|
|
namespace Common; |
|
|
|
|
|
|
|
|
|
|
|
use Doctrine\Common\Proxy\AbstractProxyFactory; |
|
|
|
|
|
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) : EntityManager |
|
|
|
|
|
{ |
|
|
|
|
|
self::checkRequiredParams($params, ['paths', 'database', 'proxy_dir']); |
|
|
|
|
|
|
|
|
|
|
|
$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 ? AbstractProxyFactory::AUTOGENERATE_ALWAYS : AbstractProxyFactory::AUTOGENERATE_NEVER); |
|
|
|
|
|
|
|
|
|
|
|
$connection = DriverManager::getConnection($params['database'], $config); |
|
|
|
|
|
$em = new EntityManager($connection, $config); |
|
|
|
|
|
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)); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |