Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GH-8327] Make EntityManagerProvider compatible with expected DoctrineBundle usage #8646

Merged
merged 3 commits into from
Apr 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ final protected function getEntityManager(InputInterface $input): EntityManagerI
return $this->getHelper('em')->getEntityManager();
}

return $this->entityManagerProvider->getManager($input->getOption('em'));
return $input->getOption('em') === null
? $this->entityManagerProvider->getDefaultManager()
: $this->entityManagerProvider->getManager($input->getOption('em'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected function configure()
->addArgument('owner-class', InputArgument::OPTIONAL, 'The owner entity name.')
->addArgument('association', InputArgument::OPTIONAL, 'The association collection name.')
->addArgument('owner-id', InputArgument::OPTIONAL, 'The owner identifier.')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on', 'default')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on')
->addOption('all', null, InputOption::VALUE_NONE, 'If defined, all entity regions will be deleted/invalidated.')
->addOption('flush', null, InputOption::VALUE_NONE, 'If defined, all cache entries will be flushed.')
->setHelp(<<<EOT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected function configure()
->setDescription('Clear a second-level cache entity region')
->addArgument('entity-class', InputArgument::OPTIONAL, 'The entity name.')
->addArgument('entity-id', InputArgument::OPTIONAL, 'The entity identifier.')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on', 'default')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on')
->addOption('all', null, InputOption::VALUE_NONE, 'If defined, all entity regions will be deleted/invalidated.')
->addOption('flush', null, InputOption::VALUE_NONE, 'If defined, all cache entries will be flushed.')
->setHelp(<<<EOT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected function configure()
{
$this->setName('orm:clear-cache:metadata')
->setDescription('Clear all metadata cache of the various cache drivers')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on', 'default')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on')
->addOption('flush', null, InputOption::VALUE_NONE, 'If defined, cache entries will be flushed instead of deleted/invalidated.')
->setHelp(<<<EOT
The <info>%command.name%</info> command is meant to clear the metadata cache of associated Entity Manager.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected function configure()
{
$this->setName('orm:clear-cache:query')
->setDescription('Clear all query cache of the various cache drivers')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on', 'default')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on')
->addOption('flush', null, InputOption::VALUE_NONE, 'If defined, cache entries will be flushed instead of deleted/invalidated.')
->setHelp(<<<EOT
The <info>%command.name%</info> command is meant to clear the query cache of associated Entity Manager.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected function configure()
$this->setName('orm:clear-cache:region:query')
->setDescription('Clear a second-level cache query region')
->addArgument('region-name', InputArgument::OPTIONAL, 'The query region to clear.')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on', 'default')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on')
->addOption('all', null, InputOption::VALUE_NONE, 'If defined, all query regions will be deleted/invalidated.')
->addOption('flush', null, InputOption::VALUE_NONE, 'If defined, all cache entries will be flushed.')
->setHelp(<<<EOT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected function configure()
{
$this->setName('orm:clear-cache:result')
->setDescription('Clear all result cache of the various cache drivers')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on', 'default')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on')
->addOption('flush', null, InputOption::VALUE_NONE, 'If defined, cache entries will be flushed instead of deleted/invalidated.')
->setHelp(<<<EOT
The <info>%command.name%</info> command is meant to clear the result cache of associated Entity Manager.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected function configure()
->setDescription('Convert mapping information between supported formats')
->addArgument('to-type', InputArgument::REQUIRED, 'The mapping type to be converted.')
->addArgument('dest-path', InputArgument::REQUIRED, 'The path to generate your entities classes.')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on', 'default')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on')
->addOption('filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'A string pattern used to match entities that should be processed.')
->addOption('force', 'f', InputOption::VALUE_NONE, 'Force to overwrite existing mapping files.')
->addOption('from-database', null, null, 'Whether or not to convert mapping information from existing database.')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected function configure()
{
$this->setName('orm:ensure-production-settings')
->setDescription('Verify that Doctrine is properly configured for a production environment')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on', 'default')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on')
->addOption('complete', null, InputOption::VALUE_NONE, 'Flag to also inspect database connection existence.')
->setHelp('Verify that Doctrine is properly configured for a production environment.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected function configure()
->setAliases(['orm:generate:entities'])
->setDescription('Generate entity classes and method stubs from your mapping information')
->addArgument('dest-path', InputArgument::REQUIRED, 'The path to generate your entity classes.')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on', 'default')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on')
->addOption('filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'A string pattern used to match entities that should be processed.')
->addOption('generate-annotations', null, InputOption::VALUE_OPTIONAL, 'Flag to define if generator should generate annotation metadata on entities.', false)
->addOption('generate-methods', null, InputOption::VALUE_OPTIONAL, 'Flag to define if generator should generate stub methods on entities.', true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected function configure()
->setAliases(['orm:generate:proxies'])
->setDescription('Generates proxy classes for entity classes')
->addArgument('dest-path', InputArgument::OPTIONAL, 'The path to generate your proxy classes. If none is provided, it will attempt to grab from configuration.')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on', 'default')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on')
->addOption('filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'A string pattern used to match entities that should be processed.')
->setHelp('Generates proxy classes for entity classes.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function configure()
->setAliases(['orm:generate:repositories'])
->setDescription('Generate repository classes from your mapping information')
->addArgument('dest-path', InputArgument::REQUIRED, 'The path to generate your repository classes.')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on', 'default')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on')
->addOption('filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'A string pattern used to match entities that should be processed.')
->setHelp('Generate repository classes from your mapping information.');
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Tools/Console/Command/InfoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function configure()
{
$this->setName('orm:info')
->setDescription('Show basic information about all mapped entities')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on', 'default')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on')
->setHelp(<<<EOT
The <info>%command.name%</info> shows basic information about which
entities exist and possibly if their mapping information contains errors or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected function configure(): void
$this->setName('orm:mapping:describe')
->addArgument('entityName', InputArgument::REQUIRED, 'Full or partial name of entity')
->setDescription('Display information about mapped objects')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on', 'default')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on')
->setHelp(<<<EOT
The %command.full_name% command describes the metadata for the given full or partial entity class name.

Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Tools/Console/Command/RunDqlCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected function configure()
$this->setName('orm:run-dql')
->setDescription('Executes arbitrary DQL directly from the command line')
->addArgument('dql', InputArgument::REQUIRED, 'The DQL to execute.')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on', 'default')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on')
->addOption('hydrate', null, InputOption::VALUE_REQUIRED, 'Hydration mode of result set. Should be either: object, array, scalar or single-scalar.', 'object')
->addOption('first-result', null, InputOption::VALUE_REQUIRED, 'The first result in the result set.')
->addOption('max-result', null, InputOption::VALUE_REQUIRED, 'The maximum number of results in the result set.')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected function configure()
{
$this->setName('orm:validate-schema')
->setDescription('Validate the mapping files')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on', 'default')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on')
->addOption('skip-mapping', null, InputOption::VALUE_NONE, 'Skip the mapping validation check')
->addOption('skip-sync', null, InputOption::VALUE_NONE, 'Skip checking if the mapping is in sync with the database')
->setHelp('Validate that the mapping files are correct and in sync with the database.');
Expand Down
4 changes: 3 additions & 1 deletion lib/Doctrine/ORM/Tools/Console/EntityManagerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@

interface EntityManagerProvider
{
public function getManager(string $name = 'default'): EntityManagerInterface;
public function getDefaultManager(): EntityManagerInterface;

public function getManager(string $name): EntityManagerInterface;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(EntityManagerProvider $entityManagerProvider)

public function getDefaultConnection(): Connection
{
return $this->entityManagerProvider->getManager('default')->getConnection();
return $this->entityManagerProvider->getDefaultManager()->getConnection();
}

public function getConnection(string $name): Connection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@ public function __construct(HelperSet $helperSet)
);
}

public function getManager(string $name = 'default'): EntityManagerInterface
public function getManager(string $name): EntityManagerInterface
{
if ($name !== 'default') {
throw UnknownManagerException::unknownManager($name, ['default']);
}

return $this->getDefaultManager();
}

public function getDefaultManager(): EntityManagerInterface
{
$helper = $this->helperSet->get('entityManager');

assert($helper instanceof EntityManagerHelper);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,24 @@ final class SingleManagerProvider implements EntityManagerProvider
/** @var EntityManagerInterface */
private $entityManager;

public function __construct(EntityManagerInterface $entityManager)
/** @var string */
private $defaultManagerName;

public function __construct(EntityManagerInterface $entityManager, string $defaultManagerName = 'default')
{
$this->entityManager = $entityManager;
$this->entityManager = $entityManager;
$this->defaultManagerName = $defaultManagerName;
}

public function getDefaultManager(): EntityManagerInterface
{
return $this->entityManager;
}

public function getManager(string $name = 'default'): EntityManagerInterface
public function getManager(string $name): EntityManagerInterface
{
if ($name !== 'default') {
throw UnknownManagerException::unknownManager($name, ['default']);
if ($name !== $this->defaultManagerName) {
throw UnknownManagerException::unknownManager($name, [$this->defaultManagerName]);
}

return $this->entityManager;
Expand Down