|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * This file is part of the phpflo/phpflo package. |
| 4 | + * |
| 5 | + * (c) Henri Bergius <henri.bergius@iki.fi> |
| 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 | +namespace PhpFlo\Component; |
| 12 | + |
| 13 | +use PhpFlo\Component; |
| 14 | + |
| 15 | +/** |
| 16 | + * Component for creating a simple queue of messages. |
| 17 | + * |
| 18 | + * All incomming messages are kept in memory. As soon as the queue reaches the |
| 19 | + * pre-configured size of 100 messages, the current list of messages is send. |
| 20 | + * |
| 21 | + * You can reconfigure the queue size of the component by sending a message to |
| 22 | + * the `size` input port of this component. If the incomming data is not a |
| 23 | + * positive integer, an error message will be send to the `err` out port of this |
| 24 | + * component. |
| 25 | + * |
| 26 | + * @author Marijn Huizendveld <marijn@pink-tie.com> |
| 27 | + */ |
| 28 | +class Queue extends Component |
| 29 | +{ |
| 30 | + /** |
| 31 | + * @var integer |
| 32 | + */ |
| 33 | + private $size; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var array<mixed> |
| 37 | + */ |
| 38 | + private $messages; |
| 39 | + |
| 40 | + public function __construct() |
| 41 | + { |
| 42 | + $this->size = 100; |
| 43 | + $this->messages = []; |
| 44 | + |
| 45 | + $this->inPorts()->add('in', ['datatype' => 'all']); |
| 46 | + $this->inPorts()->add('size', ['datatype' => 'all']); |
| 47 | + |
| 48 | + $this->outPorts()->add('error', ['datatype' => 'all']); |
| 49 | + $this->outPorts()->add('messages', ['datatype' => 'all']); |
| 50 | + |
| 51 | + $this->inPorts()->in->on('data', [$this, 'onAppendQueue']); |
| 52 | + $this->inPorts()->in->on('detach', [$this, 'onStreamEnded']); |
| 53 | + |
| 54 | + $this->inPorts()->size->on('data', [$this, 'onResize']); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @param mixed $data |
| 59 | + */ |
| 60 | + public function onAppendQueue($data) |
| 61 | + { |
| 62 | + $this->messages[] = $data; |
| 63 | + |
| 64 | + $this->sendQueue(); |
| 65 | + } |
| 66 | + |
| 67 | + public function onStreamEnded() |
| 68 | + { |
| 69 | + $this->flushQueue(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @param mixed $data |
| 74 | + */ |
| 75 | + public function onResize($data) |
| 76 | + { |
| 77 | + if (!is_int($data) || 0 > $data) { |
| 78 | + $dumped = var_dump($data); |
| 79 | + |
| 80 | + $this->outPorts()->error->send( |
| 81 | + "Invalid queue size: '{$dumped}'. Queue resize operation expects a positive integer value." |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + $this->size = $data; |
| 86 | + |
| 87 | + $this->sendQueue(); |
| 88 | + } |
| 89 | + |
| 90 | + private function sendQueue() |
| 91 | + { |
| 92 | + if ($this->size <= count($this->messages)) { |
| 93 | + $this->flushQueue(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private function flushQueue() |
| 98 | + { |
| 99 | + $this->outPorts()->messages->send($this->messages); |
| 100 | + $this->outPorts()->messages->disconnect(); |
| 101 | + |
| 102 | + $this->messages = []; |
| 103 | + } |
| 104 | +} |
0 commit comments