Commit 7441415a authored by François Agneray's avatar François Agneray
Browse files

Ajout classe entity manager metamodel factory

parent b942c408
......@@ -33,17 +33,31 @@ class Instance implements \JsonSerializable
/**
* @var string
*
*
* @Column(type="string", nullable=false)
*/
protected $dbname;
protected $pathProxy;
/**
* @var bool
*
* @Column(type="boolean", nullabel=false)
*/
protected $devMode;
/**
* @var string
*
*
* @Column(type="string", nullable=false)
*/
protected $type;
protected $driver;
/**
* @var string
*
* @Column(type="string", nullable=false)
*/
protected $path;
/**
* @var string
......@@ -59,6 +73,13 @@ class Instance implements \JsonSerializable
*/
protected $port;
/**
* @var string
*
* @Column(type="string", nullable=false)
*/
protected $dbname;
/**
* @var string
*
......@@ -73,6 +94,9 @@ class Instance implements \JsonSerializable
*/
protected $password;
/**
* Create the classe with the property name (primary key)
*/
public function __construct(string $name)
{
$this->name = $name;
......@@ -83,7 +107,7 @@ class Instance implements \JsonSerializable
*
* @return string
*/
public function getName()
public function getName(): string
{
return $this->name;
}
......@@ -93,7 +117,7 @@ class Instance implements \JsonSerializable
*
* @return string
*/
public function getLabel()
public function getLabel(): string
{
return $this->label;
}
......@@ -103,90 +127,208 @@ class Instance implements \JsonSerializable
*
* @param string $label
*/
public function setLabel($label)
public function setLabel(string $label): void
{
$this->label = $label;
}
/**
* Getter property dbname
*
* Getter property pathProxy
*
* @return string
*/
public function getDbName()
public function getPathProxy(): string
{
return $this->dbname;
return $this->pathProxy;
}
/**
* Setter property dbname
*
* @param string $dbname
* Setter property pathProxy
*
* @param string $pathProxy
*/
public function setDbName($dbname)
public function setPathProxy(string $pathProxy): string
{
$this->dbname = $dbname;
return $this->pathProxy = $pathProxy;
}
/**
* Getter property devMode
*
* @return bool
*/
public function getDevMode(): bool
{
return $this->devMode;
}
/**
* @Setter property devMode
*
* @param bool $devMode
*/
public function setDevMode(bool $devMode): void
{
$this->devMode = $devMode;
}
/**
* Getter property driver
*
* @return string
*/
public function getDriver(): string
{
return $this->driver;
}
/**
* Setter property driver
*
* @param string $driver
*/
public function setDriver(string $driver): void
{
$this->driver = $driver;
}
public function getType()
/**
* Getter property path
*
* @return string
*/
public function getPath(): string
{
return $this->type;
return $this->path;
}
public function setType($type)
/**
* Setter property path
*
* @param string $path
*/
public function setPath(string $path): void
{
$this->type = $type;
$this->path = $path;
}
public function getHost()
/**
* Getter property host
*
* @return string
*/
public function getHost(): string
{
return $this->host;
}
public function setHost($host)
/**
* Setter property host
*
* @param string $host
*/
public function setHost(string $host): void
{
$this->host = $host;
}
public function getPort()
/**
* Getter property port
*
* @return int
*/
public function getPort(): int
{
return $this->port;
}
public function setPort($port)
/**
* Setter property port
*
* @param int $port
*/
public function setPort(int $port): void
{
$this->port = $port;
}
public function getLogin()
/**
* Getter property dbname
*
* @return string
*/
public function getDbName(): string
{
return $this->dbname;
}
/**
* Setter property dbname
*
* @param string $dbname
*/
public function setDbName(string $dbname): void
{
$this->dbname = $dbname;
}
/**
* Getter property login
*
* @return string
*/
public function getLogin(): string
{
return $this->login;
}
public function setLogin($login)
/**
* Setter property login
*
* @param string $login
*/
public function setLogin(string $login): void
{
$this->login = $login;
}
public function getPassword()
/**
* Getter property password
*
* @return string
*/
public function getPassword(): string
{
return $this->password;
}
public function setPassword($password)
/**
* Setter property password
*
* @param string $password
*/
public function setPassword(string $password): void
{
$this->password = $password;
}
public function jsonSerialize()
/**
* Return objet to array
*
* @return array
*/
public function jsonSerialize(): array
{
return [
'id' => $this->getId(),
'name' => $this->getName(),
'label' => $this->getLabel(),
'dbname' => $this->getDbName(),
'dbtype' => $this->getType(),
'path_proxy' => $this->getPathProxy(),
'dev_mode' => $this->getDevMode(),
'driver' => $this->getDriver(),
'path' => $this->getPath(),
'dbhost' => $this->getHost(),
'dbport' => $this->getPort(),
'dbname' => $this->getDbName(),
'dblogin' => $this->getLogin(),
'dbpassword' => $this->getPassword()
];
......
<?php declare(strict_types=1);
/*
* This file is part of ANIS SERVER API.
*
* (c) François Agneray <francois.agneray@lam.fr>
* (c) Chrystel Moreau <chrystel.moreau@lam.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Utils;
use Doctrine\ORM\EntityManager;
use App\Entity\Admin\Instance;
/**
* Factory used to create a Doctrine Entity Manager to the project metamodel database
*
* @author François Agneray <francois.agneray@lam.fr>
* @package App\Utils
*/
class MetaEntityManagerFactory
{
/**
* Doctrine Entity Manager to the anis admin database
*
* @var EntityManager
*/
private $adminEntityManager;
/**
* Create the classe with the adminEntityManager object
*/
public function __construct(EntityManager $adminEntityManager)
{
$this->adminEntityManager = $adminEntityManager;
}
/**
* Create and return an Entity Manager to the correct metamodel database (databases, datasets, attributes...)
*
* @param string $instanceName Instance name necessary to find the correct metamodel database
* @return EntityManager
*/
public function getMetaEntityManager(string $instanceName): EntityManager
{
// Get instance information from anis admin database
$instance = $this->adminEntityManager->find('App\Entity\Admin\Instance', $instanceName);
// Create the entity manager to drive the project metamodel database (databases, datasets, attributes...)
$c = \Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration(
array('src/Entity/Metamodel'),
$instance->getDevMode()
);
$c->setProxyDir($instance->getPathProxy());
if ($instance->getDevMode()) {
$c->setAutoGenerateProxyClasses(true);
} else {
$c->setAutoGenerateProxyClasses(false);
}
return EntityManager::create($this->getConnectionOptions($instance), $c);
}
/**
* Returns the doctrine connection options array created with the $instance row object
*
* @param Instance $instance The doctrine object instance find with the $instanceName
* @return array
*/
private function getConnectionOptions(Instance $instance): array
{
return [
'driver' => $instance->getDriver(),
'path' => $instance->getPath(),
'host' => $instance->getHost(),
'port' => $instance->getPort(),
'dbname' => $instance->getDbName(),
'user' => $instance->getLogin(),
'password' => $instance->getPassword()
];
}
}
\ No newline at end of file
......@@ -39,24 +39,6 @@ $container['admin_em'] = function ($c) {
return $em;
};
// Metamodel database Doctrine 2 Entity Manager
$container['em'] = function ($c) {
$settings = $c->get('settings');
$metadata = $settings['metadata'];
$c = \Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration(
array('src/Entity/Metamodel'),
$metadata['dev_mode']
);
$c->setProxyDir($metadata['path_proxy']);
if ($metadata['dev_mode']) {
$c->setAutoGenerateProxyClasses(true);
} else {
$c->setAutoGenerateProxyClasses(false);
}
$em = \Doctrine\ORM\EntityManager::create($metadata['connection_options'], $c);
return $em;
};
// Monolog
$container['logger'] = function ($c) {
$settings = $c->get('settings');
......@@ -74,6 +56,12 @@ $container['mailer'] = function ($c) {
return $mailer;
};
// MetaEntityManagerFactory
$container['memf'] = function ($c) {
$adminEntityManager = $c->get('admin_em');
return new App\Utils\MetaEntityManagerFactory($adminEntityManager);
};
// OperatorFactory
$container['of'] = function ($c) {
return new App\Utils\Operator\OperatorFactory();
......
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