PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

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