Skip to content

Commit eb38bb7

Browse files
authored
feat: support ops/closure v4 (#8559)
1 parent ecdcc10 commit eb38bb7

10 files changed

Lines changed: 94 additions & 16 deletions

File tree

Core/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"phpdocumentor/reflection": "^5.3.3||^6.0",
2222
"phpdocumentor/reflection-docblock": "^5.3",
2323
"erusev/parsedown": "^1.6",
24-
"opis/closure": "^3",
24+
"opis/closure": "^3.7|^4.0",
2525
"google/cloud-common-protos": "~0.5"
2626
},
2727
"suggest": {

Core/src/Batch/OpisClosureSerializer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* A closure serializer utilizing
2424
* [Opis Closure Library](https://github.com/opis/closure).
2525
*
26+
* @deprecated use OpisClosureSerializerV4
2627
* @experimental The experimental flag means that while we believe this method
2728
* or class is ready for use, it may change before release in backwards-
2829
* incompatible ways. Please use with caution, and test thoroughly when
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Copyright 2018 Google Inc. All Rights Reserved.
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\Cloud\Core\Batch;
19+
20+
use Opis\Closure;
21+
22+
/**
23+
* A closure serializer utilizing
24+
* [Opis Closure Library](https://github.com/opis/closure).
25+
*
26+
* @experimental The experimental flag means that while we believe this method
27+
* or class is ready for use, it may change before release in backwards-
28+
* incompatible ways. Please use with caution, and test thoroughly when
29+
* upgrading.
30+
*/
31+
class OpisClosureSerializerV4 implements ClosureSerializerInterface
32+
{
33+
/**
34+
* Recursively serializes closures.
35+
*
36+
* @param mixed $data
37+
*/
38+
public function wrapClosures(&$data)
39+
{
40+
$data = Closure\serialize($data);
41+
}
42+
43+
/**
44+
* Recursively unserializes closures.
45+
*
46+
* @param mixed $data
47+
*/
48+
public function unwrapClosures(&$data)
49+
{
50+
$data = Closure\unserialize($data);
51+
}
52+
}

Core/src/Batch/SerializableClientTrait.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ private function getUnwrappedClientConfig()
9696
*/
9797
private function getDefaultClosureSerializer()
9898
{
99+
if (function_exists('Opis\Closure\serialize')) {
100+
return new OpisClosureSerializerV4();
101+
}
99102
if (class_exists(SerializableClosure::class)) {
100103
return new OpisClosureSerializer();
101104
}

Core/tests/Unit/Batch/OpisClosureSerializerTest.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace Google\Cloud\Core\Tests\Unit\Batch;
1919

2020
use Google\Cloud\Core\Batch\OpisClosureSerializer;
21+
use Google\Cloud\Core\Batch\OpisClosureSerializerV4;
2122
use Opis\Closure\SerializableClosure;
2223
use PHPUnit\Framework\TestCase;
2324

@@ -27,25 +28,40 @@
2728
*/
2829
class OpisClosureSerializerTest extends TestCase
2930
{
30-
private $serialzer;
31-
32-
public function setUp(): void
31+
public function testWrapAndUnwrapClosures()
3332
{
34-
$this->serializer = new OpisClosureSerializer();
33+
if (!method_exists(SerializableClosure::class, 'enterContext')) {
34+
$this->markTestSkipped('Requires ops/serializer:v3');
35+
}
36+
37+
$data['closure'] = function () {
38+
return true;
39+
};
40+
41+
$serializer = new OpisClosureSerializer();
42+
43+
$serializer->wrapClosures($data);
44+
$this->assertInstanceOf(SerializableClosure::class, $data['closure']);
45+
46+
$serializer->unwrapClosures($data);
47+
$this->assertTrue($data['closure']());
3548
}
3649

37-
public function testWrapAndUnwrapClosures()
50+
public function testWrapAndUnwrapClosuresV4()
3851
{
52+
if (!function_exists('Opis\Closure\serialize')) {
53+
$this->markTestSkipped('Requires ops/serializer:v3');
54+
}
55+
3956
$data['closure'] = function () {
4057
return true;
4158
};
4259

43-
$this->serializer
44-
->wrapClosures($data);
60+
$serializer = new OpisClosureSerializerV4();
61+
$serializer->wrapClosures($data);
62+
$this->assertIsString($data);
4563

46-
$this->assertInstanceOf(SerializableClosure::class, $data['closure']);
47-
$this->serializer
48-
->unwrapClosures($data);
64+
$serializer->unwrapClosures($data);
4965
$this->assertTrue($data['closure']());
5066
}
5167
}

ErrorReporting/tests/Unit/BootstrapTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ public function testExceptionHandler($exception)
141141
/**
142142
* @dataProvider exceptionProvider
143143
* @runInSeparateProcess
144+
* @preserveGlobalState disabled
144145
*/
145146
public function testExceptionHandlerWithHttpContext($exception)
146147
{

Logging/tests/Unit/LoggingClientTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
use Google\Cloud\Core\Batch\BatchRunner;
2121
use Google\Cloud\Core\Batch\OpisClosureSerializer;
22+
use Google\Cloud\Core\Batch\OpisClosureSerializerV4;
2223
use Google\Cloud\Core\Report\EmptyMetadataProvider;
2324
use Google\Cloud\Core\Testing\GrpcTestTrait;
2425
use Google\Cloud\Core\Testing\TestHelpers;
@@ -313,6 +314,7 @@ public function testGetsPsrLogger()
313314

314315
public function testOptionsArePassedToPsrLogger()
315316
{
317+
$opisV4 = class_exists(OpisClosureSerializerV4::class);
316318
$options = [
317319
'metadataProvider' => new EmptyMetadataProvider,
318320
'batchEnabled' => true,
@@ -326,7 +328,7 @@ public function testOptionsArePassedToPsrLogger()
326328
'projectId' => 'test'
327329
],
328330
'batchRunner' => new BatchRunner,
329-
'closureSerializer' => new OpisClosureSerializer,
331+
'closureSerializer' => $opisV4 ? new OpisClosureSerializerV4 : new OpisClosureSerializer,
330332
'debugOutputResource' => fopen('php://temp', 'wb')
331333
];
332334

@@ -335,9 +337,8 @@ public function testOptionsArePassedToPsrLogger()
335337
$reflection = new \ReflectionClass($psrLogger);
336338
foreach ($options as $name => $value) {
337339
$attr = $reflection->getProperty($name);
338-
$attr->setAccessible(true);
339340
$this->assertEquals(
340-
$value,
341+
$name === 'clientConfig' && $opisV4 ? serialize($value) : $value,
341342
$attr->getValue($psrLogger),
342343
"$name assertion failed."
343344
);

Logging/tests/Unit/PsrLoggerTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace Google\Cloud\Logging\Tests\Unit;
1919

20+
use Google\Cloud\Core\Batch\OpisClosureSerializerV4;
2021
use Google\Cloud\Core\Report\EmptyMetadataProvider;
2122
use Google\Cloud\Logging\Logger;
2223
use Google\Cloud\Logging\PsrLogger;
@@ -267,7 +268,9 @@ public function testSerializesCorrectly()
267268
$attr->setAccessible(true);
268269
$this->assertEquals(
269270
$attr->getValue($psrLogger),
270-
$options[$attributeName]
271+
$attributeName === 'clientConfig'&& class_exists(OpisClosureSerializerV4::class)
272+
? serialize($options[$attributeName])
273+
: $options[$attributeName]
271274
);
272275
}
273276
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
"erusev/parsedown": "^1.6",
7373
"phpseclib/phpseclib": "^3.0",
7474
"google/cloud-tools": "^0.16.0",
75-
"opis/closure": "^3.0",
75+
"opis/closure": "^3.7||^4.0",
7676
"flix-tech/avro-php": "^5.0.0",
7777
"phpspec/prophecy-phpunit": "^2.1",
7878
"kreait/firebase-php": "^6.9",

phpstan.neon.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ parameters:
66
# Ignore deprecated classes
77
- Core/src/Lock/SymfonyLockAdapter.php
88
- Core/src/PhpArray.php
9+
- Core/src/Batch/OpisClosureSerializer.php
910
# Ignore Monolog3 for now, as these tests run using Monolog2
1011
- Logging/src/LogMessageProcessor/MonologV3MessageProcessor.php
1112
- Logging/src/LogMessageProcessor/MonologV3MessageProcessor.php

0 commit comments

Comments
 (0)