PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/Qafoo/Review/Configuration.php

https://github.com/sellami/review
PHP | 132 lines | 72 code | 15 blank | 45 comment | 7 complexity | 3ebd82c46bbb8038f60b937989301456 MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php
  2. /**
  3. * This file is part of qaReview
  4. *
  5. * @version $Revision$
  6. * @license APGLv3
  7. * @copyright Qafoo GmbH
  8. */
  9. namespace Qafoo\Review;
  10. /**
  11. * Base class for an *.ini based configuration implementation.
  12. *
  13. * @version $Revision$
  14. * @license APGLv3
  15. */
  16. class Configuration
  17. {
  18. /**
  19. * @var array
  20. */
  21. private $inheritance = array(
  22. 'development' => 'testing',
  23. 'testing' => 'staging',
  24. 'staging' => 'production'
  25. );
  26. /**
  27. * @var array
  28. */
  29. private $configuration;
  30. /**
  31. * Constructs a new ini based configuration instance.
  32. *
  33. * @param string $iniFile
  34. * @param string $environment
  35. */
  36. public function __construct( $iniFile, $environment )
  37. {
  38. if ( !is_file( $iniFile ) &&
  39. is_file( $iniFile . '.dist' ) )
  40. {
  41. $iniFile = $iniFile . '.dist';
  42. }
  43. $this->parseIniFile( $iniFile, $environment );
  44. }
  45. /**
  46. * Parse ini file
  47. *
  48. * @param string $iniFile
  49. * @param string $environment
  50. * @return void
  51. */
  52. public function parseIniFile( $iniFile, $environment )
  53. {
  54. $configuration = parse_ini_file( $iniFile, true );
  55. if ( false === isset( $configuration[$environment] ) )
  56. {
  57. throw new \UnexpectedValueException( "Unknown environment $environment." );
  58. }
  59. $this->configuration = $configuration[$environment];
  60. $this->applyInheritance( $configuration, $environment );
  61. foreach ( $this->configuration as $key => $value )
  62. {
  63. if ( strpos( $key, '.' ) === false )
  64. {
  65. continue;
  66. }
  67. $path = array_filter( explode( '.', $key ) );
  68. unset( $this->configuration[$key] );
  69. $current = &$this->configuration;
  70. foreach ( $path as $element )
  71. {
  72. if ( !isset( $current[$element] ) )
  73. {
  74. $current[$element] = array();
  75. }
  76. $current = &$current[$element];
  77. }
  78. $current = $value;
  79. }
  80. }
  81. /**
  82. * Inherit configuration options from upper level environments
  83. *
  84. * @param array $configuration
  85. * @param string $environment
  86. * @return void
  87. */
  88. protected function applyInheritance( array $configuration, $environment )
  89. {
  90. $parent = $environment;
  91. while ( isset( $this->inheritance[$parent] ) )
  92. {
  93. $parent = $this->inheritance[$parent];
  94. if ( isset( $configuration[$parent] ) )
  95. {
  96. $this->configuration = array_merge(
  97. $configuration[$parent],
  98. $this->configuration
  99. );
  100. }
  101. }
  102. }
  103. /**
  104. * Returns the configuration value for the given $key.
  105. *
  106. * @param string $key
  107. * @return string
  108. */
  109. public function __get( $key )
  110. {
  111. if ( isset( $this->configuration[$key] ) )
  112. {
  113. return $this->configuration[$key];
  114. }
  115. throw new \OutOfBoundsException( "No configuration option $key available." );
  116. }
  117. }