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 3.9KB

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