Skip to content
This repository was archived by the owner on Apr 7, 2025. It is now read-only.

Commit 51fa311

Browse files
authored
Merge pull request #8 from lukewaite/l5.7_support
L5.7 support
2 parents 02a850d + 5c8714a commit 51fa311

File tree

13 files changed

+141
-56
lines changed

13 files changed

+141
-56
lines changed

.travis.yml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
language: php
22

33
php:
4-
- 7.0
5-
- 7.1
6-
- 7.2
74
- 7.3
85

96
env:
10-
matrix:
11-
- COMPOSER_FLAGS="--prefer-lowest"
12-
- COMPOSER_FLAGS=""
7+
- LARAVEL_VERSION="~5.5.0" COMPOSER_FLAGS="--prefer-lowest"
8+
- LARAVEL_VERSION="~5.5.0" COMPOSER_FLAGS=""
9+
- LARAVEL_VERSION="~5.6.0" COMPOSER_FLAGS="--prefer-lowest"
10+
- LARAVEL_VERSION="~5.6.0" COMPOSER_FLAGS=""
11+
- LARAVEL_VERSION="~5.7.0" COMPOSER_FLAGS="--prefer-lowest"
12+
- LARAVEL_VERSION="~5.7.0" COMPOSER_FLAGS=""
13+
14+
matrix:
15+
include:
16+
- php: 7.0
17+
env: LARAVEL_VERSION="~5.1.0" COMPOSER_FLAGS=""
18+
- php: 7.1
19+
env: LARAVEL_VERSION="~5.7.0" COMPOSER_FLAGS=""
20+
- php: 7.2
21+
env: LARAVEL_VERSION="~5.7.0" COMPOSER_FLAGS=""
1322

1423
before_script:
15-
- travis_retry composer self-update
24+
- travis_retry composer require laravel/framework ${LARAVEL_VERSION}
1625
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source
1726

1827
script:

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"illuminate/support": ">5.1.0 <5.8"
2626
},
2727
"require-dev": {
28-
"laravel/framework": ">5.1.0 <5.2",
28+
"laravel/framework": ">5.1.0 <5.8",
2929
"mockery/mockery": "^0.9.6",
3030
"php-coveralls/php-coveralls": "^2.1",
3131
"phpunit/phpunit": "^6.5",

src/Contracts/Handler.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
namespace Intouch\LaravelAwsLambda\Contracts;
44

5-
abstract class Handler implements IHandler
5+
abstract class Handler
66
{
77
protected $payload;
88

9-
public function __construct($payload)
9+
public function setPayload($payload)
1010
{
1111
$this->payload = $payload;
1212
}
13+
14+
abstract public function canHandle();
15+
16+
// abstract function handle();
1317
}

src/Contracts/IHandler.php

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

src/Executor.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Intouch\LaravelAwsLambda;
44

55
use Illuminate\Contracts\Container\Container;
6+
use Intouch\LaravelAwsLambda\Contracts\Handler;
67

78
class Executor
89
{
@@ -37,9 +38,11 @@ public function handle($payload)
3738
private function runHandlers($payload)
3839
{
3940
foreach ($this->handlers as $handler) {
40-
$instance = new $handler($payload);
41+
/** @var Handler $instance */
42+
$instance = $this->app->make($handler);
43+
$instance->setPayload($payload);
4144

42-
if ($instance->canHandle($payload)) {
45+
if ($instance->canHandle()) {
4346
return $this->app->call([$instance, 'handle']);
4447
}
4548
}

src/Handlers/Sqs.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,33 @@
22

33
namespace Intouch\LaravelAwsLambda\Handlers;
44

5-
use Illuminate\Contracts\Container\Container;
5+
use Illuminate\Foundation\Application;
66
use Illuminate\Queue\Worker;
7+
use Illuminate\Queue\WorkerOptions;
78
use Intouch\LaravelAwsLambda\Contracts\Handler;
89
use Intouch\LaravelAwsLambda\Queue\Jobs\LambdaSqsJob;
10+
use Intouch\LaravelAwsLambda\Queue\Jobs\LambdaSqsJobFiveOne;
911

1012
class Sqs extends Handler
1113
{
1214
/**
13-
* @param Container $container
15+
* @param Application $app
1416
* @param Worker $worker
1517
*
1618
* @return array|null
1719
* @throws \Throwable
1820
*/
19-
public function handle(Container $container, Worker $worker)
21+
public function handle(Application $app, Worker $worker)
2022
{
21-
$job = new LambdaSqsJob($container, $this->payload);
23+
if (version_compare($app->version(), '5.3.0', '>=')) {
24+
$job = new LambdaSqsJob($app, $this->payload);
2225

23-
return $worker->process('lambda', $job);
26+
return $worker->process('lambda', $job, new WorkerOptions());
27+
} else {
28+
$job = new LambdaSqsJobFiveOne($app, $this->payload);
29+
30+
return $worker->process('lambda', $job);
31+
}
2432
}
2533

2634
public function canHandle()

src/Queue/Jobs/LambdaSqsJob.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ public function __construct(Container $container, array $job)
2929

3030
/**
3131
* Fire the job.
32+
* We implement this here, and pass it up to the parent to allow the class to be loaded in older versions
33+
* of Laravel without triggering a fatal error. Those versions of Laravel use the LambadaSqsJobFiveOne class.
3234
*
3335
* @return void
3436
*/
3537
public function fire()
3638
{
37-
$this->resolveAndFire(json_decode($this->getRawBody(), true));
39+
parent::fire();
3840
}
3941

4042
/**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Intouch\LaravelAwsLambda\Queue\Jobs;
4+
5+
/**
6+
* A Laravel 5.1 and 5.2 compatible Job.
7+
*/
8+
class LambdaSqsJobFiveOne extends LambdaSqsJob
9+
{
10+
/**
11+
* Fire the job.
12+
*
13+
* @return void
14+
*/
15+
public function fire()
16+
{
17+
$this->resolveAndFire(json_decode($this->getRawBody(), true));
18+
}
19+
}

test/Handlers/ApiGatewayHandlerTest.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ public function can_handle_a_valid_api_gateway_message()
7676
{
7777
$payload = json_decode($this->validJson, true);
7878

79-
$handler = new ApiGateway($payload);
79+
$handler = new ApiGateway();
80+
$handler->setPayload($payload);
8081
$this->assertTrue($handler->canHandle());
8182
}
8283

@@ -122,15 +123,19 @@ public function it_ignores_a_payload_from_an_alb()
122123

123124
$payload = json_decode($payload, true);
124125

125-
$handler = new ApiGateway($payload);
126+
$handler = new ApiGateway();
127+
$handler->setPayload($payload);
128+
126129
$this->assertFalse($handler->canHandle());
127130
}
128131

129132
/** @test */
130133
public function it_coverts_headers_json_to_a_server_array_of_headers()
131134
{
132135
$payload = json_decode($this->validJson, true);
133-
$handler = new ApiGateway($payload);
136+
137+
$handler = new ApiGateway();
138+
$handler->setPayload($payload);
134139

135140
$this->assertEquals(
136141
[
@@ -161,7 +166,8 @@ public function it_coverts_headers_json_to_a_server_array_of_headers()
161166
public function it_coverts_headers_json_to_a_server_array_of_headers_not_prefixing_content_type()
162167
{
163168
$payload = json_decode($this->validJson, true);
164-
$handler = new ApiGateway($payload);
169+
$handler = new ApiGateway();
170+
$handler->setPayload($payload);
165171
$headers = [
166172
'Accept' => 'application/json',
167173
'Content-Type' => 'application/json',
@@ -184,7 +190,8 @@ public function it_handles_non_base64_encoded_body()
184190
'body' => 'I am a sample body',
185191
'isBase64Encoded' => false,
186192
];
187-
$handler = new ApiGateway($payload);
193+
$handler = new ApiGateway();
194+
$handler->setPayload($payload);
188195

189196
$this->assertEquals('I am a sample body', $handler->getBodyFromPayload());
190197
}
@@ -196,7 +203,8 @@ public function it_handles_a_base64_encoded_body()
196203
'body' => 'SSBhbSBhIHNhbXBsZSBib2R5IHRoYXQgaXMgYmFzZTY0IGVuY29kZWQ=',
197204
'isBase64Encoded' => true,
198205
];
199-
$handler = new ApiGateway($payload);
206+
$handler = new ApiGateway();
207+
$handler->setPayload($payload);
200208

201209
$this->assertEquals('I am a sample body that is base64 encoded', $handler->getBodyFromPayload());
202210
}
@@ -212,7 +220,8 @@ public function it_correctly_converts_request_uri_to_parsable_uri($path, $expect
212220
->andReturn($config = \Mockery::mock('Illuminate\Config\Repository'));
213221
$config->shouldReceive('get')->once()->with('app.url')->andReturn('http://localhost');
214222

215-
$handler = new ApiGateway([
223+
$handler = new ApiGateway();
224+
$handler->setPayload([
216225
'path' => $path,
217226
]);
218227

test/Handlers/ArtisanHandlerTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public function can_handle_a_valid_artisan_message()
1717
{
1818
$payload = json_decode($this->validJson, true);
1919

20-
$handler = new Artisan($payload);
20+
$handler = new Artisan();
21+
$handler->setPayload($payload);
2122
$this->assertTrue($handler->canHandle());
2223
}
2324

@@ -30,7 +31,9 @@ public function it_handles_an_artisan_message_correctly()
3031

3132
$payload = json_decode($this->validJson, true);
3233

33-
$handler = new Artisan($payload);
34+
$handler = new Artisan();
35+
$handler->setPayload($payload);
36+
3437
$retval = $handler->handle($kernel);
3538

3639
// Artisan commands should return an exit code of 0

0 commit comments

Comments
 (0)