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

Closes #926 Update container to v4 #928

Merged
merged 5 commits into from
Nov 27, 2024
Merged
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
44 changes: 27 additions & 17 deletions classes/Admin/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Imagify\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
use Imagify\Dependencies\WPMedia\PluginFamily\Controller\PluginFamily;
use Imagify\User\User;

/**
* Service provider for Admin.
Expand All @@ -16,10 +17,10 @@ class ServiceProvider extends AbstractServiceProvider {
* @var array
*/
protected $provides = [
'admin_bar',
'admin_subscriber',
'plugin_family',
'plugin_family_subscriber',
AdminBar::class,
AdminSubscriber::class,
PluginFamily::class,
PluginFamilySubscriber::class,
];

/**
Expand All @@ -28,27 +29,36 @@ class ServiceProvider extends AbstractServiceProvider {
* @var array
*/
public $subscribers = [
'admin_bar',
'admin_subscriber',
'plugin_family_subscriber',
AdminBar::class,
AdminSubscriber::class,
PluginFamilySubscriber::class,
];

/**
* Check if the service provider provides a specific service.
*
* @param string $id The id of the service.
*
* @return bool
*/
public function provides( string $id ): bool {
return in_array( $id, $this->provides, true );
}

/**
* Registers the provided classes
*
* @return void
*/
public function register() {

$this->getContainer()->share( 'admin_bar', AdminBar::class )
->addArgument( $this->getContainer()->get( 'user' ) );
$this->getContainer()->share( 'admin_subscriber', AdminSubscriber::class )
->addArgument( $this->getContainer()->get( 'user' ) );

$this->getContainer()->add( 'plugin_family', PluginFamily::class );
public function register(): void {
$this->getContainer()->addShared( AdminBar::class )
->addArgument( User::class );
$this->getContainer()->addShared( AdminSubscriber::class )
->addArgument( User::class );

$this->getContainer()->add( 'plugin_family_subscriber', PluginFamilySubscriber::class )
->addArgument( $this->getContainer()->get( 'plugin_family' ) );
$this->getContainer()->add( PluginFamily::class );
$this->getContainer()->addShared( PluginFamilySubscriber::class )
->addArgument( PluginFamily::class );
}

/**
Expand Down
25 changes: 18 additions & 7 deletions classes/Avif/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class ServiceProvider extends AbstractServiceProvider {
* @var array
*/
protected $provides = [
'avif_display',
'avif_rewrite_rules',
Display::class,
RewriteRules::class,
];

/**
Expand All @@ -26,18 +26,29 @@ class ServiceProvider extends AbstractServiceProvider {
* @var array
*/
public $subscribers = [
'avif_display',
'avif_rewrite_rules',
Display::class,
RewriteRules::class,
];

/**
* Check if the service provider provides a specific service.
*
* @param string $id The id of the service.
*
* @return bool
*/
public function provides( string $id ): bool {
return in_array( $id, $this->provides, true );
}

/**
* Registers the provided classes
*
* @return void
*/
public function register() {
$this->getContainer()->share( 'avif_display', Display::class );
$this->getContainer()->share( 'avif_rewrite_rules', RewriteRules::class );
public function register(): void {
$this->getContainer()->addShared( Display::class );
$this->getContainer()->addShared( RewriteRules::class );
}

/**
Expand Down
19 changes: 15 additions & 4 deletions classes/CDN/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ServiceProvider extends AbstractServiceProvider {
* @var array
*/
protected $provides = [
'cdn',
CDN::class,
];

/**
Expand All @@ -24,16 +24,27 @@ class ServiceProvider extends AbstractServiceProvider {
* @var array
*/
public $subscribers = [
'cdn',
CDN::class,
];

/**
* Check if the service provider provides a specific service.
*
* @param string $id The id of the service.
*
* @return bool
*/
public function provides( string $id ): bool {
return in_array( $id, $this->provides, true );
}

/**
* Registers the provided classes
*
* @return void
*/
public function register() {
$this->getContainer()->share( 'cdn', CDN::class );
public function register(): void {
$this->getContainer()->addShared( CDN::class );
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Imagify\Dependencies\League\Container\Argument;

interface RawArgumentInterface
interface ArgumentInterface
{
/**
* Return the value of the raw argument.
*
* @return mixed
*/
public function getValue();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Imagify\Dependencies\League\Container\Argument;

Expand All @@ -7,22 +9,6 @@

interface ArgumentResolverInterface extends ContainerAwareInterface
{
/**
* Resolve an array of arguments to their concrete implementations.
*
* @param array $arguments
*
* @return array
*/
public function resolveArguments(array $arguments) : array;

/**
* Resolves the correct arguments to be passed to a method.
*
* @param ReflectionFunctionAbstract $method
* @param array $args
*
* @return array
*/
public function reflectArguments(ReflectionFunctionAbstract $method, array $args = []) : array;
public function resolveArguments(array $arguments): array;
public function reflectArguments(ReflectionFunctionAbstract $method, array $args = []): array;
}
Original file line number Diff line number Diff line change
@@ -1,120 +1,111 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Imagify\Dependencies\League\Container\Argument;

use Imagify\Dependencies\League\Container\Container;
use Imagify\Dependencies\League\Container\DefinitionContainerInterface;
use Imagify\Dependencies\League\Container\Exception\{ContainerException, NotFoundException};
use Imagify\Dependencies\League\Container\ReflectionContainer;
use Imagify\Dependencies\Psr\Container\ContainerInterface;
use ReflectionFunctionAbstract;
use ReflectionParameter;
use ReflectionNamedType;

trait ArgumentResolverTrait
{
/**
* {@inheritdoc}
*/
public function resolveArguments(array $arguments) : array
public function resolveArguments(array $arguments): array

Check warning on line 16 in classes/Dependencies/League/Container/Argument/ArgumentResolverTrait.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

classes/Dependencies/League/Container/Argument/ArgumentResolverTrait.php#L16

The method resolveArguments() has an NPath complexity of 243. The configured NPath complexity threshold is 200.
{
return array_map(function ($argument) {
$justStringValue = false;

if ($argument instanceof RawArgumentInterface) {
return $argument->getValue();
} elseif ($argument instanceof ClassNameInterface) {
$id = $argument->getClassName();
} elseif (!is_string($argument)) {
return $argument;
} else {
$justStringValue = true;
$id = $argument;
try {
$container = $this->getContainer();
} catch (ContainerException $e) {
$container = ($this instanceof ReflectionContainer) ? $this : null;
}

foreach ($arguments as &$arg) {
// if we have a literal, we don't want to do anything more with it
if ($arg instanceof LiteralArgumentInterface) {
$arg = $arg->getValue();
continue;
}

$container = null;
if ($arg instanceof ArgumentInterface) {
$argValue = $arg->getValue();
} else {
$argValue = $arg;
}

try {
$container = $this->getLeagueContainer();
} catch (ContainerException $e) {
if ($this instanceof ReflectionContainer) {
$container = $this;
}
if (!is_string($argValue)) {
continue;
}

if ($container !== null) {
// resolve the argument from the container, if it happens to be another
// argument wrapper, use that value
if ($container instanceof ContainerInterface && $container->has($argValue)) {
try {
return $container->get($id);
} catch (NotFoundException $exception) {
if ($argument instanceof ClassNameWithOptionalValue) {
return $argument->getOptionalValue();
}
$arg = $container->get($argValue);

if ($justStringValue) {
return $id;
if ($arg instanceof ArgumentInterface) {
$arg = $arg->getValue();
}

throw $exception;
continue;
} catch (NotFoundException $e) {
}
}

if ($argument instanceof ClassNameWithOptionalValue) {
return $argument->getOptionalValue();
// if we have a default value, we use that, no more resolution as
// we expect a default/optional argument value to be literal
if ($arg instanceof DefaultValueInterface) {
$arg = $arg->getDefaultValue();
}
}

// Just a string value.
return $id;
}, $arguments);
return $arguments;
}

/**
* {@inheritdoc}
*/
public function reflectArguments(ReflectionFunctionAbstract $method, array $args = []) : array
public function reflectArguments(ReflectionFunctionAbstract $method, array $args = []): array
{
$arguments = array_map(function (ReflectionParameter $param) use ($method, $args) {
$params = $method->getParameters();
$arguments = [];

foreach ($params as $param) {
$name = $param->getName();
$type = $param->getType();

// if we've been given a value for the argument, treat as literal
if (array_key_exists($name, $args)) {
return new RawArgument($args[$name]);
$arguments[] = new LiteralArgument($args[$name]);
continue;
}

if ($type) {
if (PHP_VERSION_ID >= 70100) {
$typeName = $type->getName();
} else {
$typeName = (string) $type;
}
$type = $param->getType();

$typeName = ltrim($typeName, '?');
if ($type instanceof ReflectionNamedType) {
// in PHP 8, nullable arguments have "?" prefix
$typeHint = ltrim($type->getName(), '?');

if ($param->isDefaultValueAvailable()) {
return new ClassNameWithOptionalValue($typeName, $param->getDefaultValue());
$arguments[] = new DefaultValueArgument($typeHint, $param->getDefaultValue());
continue;
}

return new ClassName($typeName);
$arguments[] = new ResolvableArgument($typeHint);
continue;
}

if ($param->isDefaultValueAvailable()) {
return new RawArgument($param->getDefaultValue());
$arguments[] = new LiteralArgument($param->getDefaultValue());
continue;
}

throw new NotFoundException(sprintf(
'Unable to resolve a value for parameter (%s) in the function/method (%s)',
$name,
$method->getName()
));
}, $method->getParameters());
}

return $this->resolveArguments($arguments);
}

/**
* @return ContainerInterface
*/
abstract public function getContainer() : ContainerInterface;

/**
* @return Container
*/
abstract public function getLeagueContainer() : Container;
abstract public function getContainer(): DefinitionContainerInterface;
}
Loading
Loading