vendor/botman/botman/src/Middleware/MiddlewareManager.php line 126

Open in your IDE?
  1. <?php
  2. namespace BotMan\BotMan\Middleware;
  3. use BotMan\BotMan\BotMan;
  4. use BotMan\BotMan\Interfaces\Middleware\Captured;
  5. use BotMan\BotMan\Interfaces\Middleware\Heard;
  6. use BotMan\BotMan\Interfaces\Middleware\Matching;
  7. use BotMan\BotMan\Interfaces\Middleware\Received;
  8. use BotMan\BotMan\Interfaces\Middleware\Sending;
  9. use BotMan\BotMan\Interfaces\MiddlewareInterface;
  10. use Closure;
  11. use Mpociot\Pipeline\Pipeline;
  12. class MiddlewareManager
  13. {
  14. /** @var Received[] */
  15. protected $received = [];
  16. /** @var Captured[] */
  17. protected $captured = [];
  18. /** @var Matching[] */
  19. protected $matching = [];
  20. /** @var Heard[] */
  21. protected $heard = [];
  22. /** @var Sending[] */
  23. protected $sending = [];
  24. /** @var BotMan */
  25. protected $botman;
  26. public function __construct(BotMan $botman)
  27. {
  28. $this->botman = $botman;
  29. }
  30. /**
  31. * @param Received[] ...$middleware
  32. * @return Received[]|$this
  33. */
  34. public function received(Received ...$middleware)
  35. {
  36. if (empty($middleware)) {
  37. return $this->received;
  38. }
  39. $this->received = array_merge($this->received, $middleware);
  40. return $this;
  41. }
  42. /**
  43. * @param Captured[] ...$middleware
  44. * @return Captured[]|$this
  45. */
  46. public function captured(Captured ...$middleware)
  47. {
  48. if (empty($middleware)) {
  49. return $this->captured;
  50. }
  51. $this->captured = array_merge($this->captured, $middleware);
  52. return $this;
  53. }
  54. /**
  55. * @param Matching[] ...$middleware
  56. * @return Matching[]|$this
  57. */
  58. public function matching(Matching ...$middleware)
  59. {
  60. if (empty($middleware)) {
  61. return $this->matching;
  62. }
  63. $this->matching = array_merge($this->matching, $middleware);
  64. return $this;
  65. }
  66. /**
  67. * @param Heard[] $middleware
  68. * @return Heard[]|$this
  69. */
  70. public function heard(Heard ...$middleware)
  71. {
  72. if (empty($middleware)) {
  73. return $this->heard;
  74. }
  75. $this->heard = array_merge($this->heard, $middleware);
  76. return $this;
  77. }
  78. /**
  79. * @param Sending[] $middleware
  80. * @return Sending[]|$this
  81. */
  82. public function sending(Sending ...$middleware)
  83. {
  84. if (empty($middleware)) {
  85. return $this->sending;
  86. }
  87. $this->sending = array_merge($this->sending, $middleware);
  88. return $this;
  89. }
  90. /**
  91. * @param string $method
  92. * @param mixed $payload
  93. * @param MiddlewareInterface[] $additionalMiddleware
  94. * @param Closure|null $destination
  95. * @return mixed
  96. */
  97. public function applyMiddleware($method, $payload, array $additionalMiddleware = [], Closure $destination = null)
  98. {
  99. $destination = is_null($destination) ? function ($payload) {
  100. return $payload;
  101. }
  102. : $destination;
  103. $middleware = $this->$method + $additionalMiddleware;
  104. return (new Pipeline())
  105. ->via($method)
  106. ->send($payload)
  107. ->with($this->botman)
  108. ->through($middleware)
  109. ->then($destination);
  110. }
  111. }