-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoctrineORMServiceProvider~backup.php
114 lines (101 loc) · 4.31 KB
/
DoctrineORMServiceProvider~backup.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
<?php
namespace Mparaiso\Provider;
use Silex\ServiceProviderInterface;
use Exception;
use Mparaiso\Doctrine\ORM\DoctrineManagerRegistry;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Mapping\Driver\XmlDriver;
use Doctrine\ORM\Mapping\Driver\YamlDriver;
use Symfony\Component\Console\Application as ConsoleApplication;
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain;
use Silex\Application;
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Mparaiso\Provider\ConsoleServiceProvider;
/**
* @see https://github.com/mpmedia/dflydev-doctrine-orm-service-provider/blob/master/src/Dflydev/Pimple/Provider/DoctrineOrm/DoctrineOrmServiceProvider.php
*/
class DoctrineORMServiceProvider implements ServiceProviderInterface
{
public function boot(Application $app)
{
}
function getDriver($type, array $paths, Configuration $config)
{
$driver = NULL;
switch ($type) {
case 'yaml':
$driver = new YamlDriver($paths);
break;
case 'xml':
$driver = new XmlDriver($paths);
break;
case 'annotation' :
$driver = $config->newDefaultAnnotationDriver($paths, TRUE);
}
return $driver;
}
public function register(Application $app)
{
$self = $this;
$app["orm.proxy_dir"] = NULL;
$app["orm.cache"] = NULL;
$app["orm.is_dev_mode"] = $app["debug"];
$app['orm.driver.configs'] = array();
$app["orm.chain_driver"] = $app->share(function () {
return new MappingDriverChain();
});
/**
* EN : create entity manager config
* FR : creer la configuration de l'entity mananger
*/
$app["orm.config"] = $app->share(function ($app) {
$config = Setup::createConfiguration($app["orm.is_dev_mode"],
$app["orm.proxy_dir"],
$app["orm.cache"]);
$config->setMetadataDriverImpl($app["orm.chain_driver"]);
if (isset($app["orm.logger"])) {
$config->setSQLLogger($app["orm.logger"]);
}
$config->addCustomDatetimeFunction("DATE", '\Mparaiso\Doctrine\ORM\Function\Date');
return $config;
});
/**
* EN : create the entity manager
* FR : créer l'entity manager
*/
$app["orm.em"] = $app->share(function ($app) use ($self) {
foreach ($app["orm.driver.configs"] as $key => $config) {
if(! is_array($config['paths']))throw new Exception(' $config["paths"] must be an array of paths ');
if ($key == "default") {
$app["orm.chain_driver"]->setDefaultDriver($self->getDriver($config['type'], $config['paths'], $app["orm.config"]));
}
$app["orm.chain_driver"]->addDriver($self->getDriver($config['type'], $config['paths'], $app["orm.config"]), $config["namespace"]);
}
if (!isset($app["orm.connection"]) && $app["db"]) {
$app["orm.connection"] = $app["db"];
}
$em = EntityManager::create($app["orm.connection"], $app["orm.config"]);
return $em;
});
$app['orm.manager_registry'] = $app->share(function ($app) {
return new DoctrineManagerRegistry(array("default" => $app['orm.em']),
array("default" => $app['orm.em']->getConnection()));
});
/* call this to install Doctrine orm's commands $app['orm.console.boot_commands']() */
$app['orm.console.boot_commands']=$app->protect(function()use($app){
if (isset($app["console"])) {
$em = $app['orm.em'];
/* @var $console \Symfony\Component\Console\Application */
$console = $app["console"];
$console->getHelperSet()->set(new EntityManagerHelper($em), "em");
$console->getHelperSet()->set(new ConnectionHelper($em->getConnection()), "db");
ConsoleRunner::addCommands($app["console"]);
}
});
}
}