-
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathcontainer.rb
More file actions
722 lines (656 loc) · 21.3 KB
/
Copy pathcontainer.rb
File metadata and controls
722 lines (656 loc) · 21.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
# frozen_string_literal: true
require 'pathname'
require 'dry-auto_inject'
require 'dry-configurable'
require 'dry-container'
require 'dry/inflector'
require 'dry/core/deprecations'
require 'dry/system'
require 'dry/system/errors'
require 'dry/system/loader'
require 'dry/system/booter'
require 'dry/system/auto_registrar'
require 'dry/system/auto_inject'
require 'dry/system/manual_registrar'
require 'dry/system/importer'
require 'dry/system/component'
require 'dry/system/constants'
require 'dry/system/plugins'
module Dry
module System
# Abstract container class to inherit from
#
# Container class is treated as a global registry with all system components.
# Container can also import dependencies from other containers, which is
# useful in complex systems that are split into sub-systems.
#
# Container can be finalized, which triggers loading of all the defined
# components within a system, after finalization it becomes frozen. This
# typically happens in cases like booting a web application.
#
# Before finalization, Container can lazy-load components on demand. A
# component can be a simple class defined in a single file, or a complex
# component which has init/start/stop lifecycle, and it's defined in a boot
# file. Components which specify their dependencies using Import module can
# be safely required in complete isolation, and Container will resolve and
# load these dependencies automatically.
#
# Furthermore, Container supports auto-registering components based on
# dir/file naming conventions. This reduces a lot of boilerplate code as all
# you have to do is to put your classes under configured directories and
# their instances will be automatically registered within a container.
#
# Every container needs to be configured with following settings:
#
# * `:name` - a unique container identifier
# * `:root` - a system root directory (defaults to `pwd`)
# * `:system_dir` - directory name relative to root, where bootable components
# can be defined in `boot` dir this defaults to `system`
#
# @example
# class MyApp < Dry::System::Container
# configure do |config|
# config.name = :my_app
#
# # this will auto-register classes from 'lib/components'. ie if you add
# # `lib/components/repo.rb` which defines `Repo` class, then it's
# # instance will be automatically available as `MyApp['repo']`
# config.auto_register = %w(lib/components)
# end
#
# # this will configure $LOAD_PATH to include your `lib` dir
# load_paths!('lib')
# end
#
# @api public
class Container
extend Dry::Configurable
extend Dry::Container::Mixin
extend Dry::System::Plugins
setting :name
setting :default_namespace
setting(:root, Pathname.pwd.freeze) { |path| Pathname(path) }
setting :system_dir, 'system'
setting :registrations_dir, 'container'
setting :auto_register, []
setting :inflector, Dry::Inflector.new
setting :loader, Dry::System::Loader
setting :booter, Dry::System::Booter
setting :auto_registrar, Dry::System::AutoRegistrar
setting :manual_registrar, Dry::System::ManualRegistrar
setting :importer, Dry::System::Importer
setting(:components, {}, reader: true, &:dup)
class << self
def strategies(value = nil)
if value
@strategies = value
else
@strategies ||= Dry::AutoInject::Strategies
end
end
extend Dry::Core::Deprecations['Dry::System::Container']
# Configures the container
#
# @example
# class MyApp < Dry::System::Container
# configure do |config|
# config.root = Pathname("/path/to/app")
# config.name = :my_app
# config.auto_register = %w(lib/apis lib/core)
# end
# end
#
# @return [self]
#
# @api public
def configure(&block)
super(&block)
load_paths!(config.system_dir)
hooks[:configure].each { |hook| instance_eval(&hook) }
self
end
# Registers another container for import
#
# @example
# # system/container.rb
# class Core < Dry::System::Container
# configure do |config|
# config.root = Pathname("/path/to/app")
# config.auto_register = %w(lib/apis lib/core)
# end
# end
#
# # apps/my_app/system/container.rb
# require 'system/container'
#
# class MyApp < Dry::System::Container
# configure do |config|
# config.root = Pathname("/path/to/app")
# config.auto_register = %w(lib/apis lib/core)
# end
#
# import core: Core
# end
#
# @param other [Hash, Dry::Container::Namespace]
#
# @api public
def import(other)
case other
when Hash then importer.register(other)
when Dry::Container::Namespace then super
else
raise ArgumentError, <<-STR
+other+ must be a hash of names and systems, or a Dry::Container namespace
STR
end
end
# Registers finalization function for a bootable component
#
# By convention, boot files for components should be placed in
# `%{system_dir}/boot` and they will be loaded on demand when components
# are loaded in isolation, or during finalization process.
#
# @example
# # system/container.rb
# class MyApp < Dry::System::Container
# configure do |config|
# config.root = Pathname("/path/to/app")
# config.name = :core
# config.auto_register = %w(lib/apis lib/core)
# end
#
# # system/boot/db.rb
# #
# # Simple component registration
# MyApp.boot(:db) do |container|
# require 'db'
#
# container.register(:db, DB.new)
# end
#
# # system/boot/db.rb
# #
# # Component registration with lifecycle triggers
# MyApp.boot(:db) do |container|
# init do
# require 'db'
# DB.configure(ENV['DB_URL'])
# container.register(:db, DB.new)
# end
#
# start do
# db.establish_connection
# end
#
# stop do
# db.close_connection
# end
# end
#
# # system/boot/db.rb
# #
# # Component registration which uses another bootable component
# MyApp.boot(:db) do |container|
# use :logger
#
# start do
# require 'db'
# DB.configure(ENV['DB_URL'], logger: logger)
# container.register(:db, DB.new)
# end
# end
#
# # system/boot/db.rb
# #
# # Component registration under a namespace. This will register the
# # db object under `persistence.db` key
# MyApp.namespace(:persistence) do |persistence|
# require 'db'
# DB.configure(ENV['DB_URL'], logger: logger)
# persistence.register(:db, DB.new)
# end
#
# @param name [Symbol] a unique identifier for a bootable component
#
# @see Lifecycle
#
# @return [self]
#
# @api public
def boot(name, opts = {}, &block)
if components.key?(name)
raise DuplicatedComponentKeyError, <<-STR
Bootable component #{name.inspect} was already registered
STR
end
component =
if opts[:from]
boot_external(name, opts, &block)
else
boot_local(name, opts, &block)
end
components[name] = component
end
deprecate :finalize, :boot
# @api private
def boot_external(identifier, from:, key: nil, namespace: nil, &block)
component = System.providers[from].component(
identifier, key: key, namespace: namespace, finalize: block, container: self
)
booter.register_component(component)
component
end
# @api private
def boot_local(identifier, namespace: nil, &block)
component = Components::Bootable.new(
identifier, container: self, namespace: namespace, &block
)
booter.register_component(component)
component
end
# Return if a container was finalized
#
# @return [TrueClass, FalseClass]
#
# @api public
def finalized?
@__finalized__.equal?(true)
end
# Finalizes the container
#
# This triggers importing components from other containers, booting
# registered components and auto-registering components. It should be
# called only in places where you want to finalize your system as a
# whole, ie when booting a web application
#
# @example
# # system/container.rb
# class MyApp < Dry::System::Container
# configure do |config|
# config.root = Pathname("/path/to/app")
# config.name = :my_app
# config.auto_register = %w(lib/apis lib/core)
# end
# end
#
# # You can put finalization file anywhere you want, ie system/boot.rb
# MyApp.finalize!
#
# # If you need last-moment adjustments just before the finalization
# # you can pass a block and do it there
# MyApp.finalize! do |container|
# # stuff that only needs to happen for finalization
# end
#
# @return [self] frozen container
#
# @api public
def finalize!(freeze: true, &block)
return self if finalized?
yield(self) if block
importer.finalize!
booter.finalize!
manual_registrar.finalize!
auto_registrar.finalize!
@__finalized__ = true
self.freeze if freeze
self
end
# Boots a specific component
#
# As a result, `init` and `start` lifecycle triggers are called
#
# @example
# MyApp.start(:persistence)
#
# @param name [Symbol] the name of a registered bootable component
#
# @return [self]
#
# @api public
def start(name)
booter.start(name)
self
end
# Boots a specific component but calls only `init` lifecycle trigger
#
# This way of booting is useful in places where a heavy dependency is
# needed but its started environment is not required
#
# @example
# MyApp.init(:persistence)
#
# @param [Symbol] name The name of a registered bootable component
#
# @return [self]
#
# @api public
def init(name)
booter.init(name)
self
end
# Stop a specific component but calls only `stop` lifecycle trigger
#
# @example
# MyApp.stop(:persistence)
#
# @param [Symbol] name The name of a registered bootable component
#
# @return [self]
#
# @api public
def stop(name)
booter.stop(name)
self
end
def shutdown!
booter.shutdown
self
end
# Sets load paths relative to the container's root dir
#
# @example
# class MyApp < Dry::System::Container
# configure do |config|
# # ...
# end
#
# load_paths!('lib')
# end
#
# @param [Array<String>] dirs
#
# @return [self]
#
# @api public
def load_paths!(*dirs)
dirs.map(&root.method(:join)).each do |path|
next if load_paths.include?(path)
load_paths << path
$LOAD_PATH.unshift(path.to_s)
end
self
end
# @api public
def load_registrations!(name)
manual_registrar.(name)
self
end
# Auto-registers components from the provided directory
#
# Typically you want to configure auto_register directories, and it will
# work automatically. Use this method in cases where you want to have an
# explicit way where some components are auto-registered, or if you want
# to exclude some components from being auto-registered
#
# @example
# class MyApp < Dry::System::Container
# configure do |config|
# # ...
# end
#
# # with a dir
# auto_register!('lib/core')
#
# # with a dir and a custom registration block
# auto_register!('lib/core') do |config|
# config.instance do |component|
# # custom way of initializing a component
# end
#
# config.exclude do |component|
# # return true to exclude component from auto-registration
# end
# end
# end
#
# @param [String] dir The dir name relative to the root dir
#
# @yield AutoRegistrar::Configuration
# @see AutoRegistrar::Configuration
#
# @return [self]
#
# @api public
def auto_register!(dir, &block)
auto_registrar.(dir, &block)
self
end
# Builds injector for this container
#
# An injector is a useful mixin which injects dependencies into
# automatically defined constructor.
#
# Also, it's possible to use `allow` method for build injector
# with specific pattern.
#
# @example
# # Define an injection mixin
# #
# # system/import.rb
# Import = MyApp.injector
#
# # Use it in your auto-registered classes
# #
# # lib/user_repo.rb
# require 'import'
#
# class UserRepo
# include Import['persistence.db']
# end
#
# MyApp['user_repo].db # instance under 'persistence.db' key
#
# @example with allow pattern
# # Define an injection mixin
# #
# # system/import.rb
# Import = MyApp.injector
# PersistanceImport = Import.allow(/\Apersistence\.\w+/)
#
# # Use it in your auto-registered classes
# #
# # lib/user_repo.rb
# require 'import'
#
# class UserRepo
# include PersistanceImport['persistence.db'] # okay
# end
#
# class WrongInjection
# include PersistanceImport['operations.service'] # will raise error
# end
#
# MyApp['user_repo].db # instance under 'persistence.db' key
#
# @param options [Hash] injector options
#
# @api public
def injector(options = { strategies: strategies })
Dry::System::AutoInject::Builder.new(self, options)
end
# Requires one or more files relative to the container's root
#
# @example
# # single file
# MyApp.require_from_root('lib/core')
#
# # glob
# MyApp.require_from_root('lib/**/*')
#
# @param paths [Array<String>] one or more paths, supports globs too
#
# @api public
def require_from_root(*paths)
paths.flat_map { |path|
path.to_s.include?('*') ? ::Dir[root.join(path)].sort : root.join(path)
}.each { |path|
require path.to_s
}
end
# Returns container's root path
#
# @example
# class MyApp < Dry::System::Container
# configure do |config|
# config.root = Pathname('/my/app')
# end
# end
#
# MyApp.root # returns '/my/app' pathname
#
# @return [Pathname]
#
# @api public
def root
config.root
end
# @api public
def resolve(key, &block)
load_component(key, &block) unless finalized?
super
end
alias_method :registered?, :key?
#
# @!method registered?(key)
# Whether a +key+ is registered (doesn't trigger loading)
# @param [String,Symbol] key Identifier
# @return [Boolean]
# @api public
#
# Check if identifier is registered.
# If not, try to load the component
#
# @param [String,Symbol] key Identifier
# @return [Boolean]
#
# @api public
def key?(key)
if finalized?
registered?(key)
else
registered?(key) || resolve(key) { return false }
true
end
end
# @api private
def load_paths
@load_paths ||= []
end
# @api private
def booter
@booter ||= config.booter.new(boot_path)
end
# @api private
def boot_path
root.join("#{config.system_dir}/boot")
end
# @api private
def auto_registrar
@auto_registrar ||= config.auto_registrar.new(self)
end
# @api private
def manual_registrar
@manual_registrar ||= config.manual_registrar.new(self)
end
# @api private
def importer
@importer ||= config.importer.new(self)
end
# @api private
def component(identifier, **options)
if (component = booter.components.detect { |c| c.identifier == identifier })
component
else
Component.new(
identifier,
loader: config.loader,
namespace: config.default_namespace,
separator: config.namespace_separator,
inflector: config.inflector,
**options
)
end
end
# @api private
def require_component(component)
return if registered?(component.identifier)
raise FileNotFoundError, component unless component.file_exists?(load_paths)
require_path(component.path)
yield
end
# Allows subclasses to use a different strategy for required files.
#
# E.g. apps that use `ActiveSupport::Dependencies::Loadable#require_dependency`
# will override this method to allow container managed dependencies to be reloaded
# for non-finalized containers.
#
# @api private
def require_path(path)
require path
end
# @api private
def load_component(key, &block)
return self if registered?(key)
component(key).tap do |component|
if component.boot?
booter.start(component)
else
root_key = component.root_key
if (bootable_dep = component(root_key)).boot?
booter.start(bootable_dep)
elsif importer.key?(root_key)
load_imported_component(component.namespaced(root_key))
end
load_local_component(component, &block) unless registered?(key)
end
end
self
end
# @api private
def after(event, &block)
hooks[event] << block
end
# @api private
def hooks
@hooks ||= Hash.new { |h, k| h[k] = [] }
end
# @api private
def inherited(klass)
new_hooks = Container.hooks.dup
hooks.each do |event, blocks|
new_hooks[event].concat(blocks)
new_hooks[event].concat(klass.hooks[event])
end
klass.instance_variable_set(:@hooks, new_hooks)
super
end
private
# @api private
def load_local_component(component, default_namespace_fallback = false, &block)
if booter.bootable?(component) || component.file_exists?(load_paths)
booter.boot_dependency(component) unless finalized?
require_component(component) do
register(component.identifier) { component.instance }
end
elsif !default_namespace_fallback
load_local_component(component.prepend(config.default_namespace), true, &block)
elsif manual_registrar.file_exists?(component)
manual_registrar.(component)
elsif block_given?
yield
else
raise ComponentLoadError, component
end
end
# @api private
def load_imported_component(component)
container = importer[component.namespace]
container.load_component(component.identifier)
importer.(component.namespace, container)
end
end
end
end
end