Skip to content

Commit

Permalink
Merge branch 'master' into dispatcher-scopes
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Core/src/Container.php
#	src/Core/tests/Scope/UseCaseTest.php
  • Loading branch information
roxblnfk committed Feb 29, 2024
2 parents ced3557 + dd46fbf commit 49cbd81
Show file tree
Hide file tree
Showing 32 changed files with 913 additions and 108 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
- **Medium Impact Changes**
- [spiral/core] Interface `Spiral\Core\Container\SingletonInterface` is deprecated,
use `Spiral\Core\Attribute\Singleton` instead. Will be removed in v4.0.
- **Other Features**
- Added `Spiral\Scaffolder\Command\InfoCommand` console command for getting information about available scaffolder
commands.
- [spiral/core] Added the ability to bind the interface as a proxy via `Spiral\Core\Config\Proxy` or `Spiral\Core\Config\DeprecationProxy`.
- [spiral/core] Added the ability to configure the container using `Spiral\Core\Options`. Added option **checkScope**
to enable scope checking.

## 3.11.1 - 2023-12-29

Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/Attribute/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Spiral\Core\Attribute;

/**
* Set a scope in which the dependency can be resolved.
* Set the scope in which the dependency can be resolved.
*
* @internal We are testing this feature, it may be changed in the future.
*/
Expand Down
45 changes: 45 additions & 0 deletions src/Core/src/Config/DeprecationProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Spiral\Core\Config;

/**
* @internal
*/
final class DeprecationProxy extends Proxy
{
/**
* @param class-string $interface
*/
public function __construct(
string $interface,
bool $singleton = false,
private readonly string|\BackedEnum|null $scope = null,
private readonly ?string $version = null,
private readonly ?string $message = null,
) {
if (($scope === null || $version === null) && $message === null) {
throw new \InvalidArgumentException('Scope and version or custom message must be provided.');
}

parent::__construct($interface, $singleton);
}

/**
* @return class-string
*/
public function getInterface(): string
{
$message = $this->message ?? \sprintf(
'Using `%s` outside of the `%s` scope is deprecated and will be impossible in version %s.',
$this->interface,
$this->scope,
$this->version
);

@trigger_error($message, \E_USER_DEPRECATED);

return parent::getInterface();
}
}
33 changes: 33 additions & 0 deletions src/Core/src/Config/Proxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Spiral\Core\Config;

class Proxy extends Binding
{
/**
* @param class-string $interface
*/
public function __construct(
protected readonly string $interface,
public readonly bool $singleton = false,
) {
if (!\interface_exists($interface)) {
throw new \InvalidArgumentException(\sprintf('Interface `%s` does not exist.', $interface));
}
}

public function __toString(): string
{
return \sprintf('Proxy to `%s`', $this->interface);
}

/**
* @return class-string
*/
public function getInterface(): string
{
return $this->interface;
}
}
11 changes: 6 additions & 5 deletions src/Core/src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ final class Container implements
public function __construct(
private Config $config = new Config(),
string|\BackedEnum|null $scopeName = self::DEFAULT_ROOT_SCOPE_NAME,
private Options $options = new Options(),
) {
if (\is_object($scopeName)) {
$scopeName = (string) $scopeName->value;
Expand Down Expand Up @@ -110,7 +111,7 @@ public function validateArguments(ContextFunction $reflection, array $arguments
}

/**
* @param string|null $context Related to parameter caused injection if any.
* @param \Stringable|string|null $context Related to parameter caused injection if any.
*
* @throws ContainerException
* @throws \Throwable
Expand All @@ -134,7 +135,7 @@ public function make(string $alias, array $parameters = [], \Stringable|string|n
* @template T
*
* @param class-string<T>|string|Autowire $id
* @param string|null $context Call context.
* @param \Stringable|string|null $context Call context.
*
* @return ($id is class-string ? T : mixed)
*
Expand Down Expand Up @@ -330,7 +331,7 @@ private function initServices(
$constructor = new Internal\Common\Registry($container->config, [
'state' => $state,
'scope' => new Internal\Scope($scopeName),
]);
], $this->options);

// Create container services
foreach ($container->config as $property => $class) {
Expand Down Expand Up @@ -385,10 +386,10 @@ private function closeScope(): void
private function runIsolatedScope(Scope $config, callable $closure): mixed
{
// Open scope
$container = new self($this->config, $config->name);
$container = new self($this->config, $config->name, $this->options);

// Configure scope
$container->scope->setParent($this, $this->scope);
$container->scope->setParent($this, $this->scope, $this->factory);

// Add specific bindings
foreach ($config->bindings as $alias => $resolver) {
Expand Down
2 changes: 2 additions & 0 deletions src/Core/src/FactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ interface FactoryInterface
*
* @param class-string<T>|string $alias
* @param array $parameters Parameters to construct new class.
* @psalm \Stringable|string|null $context Related to parameter caused injection if any.
* Will be added in the signature {@since 4.0.0}
*
* @return T
* @psalm-return ($alias is class-string ? T : mixed)
Expand Down
7 changes: 7 additions & 0 deletions src/Core/src/Internal/Common/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Spiral\Core\Internal\Common;

use Spiral\Core\Config;
use Spiral\Core\Options;

/**
* @internal
Expand All @@ -17,6 +18,7 @@ final class Registry
public function __construct(
private Config $config,
private array $objects = [],
private Options $options = new Options(),
) {
}

Expand All @@ -39,4 +41,9 @@ public function get(string $name, string $interface): object
\assert($result instanceof $interface);
return $result;
}

public function getOptions(): Options
{
return $this->options;
}
}
Loading

0 comments on commit 49cbd81

Please sign in to comment.