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
|
37 |
|
|
21
|
37 |
|
public function __construct(Container $container, array $config) { |
22
|
37 |
|
$this->container = $container; |
23
|
37 |
|
$this->config = $config; |
24
|
37 |
|
$this->dbContext = $container->resolve(DbContext::class, ['config' => $config]); |
25
|
37 |
|
$this->container->bind(interfaces\ModelJoinerInterface::class) |
26
|
37 |
|
->to(Resolver::class); |
27
|
37 |
|
$this->container->bind(interfaces\TableNameResolverInterface::class) |
28
|
37 |
|
->to(Resolver::class); |
29
|
37 |
|
$this->container->bind(interfaces\ModelClassResolverInterface::class) |
30
|
37 |
|
->to(Resolver::class); |
31
|
37 |
|
$this->cache = $this->container->resolve(Cache::class); |
32
|
|
|
self::$instance = $this; |
33
|
|
|
} |
34
|
|
|
|
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
|
6 |
|
* @throws NibiiException |
40
|
|
|
*/ |
41
|
6 |
|
public function load($path) { |
42
|
6 |
|
try { |
43
|
|
|
$className = $this->getClassName($path); |
44
|
|
|
return $this->container->resolve($className); |
45
|
|
|
} catch (\ntentan\panie\exceptions\ResolutionException $e) { |
46
|
|
|
throw new |
47
|
|
|
NibiiException("Failed to load model [$path]. The class [$className] could not be found. Ensure that you have properly setup class name resolutions."); |
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
|
4 |
|
* @return string |
56
|
4 |
|
*/ |
57
|
4 |
|
public function joinModels($classA, $classB) { |
58
|
|
|
return$this->container->singleton(interfaces\ModelJoinerInterface::class) |
59
|
|
|
->getJunctionClassName($classA, $classB); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
37 |
|
* @param RecordWrapper $instance |
64
|
37 |
|
*/ |
65
|
37 |
|
public function getModelTable($instance) { |
|
|
|
|
66
|
|
|
return$this->container->singleton(interfaces\TableNameResolverInterface::class) |
67
|
|
|
->getTableName($instance); |
68
|
14 |
|
} |
69
|
14 |
|
|
70
|
14 |
|
public function getClassName($model, $context = null) { |
|
|
|
|
71
|
|
|
return $this->container->singleton(interfaces\ModelClassResolverInterface::class) |
72
|
|
|
->getModelClassName($model, $context); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
28 |
|
* @param string $class |
77
|
28 |
|
*/ |
78
|
|
|
public function getModelName($class) { |
79
|
|
|
return $class; |
80
|
35 |
|
} |
81
|
35 |
|
|
82
|
35 |
|
public static function getInstance() { |
83
|
|
|
if(self::$instance === null) throw new NibiiException("A context has not yet been initialized"); |
84
|
|
|
return self::$instance; |
85
|
37 |
|
} |
86
|
37 |
|
|
87
|
|
|
public function getContainer() { |
88
|
|
|
return $this->container; |
89
|
28 |
|
} |
90
|
28 |
|
|
91
|
|
|
public function getCache() { |
|
|
|
|
92
|
|
|
return $this->cache; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getConfig() { |
96
|
|
|
return $this->config; |
97
|
37 |
|
} |
98
|
37 |
|
|
99
|
|
|
/** |
100
|
|
|
* |
101
|
|
|
* @return \ntentan\atiaa\DbContext |
102
|
|
|
*/ |
103
|
|
|
public function getDbContext() { |
104
|
|
|
return $this->dbContext; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function __destruct() { |
108
|
|
|
self::$instance = null; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
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.