Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support running from PHAR #55

Merged
merged 3 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ jobs:
if: ${{ matrix.php >= 7.3 }}
- run: vendor/bin/phpunit --coverage-text -c phpunit.xml.legacy
if: ${{ matrix.php < 7.3 }}
- run: cd tests/install-as-dep && composer install && php query.php
- run: cd tests/install-as-dep && php -d phar.readonly=0 vendor/bin/phar-composer build . query.phar
continue-on-error: ${{ matrix.os == 'windows-2019' }}
id: phar
- run: php tests/install-as-dep/query.phar
if: ${{ steps.phar.outcome == 'success' }}
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<testsuites>
<testsuite name="SQLite React Test Suite">
<directory>./tests/</directory>
<exclude>./tests/install-as-dep/</exclude>
</testsuite>
</testsuites>
<coverage>
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.legacy
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<testsuites>
<testsuite name="SQLite React Test Suite">
<directory>./tests/</directory>
<exclude>./tests/install-as-dep/</exclude>
</testsuite>
</testsuites>
<filter>
Expand Down
26 changes: 22 additions & 4 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,16 @@ public function openLazy($filename, $flags = null, array $options = [])

private function openProcessIo($filename, $flags = null)
{
$command = 'exec ' . \escapeshellarg($this->bin) . ' sqlite-worker.php';
$cwd = null;
$worker = \dirname(__DIR__) . '/res/sqlite-worker.php';

if (\class_exists('Phar', false) && \Phar::running(false) !== '') {
$worker = '-r' . 'require(' . \var_export($worker, true) . ');'; // @codeCoverageIgnore
} else {
$cwd = __DIR__ . '/../res';
$worker = \basename($worker);
}
$command = 'exec ' . \escapeshellarg($this->bin) . ' ' . escapeshellarg($worker);

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

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

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

private function openSocketIo($filename, $flags = null)
{
$command = \escapeshellarg($this->bin) . ' sqlite-worker.php';
$cwd = null;
$worker = \dirname(__DIR__) . '/res/sqlite-worker.php';

if (\class_exists('Phar', false) && \Phar::running(false) !== '') {
$worker = '-r' . 'require(' . \var_export($worker, true) . ');'; // @codeCoverageIgnore
} else {
$cwd = __DIR__ . '/../res';
$worker = \basename($worker);
}
$command = \escapeshellarg($this->bin) . ' ' . escapeshellarg($worker);

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

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

$deferred = new Deferred(function () use ($process, $server) {
Expand Down
3 changes: 3 additions & 0 deletions tests/install-as-dep/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/composer.lock
/query.phar
/vendor/
20 changes: 20 additions & 0 deletions tests/install-as-dep/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"require": {
"clue/reactphp-sqlite": "*@dev"
},
"require-dev": {
"clue/phar-composer": "^1.0"
},
"bin": [
"query.php"
],
"repositories": [
{
"type": "path",
"url": "../../",
"options": {
"symlink": false
}
}
]
}
26 changes: 26 additions & 0 deletions tests/install-as-dep/query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

if (file_exists(__DIR__ . '/vendor/autoload.php')) {
require __DIR__ . '/vendor/autoload.php';
} else {
require __DIR__ . '/../../vendor/autoload.php';
}

$factory = new Clue\React\SQLite\Factory();
$db = $factory->openLazy(':memory:', null, ['idle' => 0.001]);

$query = isset($argv[1]) ? $argv[1] : 'SELECT 42 AS value';
$args = array_slice(isset($argv) ? $argv : [], 2);

$db->query('SELECT ? AS answer', [42])->then(function (Clue\React\SQLite\Result $result) {
if ($result->columns !== ['answer'] || count($result->rows) !== 1 || $result->rows[0]['answer'] !== 42) {
var_dump($result);
throw new RuntimeException('Unexpected result');
}

$answer = $result->rows[0]['answer'];
echo 'Answer: ' . $answer . PHP_EOL;
})->then(null, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
exit(1);
});