hanami 1.3.1 → 2.0.0.alpha2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (168) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +110 -6
  3. data/FEATURES.md +9 -1
  4. data/LICENSE.md +1 -1
  5. data/README.md +4 -5
  6. data/hanami.gemspec +26 -30
  7. data/lib/hanami.rb +40 -243
  8. data/lib/hanami/application.rb +338 -174
  9. data/lib/hanami/application/autoloader/inflector_adapter.rb +22 -0
  10. data/lib/hanami/application/container/boot/inflector.rb +7 -0
  11. data/lib/hanami/application/container/boot/logger.rb +8 -0
  12. data/lib/hanami/application/container/boot/rack_logger.rb +19 -0
  13. data/lib/hanami/application/container/boot/rack_monitor.rb +12 -0
  14. data/lib/hanami/application/container/boot/settings.rb +7 -0
  15. data/lib/hanami/application/router.rb +59 -0
  16. data/lib/hanami/application/routing/middleware/stack.rb +89 -0
  17. data/lib/hanami/application/routing/resolver.rb +82 -0
  18. data/lib/hanami/application/routing/resolver/node.rb +50 -0
  19. data/lib/hanami/application/routing/resolver/trie.rb +59 -0
  20. data/lib/hanami/application/settings.rb +23 -0
  21. data/lib/hanami/application/settings/definition.rb +26 -0
  22. data/lib/hanami/application/settings/loader.rb +97 -0
  23. data/lib/hanami/application/settings/struct.rb +65 -0
  24. data/lib/hanami/boot.rb +5 -0
  25. data/lib/hanami/cli/application/cli.rb +40 -0
  26. data/lib/hanami/cli/application/command.rb +47 -0
  27. data/lib/hanami/cli/application/commands.rb +16 -0
  28. data/lib/hanami/cli/application/commands/console.rb +81 -0
  29. data/lib/hanami/cli/base_command.rb +48 -0
  30. data/lib/hanami/cli/commands.rb +6 -42
  31. data/lib/hanami/cli/commands/command.rb +20 -30
  32. data/lib/hanami/cli/commands/server.rb +63 -14
  33. data/lib/hanami/configuration.rb +248 -190
  34. data/lib/hanami/configuration/middleware.rb +8 -28
  35. data/lib/hanami/configuration/router.rb +50 -0
  36. data/lib/hanami/configuration/sessions.rb +50 -0
  37. data/lib/hanami/init.rb +5 -0
  38. data/lib/hanami/server.rb +5 -69
  39. data/lib/hanami/setup.rb +8 -2
  40. data/lib/hanami/slice.rb +138 -0
  41. data/lib/hanami/version.rb +3 -1
  42. data/lib/hanami/web/rack_logger.rb +96 -0
  43. metadata +84 -228
  44. data/bin/hanami +0 -6
  45. data/lib/hanami/action/csrf_protection.rb +0 -211
  46. data/lib/hanami/action/routing_helpers.rb +0 -40
  47. data/lib/hanami/app.rb +0 -96
  48. data/lib/hanami/application_configuration.rb +0 -1495
  49. data/lib/hanami/application_name.rb +0 -108
  50. data/lib/hanami/application_namespace.rb +0 -14
  51. data/lib/hanami/assets/asset.rb +0 -72
  52. data/lib/hanami/assets/static.rb +0 -102
  53. data/lib/hanami/cli/commands/assets.rb +0 -16
  54. data/lib/hanami/cli/commands/assets/precompile.rb +0 -42
  55. data/lib/hanami/cli/commands/console.rb +0 -95
  56. data/lib/hanami/cli/commands/db.rb +0 -32
  57. data/lib/hanami/cli/commands/db/apply.rb +0 -32
  58. data/lib/hanami/cli/commands/db/console.rb +0 -44
  59. data/lib/hanami/cli/commands/db/create.rb +0 -32
  60. data/lib/hanami/cli/commands/db/drop.rb +0 -32
  61. data/lib/hanami/cli/commands/db/migrate.rb +0 -39
  62. data/lib/hanami/cli/commands/db/prepare.rb +0 -32
  63. data/lib/hanami/cli/commands/db/rollback.rb +0 -59
  64. data/lib/hanami/cli/commands/db/version.rb +0 -32
  65. data/lib/hanami/cli/commands/destroy.rb +0 -26
  66. data/lib/hanami/cli/commands/destroy/action.rb +0 -137
  67. data/lib/hanami/cli/commands/destroy/app.rb +0 -159
  68. data/lib/hanami/cli/commands/destroy/mailer.rb +0 -74
  69. data/lib/hanami/cli/commands/destroy/migration.rb +0 -51
  70. data/lib/hanami/cli/commands/destroy/model.rb +0 -84
  71. data/lib/hanami/cli/commands/generate.rb +0 -28
  72. data/lib/hanami/cli/commands/generate/action.rb +0 -321
  73. data/lib/hanami/cli/commands/generate/action/action.erb +0 -7
  74. data/lib/hanami/cli/commands/generate/action/action_spec.minitest.erb +0 -11
  75. data/lib/hanami/cli/commands/generate/action/action_spec.rspec.erb +0 -9
  76. data/lib/hanami/cli/commands/generate/action/action_without_view.erb +0 -7
  77. data/lib/hanami/cli/commands/generate/action/view.erb +0 -7
  78. data/lib/hanami/cli/commands/generate/action/view_spec.minitest.erb +0 -12
  79. data/lib/hanami/cli/commands/generate/action/view_spec.rspec.erb +0 -10
  80. data/lib/hanami/cli/commands/generate/app.rb +0 -243
  81. data/lib/hanami/cli/commands/generate/app/application.erb +0 -313
  82. data/lib/hanami/cli/commands/generate/app/favicon.ico +0 -0
  83. data/lib/hanami/cli/commands/generate/app/gitkeep.erb +0 -0
  84. data/lib/hanami/cli/commands/generate/app/layout.erb +0 -7
  85. data/lib/hanami/cli/commands/generate/app/layout_spec.minitest.erb +0 -10
  86. data/lib/hanami/cli/commands/generate/app/layout_spec.rspec.erb +0 -10
  87. data/lib/hanami/cli/commands/generate/app/routes.erb +0 -5
  88. data/lib/hanami/cli/commands/generate/app/template.erb.erb +0 -10
  89. data/lib/hanami/cli/commands/generate/app/template.haml.erb +0 -7
  90. data/lib/hanami/cli/commands/generate/app/template.slim.erb +0 -8
  91. data/lib/hanami/cli/commands/generate/mailer.rb +0 -104
  92. data/lib/hanami/cli/commands/generate/mailer/mailer.erb +0 -9
  93. data/lib/hanami/cli/commands/generate/mailer/mailer_spec.minitest.erb +0 -7
  94. data/lib/hanami/cli/commands/generate/mailer/mailer_spec.rspec.erb +0 -5
  95. data/lib/hanami/cli/commands/generate/migration.rb +0 -41
  96. data/lib/hanami/cli/commands/generate/migration/migration.erb +0 -4
  97. data/lib/hanami/cli/commands/generate/model.rb +0 -125
  98. data/lib/hanami/cli/commands/generate/model/entity.erb +0 -2
  99. data/lib/hanami/cli/commands/generate/model/entity_spec.minitest.erb +0 -5
  100. data/lib/hanami/cli/commands/generate/model/entity_spec.rspec.erb +0 -3
  101. data/lib/hanami/cli/commands/generate/model/migration.erb +0 -10
  102. data/lib/hanami/cli/commands/generate/model/repository.erb +0 -5
  103. data/lib/hanami/cli/commands/generate/model/repository_spec.minitest.erb +0 -5
  104. data/lib/hanami/cli/commands/generate/model/repository_spec.rspec.erb +0 -3
  105. data/lib/hanami/cli/commands/generate/secret.rb +0 -48
  106. data/lib/hanami/cli/commands/new.rb +0 -578
  107. data/lib/hanami/cli/commands/new/.env.development.erb +0 -3
  108. data/lib/hanami/cli/commands/new/.env.test.erb +0 -3
  109. data/lib/hanami/cli/commands/new/.gitkeep.erb +0 -0
  110. data/lib/hanami/cli/commands/new/Gemfile.erb +0 -58
  111. data/lib/hanami/cli/commands/new/README.md.erb +0 -33
  112. data/lib/hanami/cli/commands/new/config.ru.erb +0 -3
  113. data/lib/hanami/cli/commands/new/config/boot.erb +0 -2
  114. data/lib/hanami/cli/commands/new/config/environment.erb +0 -49
  115. data/lib/hanami/cli/commands/new/gitignore.erb +0 -2
  116. data/lib/hanami/cli/commands/new/gitignore_with_sqlite.erb +0 -3
  117. data/lib/hanami/cli/commands/new/hanamirc.erb +0 -3
  118. data/lib/hanami/cli/commands/new/lib/project.erb +0 -2
  119. data/lib/hanami/cli/commands/new/minitest/Rakefile.erb +0 -12
  120. data/lib/hanami/cli/commands/new/minitest/features_helper.erb +0 -11
  121. data/lib/hanami/cli/commands/new/minitest/spec_helper.erb +0 -7
  122. data/lib/hanami/cli/commands/new/rspec/Rakefile.erb +0 -9
  123. data/lib/hanami/cli/commands/new/rspec/capybara.erb +0 -8
  124. data/lib/hanami/cli/commands/new/rspec/features_helper.erb +0 -12
  125. data/lib/hanami/cli/commands/new/rspec/rspec.erb +0 -2
  126. data/lib/hanami/cli/commands/new/rspec/spec_helper.erb +0 -103
  127. data/lib/hanami/cli/commands/new/schema.sql.erb +0 -0
  128. data/lib/hanami/cli/commands/project.rb +0 -421
  129. data/lib/hanami/cli/commands/routes.rb +0 -21
  130. data/lib/hanami/cli/commands/templates.rb +0 -31
  131. data/lib/hanami/cli/commands/version.rb +0 -19
  132. data/lib/hanami/common_logger.rb +0 -93
  133. data/lib/hanami/components.rb +0 -156
  134. data/lib/hanami/components/app/assets.rb +0 -59
  135. data/lib/hanami/components/app/controller.rb +0 -74
  136. data/lib/hanami/components/app/routes.rb +0 -59
  137. data/lib/hanami/components/app/view.rb +0 -44
  138. data/lib/hanami/components/component.rb +0 -182
  139. data/lib/hanami/components/components.rb +0 -479
  140. data/lib/hanami/components/routes_inspector.rb +0 -72
  141. data/lib/hanami/config/cookies.rb +0 -69
  142. data/lib/hanami/config/framework_configuration.rb +0 -43
  143. data/lib/hanami/config/load_paths.rb +0 -46
  144. data/lib/hanami/config/mapper.rb +0 -47
  145. data/lib/hanami/config/routes.rb +0 -20
  146. data/lib/hanami/config/security.rb +0 -110
  147. data/lib/hanami/config/sessions.rb +0 -103
  148. data/lib/hanami/configuration/app.rb +0 -18
  149. data/lib/hanami/early_hints.rb +0 -129
  150. data/lib/hanami/env.rb +0 -69
  151. data/lib/hanami/environment.rb +0 -513
  152. data/lib/hanami/environment_application_configurations.rb +0 -37
  153. data/lib/hanami/frameworks.rb +0 -22
  154. data/lib/hanami/hanamirc.rb +0 -169
  155. data/lib/hanami/mailer/glue.rb +0 -27
  156. data/lib/hanami/middleware_stack.rb +0 -172
  157. data/lib/hanami/rake_helper.rb +0 -78
  158. data/lib/hanami/rake_tasks.rb +0 -2
  159. data/lib/hanami/rendering_policy.rb +0 -95
  160. data/lib/hanami/routes.rb +0 -219
  161. data/lib/hanami/routing/default.rb +0 -32
  162. data/lib/hanami/static.rb +0 -63
  163. data/lib/hanami/templates/default.html.erb +0 -30
  164. data/lib/hanami/templates/welcome.html.erb +0 -52
  165. data/lib/hanami/views/default.rb +0 -37
  166. data/lib/hanami/views/default_template_finder.rb +0 -22
  167. data/lib/hanami/views/null_view.rb +0 -15
  168. data/lib/hanami/welcome.rb +0 -36
@@ -1,12 +1,10 @@
1
- require 'hanami'
2
- require 'hanami/environment'
3
- require 'hanami/components'
4
- require 'hanami/cli/command'
5
- require 'hanami/cli/commands/project'
6
- require 'hanami/cli/commands/templates'
7
- require 'concurrent'
8
- require 'hanami/utils/files'
9
- require 'erb'
1
+ # frozen_string_literal: true
2
+
3
+ require "hanami"
4
+ require "hanami/cli/command"
5
+ require "concurrent"
6
+ require "hanami/utils/files"
7
+ require "erb"
10
8
 
11
9
  module Hanami
12
10
  # Hanami CLI
@@ -17,7 +15,7 @@ module Hanami
17
15
  # Abstract command
18
16
  #
19
17
  # @since 1.1.0
20
- class Command < Hanami::CLI::Command
18
+ class Command < Dry::CLI::Command
21
19
  # @since 1.1.0
22
20
  # @api private
23
21
  def self.inherited(component)
@@ -72,20 +70,11 @@ module Hanami
72
70
  # @since 1.1.0
73
71
  # @api private
74
72
  def call(**options)
75
- if self.class.requirements.any?
76
- environment = Hanami::Environment.new(options)
77
- environment.require_project_environment
78
-
79
- requirements.resolved('environment', environment)
80
- requirements.resolve(self.class.requirements)
81
-
82
- options = environment.to_options.merge(options)
83
- end
84
-
73
+ # FIXME: merge ENV vars (like HANAMI_ENV) into **options
85
74
  super(options)
86
- rescue StandardError => e
87
- warn e.message
88
- warn e.backtrace.join("\n\t")
75
+ rescue StandardError => exception
76
+ warn exception.message
77
+ warn exception.backtrace.join("\n\t")
89
78
  exit(1)
90
79
  end
91
80
  end
@@ -95,9 +84,8 @@ module Hanami
95
84
  def initialize(command_name:, out: $stdout, files: Utils::Files)
96
85
  super(command_name: command_name)
97
86
 
98
- @out = out
99
- @files = files
100
- @templates = Templates.new(self.class)
87
+ @out = out
88
+ @files = files
101
89
  end
102
90
 
103
91
  private
@@ -109,7 +97,7 @@ module Hanami
109
97
  class Renderer
110
98
  # @since 1.1.0
111
99
  # @api private
112
- TRIM_MODE = "-".freeze
100
+ TRIM_MODE = "-"
113
101
 
114
102
  # @since 1.1.0
115
103
  # @api private
@@ -126,7 +114,7 @@ module Hanami
126
114
 
127
115
  # @since 1.1.0
128
116
  # @api private
129
- SAY_FORMATTER = "%<operation>12s %<path>s\n".freeze
117
+ SAY_FORMATTER = "%<operation>12s %<path>s\n"
130
118
 
131
119
  # @since 1.1.0
132
120
  # @api private
@@ -167,13 +155,15 @@ module Hanami
167
155
  # @since 1.1.0
168
156
  # @api private
169
157
  def project
170
- Project
158
+ # FIXME: is this still useful?
159
+ raise "it used to be implemented as Project"
171
160
  end
172
161
 
173
162
  # @since 1.1.0
174
163
  # @api private
175
164
  def requirements
176
- Hanami::Components
165
+ # FIXME: require components via the new Hanami::Container
166
+ raise "it used to be implemented as Hanami::Components"
177
167
  end
178
168
  end
179
169
  end
@@ -1,35 +1,84 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Hanami
4
+ # Hanami CLI
5
+ #
6
+ # @since 1.1.0
2
7
  class CLI
3
8
  module Commands
4
9
  # @since 1.1.0
5
10
  # @api private
6
11
  class Server < Command
7
- requires 'code_reloading'
8
-
9
12
  desc "Start Hanami server (only for development)"
10
13
 
11
- option :server, desc: "Force a server engine (eg, webrick, puma, thin, etc..)"
12
- option :host, desc: "The host address to bind to"
13
- option :port, desc: "The port to run the server on", aliases: ["-p"]
14
- option :debug, desc: "Turn on debug output"
15
- option :warn, desc: "Turn on warnings"
16
- option :daemonize, desc: "Daemonize the server"
17
- option :pid, desc: "Path to write a pid file after daemonize"
18
- option :code_reloading, desc: "Code reloading", type: :boolean, default: true
14
+ option :server, desc: "Force a server engine (eg, webrick, puma, thin, etc..)"
15
+ option :host, desc: "The host address to bind to"
16
+ option :port, desc: "The port to run the server on", aliases: ["-p"]
17
+ option :debug, desc: "Turn on debug output", type: :boolean
18
+ option :warn, desc: "Turn on warnings", type: :boolean
19
+ option :daemonize, desc: "Daemonize the server", type: :boolean
20
+ option :pid, desc: "Path to write a pid file after daemonize"
19
21
 
20
22
  example [
21
23
  " # Basic usage (it uses the bundled server engine)",
22
24
  "--server=webrick # Force `webrick` server engine",
23
25
  "--host=0.0.0.0 # Bind to a host",
24
- "--port=2306 # Bind to a port",
25
- "--no-code-reloading # Disable code reloading"
26
+ "--port=2306 # Bind to a port"
26
27
  ]
27
28
 
28
29
  # @since 1.1.0
29
30
  # @api private
30
- def call(*)
31
+ def call(**args)
32
+ require "hanami"
33
+ require "hanami/container"
31
34
  require "hanami/server"
32
- Hanami::Server.new.start
35
+
36
+ options = parse_arguments(args)
37
+ Hanami::Server.new(options).start
38
+ end
39
+
40
+ private
41
+
42
+ DEFAULT_CONFIG = "config.ru"
43
+ private_constant :DEFAULT_CONFIG
44
+
45
+ DEFAULT_HOST = "0.0.0.0"
46
+ private_constant :DEFAULT_HOST
47
+
48
+ DEFAULT_PORT = "2300"
49
+ private_constant :DEFAULT_PORT
50
+
51
+ OPTIONAL_SETTINGS = %i[
52
+ server
53
+ debug
54
+ warn
55
+ daemonize
56
+ pid
57
+ ].freeze
58
+
59
+ def parse_arguments(args)
60
+ Hanami::Container.start(:env)
61
+
62
+ {
63
+ config: DEFAULT_CONFIG,
64
+ Host: host(args),
65
+ Port: port(args),
66
+ AccessLog: []
67
+ }.merge(
68
+ args.slice(*OPTIONAL_SETTINGS)
69
+ )
70
+ end
71
+
72
+ def host(args)
73
+ args.fetch(:host) do
74
+ ENV.fetch("HANAMI_HOST", DEFAULT_HOST)
75
+ end
76
+ end
77
+
78
+ def port(args)
79
+ args.fetch(:port) do
80
+ ENV.fetch("HANAMI_PORT", DEFAULT_PORT)
81
+ end
33
82
  end
34
83
  end
35
84
  end
@@ -1,212 +1,270 @@
1
- require 'concurrent'
2
- require 'hanami/application'
3
- require 'hanami/utils/class'
4
- require 'hanami/utils/string'
1
+ # frozen_string_literal: true
2
+
3
+ require "uri"
4
+ require "concurrent/hash"
5
+ require "concurrent/array"
6
+ require "dry/inflector"
7
+ require "pathname"
8
+ require "zeitwerk"
5
9
 
6
10
  module Hanami
7
- # @api private
11
+ # Hanami application configuration
12
+ #
13
+ # @since 2.0.0
14
+ #
15
+ # rubocop:disable Metrics/ClassLength
8
16
  class Configuration
9
- require "hanami/configuration/app"
10
- require "hanami/configuration/middleware"
11
-
12
- # @api private
13
- def initialize(&blk)
14
- @settings = Concurrent::Map.new
15
- instance_eval(&blk)
16
- end
17
-
18
- # Mount a Hanami::Application or a Rack app
19
- #
20
- # @param app [#call] an application compatible with Rack SPEC
21
- # @param options [Hash] a set of options
22
- # @option :at [String] options the mount point
23
- #
24
- # @since 0.9.0
25
- #
26
- # @example
27
- # # config/environment.rb
28
- # # ...
29
- # Hanami.configure do
30
- # mount Web::Application, at: '/'
31
- #
32
- # # ...
33
- # end
34
- def mount(app, options)
35
- mounted[app] = App.new(app, options.fetch(:at))
36
- end
37
-
38
- # Configure database
39
- #
40
- # @param blk [Proc] the database configuration
41
- #
42
- # @see Hanami::Model.configure
43
- #
44
- # @example
45
- # # config/environment.rb
46
- # # ...
47
- # Hanami.configure do
48
- # model do
49
- # adapter :sql, ENV['DATABASE_URL']
50
- #
51
- # migrations 'db/migrations'
52
- # schema 'db/schema.sql'
53
- # end
54
- #
55
- # # ...
56
- # end
57
- def model(&blk)
58
- if block_given?
59
- settings.put_if_absent(:model, blk)
60
- else
61
- settings.fetch(:model)
17
+ require_relative "configuration/middleware"
18
+ require_relative "configuration/router"
19
+ require_relative "configuration/sessions"
20
+
21
+ attr_reader :actions
22
+ attr_reader :views
23
+
24
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
25
+ def initialize(env:)
26
+ @settings = Concurrent::Hash.new
27
+
28
+ self.autoloader = Zeitwerk::Loader.new
29
+
30
+ self.env = env
31
+ self.environments = DEFAULT_ENVIRONMENTS.clone
32
+
33
+ self.root = Dir.pwd
34
+ self.slices_dir = DEFAULT_SLICES_DIR
35
+ settings[:slices] = {}
36
+
37
+ self.settings_path = DEFAULT_SETTINGS_PATH
38
+ self.settings_loader_options = {}
39
+
40
+ self.base_url = DEFAULT_BASE_URL
41
+
42
+ self.logger = DEFAULT_LOGGER.clone
43
+ self.rack_logger_filter_params = DEFAULT_RACK_LOGGER_FILTER_PARAMS.clone
44
+ self.sessions = DEFAULT_SESSIONS
45
+
46
+ self.router = Router.new(base_url)
47
+ self.middleware = Middleware.new
48
+
49
+ self.inflections = Dry::Inflector.new
50
+
51
+ @actions = begin
52
+ require_path = "hanami/action/application_configuration"
53
+ require require_path
54
+ Hanami::Action::ApplicationConfiguration.new
55
+ rescue LoadError => e
56
+ raise e unless e.path == require_path
57
+ Object.new
62
58
  end
63
- end
64
59
 
65
- # Configure mailer
66
- #
67
- # @param blk [Proc] the mailer configuration
68
- #
69
- # @see Hanami::Mailer.configure
70
- #
71
- # @example
72
- # # config/environment.rb
73
- # # ...
74
- # Hanami.configure do
75
- # mailer do
76
- # root 'lib/bookshelf/mailers'
77
- #
78
- # # See http://hanamirb.org/guides/mailers/delivery
79
- # delivery :test
80
- # end
81
- #
82
- # # ...
83
- # end
84
- def mailer(&blk)
85
- mailer_settings.push(blk) if block_given?
86
- end
87
-
88
- # @since next
89
- # @api private
90
- def mailer_settings
91
- settings.fetch_or_store(:mailers, [])
92
- end
93
-
94
- # @since 0.9.0
95
- # @api private
96
- def mounted
97
- settings.fetch_or_store(:mounted, {})
98
- end
99
-
100
- # @since 1.2.0
101
- #
102
- # @example
103
- # # config/environment.rb
104
- # # ...
105
- # Hanami.configure do
106
- # middleware.use MyRackMiddleware
107
- # end
108
- def middleware
109
- settings.fetch_or_store(:middleware, Configuration::Middleware.new)
110
- end
111
-
112
- # Setup Early Hints feature
113
- #
114
- # @since 1.2.0
115
- #
116
- # @example Enable for all the environments
117
- # # config/environment.rb
118
- # Hanami.configure do
119
- # early_hints true
120
- # end
121
- #
122
- # @example Enable only for production
123
- # # config/environment.rb
124
- # Hanami.configure do
125
- # environment :production do
126
- # early_hints true
127
- # end
128
- # end
129
- def early_hints(value = nil)
130
- if value.nil?
131
- settings.fetch(:early_hints, false)
132
- else
133
- settings[:early_hints] = value
60
+ @views = begin
61
+ require_path = "hanami/view/application_configuration"
62
+ require require_path
63
+ Hanami::View::ApplicationConfiguration.new
64
+ rescue LoadError => e
65
+ raise e unless e.path == require_path
66
+ Object.new
134
67
  end
135
68
  end
69
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
136
70
 
137
- # @since 0.9.0
138
- # @api private
139
- def apps
140
- mounted.each_pair do |klass, app|
141
- yield(app) if klass <= Hanami::Application
71
+ def finalize
72
+ environment_for(env).each do |blk|
73
+ instance_eval(&blk)
142
74
  end
75
+
76
+ # Finalize nested configuration
77
+ #
78
+ # TODO: would be good to just create empty configurations for actions/views
79
+ # instead of plain objects
80
+ actions.finalize! if actions.respond_to?(:finalize!)
81
+ views.finalize! if views.respond_to?(:finalize!)
82
+
83
+ self
84
+ end
85
+
86
+ def environment(name, &blk)
87
+ environment_for(name).push(blk)
88
+ end
89
+
90
+ def autoloader=(loader)
91
+ settings[:autoloader] = loader || nil
143
92
  end
144
93
 
145
- # Configure logger
146
- #
147
- # @since 1.0.0
148
- #
149
- # @param options [Array] a set of options
150
- #
151
- # @see Hanami.logger
152
- # @see Hanami::Logger
153
- #
154
- # @see http://hanamirb.org/guides/projects/logging/
155
- #
156
- # @example Basic Usage
157
- # # config/environment.rb
158
- # # ...
159
- # Hanami.configure do
160
- # # ...
161
- # environment :development do
162
- # logger level: :debug
163
- # end
164
- # end
165
- #
166
- # @example Daily Rotation
167
- # # config/environment.rb
168
- # # ...
169
- # Hanami.configure do
170
- # # ...
171
- # environment :development do
172
- # logger 'daily', level: :debug
173
- # end
174
- # end
175
- def logger(*options)
176
- if options.empty?
177
- settings.fetch(:logger, nil)
94
+ def autoloader
95
+ settings.fetch(:autoloader)
96
+ end
97
+
98
+ def env=(value)
99
+ settings[:env] = value
100
+ end
101
+
102
+ def env
103
+ settings.fetch(:env)
104
+ end
105
+
106
+ def root=(root)
107
+ settings[:root] = Pathname(root)
108
+ end
109
+
110
+ def root
111
+ settings.fetch(:root)
112
+ end
113
+
114
+ def slices_dir=(dir)
115
+ settings[:slices_dir] = dir
116
+ end
117
+
118
+ def slices_dir
119
+ settings.fetch(:slices_dir)
120
+ end
121
+
122
+ def slices_namespace=(namespace)
123
+ settings[:slices_namespace] = namespace
124
+ end
125
+
126
+ def slices_namespace
127
+ settings.fetch(:slices_namespace) { Object }
128
+ end
129
+
130
+ def slice(slice_name, &block)
131
+ settings[:slices][slice_name] = block
132
+ end
133
+
134
+ def slices
135
+ settings[:slices]
136
+ end
137
+
138
+ def settings_path=(value)
139
+ settings[:settings_path] = value
140
+ end
141
+
142
+ def settings_path
143
+ settings.fetch(:settings_path)
144
+ end
145
+
146
+ def settings_loader=(loader)
147
+ settings[:settings_loader] = loader
148
+ end
149
+
150
+ def settings_loader
151
+ settings.fetch(:settings_loader) {
152
+ require "hanami/application/settings/loader"
153
+ settings[:settings_loader] = Application::Settings::Loader
154
+ }
155
+ end
156
+
157
+ def settings_loader_options=(options)
158
+ settings[:settings_loader_options] = options
159
+ end
160
+
161
+ def settings_loader_options
162
+ settings[:settings_loader_options]
163
+ end
164
+
165
+ def base_url=(value)
166
+ settings[:base_url] = URI.parse(value)
167
+ end
168
+
169
+ def base_url
170
+ settings.fetch(:base_url)
171
+ end
172
+
173
+ def logger=(options)
174
+ settings[:logger] = options
175
+ end
176
+
177
+ def logger
178
+ settings.fetch(:logger)
179
+ end
180
+
181
+ def rack_logger_filter_params=(params)
182
+ settings[:rack_logger_filter_params] = params
183
+ end
184
+
185
+ def rack_logger_filter_params
186
+ settings[:rack_logger_filter_params]
187
+ end
188
+
189
+ def router=(value)
190
+ settings[:router] = value
191
+ end
192
+
193
+ def router
194
+ settings.fetch(:router)
195
+ end
196
+
197
+ def sessions=(*args)
198
+ settings[:sessions] = Sessions.new(args)
199
+ end
200
+
201
+ def sessions
202
+ settings.fetch(:sessions)
203
+ end
204
+
205
+ def middleware
206
+ settings.fetch(:middleware)
207
+ end
208
+
209
+ def inflections(&blk)
210
+ if blk.nil?
211
+ settings.fetch(:inflections)
178
212
  else
179
- settings[:logger] = options
213
+ settings[:inflections] = Dry::Inflector.new(&blk)
180
214
  end
181
215
  end
182
216
 
183
- # Configure settings for the current environment
184
- # @since 1.0.0
185
- #
186
- # @param name [Symbol] the name of the Hanami environment
187
- #
188
- # @see Hanami.env
189
- #
190
- # @example Configure Logging for Different Environments
191
- # # config/environment.rb
192
- # # ...
193
- # Hanami.configure do
194
- # # ...
195
- # environment :development do
196
- # logger level: :debug
197
- # end
198
- #
199
- # environment :production do
200
- # logger level: :info, formatter: :json
201
- # end
202
- # end
203
- def environment(name)
204
- yield if ENV['HANAMI_ENV'] == name.to_s
217
+ alias inflector inflections
218
+
219
+ def for_each_middleware(&blk)
220
+ stack = middleware.stack.dup
221
+ stack += sessions.middleware if sessions.enabled?
222
+
223
+ stack.each(&blk)
224
+ end
225
+
226
+ protected
227
+
228
+ def environment_for(name)
229
+ settings[:environments][name]
230
+ end
231
+
232
+ def environments=(values)
233
+ settings[:environments] = values
234
+ end
235
+
236
+ def middleware=(value)
237
+ settings[:middleware] = value
238
+ end
239
+
240
+ def inflections=(value)
241
+ settings[:inflections] = value
205
242
  end
206
243
 
207
244
  private
208
245
 
209
- # @api private
246
+ DEFAULT_ENVIRONMENTS = Concurrent::Hash.new { |h, k| h[k] = Concurrent::Array.new }
247
+ private_constant :DEFAULT_ENVIRONMENTS
248
+
249
+ DEFAULT_SLICES_DIR = "slices"
250
+ private_constant :DEFAULT_SLICES_DIR
251
+
252
+ DEFAULT_BASE_URL = "http://0.0.0.0:2300"
253
+ private_constant :DEFAULT_BASE_URL
254
+
255
+ DEFAULT_LOGGER = { level: :debug }.freeze
256
+ private_constant :DEFAULT_LOGGER
257
+
258
+ DEFAULT_RACK_LOGGER_FILTER_PARAMS = %w[_csrf password password_confirmation].freeze
259
+ private_constant :DEFAULT_RACK_LOGGER_FILTER_PARAMS
260
+
261
+ DEFAULT_SETTINGS_PATH = File.join("config", "settings")
262
+ private_constant :DEFAULT_SETTINGS_PATH
263
+
264
+ DEFAULT_SESSIONS = Sessions.null
265
+ private_constant :DEFAULT_SESSIONS
266
+
210
267
  attr_reader :settings
211
268
  end
269
+ # rubocop:enable Metrics/ClassLength
212
270
  end