Skip to content

Commit

Permalink
Merge pull request #1 from chalasr/commands-sf42
Browse files Browse the repository at this point in the history
Add BC layer for container-aware commands
  • Loading branch information
chalasr committed Nov 30, 2018
2 parents a5e39ed + 70548fd commit f069ec6
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Command/CreateDatabaseDoctrineCommand.php
Expand Up @@ -46,7 +46,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$connectionName = $input->getOption('connection');
if (empty($connectionName)) {
$connectionName = $this->doctrine->getDefaultConnectionName();
$connectionName = $this->getDoctrine()->getDefaultConnectionName();
}
$connection = $this->getDoctrineConnection($connectionName);

Expand Down
63 changes: 58 additions & 5 deletions Command/DoctrineCommand.php
Expand Up @@ -9,6 +9,7 @@
use Doctrine\ORM\Tools\EntityGenerator;
use LogicException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Base class for Doctrine console commands to extend from.
Expand All @@ -17,16 +18,60 @@
*/
abstract class DoctrineCommand extends Command
{
/** @var ManagerRegistry */
protected $doctrine;
/** @var ManagerRegistry|null */
private $doctrine;

public function __construct(ManagerRegistry $doctrine)
/** @var ContainerInterface|null */
private $container;

public function __construct(ManagerRegistry $doctrine = null)
{
parent::__construct();

if (null === $doctrine) {
@trigger_error(sprintf(
'The "%s" constructor expects a "%s" instance as first argument, not passing it will throw a \TypeError in DoctrineBundle 2.0.',
get_class($this),
ManagerRegistry::class
), E_USER_DEPRECATED);
}

$this->doctrine = $doctrine;
}

/**
* @deprecated
*/
public function setContainer(ContainerInterface $container = null)
{
@trigger_error(sprintf('The "%s()" method is deprecated and will be removed in DoctrineBundle 2.0.', __METHOD__), E_USER_DEPRECATED);

$this->container = $container;
}

/**
* @return ContainerInterface
*
* @throws \LogicException
*
* @deprecated
*/
protected function getContainer()
{
@trigger_error(sprintf('The "%s()" method is deprecated and will be removed in DoctrineBundle 2.0.', __METHOD__), E_USER_DEPRECATED);

if (null === $this->container) {
$application = $this->getApplication();
if (null === $application) {
throw new \LogicException('The container cannot be retrieved as the application instance is not yet set.');
}

$this->container = $application->getKernel()->getContainer();
}

return $this->container;
}

/**
* get a doctrine entity generator
*
Expand Down Expand Up @@ -55,7 +100,7 @@ protected function getEntityGenerator()
*/
protected function getEntityManager($name, $shardId = null)
{
$manager = $this->doctrine->getManager($name);
$manager = $this->getDoctrine()->getManager($name);

if ($shardId) {
if (! $manager->getConnection() instanceof PoolingShardConnection) {
Expand All @@ -77,6 +122,14 @@ protected function getEntityManager($name, $shardId = null)
*/
protected function getDoctrineConnection($name)
{
return $this->doctrine->getConnection($name);
return $this->getDoctrine()->getConnection($name);
}

/**
* @return ManagerRegistry
*/
protected function getDoctrine()
{
return $this->doctrine ?: $this->doctrine = $this->getContainer()->get('doctrine');
}
}
2 changes: 1 addition & 1 deletion Command/DropDatabaseDoctrineCommand.php
Expand Up @@ -55,7 +55,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$connectionName = $input->getOption('connection');
if (empty($connectionName)) {
$connectionName = $this->doctrine->getDefaultConnectionName();
$connectionName = $this->getDoctrine()->getDefaultConnectionName();
}
$connection = $this->getDoctrineConnection($connectionName);

Expand Down
4 changes: 2 additions & 2 deletions Command/GenerateEntitiesDoctrineCommand.php
Expand Up @@ -83,7 +83,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
' If you wish to generate your entities, use <info>make:entity --regenerate</info> from MakerBundle instead.',
]);

$manager = new DisconnectedMetadataFactory($this->doctrine);
$manager = new DisconnectedMetadataFactory($this->getDoctrine());

try {
$bundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('name'));
Expand All @@ -95,7 +95,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$pos = strpos($name, ':');

if ($pos !== false) {
$name = $this->doctrine->getAliasNamespace(substr($name, 0, $pos)) . '\\' . substr($name, $pos + 1);
$name = $this->getDoctrine()->getAliasNamespace(substr($name, 0, $pos)) . '\\' . substr($name, $pos + 1);
}

if (class_exists($name)) {
Expand Down

0 comments on commit f069ec6

Please sign in to comment.