Skip to content

Commit b9049f4

Browse files
committed
Small refactor.
- added new expressions - added readme
1 parent cd34bba commit b9049f4

File tree

15 files changed

+583
-171
lines changed

15 files changed

+583
-171
lines changed

.github/workflows/run-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
strategy:
99
fail-fast: true
1010
matrix:
11-
os: [ubuntu-latest, windows-latest]
11+
os: [ubuntu-latest]
1212
php: [8.0]
1313
stability: [prefer-lowest, prefer-stable]
1414

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
build
66
composer.lock
77
coverage
8-
docs
9-
phpunit.xml
10-
psalm.xml
118
vendor
129
.php-cs-fixer.cache
1310

CHANGELOG.md

Lines changed: 0 additions & 7 deletions
This file was deleted.

README.md

Lines changed: 278 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,311 @@
11
# Cron expression generator
22

3-
[![Latest Version on Packagist](https://img.shields.io/packagist/v/butschster/cronexpressiongenerator.svg?style=flat-square)](https://packagist.org/packages/butschster/cronexpressiongenerator)
4-
[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/butschster/cronexpressiongenerator/run-tests?label=tests)](https://github.com/butschster/cronexpressiongenerator/actions?query=workflow%3ATests+branch%3Amaster)
5-
[![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/butschster/cronexpressiongenerator/Check%20&%20fix%20styling?label=code%20style)](https://github.com/butschster/cronexpressiongenerator/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amaster)
6-
[![Total Downloads](https://img.shields.io/packagist/dt/butschster/cronexpressiongenerator.svg?style=flat-square)](https://packagist.org/packages/butschster/cronexpressiongenerator)
3+
[![Latest Version on Packagist](https://img.shields.io/packagist/v/butschster/cron-expression-generator.svg?style=flat-square)](https://packagist.org/packages/butschster/cron-expression-generator)
4+
[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/butschster/cron-expression-generator/run-tests?label=tests)](https://github.com/butschster/cron-expression-generator/actions?query=workflow%3ATests+branch%3Amaster)
5+
[![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/butschster/cron-expression-generator/Check%20&%20fix%20styling?label=code%20style)](https://github.com/butschster/cron-expression-generator/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amaster)
6+
[![Total Downloads](https://img.shields.io/packagist/dt/butschster/cron-expression-generator.svg?style=flat-square)](https://packagist.org/packages/butschster/cron-expression-generator)
77

8-
---
9-
This package can be used as to scaffold a framework agnostic package. Follow these steps to get started:
8+
## Installation
109

11-
1. Press the "Use template" button at the top of this repo to create a new repo with the contents of this cronexpressiongenerator
12-
2. Run "./configure.sh" to run a script that will replace all placeholders throughout all the files
13-
3. Have fun creating your package.
14-
4. If you need help creating a package, consider picking up our <a href="https://laravelpackage.training">Laravel Package Training</a> video course.
15-
---
10+
You can install the package via composer:
1611

17-
This is where your description should go. Try and limit it to a paragraph or two. Consider adding a small example.
12+
```bash
13+
composer require butschster/cron-expression-generator
14+
```
1815

19-
## Support us
16+
## Usage
2017

21-
[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/CronExpressionGenerator.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/CronExpressionGenerator)
18+
### Creates a new generator
19+
```php
20+
use Butschster\CronExpression\Generator;
21+
use Butschster\CronExpression\Expression;
2222

23-
We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
23+
$generator = new Generator();
24+
// or
25+
$generator = new Generator(new Expression('* * * * *'));
26+
// or
27+
$generator = Generator::create();
28+
// or
29+
$generator = Generator::create(new Expression('* * * * *'));
30+
```
2431

25-
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
32+
### Gets expression object
33+
```php
34+
$expression = $generator->getExpression(); // \Cron\CronExpression
35+
```
2636

27-
## Installation
37+
### Converts expression to a string
38+
```php
39+
echo $generator->toExpression(); // * * * * *
2840

29-
You can install the package via composer:
41+
echo (string) $generator; // * * * * *
3042

31-
```bash
32-
composer require butschster/cronexpressiongenerator
43+
echo (string) $generator->getExpression(); // * * * * *
44+
45+
echo $generator->getExpression()->getExpression(); // * * * * *
3346
```
3447

35-
## Usage
48+
### Sets specific cron expression
49+
```php
50+
echo $generator->cron('* */3 * * *'); // * */3 * * *
3651

52+
echo $generator->cron('* */3 * * *')->everyTwoMinutes(); // */2 */3 * * *
53+
```
54+
55+
### Manipulate minutes
3756
```php
38-
$cronexpressiongenerator = new Butschster\Cronexpressiongenerator();
39-
echo $cronexpressiongenerator->echoPhrase('Hello, Butschster!');
57+
// Every minute
58+
echo $generator->everyMinute(); // * * * * *
59+
60+
// Every even minute
61+
echo $generator->everyEvenMinute(); // */2 * * * *
62+
63+
// Every two minutes
64+
echo $generator->everyTwoMinutes(); // */2 * * * *
65+
66+
// Every three minutes
67+
echo $generator->everyThreeMinutes(); // */3 * * * *
68+
69+
// Every four minutes
70+
echo $generator->everyFourMinutes(); // */4 * * * *
71+
72+
// Every five minutes
73+
echo $generator->everyFiveMinutes(); // */5 * * * *
74+
75+
// Every ten minutes
76+
echo $generator->everyTenMinutes(); // */10 * * * *
77+
78+
// Every fifteen minutes
79+
echo $generator->everyFifteenMinutes(); // */15 * * * *
80+
81+
// Every 00 and 30 minutes
82+
echo $generator->everyThirtyMinutes(); // 0,30 * * * *
83+
84+
// Every minute
85+
echo $generator->minutes('*'); // * * * * *
86+
87+
// Every ten minutes
88+
echo $generator->minutes('*/10'); // */10 * * * *
89+
90+
// Every hh:10, hh:11, ..., hh:30 minutes
91+
echo $generator->minutes('10-30'); // 10-30 * * * *
92+
93+
// Hourly at hh:01
94+
echo $generator->minutes(1); // 1 * * * *
95+
96+
// Hourly at hh:05, hh:10, hh:15
97+
echo $generator->minutes(5, 10, 15, ...); // 5,10,15,... * * * *
98+
99+
echo $generator->minutes(61); // Throws an exception: Invalid CRON field value 61 at position 0
40100
```
41101

42-
## Testing
102+
### Manipulate hours
103+
```php
104+
// Every hour at 00 minutes
105+
echo $generator->hourly(); // 0 * * * *
43106

44-
```bash
45-
composer test
107+
// Every hour at 15 minutes
108+
echo $generator->hourlyAt(15); // 15 * * * *
109+
110+
// Every hour at 15, 30, 45 minutes
111+
echo $generator->hourlyAt(15, 30, 45); // 15,30,45 * * * *
112+
113+
// Every two hours
114+
echo $generator->everyTwoHours(); // 0 */2 * * *
115+
116+
// Every three hours
117+
echo $generator->everyThreeHours(); // 0 */3 * * *
118+
119+
// Every four hours
120+
echo $generator->everyFourHours(); // 0 */4 * * *
121+
122+
// Every six hours
123+
echo $generator->everySixHours(); // 0 */6 * * *
124+
125+
// Every 1, 2, 3 hours at 15 minutes
126+
echo $generator->hourlyAt(15)->hours(1, 2, 3); // 15 */3 * * *
127+
128+
// Every three hours
129+
echo $generator->hours('*/3'); // * */3 * * *
130+
131+
echo $generator->hours(25); // Throws an exception: Invalid CRON field value 25 at position 1
46132
```
47133

48-
## Changelog
134+
### Manipulate days
135+
```php
136+
// Every day at 00:00
137+
echo $generator->daily(); // 0 0 * * *
138+
139+
// Every day at 01:00
140+
echo $generator->daily(1); // 0 1 * * *
49141

50-
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
142+
// Every day at 03:00, 15:00, 23:00
143+
echo $generator->daily(3, 15, 23); // 0 3,15,23 * * *
51144

52-
## Contributing
145+
// Every day at 13:00
146+
echo $generator->dailyAt(13); // 0 13 * * *
53147

54-
Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.
148+
// Every day at 13:25
149+
echo $generator->dailyAt(13, 25); // 25 13 * * *
55150

56-
## Security Vulnerabilities
151+
// Every day at 03:00, 15:00
152+
echo $generator->twiceDaily(3, 15); // 0 3,15 * * *
57153

58-
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
154+
// Every day at 03:05, 15:05
155+
echo $generator->twiceDailyAt(3, 15, 5); // 5 3,15 * * *
156+
```
157+
158+
### Manipulate days of week
159+
```php
160+
// Every week on monday
161+
echo $generator->weekly(); // 0 0 * * 0
162+
163+
// Every week on monday and thursday
164+
echo $generator->weekly(Generator::MONDAY, Generator::THURSDAY); // 0 0 * * 1,4
165+
166+
// Every week on weekdays
167+
echo $generator->daily()->weekdays(); // 0 0 * * 1-5
168+
169+
// Every week on weekends
170+
echo $generator->daily()->weekends(); // 0 0 * * 6,0
171+
172+
// Every monday
173+
echo $generator->daily()->mondays(); // 0 0 * * 1
174+
// or
175+
echo $generator->weeklyOnMonday();
176+
// or
177+
echo $generator->weeklyOnMonday(8, 6); // 6 8 * * 1
178+
179+
// Every tuesday
180+
echo $generator->daily()->tuesdays(); // 0 0 * * 2
181+
// or
182+
echo $generator->weeklyOnTuesday();
183+
184+
// Every wednesday
185+
echo $generator->daily()->wednesdays(); // 0 0 * * 3
186+
// or
187+
echo $generator->weeklyOnWednesday();
188+
189+
// Every thursday
190+
echo $generator->daily()->thursdays(); // 0 0 * * 4
191+
// or
192+
echo $generator->weeklyOnThursday();
193+
194+
// Every friday
195+
echo $generator->daily()->fridays(); // 0 0 * * 5
196+
// or
197+
echo $generator->weeklyOnFriday();
198+
199+
// Every saturday
200+
echo $generator->daily()->saturdays(); // 0 0 * * 6
201+
// or
202+
echo $generator->weeklyOnSaturday();
203+
204+
// Every sunday
205+
echo $generator->daily()->sundays(); // 0 0 * * 0
206+
// or
207+
echo $generator->weeklyOnSunday();
208+
209+
// Every monday
210+
echo $generator->weeklyOn(Generator::MONDAY); // 0 0 * * 1
211+
212+
// Every monday at 8am
213+
echo $generator->weeklyOn(Generator::MONDAY, 8); // 0 8 * * 1
214+
215+
// Every monday at 08:06
216+
echo $generator->weeklyOn(Generator::MONDAY, 8, 6); // 6 8 * * 1
217+
```
218+
219+
### Manipulate months
220+
```php
221+
// Every month on 1-st day at 00:00
222+
echo $generator->monthly(); // 0 0 1 * *
223+
224+
// Every month on 1-st day at 12:00
225+
echo $generator->monthly(12); // 00 12 1 * *
226+
227+
// Every month on 1-st day at 12:30
228+
echo $generator->monthly(12, 30); // 30 12 1 * *
229+
230+
// Every month on 15-st day at 12:00
231+
echo $generator->monthlyOn(15, 12); // 0 12 15 * *
232+
233+
// Every month on 15-st day at 12:30
234+
echo $generator->monthlyOn(15, 12, 30); // 30 12 15 * *
235+
236+
// Every month two times on 15, 24 day at 00:00
237+
echo $generator->twiceMonthly(15, 24); // 0 0 15,24 * *
238+
239+
// Every month two times on 15, 24 day at 10:00
240+
echo $generator->twiceMonthly(15, 24, 10); // 0 10 15,24 * *
241+
242+
// Every month two times on 15, 24 day at 10:30
243+
echo $generator->twiceMonthly(15, 24, 10, 30); // 30 10 15,24 * *
244+
245+
// Every month three times on 12, 24, 30 day at 10:345
246+
echo $generator->dailyAt(10, 45)->daysOfMonth(12, 24, 30); // 45 10 12,24,30 * *
247+
248+
// Every month on the last day at 00:00
249+
echo $generator->lastDayOfMonth(); // 0 0 31 * *
250+
251+
// Every month on the last day at 12:00
252+
echo $generator->lastDayOfMonth(12); // 0 12 31 * *
253+
254+
// Every month on the last day at 12:30
255+
echo $generator->lastDayOfMonth(12, 30); // 30 12 31 * *
256+
257+
// Every month on the last day at specific date
258+
$date = new DateTime('2021-02-05');
259+
echo $generator->lastDayOfMonth(12, 30, $date); // 30 12 28 * *
260+
261+
// Every quarter yyyy-01,03,06,09-01 00:00
262+
echo $generator->quarterly(); // 0 0 1 1-12/3 *
263+
264+
// Every year yyyy-01-01 00:00
265+
echo $generator->yearly(); // 0 0 1 1 *
266+
267+
// Every year yyyy-04-01 00:00
268+
echo $generator->yearlyOn(Generator::APR); // 0 0 1 4 *
269+
270+
// Every year yyyy-04-05 00:00
271+
echo $generator->yearlyOn(Generator::APR, 5); // 0 0 5 4 *
272+
273+
// Every year yyyy-04-05 08:00
274+
echo $generator->yearlyOn(Generator::APR, 5, 8); // 0 8 5 4 *
275+
276+
// Every year yyyy-04-05 08:30
277+
echo $generator->yearlyOn(Generator::APR, 5, 8, 30); // 30 8 5 4 *
278+
```
279+
280+
### Specific time
281+
```php
282+
$date = new DateTime('2021-02-05 12:34:26');
283+
284+
// Every year yyyy-02-05 12:34
285+
echo $generator->on($date); // 34 12 5 2 *
286+
```
287+
288+
### Custom expression
289+
```php
290+
// * */2 5,10,15,20,25,30 3,6,9,12 1,3,5,0
291+
echo $generator
292+
->yearly()
293+
->months(Generator::MAR, Generator::JUN, Generator::SEP, Generator::DEC)
294+
->daysOfMonth(5, 10, 15, 20, 25, 30)
295+
->daysOfWeek(Generator::MONDAY, Generator::WEDNESDAY, Generator::FRIDAY, Generator::SUNDAY)
296+
->everyTwoHours()
297+
->everyMinute();
298+
```
299+
300+
## Testing
301+
302+
```bash
303+
composer test
304+
```
59305

60306
## Credits
61307

62308
- [butschster](https://github.com/butschster)
63-
- [All Contributors](../../contributors)
64309

65310
## License
66311

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "butschster/cron-expression-generator",
33
"description": "Cron expression generator",
44
"keywords": [
5-
"cron"
5+
"cron", "php8", "generator"
66
],
77
"license": "MIT",
88
"authors": [

phpunit.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage>
4+
<include>
5+
<directory suffix=".php">./src</directory>
6+
</include>
7+
</coverage>
8+
<testsuites>
9+
<testsuite name="Unit">
10+
<directory suffix="Test.php">./tests</directory>
11+
</testsuite>
12+
</testsuites>
13+
</phpunit>

0 commit comments

Comments
 (0)