Skip to content

Commit

Permalink
Amended Doctrine provider
Browse files Browse the repository at this point in the history
Now allow for retrieving the Doctrine DBAL connection from the container
  • Loading branch information
matthewbdaly committed May 27, 2019
1 parent 8525de5 commit 9a1f2b9
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions app/Providers/DoctrineProvider.php
Expand Up @@ -5,26 +5,31 @@
use League\Container\ServiceProvider\AbstractServiceProvider;
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\DBAL\DriverManager;

class DoctrineProvider extends AbstractServiceProvider
{
protected $provides = [
'Doctrine\DBAL\Connection',
'Doctrine\ORM\EntityManager',
];

public function register(): void
{
// Register items
$this->getContainer()
->add('Doctrine\ORM\EntityManager', function () {
$paths = ['app/Doctrine/Entities'];
$isDevMode = false;
$dbParams = array(
'driver' => getenv('DB_TYPE'),
'path' => getenv('DB_PATH'),
);
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
return EntityManager::create($dbParams, $config);
});
$container = $this->getContainer();
$container->add('Doctrine\DBAL\Connection', function () {
$dbParams = array(
'driver' => getenv('DB_TYPE'),
'path' => getenv('DB_PATH'),
);
return DriverManager::getConnection($dbParams);
});
$container->add('Doctrine\ORM\EntityManager', function () use ($container) {
$paths = ['app/Doctrine/Entities'];
$isDevMode = false;
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
return EntityManager::create($container->get('Doctrine\DBAL\Connection'), $config);
});
}
}

0 comments on commit 9a1f2b9

Please sign in to comment.