PageRenderTime 22ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

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