Skip to content

Commit 36a4bd5

Browse files
authored
Support for Google Cloud events (#36)
* Add support for Google CloudEvent * Copy tests form Google * cs * fix * Revert typo * Added small test * Bugfix
1 parent a042e45 commit 36a4bd5

11 files changed

+1031
-0
lines changed

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@ still need to create an `index.php`.
3838
// This file is needed for google cloud
3939
```
4040

41+
## Using CloudEvent
42+
43+
```php
44+
// index.php
45+
use Google\CloudFunctions\CloudEvent;
46+
47+
require_once dirname(__DIR__).'/vendor/autoload_runtime.php';
48+
49+
return function(CloudEvent $cloudevent) {
50+
// Print the whole CloudEvent
51+
$stdout = fopen('php://stdout', 'wb');
52+
fwrite($stdout, $cloudevent);
53+
};
54+
```
55+
4156
## Troubleshooting
4257

4358
### Cache/Build directory

composer.json

+15
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,26 @@
1010
}
1111
],
1212
"require": {
13+
"php": ">=7.2.5",
14+
"ext-json": "*",
1315
"symfony/runtime": "5.x-dev"
1416
},
1517
"require-dev": {
1618
"symfony/phpunit-bridge": "^5.2"
1719
},
20+
"autoload": {
21+
"psr-4": {
22+
"Runtime\\GoogleCloud\\": "src/",
23+
"Symfony\\Runtime\\Google\\CloudFunctions\\": "runtime/",
24+
"Google\\CloudFunctions\\": "google/"
25+
}
26+
},
27+
"autoload-dev": {
28+
"psr-4": {
29+
"Runtime\\GoogleCloud\\Tests\\": "tests/runtime",
30+
"Google\\CloudFunctions\\Tests\\": "tests/google/"
31+
}
32+
},
1833
"minimum-stability": "dev",
1934
"prefer-stable": true,
2035
"bin": [

google/CloudEvent.php

+164
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2020 Google LLC.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
namespace Google\CloudFunctions;
20+
21+
use JsonSerializable;
22+
23+
class CloudEvent implements JsonSerializable
24+
{
25+
// Required Fields
26+
private $id;
27+
private $source;
28+
private $specversion;
29+
private $type;
30+
31+
// Optional Fields
32+
private $datacontenttype;
33+
private $dataschema;
34+
private $subject;
35+
private $time;
36+
/**
37+
* @var mixed
38+
*/
39+
private $data;
40+
41+
final public function __construct(
42+
string $id,
43+
string $source,
44+
string $specversion,
45+
string $type,
46+
?string $datacontenttype,
47+
?string $dataschema,
48+
?string $subject,
49+
?string $time,
50+
$data
51+
) {
52+
$this->id = $id;
53+
$this->source = $source;
54+
$this->specversion = $specversion;
55+
$this->type = $type;
56+
$this->datacontenttype = $datacontenttype;
57+
$this->dataschema = $dataschema;
58+
$this->subject = $subject;
59+
$this->time = $time;
60+
$this->data = $data;
61+
}
62+
63+
public function getId(): string
64+
{
65+
return $this->id;
66+
}
67+
68+
public function getSource(): string
69+
{
70+
return $this->source;
71+
}
72+
73+
public function getSpecVersion(): string
74+
{
75+
return $this->specversion;
76+
}
77+
78+
public function getType(): string
79+
{
80+
return $this->type;
81+
}
82+
83+
public function getDataContentType(): ?string
84+
{
85+
return $this->datacontenttype;
86+
}
87+
88+
public function getDataSchema(): ?string
89+
{
90+
return $this->dataschema;
91+
}
92+
93+
public function getSubject(): ?string
94+
{
95+
return $this->subject;
96+
}
97+
98+
public function getTime(): ?string
99+
{
100+
return $this->time;
101+
}
102+
103+
/**
104+
* @return mixed
105+
*/
106+
public function getData()
107+
{
108+
return $this->data;
109+
}
110+
111+
public static function fromArray(array $arr)
112+
{
113+
$args = [];
114+
$argKeys = [
115+
'id',
116+
'source',
117+
'specversion',
118+
'type',
119+
'datacontenttype',
120+
'dataschema',
121+
'subject',
122+
'time',
123+
'data',
124+
];
125+
126+
foreach ($argKeys as $key) {
127+
$args[] = $arr[$key] ?? null;
128+
}
129+
130+
return new static(...$args);
131+
}
132+
133+
public function jsonSerialize()
134+
{
135+
return [
136+
'id' => $this->id,
137+
'source' => $this->source,
138+
'specversion' => $this->specversion,
139+
'type' => $this->type,
140+
'datacontenttype' => $this->datacontenttype,
141+
'dataschema' => $this->dataschema,
142+
'subject' => $this->subject,
143+
'time' => $this->time,
144+
'data' => $this->data,
145+
];
146+
}
147+
148+
public function __toString()
149+
{
150+
$output = implode("\n", [
151+
'CLOUDEVENT metadata:',
152+
"- id: $this->id",
153+
"- source: $this->source",
154+
"- specversion: $this->specversion",
155+
"- type: $this->type",
156+
"- datacontenttype: $this->datacontenttype",
157+
"- dataschema: $this->dataschema",
158+
"- subject: $this->subject",
159+
"- time: $this->time",
160+
]);
161+
162+
return $output;
163+
}
164+
}

google/Context.php

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* Copyright 2019 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\CloudFunctions;
19+
20+
class Context
21+
{
22+
private $eventId;
23+
private $timestamp;
24+
private $eventType;
25+
private $resource;
26+
27+
final public function __construct(
28+
?string $eventId,
29+
?string $timestamp,
30+
?string $eventType,
31+
?array $resource
32+
) {
33+
$this->eventId = $eventId;
34+
$this->timestamp = $timestamp;
35+
$this->eventType = $eventType;
36+
$this->resource = $resource;
37+
}
38+
39+
public function getEventId(): ?string
40+
{
41+
return $this->eventId;
42+
}
43+
44+
public function getEventType(): ?string
45+
{
46+
return $this->eventType;
47+
}
48+
49+
public function getTimestamp(): ?string
50+
{
51+
return $this->timestamp;
52+
}
53+
54+
public function getResource(): ?array
55+
{
56+
return $this->resource;
57+
}
58+
59+
public function getService(): ?string
60+
{
61+
return $this->resource['service'] ?? null;
62+
}
63+
64+
public function getResourceName(): ?string
65+
{
66+
return $this->resource['name'] ?? null;
67+
}
68+
69+
public static function fromArray(array $arr)
70+
{
71+
// When "resource" is defined in the root (instead of in "context") it
72+
// is a string representing the resource name
73+
if (isset($arr['resource']) && is_string($arr['resource'])) {
74+
$arr['resource'] = ['name' => $arr['resource']];
75+
}
76+
77+
$args = [];
78+
$argKeys = ['eventId', 'timestamp', 'eventType', 'resource'];
79+
foreach ($argKeys as $key) {
80+
$args[] = $arr[$key] ?? null;
81+
}
82+
83+
return new static(...$args);
84+
}
85+
}

0 commit comments

Comments
 (0)