Skip to content

Instantly share code, notes, and snippets.

@nhalstead
Created April 23, 2018 16:17
Show Gist options
  • Save nhalstead/5a58450664c7570b33eb6c8a55ab9548 to your computer and use it in GitHub Desktop.
Save nhalstead/5a58450664c7570b33eb6c8a55ab9548 to your computer and use it in GitHub Desktop.
Simple Queue Processor in PHP
MIT License
Copyright (c) 2018 Noah Halstead
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
<?php
/**
* Process Queue
* With the Process Queue class you can add and Execute functions
* by priority and by the order they were added in.
*
* @auther Noah Halstead <[email protected]>
* @license https://choosealicense.com/licenses/mit/ MIT License
*/
class ProcessQueue {
private $processQueue = array();
private $processed = false;
private $removeProcessed = false;
private $callParams = array();
/**
* Init of the Class
*/
public function __construct(){
$this->orocessQueue = array(
1 => array(),
2 => array(),
3 => array(),
4 => array(),
5 => array(),
6 => array(),
7 => array(),
8 => array(),
9 => array()
);
}
/**
* Add to the Process Queue
* @param Callable Callback function
* @param Int Priority to Store it in
* @return Boolean If Added to the Queue
*/
public function add($callback, $priority = 1){
if(!is_callable($callback)){
// Not Callable, Not a function or Anon Function
return false;
}
$this->processQueue[$priority][] = $callback;
return true;
}
/**
* Clears the Entire Queue
*/
public function clear(){
$this->processQueue = array();
$this->__construct();
}
/**
* Call all Functions in the Queue System
* @param Int Process Only a Select Priority of the Queue.
*/
public function process($OnlyPriority = 0){
if($OnlyPriority == 0){
// Loop Each Priority
foreach($this->processQueue as $priority => $functions){
// Loop Each Function
foreach($functions as $index => $function){
call_user_func_array($function, $this->callParams);
// Remove If Enabled to
if($this->removeProcessed == true){
unset($this->processQueue[$priority][$index]);
}
}
}
}
else {
// Run Specific Priority
// Loop Each Function
foreach($this->processQueue[$OnlyPriority] as $index => $function){
call_user_func_array($function, $this->callParams);
// Remove If Enabled to
if($this->removeProcessed == true){
unset($this->processQueue[$OnlyPriority][$index]);
}
}
}
}
/**
* Same as Process
* @see $this->process
*/
public function run(){
return call_user_func_array(array($this, 'process'), func_get_args());
}
/**
* Sets and Returns the Calling Param Data
* @pram Array The Array that is used with call_user_func_array to call your Funcitons in Queue
* @return Array Stored Params in the Instance
*/
public function calldata($set = null){
if($set !== null){
$this->callParams = $set;
}
return $this->callParams;
}
/**
* Appends to the Params
* @param Mixed Data that will be send on every call of the Functions
*/
public function addParam($param){
$this->callParams[] = $param;
}
/**
* Clears the Params
*/
public function clearParams(){
$this->callParams[] = array();
}
/**
* Set and Get the Value for Removed Processed.
* If set to True, When Functions are processed, they are removed from the Queue.
* @param Boolean Set Flag to remove Functions on Processing
*/
public function removeProcessed($v = false){
$this->removeProcessed = $v;
}
}
// Create Instance
$t = new ProcessQueue();
// Add Function, Priority 9
$t->add(function(){
echo "Number 9";
}, 9);
// Add Function, Priority 2
$t->add(function(){
echo "Number 2";
}, 2);
// Add Function, Priority Normal (1)
$t->add(function(){
echo "Some Number";
});
$t->run(); // Run
$t->removeProcessed(true); // Enable Remove after Run
$t->run(); // Rerun
$t->run(); // Nothing Runs Since the Past Run Removed Them from the Queue
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment