Skip to content

Commit 772c3bb

Browse files
Ensure client can be included multiple times (#112)
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 6743f97 commit 772c3bb

14 files changed

+142
-38
lines changed

agent/build/agent.phar

50 Bytes
Binary file not shown.

agent/build/signature.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7ED0671E0BC3D3BC20837BE227703B6BC3E44074F55B2DC4272CEC3E27F85F6A717C348F8EFFED9BDC60B800981607F11C3C9E7CD212B276CD1B44073DA19FB5
1+
3E3D0C08BC6FCCF589A14C1C7E511DE77511531765C98E5C4A59490423C635FFE149E3B9071EF49D01D87BAB652685F77574C55F9A6684EE0759D59BD83ABE0E

agent/composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"allow-plugins": {
4949
"pestphp/pest-plugin": true
5050
},
51-
"autoloader-suffix": "AppChecksum"
51+
"autoloader-suffix": "zUjV6thn8dELeOw6"
5252
},
5353
"minimum-stability": "stable",
5454
"prefer-stable": true

client/build/client.phar

315 Bytes
Binary file not shown.

client/build/signature.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
C4C1117363E561968FD49DB562F5BE87665BCF3CA381D76F91F3AC7F586AFCB73D11D09CF32E405E93C6F8175144A8D1808F0EFE885280E5C55F7DDF157AAF56
1+
B53311EA72F09FFB7975024C4941F38D47FC0E85523D00479457711092912C42FF31A92A27944719E49CEBC5F14504402CD103C0253325D6EBEC9F9C7A13B895

client/composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"allow-plugins": {
4545
"pestphp/pest-plugin": true
4646
},
47-
"autoloader-suffix": "AppChecksum"
47+
"autoloader-suffix": "XxO1QeBIKrkR2oWT"
4848
},
4949
"minimum-stability": "stable",
5050
"prefer-stable": true

client/entry.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Laravel\NightwatchClient;
4+
5+
use function call_user_func;
6+
use function class_exists;
7+
8+
return call_user_func(static function () {
9+
if (! class_exists(Cache::class, autoload: false)) {
10+
class Cache
11+
{
12+
/**
13+
* @var IngestFactory
14+
*/
15+
public static $ingestFactory;
16+
}
17+
18+
/** @var IngestFactory $ingestFactory */
19+
require __DIR__.'/build/client.phar';
20+
21+
Cache::$ingestFactory = $ingestFactory;
22+
}
23+
24+
return Cache::$ingestFactory;
25+
});

client/scoper.inc.php

+13
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,17 @@
22

33
return [
44
'prefix' => 'NightwatchClient_kden27khxA4QoEfj',
5+
'patchers' => [
6+
// There are situations when this file is manually required twice by
7+
// Box. This has been seen mostly during installation scripts when the
8+
// agent is not running and an exception is thrown. We ensure the file
9+
// is only ever required once to fix this issue manually.
10+
static function (string $filePath, string $prefix, string $content): string {
11+
if ($filePath === 'vendor/composer/InstalledVersions.php') {
12+
$content = str_replace('class InstalledVersions', "\nif (class_exists(InstalledVersions::class, autoload: false)) { return; }\n\nclass InstalledVersions", $content);
13+
}
14+
15+
return $content;
16+
},
17+
],
518
];

client/src/Ingest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function __construct(
1515
//
1616
}
1717

18-
public function write(string $payload): void
18+
public function __invoke(string $payload): void
1919
{
2020
if ($payload !== '') {
2121
await($this->connector->connect($this->transmitTo))->end($payload);

client/src/client.php

+13-28
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,18 @@
22

33
namespace Laravel\NightwatchClient;
44

5+
require __DIR__.'/../vendor/react/async/src/functions_include.php';
6+
require __DIR__.'/../vendor/react/promise/src/functions_include.php';
57
require __DIR__.'/../vendor/autoload.php';
68

7-
/*
8-
* Input...
9-
*/
10-
11-
/** @var ?string $payload */
12-
$payload ??= '';
13-
/** @var ?string $transmitTo */
14-
$transmitTo ??= '127.0.0.1:2407';
15-
/** @var ?float $ingestTimeout */
16-
$ingestTimeout ??= 0.5;
17-
/** @var ?float $ingestConnectionTimeout */
18-
$ingestConnectionTimeout ??= 0.5;
19-
20-
/*
21-
* Initialize services...
22-
*/
23-
24-
$ingest = (new IngestFactory)(
25-
transmitTo: $transmitTo,
26-
ingestTimeout: $ingestTimeout,
27-
ingestConnectionTimeout: $ingestConnectionTimeout,
28-
);
29-
30-
/*
31-
* Get things rolling...
32-
*/
33-
34-
$ingest->write($payload);
9+
$ingestFactory = static function (
10+
?string $transmitTo = null,
11+
?float $ingestTimeout = null,
12+
?float $ingestConnectionTimeout = null,
13+
): Ingest {
14+
return (new IngestFactory)(
15+
transmitTo: $transmitTo ?? '127.0.0.1:2407',
16+
ingestTimeout: $ingestTimeout ?? 0.5,
17+
ingestConnectionTimeout: $ingestConnectionTimeout ?? 0.5,
18+
);
19+
};

client/tests/Unit/EntryPointTest.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Symfony\Component\Process\Process;
4+
5+
test('entry point can be accessed and ingest resolved multiple times', function () {
6+
$path = __DIR__.'/fixtures/include_entry_point_multiple_times.php';
7+
$process = Process::fromShellCommandline("php -f {$path}");
8+
$process->setTimeout(10);
9+
10+
$process->mustRun();
11+
12+
expect($process->getOutput())->toBe(<<<OUTPUT
13+
Closure
14+
NightwatchClient_kden27khxA4QoEfj\\Laravel\\NightwatchClient\Ingest
15+
Closure
16+
NightwatchClient_kden27khxA4QoEfj\\Laravel\\NightwatchClient\Ingest
17+
Closure
18+
NightwatchClient_kden27khxA4QoEfj\\Laravel\\NightwatchClient\Ingest
19+
OUTPUT);
20+
});
21+
22+
test('variables do not leak into scope', function () {
23+
$path = __DIR__.'/fixtures/check_for_leaking_variables.php';
24+
$process = Process::fromShellCommandline("php -f {$path}");
25+
$process->setTimeout(10);
26+
27+
$process->mustRun();
28+
29+
expect($process->getOutput())->toBe(<<<'OUTPUT'
30+
0
31+
0
32+
OUTPUT);
33+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
// Wrapped in a closure to simulate a local scoped block so global variables are not
4+
// counted
5+
call_user_func(static function () {
6+
echo count(get_defined_vars());
7+
echo PHP_EOL;
8+
9+
require __DIR__.'./../../../entry.php';
10+
11+
echo count(get_defined_vars());
12+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
$factory = null;
4+
$factory = require __DIR__.'./../../../entry.php';
5+
echo $factory::class;
6+
echo PHP_EOL;
7+
$ingest = $factory();
8+
echo $ingest::class;
9+
echo PHP_EOL;
10+
11+
$factory = null;
12+
$factory = require __DIR__.'./../../../entry.php';
13+
echo $factory::class;
14+
echo PHP_EOL;
15+
$ingest = $factory();
16+
echo $ingest::class;
17+
echo PHP_EOL;
18+
19+
$factory = null;
20+
$factory = require __DIR__.'./../../../entry.php';
21+
echo $factory::class;
22+
echo PHP_EOL;
23+
$ingest = $factory();
24+
echo $ingest::class;

src/Ingest.php

+17-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@
44

55
use Laravel\Nightwatch\Contracts\LocalIngest;
66

7+
use function call_user_func;
8+
79
/**
810
* @internal
911
*/
1012
final class Ingest implements LocalIngest
1113
{
14+
/**
15+
* @var (callable(string): void)|null
16+
*/
17+
private $ingest = null;
18+
1219
public function __construct(
1320
private ?string $transmitTo,
1421
private ?float $ingestTimeout,
@@ -19,12 +26,17 @@ public function __construct(
1926

2027
public function write(string $payload): void
2128
{
22-
$transmitTo = $this->transmitTo;
23-
24-
$ingestTimeout = $this->ingestTimeout;
29+
if ($this->ingest === null) {
30+
/** @var (callable(string|null $transmitTo, float|null $ingestTimeout, float|null $ingestConnectionTimeout): (callable(string $payload): void)) */
31+
$factory = require __DIR__.'/../client/entry.php';
2532

26-
$ingestConnectionTimeout = $this->ingestConnectionTimeout;
33+
$this->ingest = $factory(
34+
$this->transmitTo,
35+
$this->ingestTimeout,
36+
$this->ingestConnectionTimeout,
37+
);
38+
}
2739

28-
require __DIR__.'/../client/build/client.phar';
40+
call_user_func($this->ingest, $payload);
2941
}
3042
}

0 commit comments

Comments
 (0)