|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace WMDE\Fundraising\Store; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
|
8
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
|
9
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver; |
|
10
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain; |
|
11
|
|
|
use Doctrine\DBAL\Connection; |
|
12
|
|
|
use Doctrine\ORM\EntityManager; |
|
13
|
|
|
use Doctrine\ORM\Mapping\Driver\AnnotationDriver; |
|
14
|
|
|
use Doctrine\ORM\Tools\Setup; |
|
15
|
|
|
use Gedmo\Timestampable\TimestampableListener; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @since 0.1 |
|
19
|
|
|
* |
|
20
|
|
|
* @license GNU GPL v2+ |
|
21
|
|
|
*/ |
|
22
|
|
|
class Factory { |
|
23
|
|
|
|
|
24
|
|
|
private $entityManager; |
|
25
|
|
|
private $proxyDir; |
|
26
|
|
|
private $doctrineEntityPaths; |
|
27
|
|
|
private $additionalMetadataDrivers; |
|
28
|
|
|
|
|
29
|
|
|
private const DEFAULT_DOCTRINE_ENTITY_PATHS = [__DIR__ . '/../Entities/']; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Factory constructor. |
|
33
|
|
|
* @param Connection $connection |
|
34
|
|
|
* @param string $proxyDir |
|
35
|
|
|
* @param string[] $doctrineEntityPaths Paths to additional annotated entities that are not part of the store |
|
36
|
|
|
* @param MappingDriver[] $additionalMetadataDrivers namespace => driver |
|
37
|
|
|
*/ |
|
38
|
7 |
|
public function __construct( Connection $connection, $proxyDir = '/tmp', array $doctrineEntityPaths = [], |
|
39
|
|
|
$additionalMetadataDrivers = [] ) { |
|
40
|
7 |
|
$this->connection = $connection; |
|
|
|
|
|
|
41
|
7 |
|
$this->proxyDir = $proxyDir; |
|
42
|
7 |
|
$this->doctrineEntityPaths = $doctrineEntityPaths; |
|
43
|
7 |
|
$this->additionalMetadataDrivers = $additionalMetadataDrivers; |
|
44
|
7 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @since 0.1 |
|
48
|
|
|
* @return Installer |
|
49
|
|
|
*/ |
|
50
|
6 |
|
public function newInstaller() { |
|
51
|
6 |
|
return new Installer( $this->getEntityManager() ); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @since 0.1 |
|
56
|
|
|
* @return EntityManager |
|
57
|
|
|
* @throws \Doctrine\DBAL\DBALException |
|
58
|
|
|
* @throws \Doctrine\ORM\ORMException |
|
59
|
|
|
*/ |
|
60
|
7 |
|
public function getEntityManager() { |
|
61
|
7 |
|
if ( !$this->entityManager ) { |
|
62
|
7 |
|
$this->entityManager = $this->setupEntityManager(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
7 |
|
return $this->entityManager; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
7 |
|
private function setupEntityManager() { |
|
69
|
7 |
|
$config = Setup::createConfiguration(); |
|
70
|
7 |
|
$paths = array_merge( self::DEFAULT_DOCTRINE_ENTITY_PATHS, $this->doctrineEntityPaths ); |
|
71
|
|
|
|
|
72
|
7 |
|
$annotationReader = new AnnotationReader(); |
|
73
|
7 |
|
$annotationDriver = new AnnotationDriver( $annotationReader, $paths ); |
|
74
|
7 |
|
AnnotationRegistry::registerLoader( 'class_exists' ); |
|
|
|
|
|
|
75
|
7 |
|
$driver = new MappingDriverChain(); |
|
76
|
7 |
|
$driver->addDriver( $annotationDriver, 'WMDE\Fundraising\Entities' ); |
|
|
|
|
|
|
77
|
7 |
|
foreach ( $this->additionalMetadataDrivers as $namespace => $additionalMetadataDriver ) { |
|
78
|
|
|
$driver->addDriver( $additionalMetadataDriver, $namespace ); |
|
79
|
|
|
} |
|
80
|
7 |
|
$config->setMetadataDriverImpl( $driver ); |
|
81
|
7 |
|
$config->setProxyDir( $this->proxyDir ); |
|
82
|
|
|
|
|
83
|
7 |
|
$eventManager = $this->connection->getEventManager(); |
|
84
|
7 |
|
$timestampableListener = new TimestampableListener; |
|
85
|
7 |
|
$timestampableListener->setAnnotationReader( $annotationReader ); |
|
86
|
7 |
|
$eventManager->addEventSubscriber( $timestampableListener ); |
|
87
|
|
|
|
|
88
|
7 |
|
$entityManager = EntityManager::create( $this->connection, $config, $eventManager ); |
|
89
|
|
|
|
|
90
|
7 |
|
$platform = $entityManager->getConnection()->getDatabasePlatform(); |
|
91
|
7 |
|
$platform->registerDoctrineTypeMapping( 'enum', 'string' ); |
|
92
|
|
|
|
|
93
|
7 |
|
return $entityManager; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @since 0.1 |
|
98
|
|
|
* @return Connection |
|
99
|
|
|
*/ |
|
100
|
6 |
|
public function getConnection() { |
|
101
|
6 |
|
return $this->connection; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: