| 1 |
<?php |
| 2 |
/* |
| 3 |
* This file is part of the ManageWP Worker plugin. |
| 4 |
* |
| 5 |
* (c) ManageWP LLC <contact@managewp.com> |
| 6 |
* |
| 7 |
* For the full copyright and license information, please view the LICENSE |
| 8 |
* file that was distributed with this source code. |
| 9 |
*/ |
| 10 |
|
| 11 |
class MWP_Worker_RequestStack |
| 12 |
{ |
| 13 |
/** |
| 14 |
* @var MWP_Worker_Request[] |
| 15 |
*/ |
| 16 |
private $requests = array(); |
| 17 |
|
| 18 |
/** |
| 19 |
* Pushes a Request on the stack. |
| 20 |
* |
| 21 |
* This method should generally not be called directly as the stack |
| 22 |
* management should be taken care of by the application itself. |
| 23 |
*/ |
| 24 |
public function push(MWP_Worker_Request $request) |
| 25 |
{ |
| 26 |
$this->requests[] = $request; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Pops the current request from the stack. |
| 31 |
* |
| 32 |
* This operation lets the current request go out of scope. |
| 33 |
* |
| 34 |
* This method should generally not be called directly as the stack |
| 35 |
* management should be taken care of by the application itself. |
| 36 |
* |
| 37 |
* @return MWP_Worker_Request|null |
| 38 |
*/ |
| 39 |
public function pop() |
| 40 |
{ |
| 41 |
if (!$this->requests) { |
| 42 |
return null; |
| 43 |
} |
| 44 |
|
| 45 |
return array_pop($this->requests); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* @return MWP_Worker_Request|null |
| 50 |
*/ |
| 51 |
public function getCurrentRequest() |
| 52 |
{ |
| 53 |
$lastRequest = end($this->requests); |
| 54 |
|
| 55 |
return $lastRequest ? $lastRequest : null; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Gets the master Request. |
| 60 |
* |
| 61 |
* @return MWP_Worker_Request|null |
| 62 |
*/ |
| 63 |
public function getMasterRequest() |
| 64 |
{ |
| 65 |
if (!$this->requests) { |
| 66 |
return null; |
| 67 |
} |
| 68 |
|
| 69 |
return $this->requests[0]; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Returns the parent request of the current. |
| 74 |
* |
| 75 |
* If current Request is the master request, it returns null. |
| 76 |
* |
| 77 |
* @return MWP_Worker_Request|null |
| 78 |
*/ |
| 79 |
public function getParentRequest() |
| 80 |
{ |
| 81 |
$pos = count($this->requests) - 2; |
| 82 |
|
| 83 |
if (!isset($this->requests[$pos])) { |
| 84 |
return null; |
| 85 |
} |
| 86 |
|
| 87 |
return $this->requests[$pos]; |
| 88 |
} |
| 89 |
} |
| 90 |
|