The Boson\Application
is the central
component of the Boson and is responsible for managing the application
lifecycle. It provides a single entry point for creating and managing web
applications using WebView.
The application is responsible for:
- Lifecycle management (startup, shutdown, etc).
- Window creation and management.
- WebView integration for web content display.
- Application, Window and WebView event handling.
...and more
Architecturally, the application, like most key components, is divided into two "layers":
- The main functionality belonging to the application itself.
- Facades over methods and properties of descendants for quick access to the main internal components of the core.
#Creating
To create an application, simply create a new Boson\Application
object.
This will be sufficient for the vast majority of cases.
$app = new Boson\Application();
The application constructor also contains several optional arguments that you can pass explicitly if you wish.
The first optional argument is responsible for the Boson\ApplicationCreateInfo
application settings and allows you to fine-tune the application's operation.
More details about the application configuration are written on the corresponding documentation pages.
$config = new Boson\ApplicationCreateInfo( // application configuration options ); $app = new Boson\Application(info: $config);
The remaining optional parameters are responsible for passing external dependencies.
For example, the second argument takes an optional reference to an external
Psr\EventDispatcher\EventDispatcherInterface
event dispatcher
to which all events within the application can be delegated.
$dispatcher = new Any\Vendor\PsrEventDispatcher(); $app = new Boson\Application(dispatcher: $dispatcher);
After creating the application, you will have access to the API to work with it, and after the necessary actions, the application will automatically start, unless otherwise specified.
#Launching
The application can be started manually using the run()
method.
$app = new Boson\Application(); $app->run();
The
run()
method is blocking, which means it will block the current execution thread until the application is stopped.$app = new Boson\Application(); echo 'Application will start...'; $app->run(); // This is a blocking operation echo 'Application WAS stopped'; // The code will be executed ONLY // after stopping an application
#Stopping
The application can be stopped at any time using the quit()
method:
$app->quit();
For correct organization of the code, the stop should be made from the event subscription
$app = new Boson\Application(); $app->on(function (SomeEvent $e) use ($app): void { $app->quit(); }); $app->run();
To find out if the application is running, you can use the
Application::$isRunning
property, which returns true
if the application
is currently running.
$app = new Boson\Application(); // any code if ($app->isRunning === false) { $app->run(); }
#Identifier
The Boson\ApplicationId
is a unique identifier for each application
instance. The identifier is needed to compare different applications
for their equivalence.
To get application identifier use the Application::$id
property.
$app = new Boson\Application(); echo 'ID: ' . $app->id;
An identifier is a value object and contains methods for comparison and conversion to scalars.
if ($app1->id->equals($app2->id)) { echo sprintf('The %s app is equals to %s app', $app1, $app2); }