api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Config.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace Tqdev\PhpCrudApi;
  3. class Config
  4. {
  5. private $values = [
  6. 'driver' => null,
  7. 'address' => 'localhost',
  8. 'port' => null,
  9. 'username' => null,
  10. 'password' => null,
  11. 'database' => null,
  12. 'middlewares' => 'cors',
  13. 'controllers' => 'records,geojson,openapi',
  14. 'cacheType' => 'TempFile',
  15. 'cachePath' => '',
  16. 'cacheTime' => 10,
  17. 'debug' => false,
  18. 'basePath' => '',
  19. 'openApiBase' => '{"info":{"title":"PHP-CRUD-API","version":"1.0.0"}}',
  20. ];
  21. private function getDefaultDriver(array $values): string
  22. {
  23. if (isset($values['driver'])) {
  24. return $values['driver'];
  25. }
  26. return 'mysql';
  27. }
  28. private function getDefaultPort(string $driver): int
  29. {
  30. switch ($driver) {
  31. case 'mysql':return 3306;
  32. case 'pgsql':return 5432;
  33. case 'sqlsrv':return 1433;
  34. }
  35. }
  36. private function getDefaultAddress(string $driver): string
  37. {
  38. switch ($driver) {
  39. case 'mysql':return 'localhost';
  40. case 'pgsql':return 'localhost';
  41. case 'sqlsrv':return 'localhost';
  42. }
  43. }
  44. private function getDriverDefaults(string $driver): array
  45. {
  46. return [
  47. 'driver' => $driver,
  48. 'address' => $this->getDefaultAddress($driver),
  49. 'port' => $this->getDefaultPort($driver),
  50. ];
  51. }
  52. public function __construct(array $values)
  53. {
  54. $driver = $this->getDefaultDriver($values);
  55. $defaults = $this->getDriverDefaults($driver);
  56. $newValues = array_merge($this->values, $defaults, $values);
  57. $newValues = $this->parseMiddlewares($newValues);
  58. $diff = array_diff_key($newValues, $this->values);
  59. if (!empty($diff)) {
  60. $key = array_keys($diff)[0];
  61. throw new \Exception("Config has invalid value '$key'");
  62. }
  63. $this->values = $newValues;
  64. }
  65. private function parseMiddlewares(array $values): array
  66. {
  67. $newValues = array();
  68. $properties = array();
  69. $middlewares = array_map('trim', explode(',', $values['middlewares']));
  70. foreach ($middlewares as $middleware) {
  71. $properties[$middleware] = [];
  72. }
  73. foreach ($values as $key => $value) {
  74. if (strpos($key, '.') === false) {
  75. $newValues[$key] = $value;
  76. } else {
  77. list($middleware, $key2) = explode('.', $key, 2);
  78. if (isset($properties[$middleware])) {
  79. $properties[$middleware][$key2] = $value;
  80. } else {
  81. throw new \Exception("Config has invalid value '$key'");
  82. }
  83. }
  84. }
  85. $newValues['middlewares'] = array_reverse($properties, true);
  86. return $newValues;
  87. }
  88. public function getDriver(): string
  89. {
  90. return $this->values['driver'];
  91. }
  92. public function getAddress(): string
  93. {
  94. return $this->values['address'];
  95. }
  96. public function getPort(): int
  97. {
  98. return $this->values['port'];
  99. }
  100. public function getUsername(): string
  101. {
  102. return $this->values['username'];
  103. }
  104. public function getPassword(): string
  105. {
  106. return $this->values['password'];
  107. }
  108. public function getDatabase(): string
  109. {
  110. return $this->values['database'];
  111. }
  112. public function getMiddlewares(): array
  113. {
  114. return $this->values['middlewares'];
  115. }
  116. public function getControllers(): array
  117. {
  118. return array_map('trim', explode(',', $this->values['controllers']));
  119. }
  120. public function getCacheType(): string
  121. {
  122. return $this->values['cacheType'];
  123. }
  124. public function getCachePath(): string
  125. {
  126. return $this->values['cachePath'];
  127. }
  128. public function getCacheTime(): int
  129. {
  130. return $this->values['cacheTime'];
  131. }
  132. public function getDebug(): bool
  133. {
  134. return $this->values['debug'];
  135. }
  136. public function getBasePath(): string
  137. {
  138. return $this->values['basePath'];
  139. }
  140. public function getOpenApiBase(): array
  141. {
  142. return json_decode($this->values['openApiBase'], true);
  143. }
  144. }