|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WMDE\Fundraising\Store; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
|
6
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
|
7
|
|
|
use Doctrine\DBAL\Connection; |
|
8
|
|
|
use Doctrine\ORM\EntityManager; |
|
9
|
|
|
use Doctrine\ORM\Mapping\Driver\AnnotationDriver; |
|
10
|
|
|
use Doctrine\ORM\Tools\Setup; |
|
11
|
|
|
use Gedmo\Timestampable\TimestampableListener; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @since 0.1 |
|
15
|
|
|
* |
|
16
|
|
|
* @licence GNU GPL v2+ |
|
17
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
18
|
|
|
* @author Jonas Kress |
|
19
|
|
|
* @author Kai Nissen < [email protected] > |
|
20
|
|
|
*/ |
|
21
|
|
|
class Factory { |
|
22
|
|
|
|
|
23
|
|
|
private $entityManager; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct( Connection $connection ) { |
|
26
|
|
|
$this->connection = $connection; |
|
|
|
|
|
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @since 0.1 |
|
31
|
|
|
* @return Installer |
|
32
|
|
|
*/ |
|
33
|
|
|
public function newInstaller() { |
|
34
|
|
|
return new Installer( $this->getEntityManager() ); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @since 0.1 |
|
39
|
|
|
* @return EntityManager |
|
40
|
|
|
* @throws \Doctrine\DBAL\DBALException |
|
41
|
|
|
* @throws \Doctrine\ORM\ORMException |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getEntityManager() { |
|
44
|
|
|
if ( !$this->entityManager ) { |
|
45
|
|
|
$this->entityManager = $this->setupEntityManager(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $this->entityManager; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
private function setupEntityManager() { |
|
52
|
|
|
$paths = [ __DIR__ . '/../Entities/' ]; |
|
53
|
|
|
$config = Setup::createConfiguration(); |
|
54
|
|
|
|
|
55
|
|
|
$annotationReader = new AnnotationReader(); |
|
56
|
|
|
$driver = new AnnotationDriver( $annotationReader, $paths ); |
|
57
|
|
|
AnnotationRegistry::registerLoader( 'class_exists' ); |
|
58
|
|
|
$config->setMetadataDriverImpl( $driver ); |
|
59
|
|
|
|
|
60
|
|
|
$eventManager = $this->connection->getEventManager(); |
|
61
|
|
|
$timestampableListener = new TimestampableListener; |
|
62
|
|
|
$timestampableListener->setAnnotationReader( $annotationReader ); |
|
63
|
|
|
$eventManager->addEventSubscriber( $timestampableListener ); |
|
64
|
|
|
|
|
65
|
|
|
$entityManager = EntityManager::create( $this->connection, $config, $eventManager ); |
|
66
|
|
|
|
|
67
|
|
|
$platform = $entityManager->getConnection()->getDatabasePlatform(); |
|
68
|
|
|
$platform->registerDoctrineTypeMapping( 'enum', 'string' ); |
|
69
|
|
|
|
|
70
|
|
|
return $entityManager; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @since 0.1 |
|
75
|
|
|
* @return Connection |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getConnection() { |
|
78
|
|
|
return $this->connection; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
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: