Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Sposób na GLOBALNY konfig aplikacji, Co o tym sądzicie?
LowiczakPL
post
Post #1





Grupa: Zarejestrowani
Postów: 531
Pomógł: 55
Dołączył: 3.01.2016
Skąd: Łowicz

Ostrzeżenie: (0%)
-----


Witam

mam pytanie co sądzicie o takim sposobie rozwiązania na globalną konfigurację aplikacji

Klasa kontrolera konfiguracji
  1. <?php
  2.  
  3. namespace Core;
  4.  
  5. final class Config {
  6.  
  7. public static $config = array();
  8.  
  9. public static function getValue($module, $var) {
  10. if (isset(static::$config[$module][$var])) {
  11. return static::$config[$module][$var];
  12. }
  13. else {
  14. include_once('conf/conf.php');
  15. static::$config = $configs;
  16. return static::$config[$module][$var];
  17. }
  18. }
  19.  
  20. }



plik z konfiguracją

  1. <?php
  2.  
  3. switch (ENVIRONMENT) {
  4. case 'development':
  5. $configs['app']['url_website'] = 'http://testowa.rr/';
  6. $configs['app']['debug'] = 'on';
  7. break;
  8. case 'production':
  9. $configs['app']['url_website'] = 'http://mojadomena.pl/';
  10. $configs['app']['debug'] = 'off';
  11.  
  12. break;
  13. default:
  14. exit('Aplikacja nie działa poprawnie, ustaw tryb pracy.');
  15. }
  16.  
  17. $configs['app']['url_ogloszenia'] = $configs['app']['url_website'].'ogloszenia/';
  18. $configs['app']['router_rejestruj'] = 'rejestruj.html';
  19. ...
  20.  


Użycie konfiguracji w klasach

  1. <?php
  2. namespace Controllers;
  3.  
  4. use Helpers\Url;
  5. use Core\Config;
  6.  
  7. public function mojeKonto()
  8. {
  9. ...
  10. Url::redirect(Config::getValue('users','router_mojekonto'));
  11. }
  12.  


Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
LowiczakPL
post
Post #2





Grupa: Zarejestrowani
Postów: 531
Pomógł: 55
Dołączył: 3.01.2016
Skąd: Łowicz

Ostrzeżenie: (0%)
-----


Przysiadłem dziś do klasy i zastosowałem się do wskazówek, jak to wygląda po zmianach Waszym zdaniem.

plik z konfiguracją ogloszenia.php
  1. <?php
  2.  
  3. /*
  4.   * Ogłoszenia
  5.   */
  6. return [
  7. 'base_path_img' => 'dir/users/ogloszenia/',
  8. 'base_url_img' => 'dir/users/ogloszenia/',
  9. 'img' => [
  10. 'path_thumbs' => 'dir/user/thumbs/path/id/',
  11. 'path_big_foto' => 'dir/user/big_foto/path/id/',
  12. 'path_mini_foto' => 'dir/user/mini_foto/path/id/',
  13. ]
  14. ];
  15.  


użycie klasy

  1. <?php
  2. namespace Controllers;
  3.  
  4. use Helpers\Url;
  5. use Core\Config;
  6.  
  7. public function mojeKonto()
  8. {
  9. ...
  10. $this->cfg = new \Core\Config();
  11.  
  12. $this->cfg->get('ogloszenia.img.path_thumbs');
  13. $this->cfg->get('ogloszenia.base_url_img');
  14. }


nowa klasa

  1. <?php
  2.  
  3. namespace Core;
  4.  
  5. class Config {
  6.  
  7. public $config = array();
  8. private $configs = array();
  9. private $filePath = 'app/config/';
  10.  
  11. public function __construct() {
  12.  
  13. }
  14.  
  15. public function get($var) {
  16. if (isset($this->config[$var])) {
  17. return $this->config[$var];
  18. } else {
  19. $this->getValue($var);
  20. return $this->config[$var];
  21. }
  22. }
  23.  
  24. private function getValue($var) {
  25. $list = explode('.', $var);
  26. $key = end($list);
  27. $array = $this->getFile($list);
  28.  
  29. if (isset($array[$key])) {
  30. $this->config[$var] = $array[$key];
  31. } else {
  32. foreach ($array as $segment) {
  33. if (isset($segment[$key])) {
  34. $this->config[$var] = $segment[$key];
  35. }
  36. }
  37. }
  38. }
  39.  
  40. private function getFile($list) {
  41. if (!isset($this->configs[$list[0]])) {
  42. $configFile = $this->filePath . $list[0] . '.php';
  43. if (file_exists($configFile)) {
  44. $this->configs[$list[0]] = require_once($configFile);
  45. } else {
  46. throw new Exception('Cannot read the configuration file: ' . $configFile);
  47. }
  48. }
  49. return $this->configs[$list[0]];
  50. }
  51.  
  52. }
  53.  
  54.  


Ten post edytował LowiczakPL 17.06.2016, 19:22:37
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 28.12.2025 - 05:57