CounterCacheCommand.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 5.2.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Command;
  17. use Cake\Console\Arguments;
  18. use Cake\Console\ConsoleIo;
  19. use Cake\Console\ConsoleOptionParser;
  20. /**
  21. * Command for updating counter cache.
  22. */
  23. class CounterCacheCommand extends Command
  24. {
  25. /**
  26. * @inheritDoc
  27. */
  28. public static function defaultName(): string
  29. {
  30. return 'counter_cache';
  31. }
  32. /**
  33. * @inheritDoc
  34. */
  35. public static function getDescription(): string
  36. {
  37. return 'Update counter cache for a model.';
  38. }
  39. /**
  40. * Execute the command.
  41. *
  42. * Updates the counter cache for the specified model and association based
  43. * on the model's counter cache behavior's configuration.
  44. *
  45. * @param \Cake\Console\Arguments $args The command arguments.
  46. * @param \Cake\Console\ConsoleIo $io The console io
  47. * @return int The exit code or null for success
  48. */
  49. public function execute(Arguments $args, ConsoleIo $io): int
  50. {
  51. $table = $this->fetchTable($args->getArgument('model'));
  52. if (!$table->hasBehavior('CounterCache')) {
  53. $io->error('The specified model does not have the CounterCache behavior attached.');
  54. return static::CODE_ERROR;
  55. }
  56. $methodArgs = [];
  57. if ($args->hasOption('assoc')) {
  58. $methodArgs['assocName'] = $args->getOption('assoc');
  59. }
  60. if ($args->hasOption('limit')) {
  61. $methodArgs['limit'] = (int)$args->getOption('limit');
  62. }
  63. if ($args->hasOption('page')) {
  64. $methodArgs['page'] = (int)$args->getOption('page');
  65. }
  66. /** @phpstan-ignore-next-line */
  67. $table->updateCounterCache(...$methodArgs);
  68. $io->success('Counter cache updated successfully.');
  69. return static::CODE_SUCCESS;
  70. }
  71. /**
  72. * @inheritDoc
  73. */
  74. public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
  75. {
  76. $parser->setDescription(static::getDescription())
  77. ->addArgument('model', [
  78. 'help' => 'The model to update the counter cache for.',
  79. 'required' => true,
  80. ])->addOption('assoc', [
  81. 'help' => 'The association to update the counter cache for. By default all associations are updated.',
  82. 'short' => 'a',
  83. 'default' => null,
  84. ])
  85. ->addOption('limit', [
  86. 'help' => 'The number of records to update per page/iteration',
  87. 'short' => 'l',
  88. 'default' => null,
  89. ])
  90. ->addOption('page', [
  91. 'help' => 'The page/iteration number. By default all records will be updated one page at a time.',
  92. 'short' => 'p',
  93. 'default' => null,
  94. ]);
  95. return $parser;
  96. }
  97. }