Skip to content

Commit

Permalink
Allow for multiple boots of a single application. Adds a booted() f…
Browse files Browse the repository at this point in the history
…unction for checking whether we have already booted the app. The Application class adds new `$booted_providers` and `$registered_proxies` properties for storing providers and proxies that we've already launched. This allows multiple projects to use a single Application instance.
  • Loading branch information
justintadlock committed Jun 21, 2021
1 parent bf5ff55 commit 31f89e3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
40 changes: 35 additions & 5 deletions src/Core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ class Application extends Container implements ApplicationContract, Bootable {
*/
protected $proxies = [];

/**
* Array of booted service providers.
*
* @since 6.0.0
* @access protected
* @var array
*/
protected $booted_providers = [];

/**
* Array of registered proxies.
*
* @since 6.0.0
* @access protected
* @var array
*/
protected $registered_proxies = [];

/**
* Registers the default bindings, providers, and proxies for the
* framework.
Expand All @@ -83,6 +101,10 @@ public function boot() {
$this->registerProviders();
$this->bootProviders();
$this->registerProxies();

if ( ! defined( 'HYBRID_BOOTED' ) ) {
define( 'HYBRID_BOOTED', true );
}
}

/**
Expand All @@ -98,10 +120,7 @@ protected function registerDefaultBindings() {
$this->instance( 'app', $this );

// Adds the directory path for the framework.
// This shouldn't need changing
// unless doing something really out there or just for clarity.
if ( ! $this->has( 'path' ) ) {
// Adds the directory path for the framework.
$this->instance( 'path', untrailingslashit( HYBRID_DIR ) );
}

Expand Down Expand Up @@ -176,8 +195,14 @@ protected function registerProvider( $provider ) {
*/
protected function bootProvider( $provider ) {

// Bail if the provider has already been booted.
if ( in_array( $provider, $this->booted_providers ) ) {
return;
}

if ( method_exists( $provider, 'boot' ) ) {
$provider->boot();
$this->booted_providers[] = $provider;
}
}

Expand Down Expand Up @@ -245,11 +270,16 @@ public function proxy( $class_name, $alias ) {
*/
protected function registerProxies() {

Proxy::setContainer( $this );
// Only set the container on first instance.
if ( ! $this->registered_proxies ) {
Proxy::setContainer( $this );
}

foreach ( $this->proxies as $class => $alias ) {
if ( ! class_exists( $alias ) ) {

if ( ! in_array( $alias, $this->registered_proxies ) ) {
class_alias( $class, $alias );
$this->registered_proxies[] = $alias;
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/functions-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ function app( $abstract = '', $params = [] ) {
return App::resolve( $abstract ?: 'app', $params );
}

/**
* Conditional function for checking whether the application has been booted.
* Use before launching a new application. If booted, reference the `app()`
* instance directly.
*
* @since 6.0.0
* @access public
* @return bool
*/
function booted() {

return defined( 'HYBRID_BOOTED' ) && true === HYBRID_BOOTED;
}

/**
* Wrapper function for the `Collection` class.
*
Expand Down

0 comments on commit 31f89e3

Please sign in to comment.