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
|
|
|
private $container; |
16
|
|
|
private $dbContext; |
17
|
|
|
private static $instance; |
18
|
|
|
private $cache; |
19
|
|
|
private $config; |
20
|
|
|
|
21
|
36 |
|
public function __construct(Container $container, array $config) { |
22
|
36 |
|
$this->container = $container; |
23
|
36 |
|
$this->config = $config; |
24
|
36 |
|
$this->dbContext = $container->resolve(DbContext::class, ['config' => $config]); |
25
|
36 |
|
$this->container->setup([ |
26
|
36 |
|
interfaces\ModelJoinerInterface::class => Resolver::class, |
27
|
|
|
interfaces\TableNameResolverInterface::class => Resolver::class, |
28
|
|
|
interfaces\ModelClassResolverInterface::class => Resolver::class |
29
|
36 |
|
], false); |
30
|
36 |
|
$this->cache = $this->container->resolve(Cache::class); |
31
|
36 |
|
self::$instance = $this; |
32
|
36 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
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
|
6 |
|
public function load($path) { |
41
|
|
|
try { |
42
|
6 |
|
$className = $this->getClassName($path); |
43
|
6 |
|
return $this->container->resolve($className); |
44
|
|
|
} catch (\ntentan\panie\exceptions\ResolutionException $e) { |
45
|
|
|
throw new |
46
|
|
|
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
|
4 |
|
public function joinModels($classA, $classB) { |
57
|
4 |
|
return$this->container->singleton(interfaces\ModelJoinerInterface::class) |
58
|
4 |
|
->getJunctionClassName($classA, $classB); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param RecordWrapper $instance |
63
|
|
|
*/ |
64
|
36 |
|
public function getModelTable($instance) { |
|
|
|
|
65
|
36 |
|
return$this->container->singleton(interfaces\TableNameResolverInterface::class) |
66
|
36 |
|
->getTableName($instance); |
67
|
|
|
} |
68
|
|
|
|
69
|
14 |
|
public function getClassName($model, $context = null) { |
|
|
|
|
70
|
14 |
|
return $this->container->singleton(interfaces\ModelClassResolverInterface::class) |
71
|
14 |
|
->getModelClassName($model, $context); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $class |
76
|
|
|
*/ |
77
|
28 |
|
public function getModelName($class) { |
78
|
28 |
|
return $class; |
79
|
|
|
} |
80
|
|
|
|
81
|
36 |
|
public static function getInstance() { |
82
|
36 |
|
if(self::$instance === null){ |
83
|
|
|
throw new NibiiException("A context has not yet been initialized"); |
84
|
|
|
} |
85
|
36 |
|
return self::$instance; |
86
|
|
|
} |
87
|
|
|
|
88
|
36 |
|
public function getContainer() { |
89
|
36 |
|
return $this->container; |
90
|
|
|
} |
91
|
|
|
|
92
|
28 |
|
public function getCache() { |
|
|
|
|
93
|
28 |
|
return $this->cache; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getConfig() { |
97
|
|
|
return $this->config; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* |
102
|
|
|
* @return \ntentan\atiaa\DbContext |
103
|
|
|
*/ |
104
|
36 |
|
public function getDbContext() { |
105
|
36 |
|
return $this->dbContext; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function __destruct() { |
109
|
|
|
self::$instance = null; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.