From 2eb5a432d2229788ce2fdb09f36c6f4bebdea813 Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Fri, 7 Feb 2020 09:44:59 +0100 Subject: Initial commit --- src/Env.php | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/Env.php (limited to 'src/Env.php') diff --git a/src/Env.php b/src/Env.php new file mode 100644 index 0000000..34b9f1f --- /dev/null +++ b/src/Env.php @@ -0,0 +1,80 @@ +lazyLoaded[$ident])) { + $this->lazyLoaded[$ident] = $callback(); + } + return $this->lazyLoaded[$ident]; + } + + public static function fromConfig(array $config) + { + $env = new self; + $env->config = $config; + return $env; + } + + public function documentRoot(): string { return "/"; } + + public function twig(): Environment + { + return $this->lazy("twig", function () { + $loader = new FilesystemLoader($this->config["templates_path"]); + $env = new Environment($loader, [ + "cache" => $this->config["templates_cache"], + ]); + + $env->addFunction(new TwigFunction("url", function (string $url, ...$args) { + return $this->documentRoot() . sprintf($url, ...$args); + }, ["is_variadic" => true])); + + $env->addFilter(new TwigFilter("search_escape", static function (string $s) { + $s = str_replace("\\", "\\\\", $s); + $s = str_replace("#", "\\#", $s); + $s = str_replace(" ", "\\ ", $s); + $s = str_replace("\t", "\\\t", $s); + $s = str_replace("(", "\\(", $s); + $s = str_replace(")", "\\)", $s); + return $s; + })); + + return $env; + }); + } + + public function rawDbCon(): SQLite3 + { + return $this->lazy("rawDbCon", function () { + return new SQLite3($this->config["sqlitedb"]); + }); + } + + public function db(): SQLite3 + { + return $this->lazy("db", function () { + $db = $this->rawDbCon(); + $db->exec("PRAGMA foreign_keys = ON"); + + (new Schema($db))->migrate(); + + return $db; + }); + } +} -- cgit v1.2.3-70-g09d2