|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Cycle\ORM\Promise; |
|
5
|
|
|
|
|
6
|
|
|
use Cycle\ORM\ORMInterface; |
|
7
|
|
|
use Cycle\ORM\Promise\Declaration\Declaration; |
|
8
|
|
|
use Cycle\ORM\Promise\Naming\DatetimeNaming; |
|
9
|
|
|
use Cycle\ORM\PromiseFactoryInterface; |
|
10
|
|
|
use Cycle\ORM\Schema; |
|
11
|
|
|
|
|
12
|
|
|
class Factory implements PromiseFactoryInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var ProxyPrinter */ |
|
15
|
|
|
private $creator; |
|
16
|
|
|
|
|
17
|
|
|
/** @var MaterializerInterface */ |
|
18
|
|
|
private $materializer; |
|
19
|
|
|
|
|
20
|
|
|
/** @var NamingInterface */ |
|
21
|
|
|
private $naming; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(ProxyPrinter $creator, MaterializerInterface $materializer, ?NamingInterface $naming) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->creator = $creator; |
|
26
|
|
|
$this->materializer = $materializer; |
|
27
|
|
|
$this->naming = $naming ?? new DatetimeNaming(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function promise(ORMInterface $orm, string $role, array $scope): ?ReferenceInterface |
|
31
|
|
|
{ |
|
32
|
|
|
$class = $orm->getSchema()->define($role, Schema::ENTITY); |
|
33
|
|
|
if (empty($class)) { |
|
34
|
|
|
return null; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$declaration = new Declaration($class, $this->naming->name($class)); |
|
38
|
|
|
$output = $this->creator->make($declaration); |
|
39
|
|
|
|
|
40
|
|
|
$this->materializer->materialize($output, $declaration); |
|
41
|
|
|
|
|
42
|
|
|
return $this->instantiate($orm, $role, $scope, $declaration->class->getNamespacesName()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
private function instantiate(ORMInterface $orm, string $role, array $scope, string $className): PromiseInterface |
|
46
|
|
|
{ |
|
47
|
|
|
return new $className($orm, $role, $scope); |
|
48
|
|
|
} |
|
49
|
|
|
} |