-
Notifications
You must be signed in to change notification settings - Fork 129
Updated FlatFileDriver.php #308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,8 +10,14 @@ | |
*/ | ||
class FlatFileDriver implements \Bernard\Driver | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $baseDirectory; | ||
|
||
/** | ||
* @var integer | ||
*/ | ||
private $permissions; | ||
|
||
/** | ||
|
@@ -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)); | ||
} | ||
|
||
/** | ||
|
@@ -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); | ||
} | ||
|
||
/** | ||
|
@@ -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; | ||
} | ||
|
||
|
@@ -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; | ||
|
@@ -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); | ||
|
||
|
@@ -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)); | ||
} | ||
|
||
/** | ||
|
@@ -226,4 +210,43 @@ private function getJobFilename($queueName) | |
|
||
return $filename; | ||
} | ||
|
||
/** | ||
* Creates an iterator of all message files in the queue | ||
* @param string $queueName | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add an empty line before return statements. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a new line after control structures. |
||
} | ||
} |
There was a problem hiding this comment.
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.