1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Micro framework package. |
7
|
|
|
* |
8
|
|
|
* (c) Stanislau Komar <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Micro\Plugin\Doctrine; |
15
|
|
|
|
16
|
|
|
use Micro\Component\DependencyInjection\Container; |
17
|
|
|
use Micro\Framework\Kernel\KernelInterface; |
18
|
|
|
use Micro\Framework\Kernel\Plugin\ConfigurableInterface; |
19
|
|
|
use Micro\Framework\Kernel\Plugin\DependencyProviderInterface; |
20
|
|
|
use Micro\Framework\Kernel\Plugin\PluginConfigurationTrait; |
21
|
|
|
use Micro\Plugin\Doctrine\Business\Connection\ConnectionFactory; |
22
|
|
|
use Micro\Plugin\Doctrine\Business\Connection\ConnectionFactoryInterface; |
23
|
|
|
use Micro\Plugin\Doctrine\Business\EntityManager\EntityManagerFactory; |
24
|
|
|
use Micro\Plugin\Doctrine\Business\EntityManager\EntityManagerFactoryInterface; |
25
|
|
|
use Micro\Plugin\Doctrine\Business\Locator\EntityFileConfigurationLocatorFactory; |
26
|
|
|
use Micro\Plugin\Doctrine\Business\Locator\EntityFileConfigurationLocatorFactoryInterface; |
27
|
|
|
use Micro\Plugin\Doctrine\Business\Metadata\DriverMetadataFactory; |
28
|
|
|
use Micro\Plugin\Doctrine\Business\Metadata\DriverMetadataFactoryInterface; |
29
|
|
|
use Micro\Plugin\Doctrine\Business\Pool\EntityManagerPoolFactory; |
30
|
|
|
use Micro\Plugin\Doctrine\Business\Pool\EntityManagerPoolFactoryInterface; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Doctrine ORM Plugin. |
34
|
|
|
* If appear Exception: |
35
|
|
|
* - ExceptionConverter.php: An exception occurred in the driver: could not find driver |
36
|
|
|
* - Doctrine\DBAL\Exception\DriverException: An exception occurred in the driver: could not find driver |
37
|
|
|
* Solve: should php-dpo installed or other necessary driver. |
38
|
|
|
* Example: apt install php8.2-pdo php8.2-mysql. |
39
|
|
|
* |
40
|
|
|
* @method DoctrinePluginConfigurationInterface configuration() |
41
|
|
|
*/ |
42
|
|
|
class DoctrinePlugin implements DependencyProviderInterface, ConfigurableInterface |
43
|
|
|
{ |
44
|
|
|
use PluginConfigurationTrait; |
45
|
|
|
|
46
|
|
|
private KernelInterface $kernel; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritDoc} |
50
|
|
|
*/ |
51
|
1 |
|
public function provideDependencies(Container $container): void |
52
|
|
|
{ |
53
|
1 |
|
$container->register(DoctrineFacadeInterface::class, function (KernelInterface $kernel): DoctrineFacadeInterface { |
54
|
1 |
|
$this->kernel = $kernel; |
55
|
|
|
|
56
|
1 |
|
return $this->createDoctrineFacade(); |
57
|
1 |
|
}); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
protected function createDoctrineFacade(): DoctrineFacadeInterface |
61
|
|
|
{ |
62
|
1 |
|
return new DoctrineFacade($this->createManagerPool()); |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
protected function createManagerPool(): EntityManagerPoolFactoryInterface |
66
|
|
|
{ |
67
|
1 |
|
return new EntityManagerPoolFactory( |
68
|
1 |
|
$this->createEntityManagerFactory() |
69
|
1 |
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
protected function createEntityManagerFactory(): EntityManagerFactoryInterface |
73
|
|
|
{ |
74
|
1 |
|
return new EntityManagerFactory( |
75
|
1 |
|
$this->createConnectionFactory(), |
76
|
1 |
|
$this->createDriverMetadataFactory(), |
77
|
1 |
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
protected function createConnectionFactory(): ConnectionFactoryInterface |
81
|
|
|
{ |
82
|
1 |
|
return new ConnectionFactory( |
83
|
1 |
|
$this->configuration() |
84
|
1 |
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
protected function createDriverMetadataFactory(): DriverMetadataFactoryInterface |
88
|
|
|
{ |
89
|
1 |
|
return new DriverMetadataFactory( |
90
|
1 |
|
$this->createEntityFileConfigurationLocatorFactory(), |
91
|
1 |
|
$this->configuration() |
92
|
1 |
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
protected function createEntityFileConfigurationLocatorFactory(): EntityFileConfigurationLocatorFactoryInterface |
96
|
|
|
{ |
97
|
1 |
|
return new EntityFileConfigurationLocatorFactory($this->kernel); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|