summaryrefslogtreecommitdiff
path: root/src/Kernel.php
blob: 29785a8406f51d1498dddd17feb7025e4b9c5169 (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
<?php

namespace Elements;

class Kernel
{
  private static array $config = [];

  /**
   * Kernel initialization.
   */
  public static function init()
  {
    $configFile = self::findAppConfigFile(dirname(__DIR__));
    $config = include $configFile;
    foreach ($config as $key => $value) {
      self::$config[$key] = $value;
    }

    Template::init();
    DB::init();
    new Router();
  }

  /**
   * Find app config file in parent directories
   */
  private static function findAppConfigFile(string $path): string
  {
    $currentDirectory = $path;
    while ($currentDirectory !== '/') {
      $configFile = $currentDirectory . '/config.php';
      if (file_exists($configFile)) {
	return $configFile;
      }

      $currentDirectory = dirname($currentDirectory);
    }

    die('config.php missing');
  }

  /**
   * Get config value
   */
  public static function config($key): mixed
  {
    return self::$config[$key];
  }
}