Skip to content

Commit a90e4e2

Browse files
committed
wip
1 parent 8b5cbee commit a90e4e2

File tree

8 files changed

+135
-44
lines changed

8 files changed

+135
-44
lines changed

.gitattributes

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# Ignore all test and documentation with "export-ignore".
55
/.gitattributes export-ignore
66
/.gitignore export-ignore
7-
/.travis.yml export-ignore
87
/phpunit.xml.dist export-ignore
98
/.scrutinizer.yml export-ignore
109
/tests export-ignore

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<!-- [[![Test](https://github.com/meemaio/laravel-cloudfront/workflows/Test/badge.svg?branch=master)](https://github.com/meemaio/laravel-cloudfront/actions) -->
99
<!-- [[![Build Status](wip)](ghactions) -->
1010

11-
This is a wrapper package for AWS CloudFront.
11+
This is a wrapper package for AWS CloudFront. Please note that while this package is used in production environments and is completely functional, it is not feature-complete. PRs & ideas are more than welcome!
1212

1313
![laravel-cloudfront package image](https://banners.beyondco.de/CloundFront.png?theme=light&packageManager=composer+require&packageName=meema%2Flaravel-cloudfront&pattern=endlessClouds&style=style_1&description=Easily+%26+quickly+integrate+your+application+with+AWS+CloudFront.&md=1&showWatermark=1&fontSize=150px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg)
1414

@@ -18,7 +18,10 @@ This is a wrapper package for AWS CloudFront.
1818
use Meema\CloudFront\Facades\CloudFront;
1919

2020
// run any of the following CloudFront methods:
21-
$result = CloudFront::invalidateCache(string $id);
21+
$client = CloudFront::getClient(); // exposes the AWS CloudFront client
22+
$result = CloudFront::invalidate(array $items, int $quantity = 1, string $distributionId = null);
23+
$message = CloudFront::getInvalidation(string $invalidationId, string $distributionId = null);
24+
$messages = CloudFront::listInvalidations(string $distributionId = null)
2225
```
2326

2427
## Installation
@@ -56,8 +59,20 @@ return [
5659
'secret' => env('AWS_SECRET_ACCESS_KEY'),
5760
],
5861

62+
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
63+
64+
/**
65+
* Specify the version of the CloudFront API you would like to use.
66+
* Please only adjust this value if you know what you are doing.
67+
*/
5968
'version' => 'latest',
6069

70+
/**
71+
* Specify the CloudFront Distribution ID.
72+
* Example format: EBCYQAQALNSKL
73+
*/
74+
'distribution_id' => env('AWS_CLOUDFRONT_DISTRIBUTION_ID'),
75+
6176
];
6277
```
6378

config/config.php

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,17 @@
1010
],
1111

1212
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
13-
'version' => 'latest',
1413

1514
/**
16-
* Specify the IAM Role ARN.
17-
*
18-
* You can find the Role ARN visiting the following URL:
19-
* https://console.aws.amazon.com/iam/home?region=us-east-1#/roles
20-
* Please note to adjust the "region" in the URL above.
21-
* Tip: in case you need to create a new Role, you may name it `CloudFront_Default_Role`
22-
* by making use of this name, AWS CloudFront will default to using this IAM Role.
15+
* Specify the version of the CloudFront API you would like to use.
16+
* Please only adjust this value if you know what you are doing.
2317
*/
24-
'iam_arn' => env('AWS_IAM_ARN'),
18+
'version' => 'latest',
2519

2620
/**
27-
* Specify the queue you would like use.
28-
*
29-
* It can be found by visiting the following URL:
30-
* https://us-east-1.console.aws.amazon.com/CloudFront/home?region=us-east-1#/queues/details/Default
31-
* Please note to adjust the "region" in the URL above.
21+
* Specify the CloudFront Distribution ID.
22+
* Example format: EBCYQAQALNSKL
3223
*/
33-
'queue_arn' => env('AWS_QUEUE_ARN'),
24+
'distribution_id' => env('AWS_CLOUDFRONT_DISTRIBUTION_ID'),
3425

3526
];

src/CloudFront.php

Lines changed: 86 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Aws\Credentials\Credentials;
66
use Aws\CloudFront\CloudFrontClient;
7+
use Aws\Exception\AwsException;
78
use Meema\CloudFront\Contracts\CloudFront as CloudFrontInterface;
89

910
class CloudFront implements CloudFrontInterface
@@ -24,7 +25,7 @@ public function __construct(CloudFrontClient $client)
2425
{
2526
$config = config('cloudfront');
2627

27-
$this->client = new CloudFrontClient([
28+
$this->client = new $client([
2829
'version' => $config['version'],
2930
'region' => $config['region'],
3031
'credentials' => new Credentials($config['credentials']['key'], $config['credentials']['secret']),
@@ -42,15 +43,95 @@ public function getClient(): CloudFrontClient
4243
}
4344

4445
/**
45-
* Bust an item in CloudFront's cache.
46+
* Bust an item/s in CloudFront's cache.
4647
*
47-
* @param string $id
48+
* @param array $items
49+
* @param int $quantity
50+
* @param string|null $distributionId
4851
* @return \Aws\Result
4952
*/
50-
public function invalidateCache(string $id)
53+
public function invalidate(array $items, int $quantity = 1, string $distributionId = null)
5154
{
5255
return $this->client->createInvalidation([
53-
'Id' => $id,
56+
'DistributionId' => $distributionId ?? config('cloudfront.distribution_id'),
57+
'InvalidationBatch' => [
58+
// CallerReference is a unique value that you provide and that CloudFront uses to prevent replays of your request.
59+
// You must provide a new caller reference value and other new information in the request for CloudFront to create a new invalidation request.
60+
'CallerReference' => microtime(true),
61+
'Paths' => [
62+
'Items' => $items,
63+
'Quantity' => $quantity,
64+
],
65+
],
5466
]);
5567
}
68+
69+
/**
70+
* Get a cache "invalidation".
71+
*
72+
* @param string $invalidationId
73+
* @param string|null $distributionId
74+
* @return string
75+
*/
76+
public function getInvalidation(string $invalidationId, string $distributionId = null)
77+
{
78+
try {
79+
$result = $this->client->getInvalidation([
80+
'DistributionId' => $distributionId ?? config('cloudfront.distribution_id'),
81+
'Id' => $invalidationId,
82+
]);
83+
84+
$message = '';
85+
86+
if (isset($result['Invalidation']['Status'])) {
87+
$message = 'The status for the invalidation with the ID of '.$result['Invalidation']['Id'].' is '.$result['Invalidation']['Status'];
88+
}
89+
90+
if (isset($result['@metadata']['effectiveUri'])) {
91+
$message .= ', and the effective URI is '.$result['@metadata']['effectiveUri'].'.';
92+
} else {
93+
$message = 'Error: Could not get information about '.'the invalidation. The invalidation\'s status '.'was not available.';
94+
}
95+
96+
return $message;
97+
} catch (AwsException $e) {
98+
throw($e->getAwsErrorMessage());
99+
}
100+
}
101+
102+
/**
103+
* List all of the cache invalidations.
104+
*
105+
* @param string|null $distributionId
106+
* @return array
107+
*/
108+
public function listInvalidations(string $distributionId = null)
109+
{
110+
try {
111+
$invalidations = $this->client->listInvalidations([
112+
'DistributionId' => $distributionId ?? config('cloudfront.distribution_id') ,
113+
]);
114+
115+
$messages = [];
116+
117+
if (isset($invalidations['InvalidationList'])) {
118+
if ($invalidations['InvalidationList']['Quantity'] > 0) {
119+
foreach ($invalidations['InvalidationList']['Items'] as $invalidation) {
120+
$message = 'The invalidation with the ID of '.$invalidation['Id'].' has the status of '.$invalidation['Status'].'.';
121+
$messages[$invalidation['Id']] = $message;
122+
}
123+
} else {
124+
$message = 'Could not find any invalidations for the specified distribution.';
125+
array_push($messages, $message);
126+
}
127+
} else {
128+
$message = 'Error: Could not get invalidation information. Could not get information about the specified distribution.';
129+
array_push($messages, $message);
130+
}
131+
132+
return $messages;
133+
} catch (AwsException $e) {
134+
throw($e->getAwsErrorMessage());
135+
}
136+
}
56137
}

src/CloudFrontManager.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Aws\CloudFront\CloudFrontClient;
77
use Exception;
88
use Illuminate\Support\Manager;
9-
use Meema\CloudFront\CloudFront;
109

1110
class CloudFrontManager extends Manager
1211
{

src/Contracts/CloudFront.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,29 @@
55
interface CloudFront
66
{
77
/**
8-
* Cancels an active job.
8+
* Delete items in your CloudFront cache .
99
*
10-
* @param string $id
10+
* @param array $items
11+
* @param int $quantity
12+
* @param string|null $distributionId
1113
* @return \Aws\Result
1214
*/
13-
public function createInvalidation(string $id);
15+
public function invalidate(array $items, int $quantity = 1, string $distributionId = null);
16+
17+
/**
18+
* Delete items in your CloudFront cache .
19+
*
20+
* @param string $invalidationId
21+
* @param string|null $distributionId
22+
* @return \Aws\Result
23+
*/
24+
public function getInvalidation(string $invalidationId, string $distributionId = null);
25+
26+
/**
27+
* Delete items in your CloudFront cache .
28+
*
29+
* @param string|null $distributionId
30+
* @return \Aws\Result
31+
*/
32+
public function listInvalidations(string $distributionId = null);
1433
}

src/Facades/CloudFront.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
use Illuminate\Support\Facades\Facade;
66

77
/**
8-
* @method static \Aws\Result invalidateCache(string $id)
8+
* @method static \Aws\CloudFront\CloudFrontClient getClient()
9+
* @method static \Aws\Result invalidate(array $items, int $quantity = 1, string $distributionId = null)
10+
* @method static string getInvalidation(string $invalidationId, string $distributionId = null)
11+
* @method static array listInvalidations(string $distributionId = null)
912
*/
1013
class CloudFront extends Facade
1114
{

src/Traits/HasMediaConversions.php

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

0 commit comments

Comments
 (0)