1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Records\Mapping; |
4
|
|
|
|
5
|
|
|
use Nip\Records\Mapping\Configurator\DataConfigurator; |
6
|
|
|
use Nip\Records\Mapping\MappingManager\CanCache; |
7
|
|
|
use Nip\Records\RecordManager; |
8
|
|
|
use Nip\Utility\Traits\SingletonTrait; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class MappingManager |
12
|
|
|
* @package Nip\Records\Mapping |
13
|
|
|
*/ |
14
|
|
|
class MappingManager |
15
|
|
|
{ |
16
|
|
|
use SingletonTrait; |
17
|
|
|
use CanCache; |
18
|
|
|
|
19
|
|
|
protected $booted = false; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var MappingRepository |
23
|
|
|
*/ |
24
|
|
|
protected $repository; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* MappingManager constructor. |
28
|
|
|
*/ |
29
|
2 |
|
public function __construct() |
30
|
|
|
{ |
31
|
2 |
|
$this->repository = new MappingRepository(); |
32
|
2 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param RecordManager $manager |
36
|
|
|
* @return MappingData |
37
|
|
|
*/ |
38
|
2 |
|
public static function for(RecordManager $manager) |
39
|
|
|
{ |
40
|
2 |
|
$instance = static::instance(); |
41
|
2 |
|
$className = $manager->getClassName(); |
42
|
2 |
|
if (!$instance->repository()->has($className)) { |
43
|
2 |
|
static::init($manager); |
44
|
|
|
} |
45
|
2 |
|
return $instance->repository()->get($className); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param RecordManager $manager |
50
|
|
|
*/ |
51
|
2 |
|
public static function init(RecordManager $manager) |
52
|
|
|
{ |
53
|
2 |
|
$instance = static::instance(); |
54
|
2 |
|
$data = new MappingData(); |
55
|
2 |
|
$className = $manager->getClassName(); |
56
|
2 |
|
DataConfigurator::wire($manager, $data); |
57
|
2 |
|
$instance->needsCaching(true); |
58
|
2 |
|
$instance->repository()->set($className, $data); |
59
|
2 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return MappingRepository |
63
|
|
|
*/ |
64
|
3 |
|
public function repository(): MappingRepository |
65
|
|
|
{ |
66
|
3 |
|
$this->bootIfNeeded(); |
67
|
3 |
|
return $this->repository; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function __destruct() |
71
|
|
|
{ |
72
|
|
|
$this->checkSaveCache(); |
73
|
|
|
} |
74
|
|
|
|
75
|
3 |
|
protected function bootIfNeeded() |
76
|
|
|
{ |
77
|
3 |
|
if ($this->booted === true) { |
78
|
3 |
|
return; |
79
|
|
|
} |
80
|
2 |
|
$this->boot(); |
81
|
2 |
|
$this->booted = true; |
82
|
2 |
|
} |
83
|
|
|
|
84
|
1 |
|
protected function boot() |
85
|
|
|
{ |
86
|
1 |
|
$this->initFromCache(); |
87
|
1 |
|
} |
88
|
|
|
} |
89
|
|
|
|