1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* The MIT License |
5
|
|
|
* |
6
|
|
|
* Copyright 2014-2018 James Ekow Abaka Ainooson |
7
|
|
|
* |
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
10
|
|
|
* in the Software without restriction, including without limitation the rights |
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
13
|
|
|
* furnished to do so, subject to the following conditions: |
14
|
|
|
* |
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
16
|
|
|
* all copies or substantial portions of the Software. |
17
|
|
|
* |
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
24
|
|
|
* THE SOFTWARE. |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace ntentan\nibii; |
28
|
|
|
|
29
|
|
|
use ntentan\atiaa\DbContext; |
30
|
|
|
use ntentan\kaikai\Cache; |
31
|
|
|
use ntentan\nibii\interfaces\DriverAdapterFactoryInterface; |
32
|
|
|
use ntentan\nibii\interfaces\ModelFactoryInterface; |
33
|
|
|
use ntentan\nibii\interfaces\ValidatorFactoryInterface; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* A global class with information and utilities required by the rest of the ORM system. |
37
|
|
|
*/ |
38
|
|
|
class ORMContext |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
private static $instance; |
41
|
|
|
private $cache; |
42
|
|
|
private $modelFactory; |
43
|
|
|
private $modelValidatorFactory; |
44
|
|
|
private $driverAdapterFactory; |
45
|
|
|
|
46
|
36 |
|
private function __construct(ModelFactoryInterface $modelFactory, DriverAdapterFactoryInterface $driverAdapterFactory, ValidatorFactoryInterface $modelValidatorFactory, Cache $cache) |
47
|
|
|
{ |
48
|
36 |
|
$this->modelFactory = $modelFactory; |
49
|
36 |
|
$this->cache = $cache; |
50
|
36 |
|
$this->driverAdapterFactory = $driverAdapterFactory; |
51
|
36 |
|
$this->modelValidatorFactory = $modelValidatorFactory; |
52
|
36 |
|
} |
53
|
|
|
|
54
|
36 |
|
public static function initialize(ModelFactoryInterface $modelFactory, DriverAdapterFactoryInterface $driverAdapterFactory, ValidatorFactoryInterface $modelValidatorFactory, Cache $cache): self |
55
|
|
|
{ |
56
|
36 |
|
self::$instance = new self($modelFactory, $driverAdapterFactory, $modelValidatorFactory, $cache); |
57
|
|
|
|
58
|
36 |
|
return self::$instance; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* A helper for loading a method described as a string. |
63
|
|
|
* |
64
|
|
|
* @param string $path Model name as string |
65
|
|
|
* |
66
|
|
|
* @throws NibiiException |
67
|
|
|
* |
68
|
|
|
* @return \nibii\RecordWrapper |
69
|
|
|
*/ |
70
|
6 |
|
public function load($path) |
71
|
|
|
{ |
72
|
|
|
try { |
73
|
6 |
|
return $this->modelFactory->createModel($path, null); |
74
|
|
|
} catch (\ntentan\panie\exceptions\ResolutionException $e) { |
75
|
|
|
throw new |
76
|
|
|
NibiiException("Failed to load model [$path]. The class [$className] could not be found."); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param RecordWrapper $instance |
82
|
|
|
*/ |
83
|
36 |
|
public function getModelTable($instance) |
84
|
|
|
{ |
85
|
36 |
|
return $this->modelFactory->getModelTable($instance); |
86
|
|
|
} |
87
|
|
|
|
88
|
36 |
|
public function getDriverAdapter() |
89
|
|
|
{ |
90
|
36 |
|
return $this->driverAdapterFactory->createDriverAdapter(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $class |
95
|
|
|
*/ |
96
|
28 |
|
public function getModelName($class) |
97
|
|
|
{ |
98
|
28 |
|
return $class; |
99
|
|
|
} |
100
|
|
|
|
101
|
36 |
|
public static function getInstance() : self |
102
|
|
|
{ |
103
|
36 |
|
if (self::$instance === null) { |
104
|
|
|
throw new NibiiException('A context has not yet been initialized'); |
105
|
|
|
} |
106
|
|
|
|
107
|
36 |
|
return self::$instance; |
108
|
|
|
} |
109
|
|
|
|
110
|
28 |
|
public function getCache() |
111
|
|
|
{ |
112
|
28 |
|
return $this->cache; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getConfig() |
116
|
|
|
{ |
117
|
|
|
return $this->config; |
118
|
|
|
} |
119
|
|
|
|
120
|
28 |
|
public function getModelDescription($model) : ModelDescription |
121
|
|
|
{ |
122
|
28 |
|
return new ModelDescription($model); |
123
|
|
|
} |
124
|
|
|
|
125
|
10 |
|
public function getModelValidatorFactory() : ValidatorFactoryInterface |
126
|
|
|
{ |
127
|
10 |
|
return $this->modelValidatorFactory; |
128
|
|
|
} |
129
|
|
|
|
130
|
12 |
|
public function getModelFactory() : ModelFactoryInterface |
131
|
|
|
{ |
132
|
12 |
|
return $this->modelFactory; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return \ntentan\atiaa\DbContext |
137
|
|
|
*/ |
138
|
36 |
|
public function getDbContext() |
139
|
|
|
{ |
140
|
36 |
|
return DbContext::getInstance(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function __destruct() |
144
|
|
|
{ |
145
|
|
|
self::$instance = null; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|