|
| 1 | +# Application |
| 2 | + |
| 3 | +The `Application` is the central component of the Boson and is responsible for |
| 4 | +managing the application lifecycle. It provides a single entry point for |
| 5 | +creating and managing web applications using WebView. |
| 6 | + |
| 7 | +The application is responsible for: |
| 8 | + |
| 9 | +- Lifecycle management (startup, shutdown) |
| 10 | +- Window creation and management |
| 11 | +- WebView integration for web content display |
| 12 | +- Application, webview and window event handling |
| 13 | + |
| 14 | +...and more |
| 15 | + |
| 16 | +Architecturally, the application, like most key components, is divided into |
| 17 | +two "layers": |
| 18 | +- The main functionality belonging to the application itself. |
| 19 | +- Facades over methods and properties of descendants for quick access to the |
| 20 | + main internal components of the core. |
| 21 | + |
| 22 | + |
| 23 | +## Creating |
| 24 | + |
| 25 | +To create an application, simply create a new <code>Boson\Application</code> |
| 26 | +object. This will be sufficient for the vast majority of cases. |
| 27 | + |
| 28 | +<code-block lang="PHP"> |
| 29 | +$app = new Boson\Application(); |
| 30 | +</code-block> |
| 31 | + |
| 32 | +The application constructor also contains several optional arguments that you |
| 33 | +can pass explicitly if you wish. |
| 34 | + |
| 35 | +The first optional argument is responsible for the <code>Boson\ApplicationCreateInfo</code> |
| 36 | +application settings and allows you to fine-tune the application's operation. |
| 37 | + |
| 38 | +<tip> |
| 39 | +More details about the application configuration are written on the |
| 40 | +<a href="configuration.md#application">corresponding documentation pages</a>. |
| 41 | +</tip> |
| 42 | + |
| 43 | +<code-block lang="PHP"> |
| 44 | +$config = new Boson\ApplicationCreateInfo( |
| 45 | + // application configuration options |
| 46 | +); |
| 47 | + |
| 48 | +$app = new Boson\Application(info: $config); |
| 49 | +</code-block> |
| 50 | + |
| 51 | +The remaining optional parameters are responsible for passing external |
| 52 | +dependencies. |
| 53 | + |
| 54 | +For example, the second argument takes an optional reference to an external |
| 55 | +<code>Psr\EventDispatcher\EventDispatcherInterface</code> event dispatcher |
| 56 | +to which all events within the application can be delegated. |
| 57 | + |
| 58 | +<code-block lang="PHP"> |
| 59 | +$dispatcher = new Any\Vendor\PsrEventDispatcher(); |
| 60 | + |
| 61 | +$app = new Boson\Application(dispatcher: $dispatcher); |
| 62 | +</code-block> |
| 63 | + |
| 64 | +After creating the application, you will have access to the API to work with |
| 65 | +it, and after the necessary actions, the application will automatically start, |
| 66 | +<a href="configuration.md#autorun">unless otherwise specified</a>. |
| 67 | + |
| 68 | +## Running and Stopping |
| 69 | + |
| 70 | +The application can be started manually using the <code>run()</code> method. |
| 71 | + |
| 72 | +<code-block lang="PHP"> |
| 73 | +$app = new Boson\Application(); |
| 74 | +$app->run(); |
| 75 | +</code-block> |
| 76 | + |
| 77 | +<warning> |
| 78 | +The <code>run()</code> method is <b>blocking</b>, which means it will block |
| 79 | +the current execution thread until the application is stopped. |
| 80 | + |
| 81 | +<code-block lang="PHP"> |
| 82 | +$app = new Boson\Application(); |
| 83 | + |
| 84 | +echo 'Application will start...'; |
| 85 | + |
| 86 | +$app->run(); // This is a blocking operation |
| 87 | + |
| 88 | +echo 'Application WAS stopped'; // The code will be executed ONLY |
| 89 | + // after stopping an application |
| 90 | +</code-block> |
| 91 | +</warning> |
| 92 | + |
| 93 | +The application can be stopped at any time using the `quit()` method: |
| 94 | + |
| 95 | +<code-block lang="PHP"> |
| 96 | +$app->quit(); |
| 97 | +</code-block> |
| 98 | + |
| 99 | +<tip> |
| 100 | +For correct organization of the code, the stop should be made from the |
| 101 | +event subscription |
| 102 | +<code-block lang="PHP"> |
| 103 | +$app = new Boson\Application(); |
| 104 | + |
| 105 | +$app->events->addEventListener(SomeEvent::class, |
| 106 | + function() use ($app): void { |
| 107 | + $app->quit(); |
| 108 | + } |
| 109 | +); |
| 110 | + |
| 111 | +$app->run(); |
| 112 | +</code-block> |
| 113 | +</tip> |
| 114 | + |
| 115 | +To find out if the application is running, you can use the |
| 116 | +`Application::$isRunning` property, which returns `true` if the application |
| 117 | +is currently running. |
| 118 | + |
| 119 | +<code-block> |
| 120 | +$app = new Boson\Application(); |
| 121 | + |
| 122 | +// any code |
| 123 | + |
| 124 | +if ($app->isRunning === false) { |
| 125 | + $app->run(); |
| 126 | +} |
| 127 | +</code-block> |
| 128 | + |
| 129 | +## Main Window |
| 130 | + |
| 131 | +<tooltip term="TODO">TODO</tooltip> |
| 132 | + |
| 133 | +## Main WebView |
| 134 | + |
| 135 | +<tooltip term="TODO">TODO</tooltip> |
| 136 | + |
| 137 | +## Application Events |
| 138 | + |
| 139 | +The application will automatically emit the following events (and intentions) |
| 140 | +during its lifecycle: |
| 141 | + |
| 142 | +- <code>ApplicationStarting</code> - when the application is about to start. |
| 143 | + <tip>This is an <b>intention</b>: You can prevent it from running using the |
| 144 | + <code>$starting->cancel()</code> method.</tip> |
| 145 | + |
| 146 | +- <code>ApplicationStarted</code> - when the application has started. |
| 147 | + <tip>This is an <b>event</b>: Tells you that the application has already started. |
| 148 | + You cannot influence this in any way.</tip> |
| 149 | + |
| 150 | +- <code>ApplicationStopping</code> - when the application is about to stop. |
| 151 | + <tip>This is an <b>intention</b>: You can prevent it from stopping using the |
| 152 | + <code>$stopping->cancel()</code> method.</tip> |
| 153 | + |
| 154 | +- <code>ApplicationStopped</code> - when the application has stopped. |
| 155 | + <tip>This is an <b>event</b>: Tells you that the application has already stopped. |
| 156 | + You cannot influence this in any way.</tip> |
| 157 | + |
| 158 | +More information about events can be found in the |
| 159 | +<tooltip term="TODO">events documentation</tooltip>. |
0 commit comments