[spiral/boot] Added the ability to configure bootloaders via BootloadConfig #1017
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Spiral continues to evolve, offering more flexibility and efficiency in its latest update. Developers now have enhanced control over the configuration of bootloaders. This update is particularly beneficial for tailoring applications to different environments, such as HTTP, CLI, or gRPC, by enabling or disabling specific bootloaders as needed. It allows for a more nuanced and environment-specific configuration of bootloaders. This feature is particularly useful in scenarios where certain bootloaders are only relevant in specific contexts. For instance, in an HTTP environment, bootloaders like
HTTPBootloader
andRoutesBootloader
are essential, whereas they might be unnecessary in CLI or gRPC environments. By enabling the selective enabling or disabling of bootloaders, it helps in optimizing resource usage and improving application performance.BootloadConfig
There is a new DTO class
Spiral\Boot\Attribute\BootloadConfig
which enables the inclusion or exclusion of bootloaders, passing parameters that will be forwarded to the init and boot methods of the bootloader, and dynamically adjusting the bootloader loading based on environment variables. It can be used within the Kernel as a value in the array of bootloaders. Alternatively, it can be used as an attribute that needs to be added to the bootloader class.The
BootloadConfig
class constructor accepts the following parameters:Usage in Kernel
To use it in the Kernel, you should employ the full class name of the bootloader as the key in the array of bootloaders, with the corresponding value being a
Spiral\Boot\Attribute\BootloadConfig
object.In this example, we specified that the PrototypeBootloader should be loaded only if the environment variable
APP_ENV
is defined and has a value of local or dev.Instead of creating a
BootloadConfig
object directly, you can define a function that returns aBootloadConfig
object. This function can take arguments, which might be obtained from the container.Using Attributes for Bootloader Configuration
You can also use
Spiral\Boot\Attribute\BootloadConfig
class as an attribute to control how a bootloader behaves. This method is particularly useful because it allows you to set up the configuration directly in the bootloader's class, making it more straightforward and easier to understand.Here's a simple example of how you can use an attribute to configure a bootloader:
Attributes are a great choice when you want to keep the configuration close to the bootloader's code. It's a more intuitive way to set up bootloaders, especially in cases where the configuration is straightforward and doesn't require complex logic. By using attributes, you can easily see and understand the conditions under which a particular bootloader will be active, all in one place.
Extending for Custom Preconditions
By extending
BootloadConfig
, you can create custom classes that encapsulate specific conditions under which bootloaders should operate. This approach simplifies the usage of bootloaders by abstracting the configuration details into these custom classes.Here's an example
Now you can use it in your bootloaders
or use in Kernel:
The ability to extend
BootloadConfig
opens up a world of possibilities for customizing the behavior of bootloaders.