Class: Inspec::Resources::PhpConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/resources/php_config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_param, config_file_or_path = {}) ⇒ PhpConfig

Returns a new instance of PhpConfig.



23
24
25
26
# File 'lib/inspec/resources/php_config.rb', line 23

def initialize(config_param, config_file_or_path = {})
  @config_param = config_param
  @config_file_or_path = config_file_or_path
end

Instance Attribute Details

#config_file_or_pathObject (readonly)

Resource initialization.



22
23
24
# File 'lib/inspec/resources/php_config.rb', line 22

def config_file_or_path
  @config_file_or_path
end

#config_paramObject (readonly)

Resource initialization.



22
23
24
# File 'lib/inspec/resources/php_config.rb', line 22

def config_param
  @config_param
end

Instance Method Details

#resource_idObject

Unique resource id



29
30
31
# File 'lib/inspec/resources/php_config.rb', line 29

def resource_id
  config_param
end

#to_sObject

Resource appearance in test reports.



34
35
36
# File 'lib/inspec/resources/php_config.rb', line 34

def to_s
  "php_config #{resource_id}"
end

#valueObject

Returns the value evaluated for the initialized config parameter



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/inspec/resources/php_config.rb', line 39

def value
  php_utility = find_utility_or_error

  # The keys in the hash provided by user can be string or symbols.
  # Converting the key to symbols to handle scenario when "ini" key is provided as string.
  config_file_or_path.transform_keys(&:to_sym)

  # Assign the path with -c option for ini file provided by the user if any.
  php_ini_file = !config_file_or_path.empty? && config_file_or_path.key?(:ini) ? "-c #{config_file_or_path[:ini]}" : ""

  # The below command `get_cfg_var` is used to fetch the value for any config parameter.
  php_cmd = "#{php_utility} #{php_ini_file} -r 'echo get_cfg_var(\"#{config_param}\");'"
  config_value_cmd = inspec.command(php_cmd)

  raise Inspec::Exceptions::ResourceFailed, "Executing #{php_cmd} failed: #{config_value_cmd.stderr}" if config_value_cmd.exit_status.to_i != 0

  config_value = config_value_cmd.stdout.strip

  # Convert value to integer if the config value are digits.
  config_value.match(/^(\d)+$/) ? config_value.to_i : config_value
end