1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: ktammling |
5
|
|
|
* Date: 16.06.17 |
6
|
|
|
* Time: 14:56 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Framework; |
10
|
|
|
|
11
|
|
|
use Doctrine\ORM\Tools\Setup; |
12
|
|
|
use Doctrine\ORM\EntityManager; |
13
|
|
|
use Doctrine\DBAL\DriverManager; |
14
|
|
|
use Doctrine\DBAL; |
15
|
|
|
use SimpleThings\EntityAudit\AuditConfiguration; |
16
|
|
|
use SimpleThings\EntityAudit\AuditManager; |
17
|
|
|
use Doctrine\Common\EventManager; |
18
|
|
|
|
19
|
|
|
class DBase { |
20
|
|
|
|
21
|
|
|
protected $dbal; |
22
|
|
|
protected $entityManager; |
23
|
|
|
|
24
|
|
|
public function __construct($Config) |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
$dbal_config = new DBAL\Configuration(); |
28
|
|
|
$this->dbal = DriverManager::getConnection($Config['dbase'], $dbal_config); |
29
|
|
|
$orm_config = Setup::createAnnotationMetadataConfiguration(array(SCRIPT_BASE.'/src/entities'), $Config['general']['devMode'], null, null, false); |
30
|
|
|
if (extension_loaded('apcu')) { |
31
|
|
|
$orm_config->setQueryCacheImpl(new \Doctrine\Common\Cache\ApcuCache()); |
32
|
|
|
$orm_config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ApcuCache()); |
33
|
|
|
$orm_config->setResultCacheImpl(new \Doctrine\Common\Cache\ApcuCache()); |
34
|
|
|
} |
35
|
|
|
$auditconfig = new AuditConfiguration(); |
36
|
|
|
$auditconfig->setAuditedEntityClasses(array( |
37
|
|
|
'Entity\Addresses', |
38
|
|
|
'Entity\Prefixes', |
39
|
|
|
'Entity\User', |
40
|
|
|
'Entity\Vrfs' |
41
|
|
|
)); |
42
|
|
|
$auditconfig->setGlobalIgnoreColumns(array( |
43
|
|
|
'created_at', |
44
|
|
|
'updated_at' |
45
|
|
|
)); |
46
|
|
|
$auditconfig->setUsernameCallable(function() { |
47
|
|
|
return $_SESSION['Username']; |
48
|
|
|
}); |
49
|
|
|
$evm = new EventManager(); |
50
|
|
|
$auditManager = new AuditManager($auditconfig); |
51
|
|
|
$auditManager->registerEvents($evm); |
52
|
|
|
$EntityManager = EntityManager::create($Config['dbase'], $orm_config, $evm); |
53
|
|
|
$EntityManager->getConfiguration()->addCustomStringFunction('inet_aton', 'Application\DQL\InetAtonFunction'); |
54
|
|
|
$EntityManager->getConfiguration()->addCustomStringFunction('inet6_aton', 'Application\DQL\Inet6AtonFunction'); |
55
|
|
|
$EntityManager->getConfiguration()->addCustomStringFunction('MATCH_AGAINST', 'Application\DQL\MatchAgainstFunction'); |
56
|
|
|
$this->entityManager = $EntityManager; |
57
|
|
|
|
58
|
|
|
return true; |
|
|
|
|
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return DBAL\Connection |
64
|
|
|
*/ |
65
|
|
|
public function getDbal() |
66
|
|
|
{ |
67
|
|
|
return $this->dbal; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return EntityManager |
72
|
|
|
*/ |
73
|
|
|
public function getEntityManager() |
74
|
|
|
{ |
75
|
|
|
return $this->entityManager; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
|
80
|
|
|
} |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: