|
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
|
38 |
|
public function __construct(Container $container, array $config) { |
|
22
|
38 |
|
$this->container = $container; |
|
23
|
38 |
|
$this->config = $config; |
|
24
|
38 |
|
$this->dbContext = $container->resolve(DbContext::class, ['config' => $config]); |
|
25
|
38 |
|
$this->container->setup([ |
|
|
|
|
|
|
26
|
|
|
interfaces\ModelJoinerInterface::class => Resolver::class, |
|
27
|
|
|
interfaces\TableNameResolverInterface::class => Resolver::class, |
|
28
|
|
|
interfaces\ModelClassResolverInterface::class => Resolver::class |
|
29
|
|
|
], false); |
|
30
|
|
|
$this->cache = $this->container->resolve(Cache::class); |
|
31
|
|
|
self::$instance = $this; |
|
32
|
|
|
} |
|
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
|
|
|
public function load($path) { |
|
41
|
|
|
try { |
|
42
|
|
|
$className = $this->getClassName($path); |
|
43
|
|
|
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
|
|
|
public function joinModels($classA, $classB) { |
|
57
|
|
|
return$this->container->singleton(interfaces\ModelJoinerInterface::class) |
|
58
|
|
|
->getJunctionClassName($classA, $classB); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param RecordWrapper $instance |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getModelTable($instance) { |
|
|
|
|
|
|
65
|
|
|
return$this->container->singleton(interfaces\TableNameResolverInterface::class) |
|
66
|
|
|
->getTableName($instance); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function getClassName($model, $context = null) { |
|
|
|
|
|
|
70
|
|
|
return $this->container->singleton(interfaces\ModelClassResolverInterface::class) |
|
71
|
|
|
->getModelClassName($model, $context); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $class |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getModelName($class) { |
|
78
|
|
|
return $class; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public static function getInstance() { |
|
82
|
|
|
if(self::$instance === null){ |
|
83
|
|
|
throw new NibiiException("A context has not yet been initialized"); |
|
84
|
|
|
} |
|
85
|
|
|
return self::$instance; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function getContainer() { |
|
89
|
|
|
return $this->container; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function getCache() { |
|
|
|
|
|
|
93
|
|
|
return $this->cache; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function getConfig() { |
|
97
|
|
|
return $this->config; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* |
|
102
|
|
|
* @return \ntentan\atiaa\DbContext |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getDbContext() { |
|
105
|
|
|
return $this->dbContext; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
38 |
|
public function __destruct() { |
|
109
|
38 |
|
self::$instance = null; |
|
110
|
38 |
|
} |
|
111
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.