Skip to content

Instantly share code, notes, and snippets.

@clemgrim
Last active December 1, 2015 10:16
Show Gist options
  • Save clemgrim/6119cf678ecadd75d10c to your computer and use it in GitHub Desktop.
Save clemgrim/6119cf678ecadd75d10c to your computer and use it in GitHub Desktop.
<?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