PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Queue/Queue.php

https://gitlab.com/Urtekin/ertexAdmin
PHP | 213 lines | 94 code | 24 blank | 95 comment | 6 complexity | 3f4fb9b7392a0858aa25bfb012ce72cc MD5 | raw file
  1. <?php
  2. namespace Illuminate\Queue;
  3. use Closure;
  4. use DateTime;
  5. use Illuminate\Support\Arr;
  6. use SuperClosure\Serializer;
  7. use Illuminate\Container\Container;
  8. use Illuminate\Contracts\Queue\QueueableEntity;
  9. use Illuminate\Contracts\Encryption\Encrypter as EncrypterContract;
  10. abstract class Queue
  11. {
  12. /**
  13. * The IoC container instance.
  14. *
  15. * @var \Illuminate\Container\Container
  16. */
  17. protected $container;
  18. /**
  19. * Push a new job onto the queue.
  20. *
  21. * @param string $queue
  22. * @param string $job
  23. * @param mixed $data
  24. * @return mixed
  25. */
  26. public function pushOn($queue, $job, $data = '')
  27. {
  28. return $this->push($job, $data, $queue);
  29. }
  30. /**
  31. * Push a new job onto the queue after a delay.
  32. *
  33. * @param string $queue
  34. * @param \DateTime|int $delay
  35. * @param string $job
  36. * @param mixed $data
  37. * @return mixed
  38. */
  39. public function laterOn($queue, $delay, $job, $data = '')
  40. {
  41. return $this->later($delay, $job, $data, $queue);
  42. }
  43. /**
  44. * Push an array of jobs onto the queue.
  45. *
  46. * @param array $jobs
  47. * @param mixed $data
  48. * @param string $queue
  49. * @return mixed
  50. */
  51. public function bulk($jobs, $data = '', $queue = null)
  52. {
  53. foreach ((array) $jobs as $job) {
  54. $this->push($job, $data, $queue);
  55. }
  56. }
  57. /**
  58. * Create a payload string from the given job and data.
  59. *
  60. * @param string $job
  61. * @param mixed $data
  62. * @param string $queue
  63. * @return string
  64. */
  65. protected function createPayload($job, $data = '', $queue = null)
  66. {
  67. if ($job instanceof Closure) {
  68. return json_encode($this->createClosurePayload($job, $data));
  69. } elseif (is_object($job)) {
  70. return json_encode([
  71. 'job' => 'Illuminate\Queue\CallQueuedHandler@call',
  72. 'data' => ['command' => serialize(clone $job)],
  73. ]);
  74. }
  75. return json_encode($this->createPlainPayload($job, $data));
  76. }
  77. /**
  78. * Create a typical, "plain" queue payload array.
  79. *
  80. * @param string $job
  81. * @param mixed $data
  82. * @return array
  83. */
  84. protected function createPlainPayload($job, $data)
  85. {
  86. return ['job' => $job, 'data' => $this->prepareQueueableEntities($data)];
  87. }
  88. /**
  89. * Prepare any queueable entities for storage in the queue.
  90. *
  91. * @param mixed $data
  92. * @return mixed
  93. */
  94. protected function prepareQueueableEntities($data)
  95. {
  96. if ($data instanceof QueueableEntity) {
  97. return $this->prepareQueueableEntity($data);
  98. }
  99. if (is_array($data)) {
  100. $data = array_map(function ($d) {
  101. if (is_array($d)) {
  102. return $this->prepareQueueableEntities($d);
  103. }
  104. return $this->prepareQueueableEntity($d);
  105. }, $data);
  106. }
  107. return $data;
  108. }
  109. /**
  110. * Prepare a single queueable entity for storage on the queue.
  111. *
  112. * @param mixed $value
  113. * @return mixed
  114. */
  115. protected function prepareQueueableEntity($value)
  116. {
  117. if ($value instanceof QueueableEntity) {
  118. return '::entity::|'.get_class($value).'|'.$value->getQueueableId();
  119. }
  120. return $value;
  121. }
  122. /**
  123. * Create a payload string for the given Closure job.
  124. *
  125. * @param \Closure $job
  126. * @param mixed $data
  127. * @return array
  128. */
  129. protected function createClosurePayload($job, $data)
  130. {
  131. $closure = $this->crypt->encrypt((new Serializer)->serialize($job));
  132. return ['job' => 'IlluminateQueueClosure', 'data' => compact('closure')];
  133. }
  134. /**
  135. * Set additional meta on a payload string.
  136. *
  137. * @param string $payload
  138. * @param string $key
  139. * @param string $value
  140. * @return string
  141. */
  142. protected function setMeta($payload, $key, $value)
  143. {
  144. $payload = json_decode($payload, true);
  145. return json_encode(Arr::set($payload, $key, $value));
  146. }
  147. /**
  148. * Calculate the number of seconds with the given delay.
  149. *
  150. * @param \DateTime|int $delay
  151. * @return int
  152. */
  153. protected function getSeconds($delay)
  154. {
  155. if ($delay instanceof DateTime) {
  156. return max(0, $delay->getTimestamp() - $this->getTime());
  157. }
  158. return (int) $delay;
  159. }
  160. /**
  161. * Get the current UNIX timestamp.
  162. *
  163. * @return int
  164. */
  165. protected function getTime()
  166. {
  167. return time();
  168. }
  169. /**
  170. * Set the IoC container instance.
  171. *
  172. * @param \Illuminate\Container\Container $container
  173. * @return void
  174. */
  175. public function setContainer(Container $container)
  176. {
  177. $this->container = $container;
  178. }
  179. /**
  180. * Set the encrypter instance.
  181. *
  182. * @param \Illuminate\Contracts\Encryption\Encrypter $crypt
  183. * @return void
  184. */
  185. public function setEncrypter(EncrypterContract $crypt)
  186. {
  187. $this->crypt = $crypt;
  188. }
  189. }