Last active
December 1, 2015 10:16
-
-
Save clemgrim/6119cf678ecadd75d10c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use Pimple\Container; | |
use Pimple\ServiceProviderInterface; | |
use Doctrine\ORM\Tools\Setup; | |
use Doctrine\ORM\EntityManager; | |
use Doctrine\ORM\Mapping\Driver\AnnotationDriver; | |
use Doctrine\Common\Annotations\AnnotationReader; | |
use Doctrine\Common\Annotations\AnnotationRegistry; | |
use Doctrine\Common\Proxy\AbstractProxyFactory; | |
class DatabaseServiceProvider implements ServiceProviderInterface | |
{ | |
public function register(Container $app) | |
{ | |
$app['db'] = function () use ($app) { | |
$config = $this->getConfig($app['debug']); | |
$conn = array( | |
'driver' => 'pdo_mysql', | |
'dbname' => 'recettage', | |
'host' => 'localhost', | |
'user' => 'root', | |
'password' => '', | |
); | |
// obtaining the entity manager | |
return EntityManager::create($conn, $config); | |
}; | |
$app['dbtest'] = function () use ($app) { | |
$config = $this->getConfig($app['debug']); | |
$conn = array( | |
'driver' => 'pdo_mysql', | |
'dbname' => 'recettage.test', | |
'host' => 'localhost', | |
'user' => 'root', | |
'password' => '', | |
); | |
// obtaining the entity manager | |
return EntityManager::create($conn, $config); | |
}; | |
$app['repo'] = $app->protect(function ($repo) use ($app) { | |
return $app['db']->getRepository('\\App\\Entities\\'.$repo); | |
}); | |
} | |
public function getConfig($isDevMode = true) | |
{ | |
AnnotationRegistry::registerLoader('class_exists'); | |
$path = realpath(__DIR__.'/../Entities'); | |
$cacheDir = __DIR__.'/../../cache/doctrine'; | |
$config = Setup::createConfiguration($isDevMode, $cacheDir); | |
$driver = new AnnotationDriver(new AnnotationReader(), [$path]); | |
$config->newDefaultAnnotationDriver($path); | |
if ($isDevMode) { | |
$cache = new \Doctrine\Common\Cache\ArrayCache(); | |
$config->setAutoGenerateProxyClasses(AbstractProxyFactory::AUTOGENERATE_ALWAYS); | |
} else { | |
$cache = new \Doctrine\Common\Cache\FilesystemCache($cacheDir); | |
$config->setAutoGenerateProxyClasses(AbstractProxyFactory::AUTOGENERATE_NEVER); | |
} | |
$config->setMetadataDriverImpl($driver); | |
$config->setMetadataCacheImpl($cache); | |
$config->setQueryCacheImpl($cache); | |
return $config; | |
} | |
public function boot(Application $app) | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment