Skip to content

Simplify usage by supporting new default loop #39

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

Merged
merged 1 commit into from
Jul 23, 2021
Merged
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
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -64,8 +64,7 @@ existing SQLite database file (or automatically create it on first run) and then
`INSERT` a new record to the database:

```php
$loop = React\EventLoop\Factory::create();
$factory = new Clue\React\SQLite\Factory($loop);
$factory = new Clue\React\SQLite\Factory();

$db = $factory->openLazy('users.db');
$db->exec('CREATE TABLE IF NOT EXISTS foo (id INTEGER PRIMARY KEY AUTOINCREMENT, bar STRING)');
@@ -78,8 +77,6 @@ $db->query('INSERT INTO foo (bar) VALUES (?)', [$name])->then(
);

$db->quit();

$loop->run();
```

See also the [examples](examples).
@@ -89,13 +86,17 @@ See also the [examples](examples).
### Factory

The `Factory` is responsible for opening your [`DatabaseInterface`](#databaseinterface) instance.
It also registers everything with the main [`EventLoop`](https://github.com/reactphp/event-loop#usage).

```php
$loop = React\EventLoop\Factory::create();
$factory = new Clue\React\SQLite\Factory($loop);
$factory = new Clue\React\SQLite\Factory();
```

This class takes an optional `LoopInterface|null $loop` parameter that can be used to
pass the event loop instance to use for this object. You can use a `null` value
here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
This value SHOULD NOT be given unless you're sure you want to explicitly use a
given event loop instance.

#### open()

The `open(string $filename, int $flags = null): PromiseInterface<DatabaseInterface>` method can be used to
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -10,21 +10,21 @@
"email": "christian@clue.engineering"
}
],
"autoload": {
"psr-4": { "Clue\\React\\SQLite\\": "src/" }
},
"autoload-dev": {
"psr-4": { "Clue\\Tests\\React\\SQLite\\": "tests/" }
},
"require": {
"php": ">=5.4",
"ext-sqlite3": "*",
"clue/ndjson-react": "^1.0",
"react/child-process": "^0.6",
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
"react/event-loop": "^1.2",
"react/promise": "^2.7 || ^1.2.1"
},
"require-dev": {
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35"
},
"autoload": {
"psr-4": { "Clue\\React\\SQLite\\": "src/" }
},
"autoload-dev": {
"psr-4": { "Clue\\Tests\\React\\SQLite\\": "tests/" }
}
}
5 changes: 1 addition & 4 deletions examples/insert.php
Original file line number Diff line number Diff line change
@@ -5,8 +5,7 @@

require __DIR__ . '/../vendor/autoload.php';

$loop = React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();

$n = isset($argv[1]) ? $argv[1] : 1;
$db = $factory->openLazy('test.db');
@@ -21,5 +20,3 @@
}

$db->quit();

$loop->run();
5 changes: 1 addition & 4 deletions examples/search.php
Original file line number Diff line number Diff line change
@@ -6,8 +6,7 @@

require __DIR__ . '/../vendor/autoload.php';

$loop = React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();

$search = isset($argv[1]) ? $argv[1] : 'foo';
$db = $factory->openLazy('test.db');
@@ -20,5 +19,3 @@
}
}, 'printf');
$db->quit();

$loop->run();
19 changes: 13 additions & 6 deletions src/Factory.php
Original file line number Diff line number Diff line change
@@ -5,30 +5,37 @@
use Clue\React\SQLite\Io\LazyDatabase;
use Clue\React\SQLite\Io\ProcessIoDatabase;
use React\ChildProcess\Process;
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use React\Promise\Deferred;
use React\Stream\DuplexResourceStream;

class Factory
{
/** @var LoopInterface */
private $loop;

private $bin = PHP_BINARY;
private $useSocket;

/**
* The `Factory` is responsible for opening your [`DatabaseInterface`](#databaseinterface) instance.
* It also registers everything with the main [`EventLoop`](https://github.com/reactphp/event-loop#usage).
*
* ```php
* $loop = \React\EventLoop\Factory::create();
* $factory = new Factory($loop);
* $factory = new Clue\React\SQLite\Factory();
* ```
*
* @param LoopInterface $loop
* This class takes an optional `LoopInterface|null $loop` parameter that can be used to
* pass the event loop instance to use for this object. You can use a `null` value
* here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
* This value SHOULD NOT be given unless you're sure you want to explicitly use a
* given event loop instance.
*
* @param ?LoopInterface $loop
*/
public function __construct(LoopInterface $loop)
public function __construct(LoopInterface $loop = null)
{
$this->loop = $loop;
$this->loop = $loop ?: Loop::get();

// use socket I/O for Windows only, use faster process pipes everywhere else
$this->useSocket = DIRECTORY_SEPARATOR === '\\';