summaryrefslogtreecommitdiff
path: root/public/index.php
blob: 8a7ec7dab823d63eda443a348c4cb5bc288a18dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php

use FlatFileForms\App;
use FlatFileForms\PluginLoader;
use FlatFileForms\PreLoader;
use Yosymfony\Toml\Toml;

require_once dirname(__DIR__) . '/vendor/autoload.php';

/**
 * Find the config file
 */
function findAppConfigFile(string $path): string
{
  $currentDirectory = $path;
  while ($currentDirectory !== '/') {
    $configFile = $currentDirectory . '/config.toml';
    if (file_exists($configFile)) {
      return $configFile;
    }

    $currentDirectory = dirname($currentDirectory);
  }

  die('config.toml missing');
}

// find and parse config
$configFile = findAppConfigFile(dirname(__DIR__));
$config = Toml::parseFile($configFile);

// prepare possibly relative folders path
chdir(dirname($configFile));

$contentDirectoryPath = realpath($config['app']['contentFolderPath']);
$contentDirectoryPath === false && die('Content folder "' . $config['app']['contentFolderPath'] . '" missing');
$config['app']['contentFolderPath'] = $contentDirectoryPath;

$pluginsDirectoryPath = realpath($config['app']['pluginsFolderPath']);
$pluginsDirectoryPath === false && die('Plugins folder "' . $config['app']['pluginsFolderPath'] . '" missing');
$config['app']['pluginsFolderPath'] = $pluginsDirectoryPath;

chdir($_SERVER['DOCUMENT_ROOT']);

// set config values to global $_ENV
foreach ($config as $key => $value) {
  $_ENV[$key] = $value;
}

// load
new PreLoader();
new PluginLoader();

// run
new App();