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
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Config.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. 'tables' => '',
  13. 'middlewares' => 'cors',
  14. 'controllers' => 'records,geojson,openapi',
  15. 'customControllers' => '',
  16. 'customOpenApiBuilders' => '',
  17. 'cacheType' => 'TempFile',
  18. 'cachePath' => '',
  19. 'cacheTime' => 10,
  20. 'debug' => false,
  21. 'basePath' => '',
  22. 'openApiBase' => '{"info":{"title":"PHP-CRUD-API","version":"1.0.0"}}',
  23. ];
  24. private function getDefaultDriver(array $values): string
  25. {
  26. if (isset($values['driver'])) {
  27. return $values['driver'];
  28. }
  29. return 'mysql';
  30. }
  31. private function getDefaultPort(string $driver): int
  32. {
  33. switch ($driver) {
  34. case 'mysql':
  35. return 3306;
  36. case 'pgsql':
  37. return 5432;
  38. case 'sqlsrv':
  39. return 1433;
  40. case 'sqlite':
  41. return 0;
  42. }
  43. }
  44. private function getDefaultAddress(string $driver): string
  45. {
  46. switch ($driver) {
  47. case 'mysql':
  48. return 'localhost';
  49. case 'pgsql':
  50. return 'localhost';
  51. case 'sqlsrv':
  52. return 'localhost';
  53. case 'sqlite':
  54. return 'data.db';
  55. }
  56. }
  57. private function getDriverDefaults(string $driver): array
  58. {
  59. return [
  60. 'driver' => $driver,
  61. 'address' => $this->getDefaultAddress($driver),
  62. 'port' => $this->getDefaultPort($driver),
  63. ];
  64. }
  65. private function applyEnvironmentVariables(array $values): array
  66. {
  67. $newValues = array();
  68. foreach ($values as $key => $value) {
  69. $environmentKey = 'PHP_CRUD_API_' . strtoupper(preg_replace('/(?<!^)[A-Z]/', '_$0', str_replace('.', '_', $key)));
  70. $newValues[$key] = getenv($environmentKey, true) ?: $value;
  71. }
  72. return $newValues;
  73. }
  74. public function __construct(array $values)
  75. {
  76. $driver = $this->getDefaultDriver($values);
  77. $defaults = $this->getDriverDefaults($driver);
  78. $newValues = array_merge($this->values, $defaults, $values);
  79. $newValues = $this->parseMiddlewares($newValues);
  80. $diff = array_diff_key($newValues, $this->values);
  81. if (!empty($diff)) {
  82. $key = array_keys($diff)[0];
  83. throw new \Exception("Config has invalid value '$key'");
  84. }
  85. $newValues = $this->applyEnvironmentVariables($newValues);
  86. $this->values = $newValues;
  87. }
  88. private function parseMiddlewares(array $values): array
  89. {
  90. $newValues = array();
  91. $properties = array();
  92. $middlewares = array_map('trim', explode(',', $values['middlewares']));
  93. foreach ($middlewares as $middleware) {
  94. $properties[$middleware] = [];
  95. }
  96. foreach ($values as $key => $value) {
  97. if (strpos($key, '.') === false) {
  98. $newValues[$key] = $value;
  99. } else {
  100. list($middleware, $key2) = explode('.', $key, 2);
  101. if (isset($properties[$middleware])) {
  102. $properties[$middleware][$key2] = $value;
  103. } else {
  104. throw new \Exception("Config has invalid value '$key'");
  105. }
  106. }
  107. }
  108. $newValues['middlewares'] = array_reverse($properties, true);
  109. return $newValues;
  110. }
  111. public function getDriver(): string
  112. {
  113. return $this->values['driver'];
  114. }
  115. public function getAddress(): string
  116. {
  117. return $this->values['address'];
  118. }
  119. public function getPort(): int
  120. {
  121. return $this->values['port'];
  122. }
  123. public function getUsername(): string
  124. {
  125. return $this->values['username'];
  126. }
  127. public function getPassword(): string
  128. {
  129. return $this->values['password'];
  130. }
  131. public function getDatabase(): string
  132. {
  133. return $this->values['database'];
  134. }
  135. public function getTables(): array
  136. {
  137. return array_filter(array_map('trim', explode(',', $this->values['tables'])));
  138. }
  139. public function getMiddlewares(): array
  140. {
  141. return $this->values['middlewares'];
  142. }
  143. public function getControllers(): array
  144. {
  145. return array_filter(array_map('trim', explode(',', $this->values['controllers'])));
  146. }
  147. public function getCustomControllers(): array
  148. {
  149. return array_filter(array_map('trim', explode(',', $this->values['customControllers'])));
  150. }
  151. public function getCustomOpenApiBuilders(): array
  152. {
  153. return array_filter(array_map('trim', explode(',', $this->values['customOpenApiBuilders'])));
  154. }
  155. public function getCacheType(): string
  156. {
  157. return $this->values['cacheType'];
  158. }
  159. public function getCachePath(): string
  160. {
  161. return $this->values['cachePath'];
  162. }
  163. public function getCacheTime(): int
  164. {
  165. return $this->values['cacheTime'];
  166. }
  167. public function getDebug(): bool
  168. {
  169. return $this->values['debug'];
  170. }
  171. public function getBasePath(): string
  172. {
  173. return $this->values['basePath'];
  174. }
  175. public function getOpenApiBase(): array
  176. {
  177. return json_decode($this->values['openApiBase'], true);
  178. }
  179. }