summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--config.example.toml6
-rw-r--r--public/index.php29
-rw-r--r--src/App.php6
4 files changed, 39 insertions, 3 deletions
diff --git a/.gitignore b/.gitignore
index 49b567d..a23bab3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
/vendor/
/content/
+/config.toml
diff --git a/config.example.toml b/config.example.toml
new file mode 100644
index 0000000..a506a09
--- /dev/null
+++ b/config.example.toml
@@ -0,0 +1,6 @@
+[app]
+contentFolderPath = './content'
+
+[form]
+entriesFolderName = 'entries'
+
diff --git a/public/index.php b/public/index.php
index 1cd408e..b7895e7 100644
--- a/public/index.php
+++ b/public/index.php
@@ -1,8 +1,37 @@
<?php
use FlatFileForms\App;
+use Yosymfony\Toml\Toml;
require_once dirname(__DIR__) . '/vendor/autoload.php';
+function findAppConfigFile($path)
+{
+ $currentFolder = $path;
+ while ($currentFolder !== '/') {
+ $configFile = $currentFolder . '/config.toml';
+ if (file_exists($configFile)) {
+ return $configFile;
+ }
+
+ $currentFolder = dirname($currentFolder);
+ }
+
+ die('config.toml missing');
+}
+
+$configFile = findAppConfigFile(dirname(__DIR__));
+$config = Toml::parseFile($configFile);
+
+chdir(dirname($configFile));
+$contentFolderPath = realpath($config['app']['contentFolderPath']);
+$contentFolderPath === false && die('Content folder "' . $config['app']['contentFolderPath'] . '" missing');
+$config['app']['contentFolderPath'] = $contentFolderPath;
+chdir($_SERVER['DOCUMENT_ROOT']);
+
+foreach ($config as $key => $value) {
+ $_ENV[$key] = $value;
+}
+
new App();
diff --git a/src/App.php b/src/App.php
index fcf2699..7e0cabd 100644
--- a/src/App.php
+++ b/src/App.php
@@ -20,7 +20,7 @@ class App
$content = [
'data' => [],
];
- $contentRoot = dirname(__DIR__) . '/content';
+ $contentRoot = $_ENV['app']['contentFolderPath'];
$method = $request->getMethod();
$path = $request->getPathInfo();
@@ -85,7 +85,7 @@ class App
$builder->addValue($entryKey, $entryValue);
}
- $entryDirectory = $formPath . '/entries/' . $date->format('Y/m/d');
+ $entryDirectory = $formPath . '/' . $_ENV['form']['entriesFolderName'] . '/' . $date->format('Y/m/d');
@mkdir($entryDirectory, 0774, true);
$entryFilename = $date->format('Ymd_Hi_') . hash('adler32', serialize($entry)) . '.toml';
file_put_contents(
@@ -158,7 +158,7 @@ class App
$config['api']['keys'] = $apiKeys;
}
- if (str_ends_with($currentFolder, '/content') || $currentFolder == '/') {
+ if (str_ends_with($currentFolder, '/' . basename($_ENV['app']['contentFolderPath'])) || $currentFolder == '/') {
break;
}