-
Notifications
You must be signed in to change notification settings - Fork 178
/
DoctrineManager.php
99 lines (81 loc) · 3.24 KB
/
DoctrineManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
declare(strict_types=1);
namespace LaravelDoctrine\ORM;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use Illuminate\Contracts\Container\Container;
use InvalidArgumentException;
use function assert;
use function call_user_func_array;
use function class_exists;
use function is_callable;
class DoctrineManager
{
public function __construct(protected Container $container)
{
}
public function getDefaultManagerName(): string
{
return $this->container->make('registry')->getDefaultManagerName();
}
public function onResolve(callable $callback): void
{
BootChain::add(function (ManagerRegistry $registry) use ($callback): void {
call_user_func_array($callback, [$registry, $this]);
});
}
public function extend(string|null $connection, string|callable $callback): void
{
$this->onResolve(function (ManagerRegistry $registry) use ($connection, $callback): void {
$this->callExtendOn($connection, $callback, $registry);
});
}
public function extendAll(string|callable $callback): void
{
$this->onResolve(function (ManagerRegistry $registry) use ($callback): void {
foreach ($registry->getManagerNames() as $connection) {
$this->callExtendOn($connection, $callback, $registry);
}
});
}
private function callExtendOn(string|null $connection, string|callable $callback, ManagerRegistry $registry): void
{
$manager = $registry->getManager($connection);
assert($manager instanceof EntityManagerInterface);
if (! is_callable($callback)) {
if (! class_exists($callback)) {
throw new InvalidArgumentException('DoctrineExtender ' . $callback . ' does not exist');
}
$callback = [$this->container->make($callback), 'extend'];
}
if (! is_callable($callback)) {
throw new InvalidArgumentException('No valid extend callback is given. Either pass a class or closure');
}
call_user_func_array($callback, [
$manager->getConfiguration(),
$manager->getConnection(),
$manager->getEventManager(),
]);
}
/** @param mixed[] $paths */
public function addPaths(array $paths = [], string|null $connection = null): void
{
$this->onResolve(function (ManagerRegistry $registry) use ($connection, $paths): void {
$this->getMetaDataDriver($connection, $registry)->addPaths($paths);
});
}
/** @param mixed[] $mappings */
public function addMappings(array $mappings = [], string|null $connection = null): void
{
$this->onResolve(function (ManagerRegistry $registry) use ($connection, $mappings): void {
$this->getMetaDataDriver($connection, $registry)->addMappings($mappings);
});
}
public function getMetaDataDriver(string|null $connection, ManagerRegistry $registry): MappingDriver
{
$registry = $registry ?: $this->container->make('registry');
$manager = $registry->getManager($connection);
return $manager->getConfiguration()->getMetadataDriverImpl();
}
}