Skip to content

Commit 4abe03c

Browse files
authored
Merge pull request #5 from always-open/feature/laravel-11-12
Upgrading to handle laravel 11 and 12
2 parents 8fc3806 + 4101cb0 commit 4abe03c

File tree

7 files changed

+82
-18
lines changed

7 files changed

+82
-18
lines changed

.github/workflows/php-cs-fixer.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88

99
steps:
1010
- name: Checkout code
11-
uses: actions/checkout@v2
11+
uses: actions/checkout@v4
1212
with:
1313
ref: ${{ github.head_ref }}
1414

.github/workflows/phpstan.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414

1515
steps:
1616
- name: "Checkout"
17-
uses: actions/checkout@v2
17+
uses: actions/checkout@v4
1818

1919
- name: PHPStan
2020
uses: docker://oskarstark/phpstan-ga

.github/workflows/run-tests.yml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,18 @@ jobs:
1313
fail-fast: true
1414
matrix:
1515
os: [ubuntu-latest]
16-
php: [8.0, 8.1]
17-
laravel: [8.*, 9.*, 10.*]
16+
php: [8.2, 8.3, 8.4]
17+
laravel: [11.*, 12.*]
1818
stability: [prefer-stable]
1919
include:
20-
- laravel: 8.*
21-
- laravel: 9.*
22-
- laravel: 10.*
20+
- laravel: 11.*
21+
- laravel: 12.*
2322

2423
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}
2524

2625
steps:
2726
- name: Checkout code
28-
uses: actions/checkout@v2
27+
uses: actions/checkout@v4
2928

3029
- name: Setup PHP
3130
uses: shivammathur/setup-php@v2

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ You can install the package via composer:
1818
composer require always-open/laravel-request-logger
1919
```
2020

21+
### Breaking change
22+
If you are upgrading to 3.x or newer, the following steps must be taken as new fields have been added to the logging tables.
23+
- Create a migration for each logging tables with the following
24+
```php
25+
// The headers that were part of the request
26+
$table->json('request_headers')
27+
->nullable();
28+
// The headers that were part of the response
29+
$table->json('response_headers')
30+
->nullable();
31+
```
32+
Run this migration prior to upgrading the package.
33+
2134
## Configuration
2235

2336
``` php
@@ -74,6 +87,35 @@ function makeFacebookApiCall(array $body, Client $facebook_client)
7487
$request_log->save();
7588
}
7689
```
90+
91+
Instead of manually setting the response data you can instead leverage the `updateFromResponse` method:
92+
```php
93+
function makeFacebookApiCall(array $body, Client $facebook_client)
94+
{
95+
$request_headers = [
96+
'api-key' => $config->apiKey,
97+
'Content-Type' => 'application/json',
98+
];
99+
100+
$versioned_path = self::buildVersionedUrlPath($path);
101+
102+
$encoded_body = json_encode($body, JSON_UNESCAPED_SLASHES);
103+
104+
$request = new Request(
105+
'GET',
106+
'/v1/users',
107+
$request_headers,
108+
$encoded_body,
109+
);
110+
111+
$request_log = FacebookRequestLog::makeFromGuzzle($request);
112+
113+
$response = $client->send($request);
114+
$request_log->updateFromResponse($response);
115+
}
116+
```
117+
118+
77119
You can also manually set each property and then save the log instance.
78120

79121
### Testing

composer.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@
2323
}
2424
],
2525
"require": {
26-
"php": "^8.0.0|^8.1.0",
27-
"always-open/laravel-process-stamps": "^5.0|^6.0|^7.0",
26+
"php": "^8.2.0|^8.3.0|^8.4.0",
27+
"always-open/laravel-process-stamps": "^7.0|^8.0",
2828
"guzzlehttp/guzzle": "^7.4",
29-
"laravel/framework": "^8.0|^9.0|^10.0"
29+
"laravel/framework": "^11.0|^12.0"
3030
},
3131
"require-dev": {
32-
"doctrine/dbal": "^2.9|^3.0",
32+
"doctrine/dbal": "^3.0|^4.0",
3333
"friendsofphp/php-cs-fixer": "^3.1",
3434
"laravel/tinker": "^2.7",
35-
"nunomaduro/larastan": "^1.0",
36-
"orchestra/testbench": "^6.22",
37-
"phpstan/phpstan-deprecation-rules": "^1.0",
38-
"phpstan/phpstan-phpunit": "^1.0",
39-
"phpunit/phpunit": "^9.5"
35+
"nunomaduro/larastan": "^3.0",
36+
"orchestra/testbench": "^9.1",
37+
"phpstan/phpstan-deprecation-rules": "^2.0",
38+
"phpstan/phpstan-phpunit": "^2.0",
39+
"phpunit/phpunit": "^12.0"
4040
},
4141
"autoload": {
4242
"psr-4": {

src/Models/RequestLogBaseModel.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use AlwaysOpen\RequestLogger\Observers\RequestLogObserver;
66
use GuzzleHttp\Psr7\Request;
77
use Illuminate\Database\Eloquent\Model;
8+
use Psr\Http\Message\ResponseInterface;
89

910
/**
1011
* AlwaysOpen\RequestLogger\Models\RequestLogBaseModel
@@ -14,7 +15,9 @@
1415
* @property string $http_method
1516
* @property int|null $response_code
1617
* @property array|string|null $body
18+
* @property array|string|null $request_headers
1719
* @property array|string|null $response
20+
* @property array|string|null $response_headers
1821
* @property string|null $exception
1922
* @property \Carbon\Carbon|null $occurred_at
2023
*/
@@ -25,7 +28,9 @@ class RequestLogBaseModel extends Model
2528
'created_at' => 'datetime',
2629
'updated_at' => 'datetime',
2730
'body' => 'json',
31+
'request_headers' => 'json',
2832
'response' => 'json',
33+
'response_headers' => 'json',
2934
];
3035

3136
protected $guarded = [
@@ -56,7 +61,19 @@ public static function makeFromGuzzle(Request $request) : static
5661
$instance->path = $request->getUri()->getPath();
5762
$instance->http_method = $request->getMethod();
5863
$instance->body = $request->getBody()->getContents();
64+
$instance->request_headers = $request->getHeaders();
5965

6066
return $instance;
6167
}
68+
69+
public function updateFromResponse(ResponseInterface $response): self
70+
{
71+
$this->response = json_decode($response->getBody()->getContents(), true);
72+
$this->response_code = $response->getStatusCode();
73+
$this->response_headers = $response->getHeaders();
74+
75+
$this->save();
76+
77+
return $this;
78+
}
6279
}

stubs/migration.stub

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class {CLASS_NAME} extends Migration
1919
$table->string('path', 191)
2020
->index();
2121
// What parameters were passed in (e.g. ?status=new)
22-
$table->string('params')
22+
$table->string('params', 512)
2323
->nullable()
2424
->fulltext();
2525
// HTTP method (e.g. POST/PUT/DELETE)
@@ -32,9 +32,15 @@ class {CLASS_NAME} extends Migration
3232
// The entire JSON encoded payload of the request
3333
$table->json('body')
3434
->nullable();
35+
// The headers that were part of the request
36+
$table->json('request_headers')
37+
->nullable();
3538
// The entire JSON encoded responses
3639
$table->json('response')
3740
->nullable();
41+
// The headers that were part of the response
42+
$table->json('response_headers')
43+
->nullable();
3844
// Internal exceptions that occurred during the request
3945
$table->string('exception')
4046
->nullable();

0 commit comments

Comments
 (0)