| 1 |
<?php |
| 2 |
/** |
| 3 |
* Addon Service Provider |
| 4 |
* |
| 5 |
* Registers the AddonRegistry in the container and wires up the addon |
| 6 |
* bootstrap lifecycle. |
| 7 |
* |
| 8 |
* @package Forge12\DoubleOptIn\Providers |
| 9 |
* @since 4.3.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Forge12\DoubleOptIn\Providers; |
| 13 |
|
| 14 |
use Forge12\DoubleOptIn\Addon\AddonRegistry; |
| 15 |
use Forge12\DoubleOptIn\Container\BootableProviderInterface; |
| 16 |
use Forge12\DoubleOptIn\Container\Container; |
| 17 |
use Forge12\Shared\LoggerInterface; |
| 18 |
|
| 19 |
if ( ! defined( 'ABSPATH' ) ) { |
| 20 |
exit; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Class AddonServiceProvider |
| 25 |
* |
| 26 |
* Lifecycle: |
| 27 |
* 1. {@see register()} puts the AddonRegistry into the container. |
| 28 |
* 2. {@see boot()} schedules the `f12_cf7_doubleoptin_register_addons` |
| 29 |
* action for `plugins_loaded` priority 20. This is one priority step |
| 30 |
* later than the core instantiation at priority 10, giving addon |
| 31 |
* plugins that also hook at priority 10 time to load. |
| 32 |
* 3. After addons register, the registry is booted so each addon's |
| 33 |
* own {@see \Forge12\DoubleOptIn\Addon\AddonInterface::boot()} runs |
| 34 |
* with the fully wired container. |
| 35 |
*/ |
| 36 |
class AddonServiceProvider implements BootableProviderInterface { |
| 37 |
|
| 38 |
/** |
| 39 |
* {@inheritdoc} |
| 40 |
*/ |
| 41 |
public function register( Container $container ): void { |
| 42 |
$container->singleton( |
| 43 |
AddonRegistry::class, |
| 44 |
function () use ( $container ) { |
| 45 |
$registry = AddonRegistry::getInstance(); |
| 46 |
|
| 47 |
if ( $container->has( LoggerInterface::class ) ) { |
| 48 |
$registry->setLogger( $container->get( LoggerInterface::class ) ); |
| 49 |
} |
| 50 |
|
| 51 |
return $registry; |
| 52 |
} |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* {@inheritdoc} |
| 58 |
*/ |
| 59 |
public function boot( Container $container ): void { |
| 60 |
$registry = $container->get( AddonRegistry::class ); |
| 61 |
|
| 62 |
/* |
| 63 |
* Decoupling rationale: boot() itself runs during Container::boot(), |
| 64 |
* which fires at plugins_loaded priority 10 while the main plugin is |
| 65 |
* being instantiated. Addon plugins that hook on plugins_loaded:10 |
| 66 |
* may not have loaded yet, so we defer the registration window by |
| 67 |
* one priority step. Late-registering addons after priority 20 will |
| 68 |
* still be picked up by the register() method's late-boot path. |
| 69 |
*/ |
| 70 |
add_action( |
| 71 |
'plugins_loaded', |
| 72 |
function () use ( $registry, $container ) { |
| 73 |
/** |
| 74 |
* Fires once per request, giving addon plugins the opportunity |
| 75 |
* to register themselves with the AddonRegistry. |
| 76 |
* |
| 77 |
* @since 4.3.0 |
| 78 |
* |
| 79 |
* @param AddonRegistry $registry The addon registry. |
| 80 |
* @param Container $container The core DI container, for |
| 81 |
* addons that need it at |
| 82 |
* registration time (rare — |
| 83 |
* prefer AddonInterface::boot()). |
| 84 |
*/ |
| 85 |
do_action( 'f12_cf7_doubleoptin_register_addons', $registry, $container ); |
| 86 |
|
| 87 |
$registry->bootAll( $container ); |
| 88 |
}, |
| 89 |
20 |
| 90 |
); |
| 91 |
} |
| 92 |
} |
| 93 |
|