Application.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace Src;
  3. use Error;
  4. use Illuminate\Container\Container;
  5. use Illuminate\Events\Dispatcher;
  6. use Illuminate\Database\Capsule\Manager as Capsule;
  7. use Src\Auth\Auth;
  8. class Application
  9. {
  10. private Settings $settings;
  11. private Route $route;
  12. private Capsule $dbManager;
  13. private Auth $auth;
  14. public function __construct(Settings $settings)
  15. {
  16. //Привязываем класс со всеми настройками приложения
  17. $this->settings = $settings;
  18. //Привязываем класс маршрутизации с установкой префикса
  19. $this->route = new Route($this->settings->getRootPath());
  20. //Создаем класс менеджера для базы данных
  21. $this->dbManager = new Capsule();
  22. //Создаем класс для аутентификации на основе настроек приложения
  23. $this->auth = new $this->settings->app['auth'];
  24. //Настройка для работы с базой данных
  25. $this->dbRun();
  26. //Инициализация класса пользователя на основе настроек приложения
  27. $this->auth::init(new $this->settings->app['identity']);
  28. }
  29. public function __get($key)
  30. {
  31. switch ($key) {
  32. case 'settings':
  33. return $this->settings;
  34. case 'route':
  35. return $this->route;
  36. case 'auth':
  37. return $this->auth;
  38. }
  39. throw new Error('Accessing a non-existent property');
  40. }
  41. private function dbRun()
  42. {
  43. $this->dbManager->addConnection($this->settings->getDbSetting());
  44. $this->dbManager->setEventDispatcher(new Dispatcher(new Container));
  45. $this->dbManager->setAsGlobal();
  46. $this->dbManager->bootEloquent();
  47. }
  48. public function run(): void
  49. {
  50. //Запуск маршрутизации
  51. $this->route->start();
  52. }
  53. }