forked from doctrine-extensions/DoctrineExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
em.php
139 lines (114 loc) · 5.4 KB
/
em.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
declare(strict_types=1);
/*
* This file is part of the Doctrine Behavioral Extensions package.
* (c) Gediminas Morkevicius <gediminas.morkevicius@gmail.com> http://www.gediminasm.org
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
// this entity manager configuration works with the Doctrine DBAL and ORM.
// Regarding the AnnotationDriver setup, it most probably will be changed into
// XML because the annotation driver fails to read other classes in same namespace.
// Database connection configuration, modify at will
// Below is an example MySQL configuration
$connection = [
'host' => '127.0.0.1',
'port' => 3306,
'user' => 'root',
'password' => null,
'dbname' => 'doctrine_extensions_example',
'driver' => 'pdo_mysql',
'charset' => 'utf8mb4',
];
if (!file_exists(__DIR__.'/../vendor/autoload.php')) {
echo 'Composer has not been properly set up, please read the README.md file for setup instructions.';
exit(1);
}
/** @var \Composer\Autoload\ClassLoader $loader */
$loader = require __DIR__.'/../vendor/autoload.php';
// Register the example app with the autoloader
$loader->addPsr4('App\\', __DIR__.'/app');
// Ensure standard Doctrine annotations are registered
Doctrine\Common\Annotations\AnnotationRegistry::registerFile(
__DIR__.'/../vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'
);
// Define our global cache backend for the application.
// For larger applications, you may use multiple cache pools to store cacheable data in different locations.
$cache = new \Symfony\Component\Cache\Adapter\ArrayAdapter();
// Build the annotation reader for the application,
// by default we will use a decorated reader supporting a backend cache.
$annotationReader = new \Doctrine\Common\Annotations\PsrCachedReader(
new \Doctrine\Common\Annotations\AnnotationReader(),
$cache
);
// Create the mapping driver chain that will be used to read metadata from our various sources.
$mappingDriver = new \Doctrine\Persistence\Mapping\Driver\MappingDriverChain();
// Load the superclass metadata mapping for the extensions into the driver chain.
// Internally, this will also register the Doctrine Extensions annotations.
Gedmo\DoctrineExtensions::registerAbstractMappingIntoDriverChainORM(
$mappingDriver,
$annotationReader
);
// Register the application entities to our driver chain.
// Our application uses Annotations for mapping, but you can also use XML.
$mappingDriver->addDriver(
new Doctrine\ORM\Mapping\Driver\AnnotationDriver(
$annotationReader,
[__DIR__.'/app/Entity']
),
'App\Entity'
);
// Next, we will create the event manager and register the listeners for the extensions we will be using.
$eventManager = new Doctrine\Common\EventManager();
// Sluggable extension
$sluggableListener = new Gedmo\Sluggable\SluggableListener();
$sluggableListener->setAnnotationReader($annotationReader);
$sluggableListener->setCacheItemPool($cache);
$eventManager->addEventSubscriber($sluggableListener);
// Tree extension
$treeListener = new Gedmo\Tree\TreeListener();
$treeListener->setAnnotationReader($annotationReader);
$treeListener->setCacheItemPool($cache);
$eventManager->addEventSubscriber($treeListener);
// Loggable extension, not used in example
// $loggableListener = new Gedmo\Loggable\LoggableListener;
// $loggableListener->setAnnotationReader($annotationReader);
// $loggableListener->setCacheItemPool($cache);
// $loggableListener->setUsername('admin');
// $eventManager->addEventSubscriber($loggableListener);
// Timestampable extension
$timestampableListener = new Gedmo\Timestampable\TimestampableListener();
$timestampableListener->setAnnotationReader($annotationReader);
$timestampableListener->setCacheItemPool($cache);
$eventManager->addEventSubscriber($timestampableListener);
// Blameable extension
$blameableListener = new \Gedmo\Blameable\BlameableListener();
$blameableListener->setAnnotationReader($annotationReader);
$blameableListener->setCacheItemPool($cache);
$blameableListener->setUserValue('MyUsername'); // determine from your environment
$eventManager->addEventSubscriber($blameableListener);
// Translatable
$translatableListener = new Gedmo\Translatable\TranslatableListener();
// The current translation locale should be set from session or some other request data,
// but most importantly, it must be set before the entity manager is flushed.
$translatableListener->setTranslatableLocale('en');
$translatableListener->setDefaultLocale('en');
$translatableListener->setAnnotationReader($annotationReader);
$translatableListener->setCacheItemPool($cache);
$eventManager->addEventSubscriber($translatableListener);
// Sortable extension, not used in example
// $sortableListener = new Gedmo\Sortable\SortableListener;
// $sortableListener->setAnnotationReader($annotationReader);
// $sortableListener->setCacheItemPool($cache);
// $eventManager->addEventSubscriber($sortableListener);
// Now we will build our ORM configuration.
$config = new Doctrine\ORM\Configuration();
$config->setProxyDir(sys_get_temp_dir());
$config->setProxyNamespace('Proxy');
$config->setAutoGenerateProxyClasses(false);
$config->setMetadataDriverImpl($mappingDriver);
$config->setMetadataCache($cache);
$config->setQueryCache($cache);
$config->setResultCache($cache);
// Finally, we create the entity manager
return Doctrine\ORM\EntityManager::create($connection, $config, $eventManager);