-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathDoctrineORMServiceProvider.php
128 lines (109 loc) · 4.67 KB
/
DoctrineORMServiceProvider.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
<?php
namespace Nutwerk\Provider;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Configuration as DBALConfiguration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Configuration as ORMConfiguration;
use Doctrine\ORM\Mapping\Driver\DriverChain;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Mapping\Driver\XmlDriver;
use Doctrine\ORM\Mapping\Driver\YamlDriver;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Cache\ApcCache;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\EventManager;
use Silex\Application;
use Silex\ServiceProviderInterface;
class DoctrineORMServiceProvider implements ServiceProviderInterface
{
public function register(Application $app)
{
$dbal = $app['db'];
if (!$dbal instanceof \Doctrine\DBAL\Connection) {
throw new \InvalidArgumentException('$app[\'db\'] must be an instance of \Doctrine\DBAL\Connection');
}
$this->loadDoctrineConfiguration($app);
$this->setOrmDefaults($app);
$this->loadDoctrineOrm($app);
if(isset($app['db.orm.class_path'])) {
$app['autoloader']->registerNamespace('Doctrine\\ORM', $app['db.orm.class_path']);
}
}
private function loadDoctrineOrm(Application $app)
{
$self = $this;
$app['db.orm.em'] = $app->share(function() use($self, $app) {
return EntityManager::create($app['db'], $app['db.orm.config']);
});
}
private function setOrmDefaults(Application $app)
{
$defaults = array(
'entities' => array(
array(
'type' => 'annotation',
'path' => 'Entity',
'namespace' => 'Entity',
)
),
'proxies_dir' => 'cache/doctrine/Proxy',
'proxies_namespace' => 'DoctrineProxy',
'auto_generate_proxies' => true,
'cache' => new ArrayCache,
);
foreach ($defaults as $key => $value) {
if (!isset($app['db.orm.' . $key])) {
$app['db.orm.'.$key] = $value;
}
}
}
public function loadDoctrineConfiguration(Application $app)
{
$app['db.orm.config'] = $app->share(function() use($app) {
$config = new ORMConfiguration;
// Metadata Cache (defaults to db.orm.cache)
$metadataCache = (isset($app['db.orm.cache_metadata'])) ? $app['db.orm.cache_metadata'] : $app['db.orm.cache'];
$config->setMetadataCacheImpl($metadataCache);
// Query Cache (defaults to db.orm.cache)
$queryCache = (isset($app['db.orm.cache_query'])) ? $app['db.orm.cache_query'] : $app['db.orm.cache'];
$config->setQueryCacheImpl($queryCache);
// Result Cache (not configured by default)
if(isset($app['db.orm.cache_result'])) {
$config->setResultCacheImpl($app['db.orm.cache_result']);
}
// Entities / Drivers
$chain = new DriverChain;
foreach((array)$app['db.orm.entities'] as $entity) {
switch($entity['type']) {
case 'default':
case 'annotation':
$driver = $config->newDefaultAnnotationDriver((array)$entity['path']);
$chain->addDriver($driver, $entity['namespace']);
break;
case 'yml':
$driver = new YamlDriver((array)$entity['path']);
$driver->setFileExtension('.yml');
$chain->addDriver($driver, $entity['namespace']);
break;
case 'xml':
$driver = new XmlDriver((array)$entity['path'], $entity['namespace']);
$driver->setFileExtension('.xml');
$chain->addDriver($driver, $entity['namespace']);
break;
default:
throw new \InvalidArgumentException(sprintf('"%s" is not a recognized driver', $entity['type']));
break;
}
$app['autoloader']->registerNamespace($entity['namespace'], $entity['path']);
}
$config->setMetadataDriverImpl($chain);
$config->setProxyDir($app['db.orm.proxies_dir']);
$config->setProxyNamespace($app['db.orm.proxies_namespace']);
$config->setAutoGenerateProxyClasses($app['db.orm.auto_generate_proxies']);
return $config;
});
}
public function boot (Application $app)
{
}
}