Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

79 Zeilen
1.7KB

  1. <?php
  2. namespace Framework;
  3. use Dotenv\Dotenv;
  4. use Framework\Routing\Router;
  5. use Framework\Http\Response;
  6. class App extends Container
  7. {
  8. private static $instance;
  9. public static function getInstance()
  10. {
  11. if (!static::$instance) {
  12. static::$instance = new static();
  13. }
  14. return static::$instance;
  15. }
  16. private function __construct() {}
  17. private function __clone() {}
  18. public function prepare(): static
  19. {
  20. $basePath = $this->resolve('paths.base');
  21. $this->configure($basePath);
  22. $this->bindProviders($basePath);
  23. return $this;
  24. }
  25. public function run(): Response
  26. {
  27. return $this->dispatch($this->resolve('paths.base'));
  28. }
  29. private function configure(string $basePath)
  30. {
  31. $dotenv = Dotenv::createImmutable($basePath);
  32. $dotenv->load();
  33. }
  34. private function bindProviders(string $basePath)
  35. {
  36. $providers = require "{$basePath}/config/providers.php";
  37. foreach ($providers as $provider) {
  38. $instance = new $provider;
  39. if (method_exists($instance, 'bind')) {
  40. $instance->bind($this);
  41. }
  42. }
  43. }
  44. private function dispatch(string $basePath): Response
  45. {
  46. if (!$this->has(Router::class)) {
  47. $router = new Router();
  48. $routes = require "{$basePath}/app/routes.php";
  49. $routes($router);
  50. $this->bind(Router::class, fn() => $router);
  51. }
  52. $response = $this->resolve(Router::class)->dispatch();
  53. if (!$response instanceof Response) {
  54. $response = $this->resolve('response')->content($response);
  55. }
  56. return $response;
  57. }
  58. }

Powered by TurnKey Linux.