Skip to content

Commit 34d55c3

Browse files
committed
Use blocking SQLite adapter when an empty binary path is used
1 parent 80e5d7e commit 34d55c3

File tree

3 files changed

+244
-131
lines changed

3 files changed

+244
-131
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,18 @@ environment than your parent process.
113113
$factory = new Clue\React\SQLite\Factory(null, '/usr/bin/php6.0');
114114
```
115115

116+
Or you may use this parameter to pass an empty PHP binary path which will
117+
cause this project to not spawn a PHP child process for any database
118+
interactions at all. In this case, using SQLite will block the main
119+
process, but continues to provide the exact same async API. This can be
120+
useful if concurrent execution is not needed, especially when running
121+
behind a traditional web server (non-CLI SAPI).
122+
123+
```php
124+
// advanced usage: empty binary path runs blocking SQLite in same process
125+
$factory = new Clue\React\SQLite\Factory(null, '');
126+
```
127+
116128
#### open()
117129

118130
The `open(string $filename, int $flags = null): PromiseInterface<DatabaseInterface>` method can be used to

src/Factory.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Clue\React\SQLite;
44

5+
use Clue\React\SQLite\Io\BlockingDatabase;
56
use Clue\React\SQLite\Io\LazyDatabase;
67
use Clue\React\SQLite\Io\ProcessIoDatabase;
78
use React\ChildProcess\Process;
@@ -46,6 +47,18 @@ class Factory
4647
* $factory = new Clue\React\SQLite\Factory(null, '/usr/bin/php6.0');
4748
* ```
4849
*
50+
* Or you may use this parameter to pass an empty PHP binary path which will
51+
* cause this project to not spawn a PHP child process for any database
52+
* interactions at all. In this case, using SQLite will block the main
53+
* process, but continues to provide the exact same async API. This can be
54+
* useful if concurrent execution is not needed, especially when running
55+
* behind a traditional web server (non-CLI SAPI).
56+
*
57+
* ```php
58+
* // advanced usage: empty binary path runs blocking SQLite in same process
59+
* $factory = new Clue\React\SQLite\Factory(null, '');
60+
* ```
61+
*
4962
* @param ?LoopInterface $loop
5063
* @param ?string $binary
5164
*/
@@ -109,6 +122,17 @@ public function __construct(LoopInterface $loop = null, $binary = null)
109122
public function open($filename, $flags = null)
110123
{
111124
$filename = $this->resolve($filename);
125+
126+
if ($this->bin === '') {
127+
try {
128+
return \React\Promise\resolve(new BlockingDatabase($filename, $flags));
129+
} catch (\Exception $e) {
130+
return \React\Promise\reject(new \RuntimeException($e->getMessage()) );
131+
} catch (\Error $e) {
132+
return \React\Promise\reject(new \RuntimeException($e->getMessage()));
133+
}
134+
}
135+
112136
return $this->useSocket ? $this->openSocketIo($filename, $flags) : $this->openProcessIo($filename, $flags);
113137
}
114138

0 commit comments

Comments
 (0)