App.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. declare(strict_types=1);
  3. namespace Michel\Framework\Core;
  4. use Psr\Container\ContainerInterface;
  5. use Psr\Http\Message\ResponseFactoryInterface;
  6. use Psr\Http\Message\ServerRequestFactoryInterface;
  7. use Psr\Http\Message\ServerRequestInterface;
  8. /**
  9. * @package Michel.F
  10. * @author Michel
  11. * @license https://opensource.org/license/mpl-2-0 Mozilla Public License v2.0
  12. */
  13. final class App
  14. {
  15. private array $options;
  16. private static App $instance;
  17. private ?ContainerInterface $container = null;
  18. private function __construct(array $options)
  19. {
  20. $required = ['server_request', 'server_request_factory', 'response_factory', 'container'];
  21. foreach ($required as $key) {
  22. if (!isset($options[$key]) || !$options[$key] instanceof \Closure) {
  23. throw new \InvalidArgumentException(sprintf('The option "%s" is required and must be a Closure.', $key));
  24. }
  25. }
  26. if (isset($options['custom_environments'])) {
  27. $environmentsFiltered = array_filter($options['custom_environments'], function ($value) {
  28. return is_string($value) === false;
  29. });
  30. if ($environmentsFiltered !== []) {
  31. throw new \InvalidArgumentException('custom_environments array values must be string only');
  32. }
  33. } else {
  34. $options['custom_environments'] = [];
  35. }
  36. $this->options = $options;
  37. }
  38. public static function initWithPath(string $path): void
  39. {
  40. if (!file_exists($path)) {
  41. throw new \InvalidArgumentException(sprintf('%s does not exist', $path));
  42. }
  43. self::init(require $path);
  44. }
  45. public static function init(array $options): void
  46. {
  47. self::$instance = new self($options);
  48. }
  49. public static function createServerRequest(): ServerRequestInterface
  50. {
  51. $serverRequest = self::getApp()->options['server_request'];
  52. return $serverRequest();
  53. }
  54. public static function getServerRequestFactory(): ServerRequestFactoryInterface
  55. {
  56. $serverRequest = self::getApp()->options['server_request_factory'];
  57. return $serverRequest();
  58. }
  59. public static function getResponseFactory(): ResponseFactoryInterface
  60. {
  61. $responseFactory = self::getApp()->options['response_factory'];
  62. return $responseFactory();
  63. }
  64. public static function createContainer($definitions, $options): ContainerInterface
  65. {
  66. if (self::getApp()->container instanceof ContainerInterface) {
  67. throw new \LogicException('A container has already been built in ' . self::class);
  68. }
  69. self::getApp()->container = self::getApp()->options['container']($definitions, $options);
  70. return self::getContainer();
  71. }
  72. public static function getContainer(): ContainerInterface
  73. {
  74. return self::getApp()->container;
  75. }
  76. public static function getCustomEnvironments(): array
  77. {
  78. return self::getApp()->options['custom_environments'];
  79. }
  80. private static function getApp(): self
  81. {
  82. if (self::$instance === null) {
  83. throw new \LogicException('Please call ::init() method before get ' . self::class);
  84. }
  85. return self::$instance;
  86. }
  87. }