Class: Laravel::Configuration

Inherits:
Object
  • Object
show all
Includes:
AppSupport, Helpers
Defined in:
lib/laravel/configuration.rb

Overview

This class handles the configuration aspects of a Laravel based application. This allows us to read and update settings in ./application/config/application.php Furthermore, it allows us to read and update various options, at one go.

Constant Summary

Constants included from Helpers

Helpers::CacheFolder, Helpers::LaravelRepo

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AppSupport

#apply_force, #artisan, #cache_directory, #clean_up, #config_file, #copy_over_cache_files, #create_in_current_directory?, #create_in_empty_directory?, #download_or_update_local_cache, #has_cache?, #has_laravel?, #required_force_is_missing?, #source_is_local?, #tasks_file, #update_permissions_on_storage

Methods included from Helpers

#download_resource, #is_blank?, #is_current_directory?, #is_empty_directory?, #laravel_exists_in_directory?, #make_md5, #say, #say_failed, #say_success, #show_info

Constructor Details

#initialize(path = nil, config = nil) ⇒ Configuration

Create a new Configuration class, which helps us to configure an existing Laravel application.

Parameters

path

Path to an existing Laravel application, which can

either be relative or an absolute path. If path is not supplied, we assume current directory.

config

It can be a comma-separated string or an array or a hash

of ‘key:value` pairs. When creating a new app, config is passed as a comma-separated string.

Examples:
  index:"",key,profiler:on,url:http://laravel.com
  index,key:some_secret_key,ssl:enabled


29
30
31
32
33
34
35
36
37
38
39
# File 'lib/laravel/configuration.rb', line 29

def initialize(path = nil, config = nil)
  self.path = path

  # try to do anything only if this is a Laravel application
  # otherwise, raise an error
  raise LaravelNotFoundError unless has_laravel?

  # set the desired configuration options
  self.config = config

end

Instance Attribute Details

#configObject

Returns the value of attribute config.



11
12
13
# File 'lib/laravel/configuration.rb', line 11

def config
  @config
end

#pathObject

Returns the value of attribute path.



11
12
13
# File 'lib/laravel/configuration.rb', line 11

def path
  @path
end

Instance Method Details

#from_optionsObject

update the configuration settings by looking at the options that the user has provided when running the ‘new’ task.



117
118
119
120
121
# File 'lib/laravel/configuration.rb', line 117

def from_options
  # don't do anything, unless options were provided
  return if not @config or @config.empty?
  @config.each { |key, value| update(key, value) }
end

#process_input_for_key(value = nil) ⇒ Object

Process input when we are configuring the Application Key. By default, generates a random 32 char string, but if a string is passed as value, returns it without further processing.

Parameters

value

optional string, that if provided will be used as the new key

instead of generating a random one.

Returns

String

the new key for the application



134
135
136
137
# File 'lib/laravel/configuration.rb', line 134

def process_input_for_key(value = nil)
  return value unless is_blank?(value)
  make_md5
end

#process_input_for_profiler(value) ⇒ Object

Process input when we are configuring the Profiler setting. Simply checks if the supplied value corresponds to a ‘truthy’ value and returns it.

Parameters

value

a string that signifies either the truth or false. It can take

multiple values like enabled, active, on, etc. to signify truth.

Return

boolean

the new value for the Profiler setting



150
151
152
# File 'lib/laravel/configuration.rb', line 150

def process_input_for_profiler(value)
  __convert_action_to_boolean value
end

#process_input_for_ssl(value) ⇒ Object

Process input when we are configuring the SSL setting. Simply checks if the supplied value corresponds to a ‘truthy’ value and returns it.

Parameters

value

a string that signifies either the truth or false. It can take

multiple values like enabled, active, on, etc. to signify truth.

Return

boolean

the new value for the SSL setting



165
166
167
# File 'lib/laravel/configuration.rb', line 165

def process_input_for_ssl(value)
  __convert_action_to_boolean value
end

#read(config) ⇒ Object

Reads a configuration setting from the configuration file

Parameters

config

one of the configuration settings as provided in the

configuration file for the application



103
104
105
106
107
108
109
110
111
112
# File 'lib/laravel/configuration.rb', line 103

def read(config)
  return if unsupported? config
  value = __read_configuration(config)
  value = "__empty_string__" if is_blank?(value)
  if value
    say_success "Configuration: #{config} => #{value}"
  else
    say_failed "Could not read configuration: #{config}"
  end
end

#update(config, value) ⇒ Object

Update a configuration setting in the configuration file

Parameters

config

one of the configuration settings as provided in the

configuration file for the application

value

the new value for the supplied config setting. Note that,

for configuration settings that take a boolean value, you can pass values like ‘true’, ‘enabled’, ‘active’, ‘on’, 1 etc. to signify truth.



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/laravel/configuration.rb', line 85

def update(config, value)
  return if unsupported? config
  value   = process_input(config, value)
  updated = __update_configuration(config, value)
  value   = "__empty_string__" if is_blank?(value)
  if updated
    say_success "Updated configuration: #{config} => #{value}"
  else
    say_failed "Could not update configuration: #{config}"
  end
end