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