Queue.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * Copyright © 2013-2017 Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Update;
  7. use Magento\Update\Queue\Reader;
  8. use Magento\Update\Queue\AbstractJob;
  9. use Magento\Update\Queue\JobFactory;
  10. use Magento\Update\Queue\Writer;
  11. /**
  12. * Class for access to the queue of Magento updater application jobs.
  13. */
  14. class Queue
  15. {
  16. /**#@+
  17. * Key used in queue file.
  18. */
  19. const KEY_JOBS = 'jobs';
  20. const KEY_JOB_NAME = 'name';
  21. const KEY_JOB_PARAMS = 'params';
  22. /**#@-*/
  23. /**
  24. * @var Reader
  25. */
  26. protected $reader;
  27. /**
  28. * @var Writer
  29. */
  30. protected $writer;
  31. /**
  32. * @var JobFactory
  33. */
  34. protected $jobFactory;
  35. /**
  36. * Initialize dependencies.
  37. *
  38. * @param Reader|null $reader
  39. * @param Writer|null $writer
  40. * @param JobFactory|null $jobFactory
  41. */
  42. public function __construct(Reader $reader = null, Writer $writer = null, JobFactory $jobFactory = null)
  43. {
  44. $this->reader = $reader ? $reader : new Reader();
  45. $this->writer = $writer ? $writer : new Writer();
  46. $this->jobFactory = $jobFactory ? $jobFactory : new JobFactory();
  47. }
  48. /**
  49. * Peek at job queue
  50. *
  51. * @throws \RuntimeException
  52. * @return array
  53. */
  54. public function peek()
  55. {
  56. $queue = json_decode($this->reader->read(), true);
  57. if (!is_array($queue)) {
  58. return [];
  59. }
  60. if (isset($queue[self::KEY_JOBS]) && is_array($queue[self::KEY_JOBS])) {
  61. $this->validateJobDeclaration($queue[self::KEY_JOBS][0]);
  62. return $queue[self::KEY_JOBS][0];
  63. } else {
  64. throw new \RuntimeException(sprintf('"%s" field is missing or is not an array.', self::KEY_JOBS));
  65. }
  66. }
  67. /**
  68. * Pop job queue.
  69. *
  70. * @return AbstractJob
  71. * @throws \RuntimeException
  72. */
  73. public function popQueuedJob()
  74. {
  75. $job = null;
  76. $queue = json_decode($this->reader->read(), true);
  77. if (!is_array($queue)) {
  78. return $job;
  79. }
  80. if (isset($queue[self::KEY_JOBS]) && is_array($queue[self::KEY_JOBS])) {
  81. $this->validateJobDeclaration($queue[self::KEY_JOBS][0]);
  82. $job = $this->jobFactory->create(
  83. $queue[self::KEY_JOBS][0][self::KEY_JOB_NAME],
  84. $queue[self::KEY_JOBS][0][self::KEY_JOB_PARAMS]
  85. );
  86. array_shift($queue[self::KEY_JOBS]);
  87. if (empty($queue[self::KEY_JOBS])) {
  88. $this->writer->write('');
  89. } else {
  90. $this->writer->write(json_encode($queue, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ));
  91. }
  92. } else {
  93. throw new \RuntimeException(sprintf('"%s" field is missing or is not an array.', self::KEY_JOBS));
  94. }
  95. return $job;
  96. }
  97. /**
  98. * Check if queue is empty
  99. *
  100. * @return bool
  101. */
  102. public function isEmpty()
  103. {
  104. $queue = json_decode($this->reader->read(), true);
  105. return empty($queue);
  106. }
  107. /**
  108. * @param array $jobs
  109. * @return void
  110. */
  111. public function addJobs(array $jobs)
  112. {
  113. foreach ($jobs as $job) {
  114. $this->validateJobDeclaration($job);
  115. $queue = json_decode($this->reader->read(), true);
  116. $queue[self::KEY_JOBS][] = $job;
  117. $this->writer->write(json_encode($queue, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ));
  118. }
  119. }
  120. /**
  121. * @return void
  122. */
  123. public function clear()
  124. {
  125. $this->writer->write('');
  126. }
  127. /**
  128. * Make sure job declaration is correct.
  129. *
  130. * @param object $job
  131. * @throws \RuntimeException
  132. */
  133. protected function validateJobDeclaration($job)
  134. {
  135. $requiredFields = [self::KEY_JOB_NAME, self::KEY_JOB_PARAMS];
  136. foreach ($requiredFields as $field) {
  137. if (!isset($job[$field])) {
  138. throw new \RuntimeException(sprintf('"%s" field is missing for one or more jobs.', $field));
  139. }
  140. }
  141. }
  142. }