Skip to content

Commit 77c1a6c

Browse files
committed
Add the persistence unit manager
1 parent 477a57f commit 77c1a6c

File tree

2 files changed

+262
-0
lines changed

2 files changed

+262
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/**
4+
* LightQL - The lightweight PHP ORM
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*
24+
* @category Library
25+
* @package LightQL
26+
* @author Axel Nana <ax.lnana@outlook.com>
27+
* @copyright 2018 Aliens Group, Inc.
28+
* @license MIT <https://github.com/ElementaryFramework/LightQL/blob/master/LICENSE>
29+
* @version GIT: 0.0.1
30+
* @link http://lightql.na2axl.tk
31+
*/
32+
33+
namespace ElementaryFramework\LightQL\Exceptions;
34+
35+
/**
36+
* Persistence Unit Exception
37+
*
38+
* @package LightQL
39+
* @author Nana Axel <ax.lnana@outlook.com>
40+
* @link http://lightql.na2axl.tk/docs/api/LightQL/Exceptions/PersistenceUnitException
41+
*/
42+
class PersistenceUnitException extends \Exception
43+
{
44+
45+
}
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
<?php
2+
3+
/**
4+
* LightQL - The lightweight PHP ORM
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*
24+
* @category Library
25+
* @package LightQL
26+
* @author Axel Nana <ax.lnana@outlook.com>
27+
* @copyright 2018 Aliens Group, Inc.
28+
* @license MIT <https://github.com/ElementaryFramework/LightQL/blob/master/LICENSE>
29+
* @version GIT: 0.0.1
30+
* @link http://lightql.na2axl.tk
31+
*/
32+
33+
namespace ElementaryFramework\LightQL\Persistence;
34+
35+
use ElementaryFramework\LightQL\Exceptions\PersistenceUnitException;
36+
37+
/**
38+
* Persistence Unit
39+
*
40+
* Configures parameters to use for database connection.
41+
*
42+
* @abstract
43+
* @category Library
44+
* @package LightQL
45+
* @author Nana Axel <ax.lnana@outlook.com>
46+
* @link http://lightql.na2axl.tk/docs/api/LightQL/Entities/PersistenceUnit
47+
*/
48+
class PersistenceUnit
49+
{
50+
/**
51+
* @var string
52+
*/
53+
private $_dbms;
54+
55+
/**
56+
* @var string
57+
*/
58+
private $_hostname;
59+
60+
/**
61+
* @var string
62+
*/
63+
private $_database;
64+
65+
/**
66+
* @var string
67+
*/
68+
private $_username;
69+
70+
/**
71+
* @var string
72+
*/
73+
private $_password;
74+
75+
/**
76+
* @var array
77+
*/
78+
private static $_registry = array();
79+
80+
/**
81+
* @var PersistenceUnit[]
82+
*/
83+
private static $_units = array();
84+
85+
/**
86+
* Registers a new persistence unit.
87+
*
88+
* @param string $key The name of the persistence unit.
89+
* @param string $path The path to the persistence unit file.
90+
*/
91+
public static function register(string $key, string $path)
92+
{
93+
self::$_registry[$key] = $path;
94+
}
95+
96+
/**
97+
* Cleans the persistence unit registry and cache.
98+
*/
99+
public static function purge()
100+
{
101+
self::$_registry = array();
102+
self::$_units = array();
103+
}
104+
105+
/**
106+
* PersistenceUnit constructor.
107+
*
108+
* @param string $key The persistence unit name.
109+
*
110+
* @throws PersistenceUnitException
111+
*/
112+
private function __construct(string $key)
113+
{
114+
if (array_key_exists($key, self::$_registry)) {
115+
$filename = basename(self::$_registry[$key]);
116+
$parts = explode(".", $filename);
117+
$extension = $parts[count($parts) - 1];
118+
119+
$content = null;
120+
if ($extension === "ini") {
121+
$content = parse_ini_file(self::$_registry[$key]);
122+
} elseif ($extension === "json") {
123+
$content = json_decode(file_get_contents(self::$_registry[$key]), true);
124+
} else {
125+
throw new PersistenceUnitException("Unsupported file type used to create persistence unit {$filename}.");
126+
}
127+
128+
if (array_key_exists("DBMS", $content)) {
129+
$this->_dbms = $content["DBMS"];
130+
} else {
131+
throw new PersistenceUnitException("Malformed persistence unit configuration file, missing the DBMS value.");
132+
}
133+
134+
if (array_key_exists("Hostname", $content)) {
135+
$this->_hostname = $content["Hostname"];
136+
} else {
137+
throw new PersistenceUnitException("Malformed persistence unit configuration file, missing the Hostname value.");
138+
}
139+
140+
if (array_key_exists("DatabaseName", $content)) {
141+
$this->_database = $content["DatabaseName"];
142+
} else {
143+
throw new PersistenceUnitException("Malformed persistence unit configuration file, missing the DatabaseName value.");
144+
}
145+
146+
if (array_key_exists("Username", $content)) {
147+
$this->_username = $content["Username"];
148+
} else {
149+
throw new PersistenceUnitException("Malformed persistence unit configuration file, missing the Username value.");
150+
}
151+
152+
if (array_key_exists("Password", $content)) {
153+
$this->_password = $content["Password"];
154+
} else {
155+
throw new PersistenceUnitException("Malformed persistence unit configuration file, missing the Password value.");
156+
}
157+
} else {
158+
throw new PersistenceUnitException('Unable to find the persistence unit with the key "' . $key . '". Have you registered this persistence unit?');
159+
}
160+
}
161+
162+
/**
163+
* Creates a new persistence unit by the given key.
164+
*
165+
* @param string $key The persistence unit name.
166+
*
167+
* @return PersistenceUnit
168+
*/
169+
public static function create(string $key)
170+
{
171+
if (array_key_exists($key, self::$_units)) {
172+
return self::$_units[$key];
173+
} else {
174+
return (self::$_units[$key] = new self($key));
175+
}
176+
}
177+
178+
/**
179+
* @return string
180+
*/
181+
public function getDbms()
182+
{
183+
return $this->_dbms;
184+
}
185+
186+
/**
187+
* @return string
188+
*/
189+
public function getDatabase(): string
190+
{
191+
return $this->_database;
192+
}
193+
194+
/**
195+
* @return string
196+
*/
197+
public function getHostname(): string
198+
{
199+
return $this->_hostname;
200+
}
201+
202+
/**
203+
* @return string
204+
*/
205+
public function getPassword(): string
206+
{
207+
return $this->_password;
208+
}
209+
210+
/**
211+
* @return string
212+
*/
213+
public function getUsername(): string
214+
{
215+
return $this->_username;
216+
}
217+
}

0 commit comments

Comments
 (0)