Skip to content

Commit 9140e09

Browse files
committed
Simples Persistence package
0 parents  commit 9140e09

18 files changed

+2027
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2016 Grupo de PHP da Zona da Mata
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

composer.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "phpzm/persistence",
3+
"description": "Simples Persistence package",
4+
"minimum-stability": "dev",
5+
"keywords": [
6+
"php",
7+
"framework",
8+
"api",
9+
"simples"
10+
],
11+
"homepage": "https://github.com/phpzm/persistence",
12+
"license": "MIT",
13+
"version": "1.0.0",
14+
"type": "package",
15+
"authors": [
16+
{
17+
"name": "William",
18+
"email": "[email protected]"
19+
},
20+
{
21+
"name": "Ezio",
22+
"email": "[email protected]"
23+
}
24+
],
25+
"require": {
26+
"php": ">=7.0",
27+
"phpzm/kernel": "dev-master"
28+
},
29+
"autoload": {
30+
"psr-4": {
31+
"Simples\\Core\\": "src/"
32+
},
33+
"files": [
34+
"bootstrap/index.php"
35+
]
36+
},
37+
"scripts": {
38+
"sami": "php sami.phar update config/sami.php"
39+
}
40+
}

src/Persistence/Connection.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace Simples\Persistence;
4+
5+
use Simples\Kernel\App;
6+
use Simples\Kernel\Wrapper;
7+
8+
/**
9+
* Class Connection
10+
* @package Simples\Persistence
11+
*/
12+
abstract class Connection
13+
{
14+
/**
15+
* @var mixed
16+
*/
17+
protected $resource = null;
18+
19+
/**
20+
* @var array
21+
*/
22+
protected $settings = [];
23+
24+
/**
25+
* @var array
26+
*/
27+
protected $logs = [];
28+
29+
/**
30+
* Connection constructor.
31+
* @param array $settings
32+
*/
33+
public function __construct(array $settings)
34+
{
35+
$this->settings = $settings;
36+
}
37+
38+
/**
39+
* @return mixed
40+
*/
41+
abstract protected function connection();
42+
43+
/**
44+
* @return array
45+
*/
46+
public function getSettings(): array
47+
{
48+
return $this->settings;
49+
}
50+
51+
/**
52+
* @return array
53+
*/
54+
public function getLogs(): array
55+
{
56+
return $this->logs;
57+
}
58+
59+
/**
60+
* @SuppressWarnings("BooleanArgumentFlag")
61+
*
62+
* @param string $command
63+
* @param array $parameters
64+
* @param bool $logging (false)
65+
* @return Connection
66+
*/
67+
public function addLog(string $command, array $parameters, bool $logging = false): Connection
68+
{
69+
$log = ['command' => $command, 'parameters' => $parameters];
70+
$this->logs[] = $log;
71+
if ($logging || App::log()) {
72+
Wrapper::log($log);
73+
}
74+
return $this;
75+
}
76+
}

src/Persistence/Driver.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Simples\Persistence;
4+
5+
/**
6+
* Interface Driver
7+
* @package Simples\Persistence
8+
*/
9+
interface Driver
10+
{
11+
/**
12+
* @return bool
13+
*/
14+
public function start();
15+
16+
/**
17+
* @return bool
18+
*/
19+
public function commit();
20+
21+
/**
22+
* @return bool
23+
*/
24+
public function rollback();
25+
26+
/**
27+
* @param array $clausules
28+
* @param array $values
29+
* @return string
30+
*/
31+
public function create(array $clausules, array $values);
32+
33+
/**
34+
* @param array $clausules
35+
* @param array $values
36+
* @return array
37+
*/
38+
public function read(array $clausules, array $values = []);
39+
40+
/**
41+
* @param array $clausules
42+
* @param array $values
43+
* @param array $filters
44+
* @return int
45+
*/
46+
public function update(array $clausules, array $values, array $filters);
47+
48+
/**
49+
* @param array $clausules
50+
* @param array $values
51+
* @return int
52+
*/
53+
public function destroy(array $clausules, array $values);
54+
}

src/Persistence/Drivers/MySQL.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Simples\Persistence\Drivers;
4+
5+
use Simples\Persistence\SQL\SQLDriver;
6+
7+
/**
8+
* Class MySQL
9+
* @package Simples\Persistence
10+
*/
11+
class MySQL extends SQLDriver
12+
{
13+
/**
14+
* @return string
15+
*/
16+
protected function dsn()
17+
{
18+
$host = "host={$this->settings['host']}";
19+
$port = "port={$this->settings['port']}";
20+
$dbname = "dbname={$this->settings['database']}";
21+
22+
return "mysql:{$host};{$port};{$dbname}";
23+
}
24+
}

src/Persistence/Engine.php

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
3+
namespace Simples\Persistence;
4+
5+
use Simples\Error\SimplesRunTimeError;
6+
use Simples\Kernel\App;
7+
8+
/**
9+
* Class Engine
10+
* @package Simples\Persistence
11+
*
12+
* @method $this source (string $source)
13+
* @method $this relation (array $relations)
14+
* @method $this fields (array $fields)
15+
* @method $this filter (array $filter)
16+
* @method $this order (array $order)
17+
* @method $this group (array $group)
18+
* @method $this having (array $having)
19+
* @method $this limit (array $limit)
20+
*
21+
* @method $this log (bool $active)
22+
*/
23+
class Engine
24+
{
25+
/**
26+
* @var Driver
27+
*/
28+
private $driver;
29+
30+
/**
31+
* @var array
32+
*/
33+
private $clausules = [];
34+
35+
/**
36+
* @var array|mixed
37+
*/
38+
private $settings = [];
39+
40+
/**
41+
* Engine constructor.
42+
* @param $id
43+
*/
44+
public function __construct($id)
45+
{
46+
$this->settings = off(App::config('database'), $id);
47+
if ($this->settings) {
48+
$this->driver = Factory::create($this->settings);
49+
}
50+
}
51+
52+
/**
53+
* @param $name
54+
* @param $arguments
55+
* @return $this
56+
*/
57+
public function __call($name, $arguments)
58+
{
59+
$clausule = $arguments[0];
60+
if (count($arguments) > 1) {
61+
$clausule = $arguments;
62+
}
63+
$name = strtolower($name);
64+
65+
$this->clausules[$name] = $clausule;
66+
if (is_null($clausule)) {
67+
unset($this->clausules[$name]);
68+
}
69+
70+
return $this;
71+
}
72+
73+
/**
74+
* @return Driver
75+
* @throws SimplesRunTimeError
76+
*/
77+
protected function driver(): Driver
78+
{
79+
if ($this->driver) {
80+
return $this->driver;
81+
}
82+
throw new SimplesRunTimeError("Cant use the driver", $this->settings);
83+
}
84+
85+
/**
86+
* @param array $values
87+
* @return string
88+
*/
89+
final public function register($values): string
90+
{
91+
return $this->driver()->create($this->clausules, $values);
92+
}
93+
94+
/**
95+
* @param $values
96+
* @return array
97+
*/
98+
final public function recover($values = []): array
99+
{
100+
return $this->driver()->read($this->clausules, $values);
101+
}
102+
103+
/**
104+
* @param $values
105+
* @param $filters
106+
* @return int
107+
*/
108+
final public function change($values, $filters = []): int
109+
{
110+
return $this->driver()->update($this->clausules, $values, $filters);
111+
}
112+
113+
/**
114+
* @param $filters
115+
* @return int
116+
*/
117+
final public function remove($filters): int
118+
{
119+
return $this->driver()->destroy($this->clausules, $filters);
120+
}
121+
122+
/**
123+
* @return Driver
124+
*/
125+
public function getDriver(): Driver
126+
{
127+
return $this->driver;
128+
}
129+
130+
/**
131+
* @return array
132+
*/
133+
public function getClausules(): array
134+
{
135+
return $this->clausules;
136+
}
137+
138+
/**
139+
* @return array|mixed
140+
*/
141+
public function getSettings()
142+
{
143+
return $this->settings;
144+
}
145+
146+
/**
147+
* Clear the clausules changes
148+
*/
149+
public function reset()
150+
{
151+
$this->clausules = [];
152+
}
153+
}

0 commit comments

Comments
 (0)