ResultCommand.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Tools\Console\Command\ClearCache;
  20. use Symfony\Component\Console\Input\InputArgument,
  21. Symfony\Component\Console\Input\InputOption,
  22. Symfony\Component\Console,
  23. Doctrine\Common\Cache;
  24. /**
  25. * Command to clear the result cache of the various cache drivers.
  26. *
  27. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  28. * @link www.doctrine-project.org
  29. * @since 2.0
  30. * @author Benjamin Eberlei <kontakt@beberlei.de>
  31. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  32. * @author Jonathan Wage <jonwage@gmail.com>
  33. * @author Roman Borschel <roman@code-factory.org>
  34. */
  35. class ResultCommand extends Console\Command\Command
  36. {
  37. /**
  38. * @see Console\Command\Command
  39. */
  40. protected function configure()
  41. {
  42. $this
  43. ->setName('orm:clear-cache:result')
  44. ->setDescription('Clear all result cache of the various cache drivers.')
  45. ->setDefinition(array(
  46. new InputOption(
  47. 'flush', null, InputOption::VALUE_NONE,
  48. 'If defined, cache entries will be flushed instead of deleted/invalidated.'
  49. )
  50. ));
  51. $fullName = $this->getName();
  52. $this->setHelp(<<<EOT
  53. The <info>$fullName</info> command is meant to clear the result cache of associated Entity Manager.
  54. It is possible to invalidate all cache entries at once - called delete -, or flushes the cache provider
  55. instance completely.
  56. The execution type differ on how you execute the command.
  57. If you want to invalidate the entries (and not delete from cache instance), this command would do the work:
  58. <info>$fullName</info>
  59. Alternatively, if you want to flush the cache provider using this command:
  60. <info>$fullName --flush</info>
  61. Finally, be aware that if <info>--flush</info> option is passed, not all cache providers are able to flush entries,
  62. because of a limitation of its execution nature.
  63. EOT
  64. );
  65. }
  66. /**
  67. * @see Console\Command\Command
  68. */
  69. protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
  70. {
  71. $em = $this->getHelper('em')->getEntityManager();
  72. $cacheDriver = $em->getConfiguration()->getResultCacheImpl();
  73. if ( ! $cacheDriver) {
  74. throw new \InvalidArgumentException('No Result cache driver is configured on given EntityManager.');
  75. }
  76. if ($cacheDriver instanceof Cache\ApcCache) {
  77. throw new \LogicException("Cannot clear APC Cache from Console, its shared in the Webserver memory and not accessible from the CLI.");
  78. }
  79. $output->write('Clearing ALL Result cache entries' . PHP_EOL);
  80. $result = $cacheDriver->deleteAll();
  81. $message = ($result) ? 'Successfully deleted cache entries.' : 'No cache entries were deleted.';
  82. if (true === $input->getOption('flush')) {
  83. $result = $cacheDriver->flushAll();
  84. $message = ($result) ? 'Successfully flushed cache entries.' : $message;
  85. }
  86. $output->write($message . PHP_EOL);
  87. }
  88. }