Reader.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * Copyright © 2013-2017 Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Update\Queue;
  7. /**
  8. * Queue content file reader.
  9. */
  10. class Reader
  11. {
  12. /**
  13. * @var string
  14. */
  15. protected $queueFilePath;
  16. /**
  17. * Initialize reader.
  18. *
  19. * @param string|null $queueFilePath
  20. */
  21. public function __construct($queueFilePath = null)
  22. {
  23. $this->queueFilePath = $queueFilePath ? $queueFilePath : MAGENTO_BP . '/var/.update_queue.json';
  24. }
  25. /**
  26. * Read Magento updater application jobs queue as a JSON string.
  27. *
  28. * @return string Queue file content (valid JSON string)
  29. * @throws \RuntimeException
  30. */
  31. public function read()
  32. {
  33. $queue = '';
  34. if (!file_exists($this->queueFilePath)) {
  35. return $queue;
  36. }
  37. $queueFileContent = file_get_contents($this->queueFilePath);
  38. if ($queueFileContent) {
  39. json_decode($queueFileContent);
  40. if (json_last_error() !== JSON_ERROR_NONE) {
  41. throw new \RuntimeException(sprintf('Content of "%s" must be a valid JSON.', $this->queueFilePath));
  42. }
  43. $queue = $queueFileContent;
  44. }
  45. return $queue;
  46. }
  47. }