1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ntentan\nibii; |
4
|
|
|
|
5
|
|
|
use ntentan\atiaa\DbContext; |
6
|
|
|
use ntentan\kaikai\Cache; |
7
|
|
|
use ntentan\nibii\interfaces\ModelFactoryInterface; |
8
|
|
|
use ntentan\nibii\interfaces\DriverAdapterFactoryInterface; |
9
|
|
|
use ntentan\nibii\interfaces\ValidatorFactoryInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* A collection of utility methods used as helpers for loading models. |
13
|
|
|
*/ |
14
|
|
|
class ORMContext |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
private static $instance; |
18
|
|
|
private $cache; |
19
|
|
|
private $modelFactory; |
20
|
|
|
private $modelValidatorFactory; |
21
|
|
|
private $driverAdapterFactory; |
22
|
|
|
|
23
|
36 |
|
private function __construct(ModelFactoryInterface $modelFactory, DriverAdapterFactoryInterface $driverAdapterFactory, ValidatorFactoryInterface $modelValidatorFactory, Cache $cache) |
24
|
|
|
{ |
25
|
36 |
|
$this->modelFactory = $modelFactory; |
26
|
36 |
|
$this->cache = $cache; |
27
|
36 |
|
$this->driverAdapterFactory = $driverAdapterFactory; |
28
|
36 |
|
$this->modelValidatorFactory = $modelValidatorFactory; |
29
|
36 |
|
} |
30
|
|
|
|
31
|
36 |
|
public static function initialize(ModelFactoryInterface $modelFactory, DriverAdapterFactoryInterface $driverAdapterFactory, ValidatorFactoryInterface $modelValidatorFactory, Cache $cache): ORMContext |
32
|
|
|
{ |
33
|
36 |
|
self::$instance = new self($modelFactory, $driverAdapterFactory, $modelValidatorFactory, $cache); |
34
|
36 |
|
return self::$instance; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* A helper for loading a method described as a string. |
39
|
|
|
* @param string $path Model name as string |
40
|
|
|
* @return \nibii\RecordWrapper |
41
|
|
|
* @throws NibiiException |
42
|
|
|
*/ |
43
|
6 |
|
public function load($path) |
44
|
|
|
{ |
45
|
|
|
try { |
46
|
6 |
|
return $this->modelFactory->createModel($path, null); |
47
|
|
|
} catch (\ntentan\panie\exceptions\ResolutionException $e) { |
|
|
|
|
48
|
|
|
throw new |
49
|
|
|
NibiiException("Failed to load model [$path]. The class [$className] could not be found."); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param RecordWrapper $instance |
55
|
|
|
*/ |
56
|
36 |
|
public function getModelTable($instance) |
|
|
|
|
57
|
|
|
{ |
58
|
36 |
|
return $this->modelFactory->getModelTable($instance); |
59
|
|
|
} |
60
|
|
|
|
61
|
36 |
|
public function getDriverAdapter() |
|
|
|
|
62
|
|
|
{ |
63
|
36 |
|
return $this->driverAdapterFactory->createDriverAdapter(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $class |
68
|
|
|
*/ |
69
|
28 |
|
public function getModelName($class) |
70
|
|
|
{ |
71
|
28 |
|
return $class; |
72
|
|
|
} |
73
|
|
|
|
74
|
36 |
|
public static function getInstance() : ORMContext |
75
|
|
|
{ |
76
|
36 |
|
if (self::$instance === null) { |
77
|
|
|
throw new NibiiException("A context has not yet been initialized"); |
78
|
|
|
} |
79
|
36 |
|
return self::$instance; |
80
|
|
|
} |
81
|
|
|
|
82
|
28 |
|
public function getCache() |
83
|
|
|
{ |
84
|
28 |
|
return $this->cache; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function getConfig() |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
return $this->config; |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
28 |
|
public function getModelDescription($model) : ModelDescription |
93
|
|
|
{ |
94
|
28 |
|
return new ModelDescription($model); |
95
|
|
|
} |
96
|
|
|
|
97
|
10 |
|
public function getModelValidatorFactory() : ValidatorFactoryInterface |
98
|
|
|
{ |
99
|
10 |
|
return $this->modelValidatorFactory; |
100
|
|
|
} |
101
|
|
|
|
102
|
12 |
|
public function getModelFactory() : ModelFactoryInterface |
103
|
|
|
{ |
104
|
12 |
|
return $this->modelFactory; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* |
109
|
|
|
* @return \ntentan\atiaa\DbContext |
110
|
|
|
*/ |
111
|
36 |
|
public function getDbContext() |
112
|
|
|
{ |
113
|
36 |
|
return DbContext::getInstance(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function __destruct() |
117
|
|
|
{ |
118
|
|
|
self::$instance = null; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|