Skip to content

Commit

Permalink
Merge pull request #1056 from spiral/singleton-interface
Browse files Browse the repository at this point in the history
[spiral/core] Add `Singleton` attribute instead of `SingletonInterface`
  • Loading branch information
butschster committed Jan 11, 2024
2 parents fb8a19f + e1fa3d2 commit 2bb7be9
Show file tree
Hide file tree
Showing 52 changed files with 155 additions and 100 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## Unreleased

- **Medium Impact Changes**
- [spiral/core] Interface `Spiral\Core\Container\SingletonInterface` is deprecated,
use `Spiral\Core\Attribute\Singleton` instead. Will be removed in v4.0.

## 3.11.1 - 2023-12-29

- **Bug Fixes**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Bootloader\Attributes\AttributesBootloader;
use Spiral\Bootloader\Http\RouterBootloader;
use Spiral\Core\Container\SingletonInterface;
use Spiral\Core\Attribute\Singleton;
use Spiral\Router\RouteLocatorListener;
use Spiral\Tokenizer\Bootloader\TokenizerListenerBootloader;
use Spiral\Tokenizer\TokenizerListenerRegistryInterface;

/**
* Configures application routes using annotations and pre-defined configuration groups.
*/
final class AnnotatedRoutesBootloader extends Bootloader implements SingletonInterface
#[Singleton]
final class AnnotatedRoutesBootloader extends Bootloader
{
protected const DEPENDENCIES = [
RouterBootloader::class,
Expand Down
5 changes: 3 additions & 2 deletions src/Boot/src/BootloadManager/AbstractBootloadManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
namespace Spiral\Boot\BootloadManager;

use Spiral\Boot\BootloadManagerInterface;
use Spiral\Core\Container;
use Spiral\Core\Attribute\Singleton;
use Spiral\Core\ScopeInterface;

/**
* Provides ability to bootload service providers.
*/
abstract class AbstractBootloadManager implements BootloadManagerInterface, Container\SingletonInterface
#[Singleton]
abstract class AbstractBootloadManager implements BootloadManagerInterface
{
public function __construct(
private readonly ScopeInterface $scope,
Expand Down
5 changes: 3 additions & 2 deletions src/Boot/src/BootloadManager/ClassesRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

use Spiral\Boot\BootloadManagerInterface;
use Spiral\Boot\Exception\BootloaderAlreadyBootedException;
use Spiral\Core\Container;
use Spiral\Core\Attribute\Singleton;

/**
* @internal
* @psalm-import-type TClass from BootloadManagerInterface
*/
final class ClassesRegistry implements Container\SingletonInterface
#[Singleton]
final class ClassesRegistry
{
/** @var TClass[] */
private array $classes = [];
Expand Down
5 changes: 3 additions & 2 deletions src/Boot/src/BootloadManager/Initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@
use Spiral\Boot\BootloadManager\Checker\ClassExistsChecker;
use Spiral\Boot\BootloadManager\Checker\ConfigChecker;
use Spiral\Boot\BootloadManagerInterface;
use Spiral\Core\Attribute\Singleton;
use Spiral\Core\BinderInterface;
use Spiral\Core\Container;
use Spiral\Core\ResolverInterface;

/**
* @internal
* @psalm-import-type TClass from BootloadManagerInterface
* @psalm-import-type TFullBinding from BootloaderInterface
*/
class Initializer implements InitializerInterface, Container\SingletonInterface
#[Singleton]
class Initializer implements InitializerInterface
{
protected ?BootloaderCheckerInterface $checker = null;

Expand Down
4 changes: 3 additions & 1 deletion src/Bridge/Monolog/src/Bootloader/MonologBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
use Spiral\Boot\FinalizerInterface;
use Spiral\Config\ConfiguratorInterface;
use Spiral\Config\Patch\Append;
use Spiral\Core\Attribute\Singleton;
use Spiral\Core\Container;
use Spiral\Logger\LogsInterface;
use Spiral\Monolog\Config\MonologConfig;
use Spiral\Monolog\LogFactory;

final class MonologBootloader extends Bootloader implements Container\SingletonInterface
#[Singleton]
final class MonologBootloader extends Bootloader
{
protected const SINGLETONS = [
LogsInterface::class => LogFactory::class,
Expand Down
5 changes: 3 additions & 2 deletions src/Bridge/Stempler/src/Bootloader/StemplerBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Config\ConfiguratorInterface;
use Spiral\Config\Patch\Append;
use Spiral\Core\Attribute\Singleton;
use Spiral\Core\Container\Autowire;
use Spiral\Core\Container\SingletonInterface;
use Spiral\Stempler\Builder;
use Spiral\Stempler\Config\StemplerConfig;
use Spiral\Stempler\Directive;
Expand All @@ -28,7 +28,8 @@
/**
* Initiates stempler engine, it's cache and directives.
*/
final class StemplerBootloader extends Bootloader implements SingletonInterface
#[Singleton]
final class StemplerBootloader extends Bootloader
{
protected const SINGLETONS = [
StemplerEngine::class => [self::class, 'stemplerEngine'],
Expand Down
5 changes: 3 additions & 2 deletions src/Bridge/Stempler/src/Directive/RouteDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
namespace Spiral\Stempler\Directive;

use Psr\Container\ContainerInterface;
use Spiral\Core\Container\SingletonInterface;
use Spiral\Core\Attribute\Singleton;
use Spiral\Router\Exception\RouterException;
use Spiral\Router\Exception\UndefinedRouteException;
use Spiral\Router\RouterInterface;
use Spiral\Stempler\Exception\DirectiveException;
use Spiral\Stempler\Node\Dynamic\Directive;

final class RouteDirective extends AbstractDirective implements SingletonInterface
#[Singleton]
final class RouteDirective extends AbstractDirective
{
public function __construct(
private readonly ContainerInterface $container
Expand Down
5 changes: 3 additions & 2 deletions src/Broadcasting/src/Bootloader/WebsocketsBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
use Spiral\Broadcasting\BroadcastInterface;
use Spiral\Broadcasting\Config\BroadcastConfig;
use Spiral\Broadcasting\Middleware\AuthorizationMiddleware;
use Spiral\Core\Attribute\Singleton;
use Spiral\Core\BinderInterface;
use Spiral\Core\Container\SingletonInterface;

final class WebsocketsBootloader extends Bootloader implements SingletonInterface
#[Singleton]
final class WebsocketsBootloader extends Bootloader
{
protected const DEPENDENCIES = [
HttpBootloader::class,
Expand Down
5 changes: 3 additions & 2 deletions src/Broadcasting/src/BroadcastManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
namespace Spiral\Broadcasting;

use Spiral\Broadcasting\Config\BroadcastConfig;
use Spiral\Core\Container\SingletonInterface;
use Spiral\Core\Attribute\Singleton;
use Spiral\Core\FactoryInterface;

final class BroadcastManager implements BroadcastManagerInterface, SingletonInterface
#[Singleton]
final class BroadcastManager implements BroadcastManagerInterface
{
/** @var BroadcastInterface[] */
private array $connections = [];
Expand Down
5 changes: 3 additions & 2 deletions src/Cache/src/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\SimpleCache\CacheInterface;
use Spiral\Cache\Config\CacheConfig;
use Spiral\Core\Container\SingletonInterface;
use Spiral\Core\Attribute\Singleton;
use Spiral\Core\FactoryInterface;

class CacheManager implements CacheStorageProviderInterface, SingletonInterface
#[Singleton]
class CacheManager implements CacheStorageProviderInterface
{
/** @var CacheInterface[] */
private array $storages = [];
Expand Down
5 changes: 3 additions & 2 deletions src/Config/src/ConfigManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Spiral\Config\Exception\ConfigDeliveredException;
use Spiral\Config\Exception\PatchException;
use Spiral\Core\Container\SingletonInterface;
use Spiral\Core\Attribute\Singleton;
use Spiral\Core\Exception\ConfiguratorException;

/**
Expand All @@ -15,7 +15,8 @@
*
* @implements ConfiguratorInterface<object>
*/
final class ConfigManager implements ConfiguratorInterface, SingletonInterface
#[Singleton]
final class ConfigManager implements ConfiguratorInterface
{
private array $data = [];
private array $defaults = [];
Expand Down
5 changes: 3 additions & 2 deletions src/Console/src/Bootloader/ConsoleBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Spiral\Console\ConsoleDispatcher;
use Spiral\Console\Sequence\CallableSequence;
use Spiral\Console\Sequence\CommandSequence;
use Spiral\Core\Container\SingletonInterface;
use Spiral\Core\Attribute\Singleton;
use Spiral\Core\CoreInterceptorInterface;
use Spiral\Core\FactoryInterface;
use Spiral\Tokenizer\Bootloader\TokenizerListenerBootloader;
Expand All @@ -24,7 +24,8 @@
/**
* Bootloads console and provides ability to register custom bootload commands.
*/
final class ConsoleBootloader extends Bootloader implements SingletonInterface
#[Singleton]
final class ConsoleBootloader extends Bootloader
{
protected const DEPENDENCIES = [
TokenizerListenerBootloader::class,
Expand Down
5 changes: 3 additions & 2 deletions src/Console/tests/Fixtures/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
namespace Spiral\Tests\Console\Fixtures;

use Spiral\Console\Command;
use Spiral\Core\Container\SingletonInterface;
use Spiral\Core\Attribute\Singleton;

class TestCommand extends Command implements SingletonInterface
#[Singleton]
class TestCommand extends Command
{
public const NAME = 'test';
public const DESCRIPTION = 'Test Command';
Expand Down
3 changes: 3 additions & 0 deletions src/Core/src/Container/SingletonInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

namespace Spiral\Core\Container;

use Spiral\Core\Attribute\Singleton;

/**
* Class treated as singleton will only be constructed once in spiral IoC.
* @deprecated Use {@see Singleton} attribute instead. Will be removed in v4.0.
*/
interface SingletonInterface
{
Expand Down
5 changes: 3 additions & 2 deletions src/Core/tests/Fixtures/DeclarativeSingleton.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

namespace Spiral\Tests\Core\Fixtures;

use Spiral\Core\Container\SingletonInterface;
use Spiral\Core\Attribute\Singleton;

class DeclarativeSingleton implements SingletonInterface
#[Singleton]
class DeclarativeSingleton
{
}
4 changes: 2 additions & 2 deletions src/Core/tests/Internal/Config/StateBinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Spiral\Tests\Core\Internal\Config;

use Spiral\Core\Attribute\Singleton;
use Spiral\Core\BinderInterface;
use Spiral\Core\Container\SingletonInterface;
use Spiral\Core\Exception\Binder\SingletonOverloadException;
use Spiral\Core\FactoryInterface;
use Spiral\Tests\Core\Fixtures\SampleClass;
Expand Down Expand Up @@ -46,7 +46,7 @@ public function testHasInstanceAfterMakeWithoutAlias(): void
$binder = $this->constructor->get('binder', BinderInterface::class);
$factory = $this->constructor->get('factory', FactoryInterface::class);

$this->bindSingleton('test', new class implements SingletonInterface {});
$this->bindSingleton('test', new #[Singleton] class {});
$factory->make('test');

$this->assertTrue($binder->hasInstance('test'));
Expand Down
4 changes: 2 additions & 2 deletions src/Core/tests/ScopesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Spiral\Core\Attribute\Singleton;
use Spiral\Core\Container;
use Spiral\Core\Container\SingletonInterface;
use Spiral\Core\ContainerScope;
use Spiral\Core\Exception\RuntimeException;
use Spiral\Tests\Core\Fixtures\Bucket;
Expand Down Expand Up @@ -136,7 +136,7 @@ public function testSingletonRebindingInScope(): void
public function testHasInstanceAfterMakeWithoutAliasInScope(): void
{
$container = new Container();
$container->bindSingleton('test', new class implements SingletonInterface {});
$container->bindSingleton('test', new #[Singleton] class {});
$container->make('test');

$container->runScoped(function (Container $container) {
Expand Down
3 changes: 1 addition & 2 deletions src/Core/tests/SingletonsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PHPUnit\Framework\TestCase;
use Spiral\Core\Attribute\Singleton;
use Spiral\Core\Container;
use Spiral\Core\Container\SingletonInterface;
use Spiral\Tests\Core\Fixtures\DeclarativeSingleton;
use Spiral\Tests\Core\Fixtures\Factory;
use Spiral\Tests\Core\Fixtures\SampleClass;
Expand Down Expand Up @@ -188,7 +187,7 @@ public function testAttributeAnonClass(): void
public function testHasShouldReturnTrueWhenSingletonIsAlreadyConstructed(): void
{
$container = new Container();
$class = new class implements SingletonInterface {};
$class = new #[Singleton] class {};

$this->assertFalse($container->has($class::class));

Expand Down
5 changes: 3 additions & 2 deletions src/Encrypter/src/EncrypterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use Defuse\Crypto\Exception\CryptoException;
use Defuse\Crypto\Key;
use Spiral\Core\Attribute\Singleton;
use Spiral\Core\Container\InjectorInterface;
use Spiral\Core\Container\SingletonInterface;
use Spiral\Encrypter\Config\EncrypterConfig;
use Spiral\Encrypter\Exception\EncrypterException;

Expand All @@ -16,7 +16,8 @@
*
* @implements InjectorInterface<EncrypterInterface>
*/
final class EncrypterFactory implements InjectorInterface, EncryptionInterface, SingletonInterface
#[Singleton]
final class EncrypterFactory implements InjectorInterface, EncryptionInterface
{
public function __construct(
private readonly EncrypterConfig $config
Expand Down
5 changes: 3 additions & 2 deletions src/Events/src/ListenerProcessorRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

namespace Spiral\Events;

use Spiral\Core\Container\SingletonInterface;
use Spiral\Core\Attribute\Singleton;
use Spiral\Events\Processor\ProcessorInterface;

final class ListenerProcessorRegistry implements ProcessorInterface, SingletonInterface
#[Singleton]
final class ListenerProcessorRegistry implements ProcessorInterface
{
/** @var ProcessorInterface[] */
private array $processors = [];
Expand Down
5 changes: 3 additions & 2 deletions src/Framework/Auth/TokenStorageScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Spiral\Auth\Exception\TokenStorageException;
use Spiral\Core\Container\SingletonInterface;
use Spiral\Core\Attribute\Singleton;
use Spiral\Core\Exception\ScopeException;

final class TokenStorageScope implements TokenStorageInterface, SingletonInterface
#[Singleton]
final class TokenStorageScope implements TokenStorageInterface
{
public function __construct(
private readonly ContainerInterface $container
Expand Down
5 changes: 3 additions & 2 deletions src/Framework/Bootloader/Auth/AuthBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
use Spiral\Auth\Exception\AuthException;
use Spiral\Auth\TokenInterface;
use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Core\Attribute\Singleton;
use Spiral\Core\Container\Autowire;
use Spiral\Core\Container\SingletonInterface;
use Spiral\Core\FactoryInterface;

/**
* Manages the set of actor providers.
*/
final class AuthBootloader extends Bootloader implements ActorProviderInterface, SingletonInterface
#[Singleton]
final class AuthBootloader extends Bootloader implements ActorProviderInterface
{
protected const SINGLETONS = [
AuthScope::class => AuthScope::class,
Expand Down
5 changes: 3 additions & 2 deletions src/Framework/Bootloader/Auth/HttpAuthBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
use Spiral\Bootloader\Http\HttpBootloader;
use Spiral\Config\ConfiguratorInterface;
use Spiral\Config\Patch\Append;
use Spiral\Core\Attribute\Singleton;
use Spiral\Core\Container\Autowire;
use Spiral\Core\Container\SingletonInterface;
use Spiral\Core\FactoryInterface;
use Spiral\Http\Config\HttpConfig;

/**
* Enables Auth middleware and http transports to read and write tokens in PSR-7 request/response.
*/
final class HttpAuthBootloader extends Bootloader implements SingletonInterface
#[Singleton]
final class HttpAuthBootloader extends Bootloader
{
protected const DEPENDENCIES = [
AuthBootloader::class,
Expand Down
Loading

0 comments on commit 2bb7be9

Please sign in to comment.