ソースを参照

added core classes and bootstrap.php

Плотников Роман Вячеславович 3 年 前
コミット
c86cbf228a
5 ファイル変更109 行追加1 行削除
  1. 10 0
      config/path.php
  2. 28 0
      core/Src/Application.php
  3. 33 0
      core/Src/Settings.php
  4. 28 0
      core/bootstrap.php
  5. 10 1
      public/index.php

+ 10 - 0
config/path.php

@@ -0,0 +1,10 @@
+<?php
+return [
+    'root' => '',
+    'classes' => [
+        'app',
+        'core'
+    ],
+    'routes' => 'routes',
+    'views' => 'views'
+];

+ 28 - 0
core/Src/Application.php

@@ -0,0 +1,28 @@
+<?php
+
+namespace Src;
+
+use Error;
+
+class Application
+{
+    private Settings $settings;
+
+    public function __construct(Settings $settings)
+    {
+        $this->settings = $settings;
+    }
+
+    public function __get($key)
+    {
+        if ($key === 'settings') {
+            return $this->settings;
+        }
+        throw new Error('Accessing a non-existent property');
+    }
+
+    public function run(): void
+    {
+        echo 'Working';
+    }
+}

+ 33 - 0
core/Src/Settings.php

@@ -0,0 +1,33 @@
+<?php
+
+namespace Src;
+
+use Error;
+
+class Settings
+{
+    private array $_settings;
+
+    public function __construct(array $settings = [])
+    {
+        $this->_settings= $settings;
+    }
+
+    public function __get($key)
+    {
+        if (array_key_exists($key, $this->_settings)) {
+            return $this->_settings[$key];
+        }
+        throw new Error('Property does not exist');
+    }
+
+    public function getRootPath(): string
+    {
+        return $this->path['root'] ? '/' . $this->path['root'] : '';
+    }
+
+    public function getViewsPath(): string
+    {
+        return '/' . $this->path['views'] ?? '';
+    }
+}

+ 28 - 0
core/bootstrap.php

@@ -0,0 +1,28 @@
+<?php
+
+const DIR_CONFIG = '/../config';
+
+spl_autoload_register(function ($className) {
+    $paths = include __DIR__ . DIR_CONFIG . '/path.php';
+    $className = str_replace('\\', '/', $className);
+
+    foreach ($paths['classes'] as $path) {
+        $filename = $_SERVER['DOCUMENT_ROOT'] . "/$paths[root]/$path/$className.php";
+        if (file_exists($filename)) {
+            require_once $filename;
+        }
+    }
+});
+
+function getConfigs(string $path = DIR_CONFIG): array {
+    $settings = [];
+    foreach (scandir(__DIR__ . $path) as $file) {
+        $name = explode('.', $file)[0];
+        if (!empty($name)) {
+            $settings[$name] = include __DIR__ . "$path/$file";
+        }
+    }
+    return $settings;
+}
+
+return new Src\Application(new Src\Settings(getConfigs()));

+ 10 - 1
public/index.php

@@ -1,3 +1,12 @@
 <?php
 
-echo "Hello";
+declare(strict_types=1);
+
+try {
+    $app = require_once __DIR__ . '/../core/bootstrap.php';
+    $app->run();
+} catch (\Throwable $exception) {
+    echo "<pre>";
+    print_r($exception);
+    echo "</pre>";
+}