Skip to content

Commit c872ae5

Browse files
Add configurable opcache clearing and static file serving options (#1035)
* Add configurable opcache clearing and static file serving - Add octane.swoole.clear_opcache configuration option (defaults to true) - Add octane.serve_static_files configuration option (defaults to true) - Maintain backward compatibility with existing behavior - Add comprehensive unit tests for both features These configurations provide better control over Octane's behavior in different deployment scenarios, particularly useful for Docker environments and API-only applications where static file handling may not be required. * Fix StyleCI issues - Remove extra blank lines - Fix spacing around operators (! operator) - Maintain consistent code style * Fix missing newline at end of file * Remove trailing empty line * Update OnWorkerStart.php * Update SwooleClient.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 3840152 commit c872ae5

File tree

4 files changed

+90
-1
lines changed

4 files changed

+90
-1
lines changed

src/Swoole/Handlers/OnWorkerStart.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ public function __construct(
3030
*/
3131
public function __invoke($server, int $workerId)
3232
{
33-
$this->clearOpcodeCache();
33+
if ($this->shouldClearOpcodeCache()) {
34+
$this->clearOpcodeCache();
35+
}
3436

3537
$this->workerState->server = $server;
3638
$this->workerState->workerId = $workerId;
@@ -107,6 +109,16 @@ protected function streamRequestsToConsole($server)
107109
});
108110
}
109111

112+
/**
113+
* Determine if the opcode cache should be cleared.
114+
*
115+
* @return bool
116+
*/
117+
protected function shouldClearOpcodeCache()
118+
{
119+
return config('octane.swoole.clear_opcache', true);
120+
}
121+
110122
/**
111123
* Clear the APCu and Opcache caches.
112124
*

src/Swoole/SwooleClient.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ public function marshalRequest(RequestContext $context): array
5050
*/
5151
public function canServeRequestAsStaticFile(Request $request, RequestContext $context): bool
5252
{
53+
$octaneConfig = $context->octaneConfig ?? [];
54+
55+
if (array_key_exists('serve_static_files', $octaneConfig) &&
56+
! $octaneConfig['serve_static_files']) {
57+
return false;
58+
}
59+
5360
if (! ($context->publicPath ?? false) ||
5461
$request->path() === '/') {
5562
return false;

tests/OnWorkerStartTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Laravel\Octane\Tests;
4+
5+
use Laravel\Octane\Swoole\Handlers\OnWorkerStart;
6+
use Mockery;
7+
8+
class OnWorkerStartTest extends TestCase
9+
{
10+
public function test_should_clear_opcache_returns_true_by_default(): void
11+
{
12+
$this->createApplication();
13+
14+
// Mock the OnWorkerStart handler to test just the shouldClearOpcodeCache method
15+
$handler = Mockery::mock(OnWorkerStart::class)->makePartial();
16+
17+
$reflection = new \ReflectionClass($handler);
18+
$method = $reflection->getMethod('shouldClearOpcodeCache');
19+
$method->setAccessible(true);
20+
21+
$this->assertTrue($method->invoke($handler));
22+
}
23+
24+
public function test_should_clear_opcache_returns_configured_value(): void
25+
{
26+
$app = $this->createApplication();
27+
$app['config']['octane.swoole.clear_opcache'] = false;
28+
29+
// Mock the OnWorkerStart handler to test just the shouldClearOpcodeCache method
30+
$handler = Mockery::mock(OnWorkerStart::class)->makePartial();
31+
32+
$reflection = new \ReflectionClass($handler);
33+
$method = $reflection->getMethod('shouldClearOpcodeCache');
34+
$method->setAccessible(true);
35+
36+
$this->assertFalse($method->invoke($handler));
37+
}
38+
}

tests/SwooleClientTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,38 @@ public function test_can_serve_static_files_if_configured_to_and_file_is_within_
7373
$this->assertTrue($client->canServeRequestAsStaticFile($request, $context));
7474
}
7575

76+
public function test_can_serve_static_files_when_explicitly_enabled(): void
77+
{
78+
$client = new SwooleClient;
79+
80+
$request = Request::create('/foo.txt', 'GET');
81+
82+
$context = new RequestContext([
83+
'publicPath' => __DIR__.'/public',
84+
'octaneConfig' => [
85+
'serve_static_files' => true,
86+
],
87+
]);
88+
89+
$this->assertTrue($client->canServeRequestAsStaticFile($request, $context));
90+
}
91+
92+
public function test_cant_serve_static_files_when_disabled(): void
93+
{
94+
$client = new SwooleClient;
95+
96+
$request = Request::create('/foo.txt', 'GET');
97+
98+
$context = new RequestContext([
99+
'publicPath' => __DIR__.'/public',
100+
'octaneConfig' => [
101+
'serve_static_files' => false,
102+
],
103+
]);
104+
105+
$this->assertFalse($client->canServeRequestAsStaticFile($request, $context));
106+
}
107+
76108
public function test_cant_serve_static_files_if_file_is_outside_public_directory(): void
77109
{
78110
$client = new SwooleClient;

0 commit comments

Comments
 (0)