Skip to content

Commit

Permalink
Merge pull request #55 from clue-labs/phar
Browse files Browse the repository at this point in the history
Support running from PHAR
  • Loading branch information
SimonFrings authored Jan 11, 2022
2 parents 8d7ecd0 + c0da996 commit 91b8736
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 4 deletions.
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);
});

0 comments on commit 91b8736

Please sign in to comment.