Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Console\Scheduling;
4
 
5
use Illuminate\Support\Carbon;
6
 
7
trait ManagesFrequencies
8
{
9
    /**
10
     * The Cron expression representing the event's frequency.
11
     *
12
     * @param  string  $expression
13
     * @return $this
14
     */
15
    public function cron($expression)
16
    {
17
        $this->expression = $expression;
18
 
19
        return $this;
20
    }
21
 
22
    /**
23
     * Schedule the event to run between start and end time.
24
     *
25
     * @param  string  $startTime
26
     * @param  string  $endTime
27
     * @return $this
28
     */
29
    public function between($startTime, $endTime)
30
    {
31
        return $this->when($this->inTimeInterval($startTime, $endTime));
32
    }
33
 
34
    /**
35
     * Schedule the event to not run between start and end time.
36
     *
37
     * @param  string  $startTime
38
     * @param  string  $endTime
39
     * @return $this
40
     */
41
    public function unlessBetween($startTime, $endTime)
42
    {
43
        return $this->skip($this->inTimeInterval($startTime, $endTime));
44
    }
45
 
46
    /**
47
     * Schedule the event to run between start and end time.
48
     *
49
     * @param  string  $startTime
50
     * @param  string  $endTime
51
     * @return \Closure
52
     */
53
    private function inTimeInterval($startTime, $endTime)
54
    {
55
        [$now, $startTime, $endTime] = [
56
            Carbon::now($this->timezone),
57
            Carbon::parse($startTime, $this->timezone),
58
            Carbon::parse($endTime, $this->timezone),
59
        ];
60
 
61
        if ($endTime->lessThan($startTime)) {
62
            if ($startTime->greaterThan($now)) {
63
                $startTime = $startTime->subDay(1);
64
            } else {
65
                $endTime = $endTime->addDay(1);
66
            }
67
        }
68
 
69
        return function () use ($now, $startTime, $endTime) {
70
            return $now->between($startTime, $endTime);
71
        };
72
    }
73
 
74
    /**
75
     * Schedule the event to run every minute.
76
     *
77
     * @return $this
78
     */
79
    public function everyMinute()
80
    {
81
        return $this->spliceIntoPosition(1, '*');
82
    }
83
 
84
    /**
85
     * Schedule the event to run every two minutes.
86
     *
87
     * @return $this
88
     */
89
    public function everyTwoMinutes()
90
    {
91
        return $this->spliceIntoPosition(1, '*/2');
92
    }
93
 
94
    /**
95
     * Schedule the event to run every three minutes.
96
     *
97
     * @return $this
98
     */
99
    public function everyThreeMinutes()
100
    {
101
        return $this->spliceIntoPosition(1, '*/3');
102
    }
103
 
104
    /**
105
     * Schedule the event to run every four minutes.
106
     *
107
     * @return $this
108
     */
109
    public function everyFourMinutes()
110
    {
111
        return $this->spliceIntoPosition(1, '*/4');
112
    }
113
 
114
    /**
115
     * Schedule the event to run every five minutes.
116
     *
117
     * @return $this
118
     */
119
    public function everyFiveMinutes()
120
    {
121
        return $this->spliceIntoPosition(1, '*/5');
122
    }
123
 
124
    /**
125
     * Schedule the event to run every ten minutes.
126
     *
127
     * @return $this
128
     */
129
    public function everyTenMinutes()
130
    {
131
        return $this->spliceIntoPosition(1, '*/10');
132
    }
133
 
134
    /**
135
     * Schedule the event to run every fifteen minutes.
136
     *
137
     * @return $this
138
     */
139
    public function everyFifteenMinutes()
140
    {
141
        return $this->spliceIntoPosition(1, '*/15');
142
    }
143
 
144
    /**
145
     * Schedule the event to run every thirty minutes.
146
     *
147
     * @return $this
148
     */
149
    public function everyThirtyMinutes()
150
    {
151
        return $this->spliceIntoPosition(1, '0,30');
152
    }
153
 
154
    /**
155
     * Schedule the event to run hourly.
156
     *
157
     * @return $this
158
     */
159
    public function hourly()
160
    {
161
        return $this->spliceIntoPosition(1, 0);
162
    }
163
 
164
    /**
165
     * Schedule the event to run hourly at a given offset in the hour.
166
     *
167
     * @param  array|int  $offset
168
     * @return $this
169
     */
170
    public function hourlyAt($offset)
171
    {
172
        $offset = is_array($offset) ? implode(',', $offset) : $offset;
173
 
174
        return $this->spliceIntoPosition(1, $offset);
175
    }
176
 
177
    /**
178
     * Schedule the event to run every odd hour.
179
     *
180
     * @return $this
181
     */
182
    public function everyOddHour()
183
    {
184
        return $this->spliceIntoPosition(1, 0)->spliceIntoPosition(2, '1-23/2');
185
    }
186
 
187
    /**
188
     * Schedule the event to run every two hours.
189
     *
190
     * @return $this
191
     */
192
    public function everyTwoHours()
193
    {
194
        return $this->spliceIntoPosition(1, 0)
195
                    ->spliceIntoPosition(2, '*/2');
196
    }
197
 
198
    /**
199
     * Schedule the event to run every three hours.
200
     *
201
     * @return $this
202
     */
203
    public function everyThreeHours()
204
    {
205
        return $this->spliceIntoPosition(1, 0)
206
                    ->spliceIntoPosition(2, '*/3');
207
    }
208
 
209
    /**
210
     * Schedule the event to run every four hours.
211
     *
212
     * @return $this
213
     */
214
    public function everyFourHours()
215
    {
216
        return $this->spliceIntoPosition(1, 0)
217
                    ->spliceIntoPosition(2, '*/4');
218
    }
219
 
220
    /**
221
     * Schedule the event to run every six hours.
222
     *
223
     * @return $this
224
     */
225
    public function everySixHours()
226
    {
227
        return $this->spliceIntoPosition(1, 0)
228
                    ->spliceIntoPosition(2, '*/6');
229
    }
230
 
231
    /**
232
     * Schedule the event to run daily.
233
     *
234
     * @return $this
235
     */
236
    public function daily()
237
    {
238
        return $this->spliceIntoPosition(1, 0)
239
                    ->spliceIntoPosition(2, 0);
240
    }
241
 
242
    /**
243
     * Schedule the command at a given time.
244
     *
245
     * @param  string  $time
246
     * @return $this
247
     */
248
    public function at($time)
249
    {
250
        return $this->dailyAt($time);
251
    }
252
 
253
    /**
254
     * Schedule the event to run daily at a given time (10:00, 19:30, etc).
255
     *
256
     * @param  string  $time
257
     * @return $this
258
     */
259
    public function dailyAt($time)
260
    {
261
        $segments = explode(':', $time);
262
 
263
        return $this->spliceIntoPosition(2, (int) $segments[0])
264
                    ->spliceIntoPosition(1, count($segments) === 2 ? (int) $segments[1] : '0');
265
    }
266
 
267
    /**
268
     * Schedule the event to run twice daily.
269
     *
270
     * @param  int  $first
271
     * @param  int  $second
272
     * @return $this
273
     */
274
    public function twiceDaily($first = 1, $second = 13)
275
    {
276
        return $this->twiceDailyAt($first, $second, 0);
277
    }
278
 
279
    /**
280
     * Schedule the event to run twice daily at a given offset.
281
     *
282
     * @param  int  $first
283
     * @param  int  $second
284
     * @param  int  $offset
285
     * @return $this
286
     */
287
    public function twiceDailyAt($first = 1, $second = 13, $offset = 0)
288
    {
289
        $hours = $first.','.$second;
290
 
291
        return $this->spliceIntoPosition(1, $offset)
292
                    ->spliceIntoPosition(2, $hours);
293
    }
294
 
295
    /**
296
     * Schedule the event to run only on weekdays.
297
     *
298
     * @return $this
299
     */
300
    public function weekdays()
301
    {
302
        return $this->days(Schedule::MONDAY.'-'.Schedule::FRIDAY);
303
    }
304
 
305
    /**
306
     * Schedule the event to run only on weekends.
307
     *
308
     * @return $this
309
     */
310
    public function weekends()
311
    {
312
        return $this->days(Schedule::SATURDAY.','.Schedule::SUNDAY);
313
    }
314
 
315
    /**
316
     * Schedule the event to run only on Mondays.
317
     *
318
     * @return $this
319
     */
320
    public function mondays()
321
    {
322
        return $this->days(Schedule::MONDAY);
323
    }
324
 
325
    /**
326
     * Schedule the event to run only on Tuesdays.
327
     *
328
     * @return $this
329
     */
330
    public function tuesdays()
331
    {
332
        return $this->days(Schedule::TUESDAY);
333
    }
334
 
335
    /**
336
     * Schedule the event to run only on Wednesdays.
337
     *
338
     * @return $this
339
     */
340
    public function wednesdays()
341
    {
342
        return $this->days(Schedule::WEDNESDAY);
343
    }
344
 
345
    /**
346
     * Schedule the event to run only on Thursdays.
347
     *
348
     * @return $this
349
     */
350
    public function thursdays()
351
    {
352
        return $this->days(Schedule::THURSDAY);
353
    }
354
 
355
    /**
356
     * Schedule the event to run only on Fridays.
357
     *
358
     * @return $this
359
     */
360
    public function fridays()
361
    {
362
        return $this->days(Schedule::FRIDAY);
363
    }
364
 
365
    /**
366
     * Schedule the event to run only on Saturdays.
367
     *
368
     * @return $this
369
     */
370
    public function saturdays()
371
    {
372
        return $this->days(Schedule::SATURDAY);
373
    }
374
 
375
    /**
376
     * Schedule the event to run only on Sundays.
377
     *
378
     * @return $this
379
     */
380
    public function sundays()
381
    {
382
        return $this->days(Schedule::SUNDAY);
383
    }
384
 
385
    /**
386
     * Schedule the event to run weekly.
387
     *
388
     * @return $this
389
     */
390
    public function weekly()
391
    {
392
        return $this->spliceIntoPosition(1, 0)
393
                    ->spliceIntoPosition(2, 0)
394
                    ->spliceIntoPosition(5, 0);
395
    }
396
 
397
    /**
398
     * Schedule the event to run weekly on a given day and time.
399
     *
400
     * @param  array|mixed  $dayOfWeek
401
     * @param  string  $time
402
     * @return $this
403
     */
404
    public function weeklyOn($dayOfWeek, $time = '0:0')
405
    {
406
        $this->dailyAt($time);
407
 
408
        return $this->days($dayOfWeek);
409
    }
410
 
411
    /**
412
     * Schedule the event to run monthly.
413
     *
414
     * @return $this
415
     */
416
    public function monthly()
417
    {
418
        return $this->spliceIntoPosition(1, 0)
419
                    ->spliceIntoPosition(2, 0)
420
                    ->spliceIntoPosition(3, 1);
421
    }
422
 
423
    /**
424
     * Schedule the event to run monthly on a given day and time.
425
     *
426
     * @param  int  $dayOfMonth
427
     * @param  string  $time
428
     * @return $this
429
     */
430
    public function monthlyOn($dayOfMonth = 1, $time = '0:0')
431
    {
432
        $this->dailyAt($time);
433
 
434
        return $this->spliceIntoPosition(3, $dayOfMonth);
435
    }
436
 
437
    /**
438
     * Schedule the event to run twice monthly at a given time.
439
     *
440
     * @param  int  $first
441
     * @param  int  $second
442
     * @param  string  $time
443
     * @return $this
444
     */
445
    public function twiceMonthly($first = 1, $second = 16, $time = '0:0')
446
    {
447
        $daysOfMonth = $first.','.$second;
448
 
449
        $this->dailyAt($time);
450
 
451
        return $this->spliceIntoPosition(3, $daysOfMonth);
452
    }
453
 
454
    /**
455
     * Schedule the event to run on the last day of the month.
456
     *
457
     * @param  string  $time
458
     * @return $this
459
     */
460
    public function lastDayOfMonth($time = '0:0')
461
    {
462
        $this->dailyAt($time);
463
 
464
        return $this->spliceIntoPosition(3, Carbon::now()->endOfMonth()->day);
465
    }
466
 
467
    /**
468
     * Schedule the event to run quarterly.
469
     *
470
     * @return $this
471
     */
472
    public function quarterly()
473
    {
474
        return $this->spliceIntoPosition(1, 0)
475
                    ->spliceIntoPosition(2, 0)
476
                    ->spliceIntoPosition(3, 1)
477
                    ->spliceIntoPosition(4, '1-12/3');
478
    }
479
 
480
    /**
481
     * Schedule the event to run quarterly on a given day and time.
482
     *
483
     * @param  int  $dayOfQuarter
484
     * @param  int  $time
485
     * @return $this
486
     */
487
    public function quarterlyOn($dayOfQuarter = 1, $time = '0:0')
488
    {
489
        $this->dailyAt($time);
490
 
491
        return $this->spliceIntoPosition(3, $dayOfQuarter)
492
                    ->spliceIntoPosition(4, '1-12/3');
493
    }
494
 
495
    /**
496
     * Schedule the event to run yearly.
497
     *
498
     * @return $this
499
     */
500
    public function yearly()
501
    {
502
        return $this->spliceIntoPosition(1, 0)
503
                    ->spliceIntoPosition(2, 0)
504
                    ->spliceIntoPosition(3, 1)
505
                    ->spliceIntoPosition(4, 1);
506
    }
507
 
508
    /**
509
     * Schedule the event to run yearly on a given month, day, and time.
510
     *
511
     * @param  int  $month
512
     * @param  int|string  $dayOfMonth
513
     * @param  string  $time
514
     * @return $this
515
     */
516
    public function yearlyOn($month = 1, $dayOfMonth = 1, $time = '0:0')
517
    {
518
        $this->dailyAt($time);
519
 
520
        return $this->spliceIntoPosition(3, $dayOfMonth)
521
                    ->spliceIntoPosition(4, $month);
522
    }
523
 
524
    /**
525
     * Set the days of the week the command should run on.
526
     *
527
     * @param  array|mixed  $days
528
     * @return $this
529
     */
530
    public function days($days)
531
    {
532
        $days = is_array($days) ? $days : func_get_args();
533
 
534
        return $this->spliceIntoPosition(5, implode(',', $days));
535
    }
536
 
537
    /**
538
     * Set the timezone the date should be evaluated on.
539
     *
540
     * @param  \DateTimeZone|string  $timezone
541
     * @return $this
542
     */
543
    public function timezone($timezone)
544
    {
545
        $this->timezone = $timezone;
546
 
547
        return $this;
548
    }
549
 
550
    /**
551
     * Splice the given value into the given position of the expression.
552
     *
553
     * @param  int  $position
554
     * @param  string  $value
555
     * @return $this
556
     */
557
    protected function spliceIntoPosition($position, $value)
558
    {
559
        $segments = preg_split("/\s+/", $this->expression);
560
 
561
        $segments[$position - 1] = $value;
562
 
563
        return $this->cron(implode(' ', $segments));
564
    }
565
}