Skip to content

Commit 91b8736

Browse files
authored
Merge pull request #55 from clue-labs/phar
Support running from PHAR
2 parents 8d7ecd0 + c0da996 commit 91b8736

File tree

7 files changed

+79
-4
lines changed

7 files changed

+79
-4
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,9 @@ jobs:
3535
if: ${{ matrix.php >= 7.3 }}
3636
- run: vendor/bin/phpunit --coverage-text -c phpunit.xml.legacy
3737
if: ${{ matrix.php < 7.3 }}
38+
- 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
40+
continue-on-error: ${{ matrix.os == 'windows-2019' }}
41+
id: phar
42+
- run: php tests/install-as-dep/query.phar
43+
if: ${{ steps.phar.outcome == 'success' }}

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<testsuites>
1010
<testsuite name="SQLite React Test Suite">
1111
<directory>./tests/</directory>
12+
<exclude>./tests/install-as-dep/</exclude>
1213
</testsuite>
1314
</testsuites>
1415
<coverage>

phpunit.xml.legacy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<testsuites>
99
<testsuite name="SQLite React Test Suite">
1010
<directory>./tests/</directory>
11+
<exclude>./tests/install-as-dep/</exclude>
1112
</testsuite>
1213
</testsuites>
1314
<filter>

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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/composer.lock
2+
/query.phar
3+
/vendor/

tests/install-as-dep/composer.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"require": {
3+
"clue/reactphp-sqlite": "*@dev"
4+
},
5+
"require-dev": {
6+
"clue/phar-composer": "^1.0"
7+
},
8+
"bin": [
9+
"query.php"
10+
],
11+
"repositories": [
12+
{
13+
"type": "path",
14+
"url": "../../",
15+
"options": {
16+
"symlink": false
17+
}
18+
}
19+
]
20+
}

tests/install-as-dep/query.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
4+
require __DIR__ . '/vendor/autoload.php';
5+
} else {
6+
require __DIR__ . '/../../vendor/autoload.php';
7+
}
8+
9+
$factory = new Clue\React\SQLite\Factory();
10+
$db = $factory->openLazy(':memory:', null, ['idle' => 0.001]);
11+
12+
$query = isset($argv[1]) ? $argv[1] : 'SELECT 42 AS value';
13+
$args = array_slice(isset($argv) ? $argv : [], 2);
14+
15+
$db->query('SELECT ? AS answer', [42])->then(function (Clue\React\SQLite\Result $result) {
16+
if ($result->columns !== ['answer'] || count($result->rows) !== 1 || $result->rows[0]['answer'] !== 42) {
17+
var_dump($result);
18+
throw new RuntimeException('Unexpected result');
19+
}
20+
21+
$answer = $result->rows[0]['answer'];
22+
echo 'Answer: ' . $answer . PHP_EOL;
23+
})->then(null, function (Exception $e) {
24+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
25+
exit(1);
26+
});

0 commit comments

Comments
 (0)