|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ntentan\nibii; |
|
4
|
|
|
|
|
5
|
|
|
use ntentan\panie\Container; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* A collection of utility methods used as helpers for loading |
|
9
|
|
|
* models. |
|
10
|
|
|
*/ |
|
11
|
|
|
class Context { |
|
12
|
|
|
|
|
13
|
|
|
private $container; |
|
14
|
|
|
private $dbContext; |
|
15
|
|
|
private static $instance; |
|
16
|
|
|
|
|
17
|
36 |
|
public function __construct(Container $container) { |
|
18
|
36 |
|
$this->container = $container; |
|
19
|
36 |
|
$this->dbContext = $container->resolve(\ntentan\atiaa\DbContext::class); |
|
20
|
|
|
$this->container->bind(interfaces\ModelJoinerInterface::class) |
|
21
|
|
|
->to(Resolver::class); |
|
22
|
|
|
$this->container->bind(interfaces\TableNameResolverInterface::class) |
|
23
|
|
|
->to(Resolver::class); |
|
24
|
|
|
$this->container->bind(interfaces\ModelClassResolverInterface::class) |
|
25
|
|
|
->to(Resolver::class); |
|
26
|
|
|
self::$instance = $this; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* A helper for loading a method described as a string. |
|
31
|
|
|
* @param string $path Model name as string |
|
32
|
|
|
* @return \nibii\RecordWrapper |
|
33
|
|
|
* @throws NibiiException |
|
34
|
|
|
*/ |
|
35
|
|
|
public function load($path) { |
|
36
|
|
|
try { |
|
37
|
|
|
return$this->container->resolve(self::getClassName($path)); |
|
38
|
|
|
} catch (\ntentan\panie\exceptions\ResolutionException $e) { |
|
39
|
|
|
throw new |
|
40
|
|
|
NibiiException("Failed to load model [$path]. Please specify a valid database driver."); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Returns a class name for junction models needed to perform joint queries. |
|
46
|
|
|
* @param string $classA |
|
47
|
|
|
* @param string $classB |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
public function joinModels($classA, $classB) { |
|
51
|
|
|
return$this->container->singleton(interfaces\ModelJoinerInterface::class) |
|
52
|
|
|
->getJunctionClassName($classA, $classB); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function getModelTable($instance) { |
|
|
|
|
|
|
56
|
|
|
return$this->container->singleton(interfaces\TableNameResolverInterface::class) |
|
57
|
|
|
->getTableName($instance); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function getClassName($model, $context = null) { |
|
|
|
|
|
|
61
|
|
|
return$this->container->singleton(interfaces\ModelClassResolverInterface::class) |
|
62
|
|
|
->getModelClassName($model, $context); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function getModelName($class) { |
|
|
|
|
|
|
66
|
|
|
return $class; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public static function getInstance() { |
|
70
|
|
|
if(self::$instance === null) throw new NibiiException("A context has not yet been initialized"); |
|
71
|
|
|
return self::$instance; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function getContainer() { |
|
75
|
|
|
return $this->container; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function getDbContext() { |
|
|
|
|
|
|
79
|
|
|
return $this->dbContext; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
36 |
|
public function __destruct() { |
|
83
|
36 |
|
self::$instance = null; |
|
84
|
36 |
|
} |
|
85
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
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
@returnannotation as described here.