Skip to content

Commit 1b3a564

Browse files
committed
Support running from PHAR
1 parent 4ed056c commit 1b3a564

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ jobs:
3636
- run: vendor/bin/phpunit --coverage-text -c phpunit.xml.legacy
3737
if: ${{ matrix.php < 7.3 }}
3838
- run: cd tests/install-as-dep && composer install && php query.php
39+
- run: cd tests/install-as-dep && php -d phar.readonly=0 vendor/bin/phar-composer build . query.phar && php query.phar

src/Factory.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,16 @@ public function openLazy($filename, $flags = null, array $options = [])
226226

227227
private function openProcessIo($filename, $flags = null)
228228
{
229-
$command = 'exec ' . \escapeshellarg($this->bin) . ' sqlite-worker.php';
229+
$cwd = null;
230+
$worker = \dirname(__DIR__) . '/res/sqlite-worker.php';
231+
232+
if (\class_exists('Phar', false) && \Phar::running(false) !== '') {
233+
$worker = '-r' . 'require(' . \var_export($worker, true) . ');'; // @codeCoverageIgnore
234+
} else {
235+
$cwd = __DIR__ . '/../res';
236+
$worker = \basename($worker);
237+
}
238+
$command = 'exec ' . \escapeshellarg($this->bin) . ' ' . escapeshellarg($worker);
230239

231240
// Try to get list of all open FDs (Linux/Mac and others)
232241
$fds = @\scandir('/dev/fd');
@@ -269,7 +278,7 @@ private function openProcessIo($filename, $flags = null)
269278
$command = 'exec bash -c ' . \escapeshellarg($command);
270279
}
271280

272-
$process = new Process($command, __DIR__ . '/../res', null, $pipes);
281+
$process = new Process($command, $cwd, null, $pipes);
273282
$process->start($this->loop);
274283

275284
$db = new ProcessIoDatabase($process);
@@ -285,7 +294,16 @@ private function openProcessIo($filename, $flags = null)
285294

286295
private function openSocketIo($filename, $flags = null)
287296
{
288-
$command = \escapeshellarg($this->bin) . ' sqlite-worker.php';
297+
$cwd = null;
298+
$worker = \dirname(__DIR__) . '/res/sqlite-worker.php';
299+
300+
if (\class_exists('Phar', false) && \Phar::running(false) !== '') {
301+
$worker = '-r' . 'require(' . \var_export($worker, true) . ');'; // @codeCoverageIgnore
302+
} else {
303+
$cwd = __DIR__ . '/../res';
304+
$worker = \basename($worker);
305+
}
306+
$command = \escapeshellarg($this->bin) . ' ' . escapeshellarg($worker);
289307

290308
// launch process without default STDIO pipes, but inherit STDERR
291309
$null = \DIRECTORY_SEPARATOR === '\\' ? 'nul' : '/dev/null';
@@ -307,7 +325,7 @@ private function openSocketIo($filename, $flags = null)
307325
stream_set_blocking($server, false);
308326
$command .= ' ' . stream_socket_get_name($server, false);
309327

310-
$process = new Process($command, __DIR__ . '/../res', null, $pipes);
328+
$process = new Process($command, $cwd, null, $pipes);
311329
$process->start($this->loop);
312330

313331
$deferred = new Deferred(function () use ($process, $server) {

tests/install-as-dep/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/composer.lock
2+
/query.phar
23
/vendor/

tests/install-as-dep/composer.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,19 @@
22
"require": {
33
"clue/reactphp-sqlite": "*@dev"
44
},
5+
"require-dev": {
6+
"clue/phar-composer": "^1.0"
7+
},
8+
"bin": [
9+
"query.php"
10+
],
511
"repositories": [
612
{
713
"type": "path",
8-
"url": "../../"
14+
"url": "../../",
15+
"options": {
16+
"symlink": false
17+
}
918
}
1019
]
1120
}

0 commit comments

Comments
 (0)