Patched to Drupal 8.4.8 level. See https://www.drupal.org/sa-core-2018-004 and patch...
[yaffs-website] / web / core / lib / Drupal / Core / Queue / Memory.php
1 <?php
2
3 namespace Drupal\Core\Queue;
4
5 /**
6  * Static queue implementation.
7  *
8  * This allows "undelayed" variants of processes relying on the Queue
9  * interface. The queue data resides in memory. It should only be used for
10  * items that will be queued and dequeued within a given page request.
11  *
12  * @ingroup queue
13  */
14 class Memory implements QueueInterface {
15   /**
16    * The queue data.
17    *
18    * @var array
19    */
20   protected $queue;
21
22   /**
23    * Counter for item ids.
24    *
25    * @var int
26    */
27   protected $idSequence;
28
29   /**
30    * Constructs a Memory object.
31    *
32    * @param string $name
33    *   An arbitrary string. The name of the queue to work with.
34    */
35   public function __construct($name) {
36     $this->queue = [];
37     $this->idSequence = 0;
38   }
39
40   /**
41    * {@inheritdoc}
42    */
43   public function createItem($data) {
44     $item = new \stdClass();
45     $item->item_id = $this->idSequence++;
46     $item->data = $data;
47     $item->created = time();
48     $item->expire = 0;
49     $this->queue[$item->item_id] = $item;
50     return $item->item_id;
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function numberOfItems() {
57     return count($this->queue);
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public function claimItem($lease_time = 30) {
64     foreach ($this->queue as $key => $item) {
65       if ($item->expire == 0) {
66         $item->expire = time() + $lease_time;
67         $this->queue[$key] = $item;
68         return $item;
69       }
70     }
71     return FALSE;
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function deleteItem($item) {
78     unset($this->queue[$item->item_id]);
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function releaseItem($item) {
85     if (isset($this->queue[$item->item_id]) && $this->queue[$item->item_id]->expire != 0) {
86       $this->queue[$item->item_id]->expire = 0;
87       return TRUE;
88     }
89     return FALSE;
90   }
91
92   /**
93    * {@inheritdoc}
94    */
95   public function createQueue() {
96     // Nothing needed here.
97   }
98
99   /**
100    * {@inheritdoc}
101    */
102   public function deleteQueue() {
103     $this->queue = [];
104     $this->idSequence = 0;
105   }
106
107 }