1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rougin\Credo; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Tools\Setup; |
6
|
|
|
|
7
|
|
|
use Rougin\Credo\Helpers\MagicMethodHelper; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Credo |
11
|
|
|
* |
12
|
|
|
* Integrates Doctrine to CodeIgniter with ease. |
13
|
|
|
* |
14
|
|
|
* @package Credo |
15
|
|
|
* @author Rougin Royce Gutib <[email protected]> |
16
|
|
|
* |
17
|
|
|
* @method \Doctrine\ORM\EntityRepository getRepository(string $entityName) |
18
|
|
|
* @method \Doctrine\ORM\Mapping\ClassMetadataFactory getMetadataFactory() |
19
|
|
|
* @method \Doctrine\ORM\Query\QueryBuilder createQueryBuilder() |
20
|
|
|
* @method object refresh($entity) |
21
|
|
|
* @method void flush(object|array $entity = null) |
22
|
|
|
* @method void remove(object $entity) |
23
|
|
|
*/ |
24
|
|
|
class Credo |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var \Doctrine\ORM\EntityManager |
28
|
|
|
*/ |
29
|
|
|
protected $entityManager; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param \CI_DB_query_builder $database |
33
|
|
|
*/ |
34
|
9 |
|
public function __construct(\CI_DB_query_builder $database) |
35
|
|
|
{ |
36
|
9 |
|
$connection = $this->prepareDatabase($database); |
37
|
9 |
|
$directories = [ APPPATH . 'models', APPPATH . 'repositories' ]; |
38
|
9 |
|
$proxies = APPPATH . 'models/proxies'; |
39
|
|
|
|
40
|
|
|
// Set $ci->db->db_debug to TRUE to disable caching while you develop |
41
|
9 |
|
$isDevMode = $database->db_debug; |
42
|
|
|
|
43
|
9 |
|
$config = Setup::createAnnotationMetadataConfiguration($directories, $isDevMode, $proxies); |
44
|
|
|
|
45
|
9 |
|
$this->entityManager = \Doctrine\ORM\EntityManager::create($connection, $config); |
46
|
9 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Gets the database configuration for specific database drivers. |
50
|
|
|
* |
51
|
|
|
* @param array $config |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
9 |
|
private function getDatabaseConfiguration(array $config) |
55
|
|
|
{ |
56
|
9 |
|
if ($config['driver'] == 'pdo' && strpos($config['dsn'], ':') !== false) { |
57
|
9 |
|
$keys = explode(':', $config['dsn']); |
58
|
|
|
|
59
|
9 |
|
$config['driver'] .= '_' . $keys[0]; |
60
|
9 |
|
} |
61
|
|
|
|
62
|
9 |
|
if ($config['driver'] == 'pdo_sqlite') { |
63
|
9 |
|
$config['path'] = str_replace('sqlite:', '', $config['dsn']); |
64
|
9 |
|
} |
65
|
|
|
|
66
|
9 |
|
return $config; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Sets up the database configuration from CodeIgniter. |
71
|
|
|
* |
72
|
|
|
* @param \CI_DB_query_builder $database |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
9 |
|
private function prepareDatabase(\CI_DB_query_builder $database) |
76
|
|
|
{ |
77
|
|
|
$connection = [ |
78
|
9 |
|
'dsn' => $database->dsn, |
79
|
9 |
|
'driver' => $database->dbdriver, |
80
|
9 |
|
'user' => $database->username, |
81
|
9 |
|
'password' => $database->password, |
82
|
9 |
|
'host' => $database->hostname, |
83
|
9 |
|
'dbname' => $database->database, |
84
|
9 |
|
'charset' => $database->char_set, |
85
|
9 |
|
]; |
86
|
|
|
|
87
|
9 |
|
return $this->getDatabaseConfiguration($connection); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Calls methods from EntityManager in underscore case. |
92
|
|
|
* |
93
|
|
|
* @param string $method |
94
|
|
|
* @param mixed $parameters |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
21 |
|
public function __call($method, $parameters) |
98
|
|
|
{ |
99
|
21 |
|
return MagicMethodHelper::call($this, $method, $parameters, $this->entityManager); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|