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