-
Notifications
You must be signed in to change notification settings - Fork 1
/
Extension.php
79 lines (67 loc) · 2.88 KB
/
Extension.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
<?php
namespace Flintstones\DoctrineOrm;
use Silex\Application;
use Silex\ExtensionInterface;
use Silex\Extension\DoctrineExtension;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Configuration;
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;
class Extension implements ExtensionInterface
{
public function register(Application $app)
{
$app->register(new DoctrineExtension());
$app['em'] = $app->share(function () use ($app) {
return EntityManager::create($app['db'], $app['em.config'], $app['db.event_manager']);
});
$app['em.config'] = $app->share(function () use ($app) {
$config = new Configuration();
$config->setMetadataCacheImpl($app['em.cache']);
$config->setQueryCacheImpl($app['em.cache']);
if (isset($app['em.proxy_dir'])) {
$config->setProxyDir($app['em.proxy_dir']);
}
if (isset($app['em.proxy_namespace'])) {
$config->setProxyNamespace($app['em.proxy_namespace']);
}
if (isset($app['em.entities'])) {
$chain = new DriverChain();
foreach ($app['em.entities'] as $entity) {
$pathes = (array)$entity['path'];
$namespace = $entity['namespace'];
switch ($entity['type']) {
case 'annotation':
$reader = new AnnotationReader();
$reader->setAnnotationNamespaceAlias('Doctrine\\ORM\\Mapping\\', 'orm');
$chain->addDriver(new AnnotationDriver($reader, $pathes), $namespace);
break;
case 'yml':
$driver = new YamlDriver($pathes);
$driver->setFileExtension('.yml');
$chain->addDriver($driver, $namespace);
break;
case 'xml':
$chain->addDriver(new XmlDriver($pathes), $namespace);
break;
default:
throw new \InvalidArgumentException('"' . $entity['type'] . '" is not a recognized driver');
break;
}
}
$config->setMetadataDriverImpl($chain);
}
return $config;
});
$app['em.cache'] = $app->share(function () {
return new ApcCache();
});
if (isset($app['db.orm.class_path'])) {
$app['autoloader']->registerNamespace('Doctrine\\ORM', $app['db.orm.class_path']);
}
}
}