Modularity implements its application flow in two phases:
- First, the application's dependencies tree is "composed" by collecting services declared in modules, adding sub-containers, and connecting other applications.
- After that, the application dependency tree is locked, and the services are "consumed" to execute their behavior.
The Package
class implements the two phases above, respectively, in the two methods:
Package::build()
Package::boot()
It must be noted that Package::boot()
, before proceeding with the "boot" phase, will execute the "build" phase if it hasn't been executed yet. In other words, it is not always necessary to explicitly call Package::build()
, and many times calling Package::boot()
will suffice.
The following two code snippets are equivalent:
Package::new($properties)->build()->boot();
Package::new($properties)->boot();
There are at least two use cases for explicitly calling Package::build()
:
- When a plugin needs to "execute" pretty late during the WordPress loading, let's say, at
"template_redirect"
, we might to callPackage::boot()
at the latest possible time, but callPackage::build()
earlier to enable other packages to connect to it. - In unit tests, it might be desirable to access services from the container without any need to add hook via
Package::boot()
. In this specific case, the production code might only callPackage::boot()
while test might just usePackage::build()
.
Both stages are implemented through a series of steps, and the application status progresses as the steps are complete. In the process, a few action hooks are fired to allow external code to interact with the flow.
At any point of the flow, by holding an instance of the Package
, it is possible to inspect the current status via Package::statusIs()
, passing as an argument one of the Package::STATUS_*
constants.
- Upon instantiation, the
Package
status is atPackage::STATUS_IDLE
- Modules can be added by directly calling
Package::addModule()
on the instance, and other packages can be added by callingPackage::connect()
. Package::build()
is called.- The
Package
status moves toPackage::STATUS_INITIALIZING
. - The
Package::ACTION_INIT
action hook is fired, passing the package instance as an argument. That allows external code to add modules and connect other packages. - The
Package
status moves toPackage::STATUS_INITIALIZED
. No more modules can be added. - The
Package::ACTION_INITIALIZED
action hook is fired, passing the package instance as an argument. That allows external code to get services from the container.
Package::boot()
is called.Package
status moves toPackage::STATUS_BOOTING
.- All executables modules run. That is when all the application behavior happens.
- The
Package
status moves toPackage::STATUS_BOOTED
. - The
Package::ACTION_BOOTED
action hook is fired, passing the package instance as an argument. - The
Package
status moves toPackage::STATUS_DONE
. The booting stage is completed.Package::boot()
returns true.
The steps listed above for the two stages represent the "happy paths". If any exception is thrown at any of the steps above, the flows are halted and the "failure flow" starts.
- The
Package
status moves toPackage::STATUS_FAILED
. - The
Package::ACTION_FAILED_BUILD
action hook is fired, passing the raisedThrowable
as an argument. - If the
Package
'sProperties
instance is in "debug mode" (Properties::isDebug()
returnstrue
), the exception bubbles up, and the flow stops here. - If the
Properties
instance is not in "debug mode", thePackage::ACTION_FAILED_BOOT
action hook is fired, passing aThrowable
whoseprevious
property is theThrowable
thrown during the building stage. The "previous hierarchy" could be several levels if during the building stage many failures happened. Package::boot()
returns false.
- The
Package
status moves toPackage::STATUS_FAILED
. - The
Package::ACTION_FAILED_BOOT
action hook is fired, passing the raisedThrowable
as an argument. - If the
Package
'sProperties
instance is in "debug mode" (Properties::isDebug()
returnstrue
), the exception bubbles up, and the flow stops here. Package::boot()
returns false.
Passing modules to add to Package::boot()
has been deprecated since Modularity v1.7.0
.
For backward compatibility, when that happens, a deprecation notice is triggered (similarly to WordPress' _deprecated_argument
) but modules are still added.
It must be noted, that when first calling Package::build()
and after that Package::boot()
passing modules as argument, we will add those modules after the status is already at Package::STATUS_INITIALIZED
(because of the Package::build()
call) and, as mentioned above, that should not be possible.
The Package
class still deals with this scenario aiming for 100% backward compatibility, but there's an edge case. If anything that listens to the Package::ACTION_INITIALIZED
hook accesses the container (which is an accepted and documented possibility) the compiled container will be created, which means we can't add modules to it anymore. In this specific case, calling something like $package->build()->boot($someModule)
will end-up in an exception.
While this is a breakage of the backward compatibility promise, it is also true that Package::build()
was introduced in v1.7.0
when passing modules to Package::boot()
was deprecated. Developers who have introduced Package::build()
should also have removed any module passed to Package::boot()
.