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
|
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
|
|
|
/*$this->container->bind(interfaces\ModelJoinerInterface::class) |
|
|
|
|
26
|
|
|
->to(Resolver::class); |
27
|
|
|
$this->container->bind(interfaces\TableNameResolverInterface::class) |
28
|
|
|
->to(Resolver::class); |
29
|
|
|
$this->container->bind(interfaces\ModelClassResolverInterface::class) |
30
|
|
|
->to(Resolver::class);*/ |
31
|
37 |
|
$this->cache = $this->container->resolve(Cache::class); |
32
|
37 |
|
self::$instance = $this; |
33
|
37 |
|
} |
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
|
|
|
* @throws NibiiException |
40
|
|
|
*/ |
41
|
2 |
|
public function load($path) { |
42
|
|
|
try { |
43
|
2 |
|
$className = $this->getClassName($path); |
44
|
|
|
return $this->container->resolve($className); |
45
|
2 |
|
} 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
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function joinModels($classA, $classB) { |
58
|
|
|
return$this->container->singleton(interfaces\ModelJoinerInterface::class) |
59
|
|
|
->getJunctionClassName($classA, $classB); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param RecordWrapper $instance |
64
|
|
|
*/ |
65
|
35 |
|
public function getModelTable($instance) { |
|
|
|
|
66
|
35 |
|
return$this->container->singleton(interfaces\TableNameResolverInterface::class) |
67
|
1 |
|
->getTableName($instance); |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
public function getClassName($model, $context = null) { |
|
|
|
|
71
|
2 |
|
return $this->container->singleton(interfaces\ModelClassResolverInterface::class) |
72
|
|
|
->getModelClassName($model, $context); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $class |
77
|
|
|
*/ |
78
|
|
|
public function getModelName($class) { |
79
|
|
|
return $class; |
80
|
|
|
} |
81
|
|
|
|
82
|
35 |
|
public static function getInstance($container = null) { |
83
|
35 |
|
if(self::$instance === null && $container !== null){ |
84
|
|
|
$container->resolve(self::class); |
85
|
35 |
|
} elseif (self::$instance === null) { |
86
|
|
|
throw new NibiiException("A context has not yet been initialized"); |
87
|
|
|
} |
88
|
35 |
|
return self::$instance; |
89
|
|
|
} |
90
|
|
|
|
91
|
35 |
|
public function getContainer() { |
92
|
35 |
|
return $this->container; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getCache() { |
|
|
|
|
96
|
|
|
return $this->cache; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getConfig() { |
100
|
|
|
return $this->config; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* |
105
|
|
|
* @return \ntentan\atiaa\DbContext |
106
|
|
|
*/ |
107
|
1 |
|
public function getDbContext() { |
108
|
1 |
|
return $this->dbContext; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function __destruct() { |
112
|
|
|
self::$instance = null; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.