1. Go to this page and download the library: Download inpsyde/wp-app-container library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
namespace AcmeInc\Foo;
use Inpsyde\App\App;
use Inpsyde\WpContext;
add_action(
App::ACTION_ADD_PROVIDERS,
function (App $app) {
$app
->addProvider(new MainProvider(), WpContext::CORE)
->addProvider(new CronRestProvider(), WpContext::CRON, WpContext::REST)
->addProvider(new AdminProvider(), WpContext::BACKOFFICE);
}
);
namespace AcmeInc\Foo\Extension;
use Inpsyde\App\App;
use Inpsyde\WpContext;
use AcmeInc\Foo\MainProvider;
add_action(
App::ACTION_REGISTERED_PROVIDER,
function (string $providerId, App $app) {
if ($providerId === MainProvider::class) {
$app->addProvider(new ExtensionProvider(), WpContext::CORE);
}
},
10,
2
);
public function register(Container $container): void;
namespace AcmeInc\Redirector;
use Inpsyde\App\Container;
use Inpsyde\App\Provider\Booted;
final class Provider extends Booted {
private const CONFIG_KEY = 'REDIRECTOR_CONFIG';
public function register(Container $container): bool
{
// class names are used as service ids...
$container->addService(
Config::class,
static function (Container $container): Config {
return Config::load($container->config()->get(self::CONFIG_KEY));
}
);
$container->addService(
Redirector::class,
static function (Container $container): Redirector {
return new Redirector($container->get(Config::class));
}
);
return true;
}
public function boot(Container $container): bool
{
return add_action(
'template_redirect',
static function () use ($container) {
/** @var AcmeInc\Redirector\Redirector $redirector */
$redirector = $container->get(Redirector::class);
$redirector->redirect();
}
);
}
}
namespace AcmeInc\Redirector;
use Inpsyde\App\Provider\Booted;
use Inpsyde\App\Container;
final class Provider extends Booted {
public function register(Container $container): bool
{
$diBuilder = new \DI\ContainerBuilder();
if ($container->config()->isProduction()) {
$cachePath = $container->config()->get('ACME_INC_CACHE_PATH');
$diBuilder->enableCompilation($cachePath);
}
$defsPath = $container->config()->get('ACME_INC_DEFS_PATH');
$diBuilder->addDefinitions("{$defsPath}/redirector/defs.php");
$container->addContainer($diBuilder->build());
return true;
}
public function boot(Container $container): bool
{
return add_action(
'template_redirect',
static function () use ($container) {
/** @var AcmeInc\Redirector\Redirector $redirector */
$redirector = $container->get(Redirector::class);
$redirector->redirect();
}
);
}
}
namespace AcmeInc;
use Inpsyde\App;
// An helper to create App on first call, then always access same instance
function app(): App\App
{
static $app;
if (!$app) {
$env = new App\EnvConfig(__NAMESPACE__ . '\\Config', __NAMESPACE__);
// Build the App container using custom config class
$container = new App\Container($env);
// Create PSR-11 container and push into the App container
$diBuilder = new \DI\ContainerBuilder();
$diBuilder->addDefinitions('./definitions.php');
$container->addContainer($diBuilder->build());
// Instantiate the app with the container
$app = App\App::new($container);
}
return $app;
}
// Finally create and bootstrap app
add_action('muplugins_loaded', [app(), 'boot']);