Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 55 additions & 32 deletions src/Driver/FlatFileDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@
*/
class FlatFileDriver implements \Bernard\Driver
{
/**
* @var string
*/
private $baseDirectory;

/**
* @var integer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, private properties have no docblock on purpose. This might change, but in the meantime they should go away for consistency.

*/
private $permissions;

/**
Expand Down Expand Up @@ -63,14 +69,7 @@ public function createQueue($queueName)
*/
public function countMessages($queueName)
{
$iterator = new \RecursiveDirectoryIterator(
$this->getQueueDirectory($queueName),
\FilesystemIterator::SKIP_DOTS
);
$iterator = new \RecursiveIteratorIterator($iterator);
$iterator = new \RegexIterator($iterator, '#\.job$#');

return iterator_count($iterator);
return iterator_count($this->getJobIterator($queueName));
}

/**
Expand All @@ -83,7 +82,7 @@ public function pushMessage($queueName, $message)
$filename = $this->getJobFilename($queueName);

file_put_contents($queueDir.DIRECTORY_SEPARATOR.$filename, $message);
chmod($queueDir . DIRECTORY_SEPARATOR . $filename, $this->permissions);
chmod($queueDir.DIRECTORY_SEPARATOR.$filename, $this->permissions);
}

/**
Expand All @@ -93,18 +92,16 @@ public function popMessage($queueName, $duration = 5)
{
$runtime = microtime(true) + $duration;
$queueDir = $this->getQueueDirectory($queueName);

$it = new \GlobIterator($queueDir.DIRECTORY_SEPARATOR.'*.job', \FilesystemIterator::KEY_AS_FILENAME);
$files = array_keys(iterator_to_array($it));
$files = $this->getJobFiles($queueName);

natsort($files);

while (microtime(true) < $runtime) {
if ($files) {
$id = array_pop($files);
$id = array_shift($files);
$data = array(file_get_contents($queueDir.DIRECTORY_SEPARATOR.$id), $id);
rename($queueDir.DIRECTORY_SEPARATOR.$id, $queueDir.DIRECTORY_SEPARATOR.$id.'.proceed');

// Set file hidden (emulating message invisibility)
rename($queueDir.DIRECTORY_SEPARATOR.$id, $queueDir.DIRECTORY_SEPARATOR.'.'.$id);
return $data;
}

Expand All @@ -120,7 +117,8 @@ public function popMessage($queueName, $duration = 5)
public function acknowledgeMessage($queueName, $receipt)
{
$queueDir = $this->getQueueDirectory($queueName);
$path = $queueDir.DIRECTORY_SEPARATOR.$receipt.'.proceed';
// Set path to hidden filename
$path = $queueDir.DIRECTORY_SEPARATOR.'.'.$receipt;

if (!is_file($path)) {
return;
Expand All @@ -136,11 +134,9 @@ public function peekQueue($queueName, $index = 0, $limit = 20)
{
$queueDir = $this->getQueueDirectory($queueName);

$it = new \GlobIterator($queueDir.DIRECTORY_SEPARATOR.'*.job', \FilesystemIterator::KEY_AS_FILENAME);
$files = array_keys(iterator_to_array($it));
$files = $this->getJobFiles($queueName);

natsort($files);
$files = array_reverse($files);

$files = array_slice($files, $index, $limit);

Expand All @@ -158,19 +154,7 @@ public function peekQueue($queueName, $index = 0, $limit = 20)
*/
public function removeQueue($queueName)
{
$iterator = new \RecursiveDirectoryIterator(
$this->getQueueDirectory($queueName),
\FilesystemIterator::SKIP_DOTS
);
$iterator = new \RecursiveIteratorIterator($iterator);
$iterator = new \RegexIterator($iterator, '#\.job(.proceed)?$#');

foreach ($iterator as $file) {
/* @var $file \DirectoryIterator */
unlink($file->getRealPath());
}

rmdir($this->getQueueDirectory($queueName));
$this->removeDirectoryRecursive($this->getQueueDirectory($queueName));
}

/**
Expand Down Expand Up @@ -226,4 +210,43 @@ private function getJobFilename($queueName)

return $filename;
}

/**
* Creates an iterator of all message files in the queue
* @param string $queueName
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use newlines between description, param and return blocks.

* @return \GlobIterator
*/
private function getJobIterator($queueName) {
$queueDir = $this->getQueueDirectory($queueName);
$iterator = new \GlobIterator($queueDir.DIRECTORY_SEPARATOR.'*.job', \FilesystemIterator::KEY_AS_FILENAME);
return $iterator;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an empty line before return statements.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact the last assignment is not necessary, just return new ....

}

/**
* Retrieves an array of all message files in the queue
* @param string $queueName
* @return array
*/
private function getJobFiles($queueName) {
$iterator = $this->getJobIterator($queueName);
$files = array_keys(iterator_to_array($iterator));
return $files;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here as above.

}

/**
* Removes a directory recursively
* @param string $directory
*/
private function removeDirectoryRecursive($directory)
{
foreach (glob("{$directory}/{,.}[!.,!..]*", GLOB_MARK|GLOB_BRACE) as $file)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please follow PSR-2

{
if (is_dir($file)) {
$this->removeDirectoryRecursive($file);
} else {
unlink($file);
}
}
rmdir($directory);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a new line after control structures.

}
}