1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Records\Locator; |
4
|
|
|
|
5
|
|
|
use Nip\Records\AbstractModels\RecordManager; |
6
|
|
|
use Nip\Records\Locator\Configuration\HasConfigurationTrait; |
7
|
|
|
use Nip\Records\Locator\Exceptions\InvalidModelException; |
8
|
|
|
use Nip\Records\Locator\ModelLocator\Traits\StaticMethodsTrait; |
9
|
|
|
use Nip\Records\Locator\Registry\HasModelRegistry; |
10
|
|
|
use Nip\Records\Locator\Resolver\Commands\Command; |
11
|
|
|
use Nip\Records\Locator\Resolver\Commands\CommandsFactory; |
12
|
|
|
use Nip\Records\Locator\Resolver\HasResolverPipelineTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class ModelLocator |
16
|
|
|
* @package Nip\Records\Locator |
17
|
|
|
*/ |
18
|
|
|
class ModelLocator |
19
|
|
|
{ |
20
|
|
|
use StaticMethodsTrait; |
21
|
|
|
use HasResolverPipelineTrait; |
22
|
|
|
use HasConfigurationTrait; |
23
|
|
|
use HasModelRegistry; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $alias |
27
|
|
|
* @return RecordManager |
28
|
|
|
* @throws InvalidModelException |
29
|
|
|
*/ |
30
|
18 |
|
public function getManager($alias) |
31
|
|
|
{ |
32
|
18 |
|
return $this->locateManager($alias); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param $alias |
37
|
|
|
* @return RecordManager |
38
|
|
|
* @throws InvalidModelException |
39
|
|
|
*/ |
40
|
18 |
|
protected function locateManager($alias) |
41
|
|
|
{ |
42
|
18 |
|
if ($this->getModelRegistry()->has($alias)) { |
43
|
15 |
|
return $this->getModelRegistry()->get($alias); |
44
|
|
|
} |
45
|
|
|
|
46
|
3 |
|
$manager = $this->locateManagerPipeline($alias); |
47
|
2 |
|
$this->getModelRegistry()->set($alias, $manager); |
48
|
2 |
|
$this->getModelRegistry()->set($manager->getClassName(), $manager); |
49
|
2 |
|
return $manager; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param $alias |
54
|
|
|
* @return RecordManager |
55
|
|
|
* @throws InvalidModelException |
56
|
|
|
*/ |
57
|
3 |
|
protected function locateManagerPipeline($alias) |
58
|
|
|
{ |
59
|
3 |
|
$command = CommandsFactory::create($alias, $this); |
60
|
3 |
|
$pipeline = $this->buildCallPipeline(); |
61
|
|
|
|
62
|
|
|
/** @var Command $command */ |
63
|
3 |
|
$command = $pipeline->process($command); |
64
|
|
|
|
65
|
3 |
|
if ($command->hasInstance()) { |
66
|
2 |
|
return $command->getInstance(); |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
throw new InvalidModelException( |
70
|
|
|
"No valid instance located for model alias " |
71
|
1 |
|
. "[" . $alias . "]" |
72
|
1 |
|
. " Tried [" . implode(', ', $command->getTries()) . "]" |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|