check php extensions when loading libraries

This commit is contained in:
Pekka Laiho 2020-12-10 19:51:49 +07:00
parent 167711c4f0
commit 882bbe8405
2 changed files with 26 additions and 17 deletions

View File

@ -67,21 +67,22 @@ class IO implements ILib
)); ));
// Readline support // Readline support
if (extension_loaded('readline')) {
$env->set('readline', new CoreFunc('readline', 'Read line of user input.', 0, 1,
fn ($prompt = null) => readline($prompt)
));
$env->set('readline', new CoreFunc('readline', 'Read line of user input.', 0, 1, $env->set('readline-add', new CoreFunc('readline-add', 'Add new line of input to history.', 1, 1,
fn ($prompt = null) => readline($prompt) fn (string $line) => readline_add_history($line)
)); ));
$env->set('readline-add', new CoreFunc('readline-add', 'Add new line of input to history.', 1, 1, $env->set('readline-load', new CoreFunc('readline-load', 'Load the history for readline from a file.', 1, 1,
fn (string $line) => readline_add_history($line) fn (string $file) => readline_read_history($file)
)); ));
$env->set('readline-load', new CoreFunc('readline-load', 'Load the history for readline from a file.', 1, 1, $env->set('readline-save', new CoreFunc('readline-save', 'Save the readline history into a file.', 1, 1,
fn (string $file) => readline_read_history($file) fn (string $file) => readline_write_history($file)
)); ));
}
$env->set('readline-save', new CoreFunc('readline-save', 'Save the readline history into a file.', 1, 1,
fn (string $file) => readline_write_history($file)
));
} }
} }

View File

@ -19,17 +19,25 @@ class LispFactory
// Register core libraries // Register core libraries
(new Lib\Collections())->register($env); (new Lib\Collections())->register($env);
(new Lib\Compare())->register($env); (new Lib\Compare())->register($env);
(new Lib\Json())->register($env); if (extension_loaded('json')) {
(new Lib\Json())->register($env);
}
(new Lib\Math())->register($env); (new Lib\Math())->register($env);
(new Lib\Regex())->register($env); if (extension_loaded('pcre')) {
(new Lib\Regex())->register($env);
}
(new Lib\Strings())->register($env); (new Lib\Strings())->register($env);
(new Lib\Time())->register($env); (new Lib\Time())->register($env);
(new Lib\Types())->register($env); (new Lib\Types())->register($env);
// Register unsafe libraries if not in safe-mode // Register unsafe libraries if not in safe-mode
if (!$safemode) { if (!$safemode) {
(new Lib\Database())->register($env); if (extension_loaded('PDO')) {
(new Lib\Http())->register($env); (new Lib\Database())->register($env);
}
if (extension_loaded('curl')) {
(new Lib\Http())->register($env);
}
(new Lib\IO())->register($env); (new Lib\IO())->register($env);
} }