FileQueue.php in Purge 8.3
File
src/Plugin/Purge/Queue/FileQueue.php
View source
<?php
namespace Drupal\purge\Plugin\Purge\Queue;
use Drupal\Core\DestructableInterface;
use Drupal\Core\StreamWrapper\PublicStream;
class FileQueue extends MemoryQueue implements QueueInterface, DestructableInterface {
protected $file = 'purge-file.queue';
const SEPARATOR = '|';
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->file = DRUPAL_ROOT . '/' . PublicStream::basePath() . '/' . $this->file;
$this
->bufferInitialize();
}
private function bufferInitialize() {
if (!$this->bufferInitialized) {
$this->bufferInitialized = TRUE;
$this->buffer = [];
if (file_exists($this->file)) {
foreach (file($this->file) as $line) {
$line = explode(self::SEPARATOR, str_replace("\n", '', $line));
$item_id = (int) array_shift($line);
$line[self::EXPIRE] = (int) $line[self::EXPIRE];
$line[self::CREATED] = (int) $line[self::CREATED];
$this->buffer[$item_id] = $line;
}
}
}
}
public function bufferCommit() {
$ob = '';
if (!file_exists($path = dirname($this->file))) {
if (!mkdir($path, 0777, TRUE)) {
throw new \Exception("Failed recursive mkdir() to create missing '{$path}'!");
}
if (!file_exists($path)) {
throw new \Exception("Path '{$path}' still does not exist after trying mkdir()!");
}
}
if (!($fh = fopen($this->file, 'w'))) {
throw new \Exception('Unable to open file resource to ' . $this->file);
}
foreach ($this->buffer as $item_id => $line) {
$ob .= $item_id . self::SEPARATOR . $line[self::DATA] . self::SEPARATOR . $line[self::EXPIRE] . self::SEPARATOR . $line[self::CREATED] . "\n";
}
fwrite($fh, $ob);
fclose($fh);
}
public function deleteQueue() {
if (file_exists($this->file)) {
unlink($this->file);
}
parent::deleteQueue();
}
public function destruct() {
if ($this->bufferInitialized) {
$this
->bufferCommit();
}
}
public function __destruct() {
if ($this->bufferInitialized) {
$this
->bufferCommit();
}
}
}