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