|
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-2015 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 /** |
|
26 * @see Zend_Service_Console_Command_ParameterSource_ParameterSourceInterface |
|
27 */ |
|
28 require_once 'Zend/Service/Console/Command/ParameterSource/ParameterSourceInterface.php'; |
|
29 |
|
30 /** |
|
31 * @category Zend |
|
32 * @package Zend_Service_Console |
|
33 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) |
|
34 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
35 * @copyright Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com) |
|
36 * @license http://phpazure.codeplex.com/license |
|
37 */ |
|
38 class Zend_Service_Console_Command_ParameterSource_ConfigFile |
|
39 implements Zend_Service_Console_Command_ParameterSource_ParameterSourceInterface |
|
40 { |
|
41 /** |
|
42 * Get value for a named parameter. |
|
43 * |
|
44 * @param mixed $parameter Parameter to get a value for |
|
45 * @param array $argv Argument values passed to the script when run in console. |
|
46 * @return mixed |
|
47 */ |
|
48 public function getValueForParameter($parameter, $argv = array()) |
|
49 { |
|
50 // Configuration file path |
|
51 $configurationFilePath = null; |
|
52 |
|
53 // Check if a path to a configuration file is specified |
|
54 foreach ($argv as $parameterInput) { |
|
55 $parameterInput = explode('=', $parameterInput, 2); |
|
56 |
|
57 if (strtolower($parameterInput[0]) == '--configfile' || strtolower($parameterInput[0]) == '-f') { |
|
58 if (!isset($parameterInput[1])) { |
|
59 require_once 'Zend/Service/Console/Exception.php'; |
|
60 throw new Zend_Service_Console_Exception("No path to a configuration file is given. Specify the path using the --ConfigFile or -F switch."); |
|
61 } |
|
62 $configurationFilePath = $parameterInput[1]; |
|
63 break; |
|
64 } |
|
65 } |
|
66 |
|
67 // Value given? |
|
68 if (is_null($configurationFilePath)) { |
|
69 return null; |
|
70 } |
|
71 if (!file_exists($configurationFilePath)) { |
|
72 require_once 'Zend/Service/Console/Exception.php'; |
|
73 throw new Zend_Service_Console_Exception("Invalid configuration file given. Specify the correct path using the --ConfigFile or -F switch."); |
|
74 } |
|
75 |
|
76 // Parse values |
|
77 $iniValues = parse_ini_file($configurationFilePath); |
|
78 |
|
79 // Default value |
|
80 $parameterValue = null; |
|
81 |
|
82 // Loop aliases |
|
83 foreach ($parameter->aliases as $alias) { |
|
84 if (array_key_exists($alias, $iniValues)) { |
|
85 $parameterValue = $iniValues[$alias]; break; |
|
86 } else if (array_key_exists(strtolower($alias), $iniValues)) { |
|
87 $parameterValue = $iniValues[strtolower($alias)]; break; |
|
88 } else if (array_key_exists(str_replace('-', '', $alias), $iniValues)) { |
|
89 $parameterValue = $iniValues[str_replace('-', '', $alias)]; break; |
|
90 } else if (array_key_exists(strtolower(str_replace('-', '', $alias)), $iniValues)) { |
|
91 $parameterValue = $iniValues[strtolower(str_replace('-', '', $alias))]; break; |
|
92 } |
|
93 } |
|
94 |
|
95 if (strtolower($parameterValue) == 'true') { |
|
96 $parameterValue = true; |
|
97 } else if (strtolower($parameterValue) == 'false') { |
|
98 $parameterValue = false; |
|
99 } |
|
100 |
|
101 // Done! |
|
102 return $parameterValue; |
|
103 } |
|
104 } |