Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor app container to modularity. #1860

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 9 additions & 39 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@
* @package WooCommerce\PayPalCommerce
*/

use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\CachingContainer;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\CompositeCachingServiceProvider;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\CompositeContainer;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\DelegatingContainer;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ProxyContainer;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface;
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Package;
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Properties\PluginProperties;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\DhiiToModularityModule;

return function (
string $root_dir,
Expand All @@ -34,38 +30,12 @@
*/
$modules = apply_filters( 'woocommerce_paypal_payments_modules', $modules );

$providers = array_map(
function ( ModuleInterface $module ): ServiceProviderInterface {
return $module->setup();
},
$modules
);
// Initialize plugin.
$properties = PluginProperties::new( __FILE__ );
$bootstrap = Package::new( $properties );

$provider = new CompositeCachingServiceProvider( $providers );
$proxy_container = new ProxyContainer();
// TODO: caching does not work currently,
// may want to consider fixing it later (pass proxy as parent to DelegatingContainer)
// for now not fixed since we were using this behavior for long time and fixing it now may break things.
$container = new DelegatingContainer( $provider );
/**
* Skip iterable vs array check.
*
* @psalm-suppress PossiblyInvalidArgument
*/
$app_container = new CachingContainer(
new CompositeContainer(
array_merge(
$additional_containers,
array( $container )
)
)
);
$proxy_container->setInnerContainer( $app_container );

foreach ( $modules as $module ) {
/* @var $module ModuleInterface module */
$module->run( $app_container );
}
$bootstrap->addModule( new DhiiToModularityModule( $modules ) );
$bootstrap->boot();

return $app_container;
return $bootstrap->container();
};
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"wikimedia/composer-merge-plugin": "^2.0",
"wp-oop/wordpress-interface": "^0.1.0-alpha1",
"dhii/versions": "^0.1.0-alpha1",
"symfony/polyfill-php80": "^1.19"
"symfony/polyfill-php80": "^1.19",
"inpsyde/modularity": "^1.7"
},
"require-dev": {
"psr/container": "^1.0",
Expand Down Expand Up @@ -77,7 +78,8 @@
"packages": [
"psr/container",
"dhii/containers",
"dhii/module-interface"
"dhii/module-interface",
"inpsyde/modularity"
],
"delete_vendor_directories": true
}
Expand Down
70 changes: 69 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

153 changes: 153 additions & 0 deletions lib/packages/Inpsyde/Modularity/Container/ContainerConfigurator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php
declare(strict_types=1);

namespace WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Container;

use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerExceptionInterface;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;

class ContainerConfigurator
{
/**
* @var array<string, callable(ContainerInterface $container):mixed>
*/
private $services = [];

/**
* @var array<string, bool>
*/
private $factoryIds = [];

/**
* @var array<string, array<callable(mixed $service, ContainerInterface $container):mixed>>
*/
private $extensions = [];

/**
* @var ContainerInterface[]
*/
private $containers = [];

/**
* @var null|ContainerInterface
*/
private $compiledContainer;

/**
* ContainerConfigurator constructor.
*
* @param ContainerInterface[] $containers
*/
public function __construct(array $containers = [])
{
array_map([$this, 'addContainer'], $containers);
}

/**
* Allowing to add child containers.
*
* @param ContainerInterface $container
*/
public function addContainer(ContainerInterface $container): void
{
$this->containers[] = $container;
}

/**
* @param string $id
* @param callable(ContainerInterface $container):mixed $factory
*/
public function addFactory(string $id, callable $factory): void
{
$this->addService($id, $factory);
// We're using a hash table to detect later
// via isset() if a Service as a Factory.
$this->factoryIds[$id] = true;
}

/**
* @param string $id
* @param callable(ContainerInterface $container):mixed $service
*
* @return void
*/
public function addService(string $id, callable $service): void
{
/*
* We are being intentionally permissive here,
* allowing a simple workflow for *intentional* overrides
* while accepting the (small?) risk of *accidental* overrides
* that could be hard to notice and debug.
*
* Clear a factory flag in case it was a factory.
* If needs be, it will get re-added after this function completes.
*/
unset($this->factoryIds[$id]);

$this->services[$id] = $service;
}

/**
* @param string $id
*
* @return bool
*/
public function hasService(string $id): bool
{
if (array_key_exists($id, $this->services)) {
return true;
}

foreach ($this->containers as $container) {
if ($container->has($id)) {
return true;
}
}

return false;
}

/**
* @param string $id
* @param callable(mixed $service, ContainerInterface $container):mixed $extender
*
* @return void
*/
public function addExtension(string $id, callable $extender): void
{
if (!isset($this->extensions[$id])) {
$this->extensions[$id] = [];
}

$this->extensions[$id][] = $extender;
}

/**
* @param string $id
*
* @return bool
*/
public function hasExtension(string $id): bool
{
return isset($this->extensions[$id]);
}

/**
* Returns a read only version of this Container.
*
* @return ContainerInterface
*/
public function createReadOnlyContainer(): ContainerInterface
{
if (!$this->compiledContainer) {
$this->compiledContainer = new ReadOnlyContainer(
$this->services,
$this->factoryIds,
$this->extensions,
$this->containers
);
}

return $this->compiledContainer;
}
}
Loading
Loading