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