Skip to content

Commit

Permalink
Merge pull request #6655 from OskarStark/symfony-style
Browse files Browse the repository at this point in the history
Use SymfonyStyle for command output
  • Loading branch information
lcobucci committed Nov 24, 2017
2 parents ba32237 + 6b5eb11 commit b47a39b
Show file tree
Hide file tree
Showing 32 changed files with 852 additions and 820 deletions.
Expand Up @@ -19,13 +19,14 @@

namespace Doctrine\ORM\Tools\Console\Command\ClearCache;

use Doctrine\ORM\Cache;
use Doctrine\ORM\Cache\Region\DefaultRegion;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\ORM\Cache\Region\DefaultRegion;
use Doctrine\ORM\Cache;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
* Command to clear a collection cache region.
Expand All @@ -40,17 +41,14 @@ class CollectionRegionCommand extends Command
*/
protected function configure()
{
$this
->setName('orm:clear-cache:region:collection')
->setDescription('Clear a second-level cache collection region.')
->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('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.');


$this->setHelp(<<<EOT
$this->setName('orm:clear-cache:region:collection')
->setDescription('Clear a second-level cache collection region')
->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('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
The <info>%command.name%</info> command is meant to clear a second-level cache collection regions for an associated Entity Manager.
It is possible to delete/invalidate all collection region, a specific collection region or flushes the cache provider.
Expand All @@ -74,14 +72,16 @@ protected function configure()
Finally, be aware that if <info>--flush</info> option is passed,
not all cache providers are able to flush entries, because of a limitation of its execution nature.
EOT
);
);
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$ui = new SymfonyStyle($input, $output);

$em = $this->getHelper('em')->getEntityManager();
$ownerClass = $input->getArgument('owner-class');
$assoc = $input->getArgument('association');
Expand All @@ -92,7 +92,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
throw new \InvalidArgumentException('No second-level cache is configured on the given EntityManager.');
}

if ( (! $ownerClass || ! $assoc) && ! $input->getOption('all')) {
if (( ! $ownerClass || ! $assoc) && ! $input->getOption('all')) {
throw new \InvalidArgumentException('Missing arguments "--owner-class" "--association"');
}

Expand All @@ -108,27 +108,40 @@ protected function execute(InputInterface $input, OutputInterface $output)

$collectionRegion->getCache()->flushAll();

$output->writeln(sprintf('Flushing cache provider configured for <info>"%s#%s"</info>', $ownerClass, $assoc));
$ui->comment(
sprintf(
'Flushing cache provider configured for <info>"%s#%s"</info>',
$ownerClass,
$assoc
)
);

return;
}

if ($input->getOption('all')) {
$output->writeln('Clearing <info>all</info> second-level cache collection regions');
$ui->comment('Clearing <info>all</info> second-level cache collection regions');

$cache->evictEntityRegions();

return;
}

if ($ownerId) {
$output->writeln(sprintf('Clearing second-level cache entry for collection <info>"%s#%s"</info> owner entity identified by <info>"%s"</info>', $ownerClass, $assoc, $ownerId));
$ui->comment(
sprintf(
'Clearing second-level cache entry for collection <info>"%s#%s"</info> owner entity identified by <info>"%s"</info>',
$ownerClass,
$assoc,
$ownerId
)
);
$cache->evictCollection($ownerClass, $assoc, $ownerId);

return;
}

$output->writeln(sprintf('Clearing second-level cache for collection <info>"%s#%s"</info>', $ownerClass, $assoc));
$ui->comment(sprintf('Clearing second-level cache for collection <info>"%s#%s"</info>', $ownerClass, $assoc));
$cache->evictCollectionRegion($ownerClass, $assoc);
}
}
Expand Up @@ -19,13 +19,14 @@

namespace Doctrine\ORM\Tools\Console\Command\ClearCache;

use Doctrine\ORM\Cache;
use Doctrine\ORM\Cache\Region\DefaultRegion;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\ORM\Cache\Region\DefaultRegion;
use Doctrine\ORM\Cache;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
* Command to clear a entity cache region.
Expand All @@ -40,16 +41,13 @@ class EntityRegionCommand extends Command
*/
protected function configure()
{
$this
->setName('orm:clear-cache:region:entity')
->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('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.');


$this->setHelp(<<<EOT
$this->setName('orm:clear-cache:region:entity')
->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('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
The <info>%command.name%</info> command is meant to clear a second-level cache entity region for an associated Entity Manager.
It is possible to delete/invalidate all entity region, a specific entity region or flushes the cache provider.
Expand All @@ -73,14 +71,16 @@ protected function configure()
Finally, be aware that if <info>--flush</info> option is passed,
not all cache providers are able to flush entries, because of a limitation of its execution nature.
EOT
);
);
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$ui = new SymfonyStyle($input, $output);

$em = $this->getHelper('em')->getEntityManager();
$entityClass = $input->getArgument('entity-class');
$entityId = $input->getArgument('entity-id');
Expand All @@ -106,27 +106,33 @@ protected function execute(InputInterface $input, OutputInterface $output)

$entityRegion->getCache()->flushAll();

$output->writeln(sprintf('Flushing cache provider configured for entity named <info>"%s"</info>', $entityClass));
$ui->comment(sprintf('Flushing cache provider configured for entity named <info>"%s"</info>', $entityClass));

return;
}

if ($input->getOption('all')) {
$output->writeln('Clearing <info>all</info> second-level cache entity regions');
$ui->comment('Clearing <info>all</info> second-level cache entity regions');

$cache->evictEntityRegions();

return;
}

if ($entityId) {
$output->writeln(sprintf('Clearing second-level cache entry for entity <info>"%s"</info> identified by <info>"%s"</info>', $entityClass, $entityId));
$ui->comment(
sprintf(
'Clearing second-level cache entry for entity <info>"%s"</info> identified by <info>"%s"</info>',
$entityClass,
$entityId
)
);
$cache->evictEntity($entityClass, $entityId);

return;
}

$output->writeln(sprintf('Clearing second-level cache for entity <info>"%s"</info>', $entityClass));
$ui->comment(sprintf('Clearing second-level cache for entity <info>"%s"</info>', $entityClass));
$cache->evictEntityRegion($entityClass);
}
}
Expand Up @@ -19,12 +19,13 @@

namespace Doctrine\ORM\Tools\Console\Command\ClearCache;

use Doctrine\Common\Cache\ApcCache;
use Doctrine\Common\Cache\XcacheCache;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\Common\Cache\ApcCache;
use Doctrine\Common\Cache\XcacheCache;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
* Command to clear the metadata cache of the various cache drivers.
Expand All @@ -43,19 +44,10 @@ class MetadataCommand extends Command
*/
protected function configure()
{
$this
->setName('orm:clear-cache:metadata')
->setDescription('Clear all metadata cache of the various cache drivers.')
->setDefinition(
[
new InputOption(
'flush', null, InputOption::VALUE_NONE,
'If defined, cache entries will be flushed instead of deleted/invalidated.'
)
]
);

$this->setHelp(<<<EOT
$this->setName('orm:clear-cache:metadata')
->setDescription('Clear all metadata cache of the various cache drivers')
->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.
It is possible to invalidate all cache entries at once - called delete -, or flushes the cache provider
instance completely.
Expand All @@ -72,14 +64,16 @@ protected function configure()
Finally, be aware that if <info>--flush</info> option is passed, not all cache providers are able to flush entries,
because of a limitation of its execution nature.
EOT
);
);
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$ui = new SymfonyStyle($input, $output);

$em = $this->getHelper('em')->getEntityManager();
$cacheDriver = $em->getConfiguration()->getMetadataCacheImpl();

Expand All @@ -95,8 +89,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
throw new \LogicException("Cannot clear XCache Cache from Console, its shared in the Webserver memory and not accessible from the CLI.");
}


$output->writeln('Clearing ALL Metadata cache entries');
$ui->comment('Clearing <info>all</info> Metadata cache entries');

$result = $cacheDriver->deleteAll();
$message = ($result) ? 'Successfully deleted cache entries.' : 'No cache entries were deleted.';
Expand All @@ -106,6 +99,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
$message = ($result) ? 'Successfully flushed cache entries.' : $message;
}

$output->writeln($message);
if ( ! $result) {
$ui->error($message);

return 1;
}

$ui->success($message);

return 0;
}
}
40 changes: 21 additions & 19 deletions lib/Doctrine/ORM/Tools/Console/Command/ClearCache/QueryCommand.php
Expand Up @@ -19,12 +19,13 @@

namespace Doctrine\ORM\Tools\Console\Command\ClearCache;

use Doctrine\Common\Cache\ApcCache;
use Doctrine\Common\Cache\XcacheCache;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\Common\Cache\ApcCache;
use Doctrine\Common\Cache\XcacheCache;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
* Command to clear the query cache of the various cache drivers.
Expand All @@ -43,19 +44,10 @@ class QueryCommand extends Command
*/
protected function configure()
{
$this
->setName('orm:clear-cache:query')
->setDescription('Clear all query cache of the various cache drivers.')
->setDefinition(
[
new InputOption(
'flush', null, InputOption::VALUE_NONE,
'If defined, cache entries will be flushed instead of deleted/invalidated.'
)
]
);

$this->setHelp(<<<EOT
$this->setName('orm:clear-cache:query')
->setDescription('Clear all query cache of the various cache drivers')
->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.
It is possible to invalidate all cache entries at once - called delete -, or flushes the cache provider
instance completely.
Expand All @@ -72,14 +64,16 @@ protected function configure()
Finally, be aware that if <info>--flush</info> option is passed, not all cache providers are able to flush entries,
because of a limitation of its execution nature.
EOT
);
);
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$ui = new SymfonyStyle($input, $output);

$em = $this->getHelper('em')->getEntityManager();
$cacheDriver = $em->getConfiguration()->getQueryCacheImpl();

Expand All @@ -94,7 +88,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
throw new \LogicException("Cannot clear XCache Cache from Console, its shared in the Webserver memory and not accessible from the CLI.");
}

$output->write('Clearing ALL Query cache entries' . PHP_EOL);
$ui->comment('Clearing <info>all</info> Query cache entries');

$result = $cacheDriver->deleteAll();
$message = ($result) ? 'Successfully deleted cache entries.' : 'No cache entries were deleted.';
Expand All @@ -104,6 +98,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
$message = ($result) ? 'Successfully flushed cache entries.' : $message;
}

$output->write($message . PHP_EOL);
if ( ! $result) {
$ui->error($message);

return 1;
}

$ui->success($message);

return 0;
}
}

0 comments on commit b47a39b

Please sign in to comment.