Skip to content

Add ability to set worker file and cwd (Phar support) #32

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

Closed
wants to merge 3 commits into from
Closed
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
18 changes: 12 additions & 6 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Factory
private $loop;
private $bin = PHP_BINARY;
private $useSocket;
private $workerScript = 'sqlite-worker.php';
private $workerCwd;

/**
* The `Factory` is responsible for opening your [`DatabaseInterface`](#databaseinterface) instance.
Expand All @@ -25,10 +27,14 @@ class Factory
* ```
*
* @param LoopInterface $loop
* @param string $workerScript
* @param string $cwd
*/
public function __construct(LoopInterface $loop)
public function __construct(LoopInterface $loop, $workerScript = null, $cwd = null)
{
$this->loop = $loop;
$this->workerScript = $workerScript ?: $this->workerScript;
$this->workerCwd = $cwd ?: __DIR__ . '/../res';

// use socket I/O for Windows only, use faster process pipes everywhere else
$this->useSocket = DIRECTORY_SEPARATOR === '\\';
Expand Down Expand Up @@ -190,7 +196,7 @@ public function openLazy($filename, $flags = null, array $options = [])

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

// Try to get list of all open FDs (Linux/Mac and others)
$fds = @\scandir('/dev/fd');
Expand Down Expand Up @@ -232,7 +238,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, $this->workerCwd, null, $pipes);
$process->start($this->loop);

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

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

// launch process without default STDIO pipes, but inherit STDERR
$null = \DIRECTORY_SEPARATOR === '\\' ? 'nul' : '/dev/null';
Expand All @@ -273,7 +279,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, $this->workerCwd, null, $pipes);
$process->start($this->loop);

$deferred = new Deferred(function () use ($process, $server) {
Expand Down Expand Up @@ -348,7 +354,7 @@ private function which($bin)
*/
private function resolve($filename)
{
if ($filename !== '' && $filename !== ':memory:' && !\preg_match('/^\/|\w+\:\\\\/', $filename)) {
if ($filename !== '' && $filename !== ':memory:' && !\preg_match('/^\/|\w+:/', $filename)) {
$filename = \getcwd() . \DIRECTORY_SEPARATOR . $filename;
}
return $filename;
Expand Down