ConfigFile.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service_Console
  17. * @subpackage Exception
  18. * @version $Id$
  19. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. * @copyright Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  22. * @license http://phpazure.codeplex.com/license
  23. */
  24. /**
  25. * @see Zend_Service_Console_Command_ParameterSource_ParameterSourceInterface
  26. */
  27. require_once 'Zend/Service/Console/Command/ParameterSource/ParameterSourceInterface.php';
  28. /**
  29. * @category Zend
  30. * @package Zend_Service_Console
  31. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @copyright Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  34. * @license http://phpazure.codeplex.com/license
  35. */
  36. class Zend_Service_Console_Command_ParameterSource_ConfigFile
  37. implements Zend_Service_Console_Command_ParameterSource_ParameterSourceInterface
  38. {
  39. /**
  40. * Get value for a named parameter.
  41. *
  42. * @param mixed $parameter Parameter to get a value for
  43. * @param array $argv Argument values passed to the script when run in console.
  44. * @return mixed
  45. */
  46. public function getValueForParameter($parameter, $argv = array())
  47. {
  48. // Configuration file path
  49. $configurationFilePath = null;
  50. // Check if a path to a configuration file is specified
  51. foreach ($argv as $parameterInput) {
  52. $parameterInput = explode('=', $parameterInput, 2);
  53. if (strtolower($parameterInput[0]) == '--configfile' || strtolower($parameterInput[0]) == '-f') {
  54. if (!isset($parameterInput[1])) {
  55. require_once 'Zend/Service/Console/Exception.php';
  56. throw new Zend_Service_Console_Exception("No path to a configuration file is given. Specify the path using the --ConfigFile or -F switch.");
  57. }
  58. $configurationFilePath = $parameterInput[1];
  59. break;
  60. }
  61. }
  62. // Value given?
  63. if (is_null($configurationFilePath)) {
  64. return null;
  65. }
  66. if (!file_exists($configurationFilePath)) {
  67. require_once 'Zend/Service/Console/Exception.php';
  68. throw new Zend_Service_Console_Exception("Invalid configuration file given. Specify the correct path using the --ConfigFile or -F switch.");
  69. }
  70. // Parse values
  71. $iniValues = parse_ini_file($configurationFilePath);
  72. // Default value
  73. $parameterValue = null;
  74. // Loop aliases
  75. foreach ($parameter->aliases as $alias) {
  76. if (array_key_exists($alias, $iniValues)) {
  77. $parameterValue = $iniValues[$alias]; break;
  78. } else if (array_key_exists(strtolower($alias), $iniValues)) {
  79. $parameterValue = $iniValues[strtolower($alias)]; break;
  80. } else if (array_key_exists(str_replace('-', '', $alias), $iniValues)) {
  81. $parameterValue = $iniValues[str_replace('-', '', $alias)]; break;
  82. } else if (array_key_exists(strtolower(str_replace('-', '', $alias)), $iniValues)) {
  83. $parameterValue = $iniValues[strtolower(str_replace('-', '', $alias))]; break;
  84. }
  85. }
  86. if (strtolower($parameterValue) == 'true') {
  87. $parameterValue = true;
  88. } else if (strtolower($parameterValue) == 'false') {
  89. $parameterValue = false;
  90. }
  91. // Done!
  92. return $parameterValue;
  93. }
  94. }