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