Skip to content

Commit 254b4b7

Browse files
committed
Added helper method
1 parent 67fb237 commit 254b4b7

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,18 @@ class GetFile extends Task
104104
}
105105
```
106106

107+
Blade template:
108+
107109
```blade
108110
cat {{ $options() }} {{ $path }}
109111
```
110112

113+
You can create a new instance of the Task using the static `make()` method:
114+
115+
```php
116+
GetFile::make('/etc/hosts')->dispatch();
117+
```
118+
111119
## Task options
112120

113121
You may specify a timeout. By default, the timeout is based on the `task-runner.default_timeout` config value.

src/Task.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,21 @@ public function pending(): PendingTask
156156
return new PendingTask($this);
157157
}
158158

159+
/**
160+
* Returns a new PendingTask with this task.
161+
*
162+
* @return \ProtoneMedia\LaravelTaskRunner\PendingTask
163+
*/
164+
public static function make(...$arguments): PendingTask
165+
{
166+
return (new static(...$arguments))->pending();
167+
}
168+
159169
/**
160170
* Helper methods to create a new PendingTask.
161171
*/
162172
public static function __callStatic($name, $arguments)
163173
{
164-
return app(static::class)->pending()->{$name}(...$arguments);
174+
return (new static)->pending()->{$name}(...$arguments);
165175
}
166176
}

tests/CustomTask.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ class CustomTask extends Task
1414

1515
public $someData = 'foo';
1616

17+
public function __construct($someData = 'foo')
18+
{
19+
$this->someData = $someData;
20+
}
21+
1722
public function someMethod()
1823
{
1924
return 'bar';

tests/TaskTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Support\Facades\View;
66
use Mockery;
77
use ProtoneMedia\LaravelTaskRunner\Connection;
8+
use ProtoneMedia\LaravelTaskRunner\PendingTask;
89

910
it('can generate defaults based on the class name and configuration', function () {
1011
$task = new DemoTask;
@@ -60,6 +61,13 @@
6061
expect($task->getScript())->toBe('baz foo bar');
6162
});
6263

64+
it('has a helper method to create the task fluently', function () {
65+
$task = CustomTask::make('otherData');
66+
67+
expect($task)->toBeInstanceOf(PendingTask::class);
68+
expect($task->task->someData)->toBe('otherData');
69+
});
70+
6371
it('can create a pending task with a static method', function () {
6472
$pendingTask = DemoTask::inBackground();
6573
expect($pendingTask->task)->toBeInstanceOf(DemoTask::class);

0 commit comments

Comments
 (0)