Skip to content

Commit 1233ae8

Browse files
committed
implement true per-class singleton in AbstractSingleton, add hook to container build
1 parent 151d7ce commit 1233ae8

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

config/container.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
$builder = new ContainerBuilder();
66

7+
$builder = apply_filters('starter_kit/container_builder', $builder);
8+
79
$builder->addDefinitions(require __DIR__ . '/dependencies.php');
810

911
return $builder->build();

src/AbstractSingleton.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,25 @@
77
*/
88
abstract class AbstractSingleton
99
{
10+
/**
11+
* Array of instances of singleton classes
12+
*
13+
* @var array
14+
*/
15+
private static array $instances = [];
16+
1017
/**
1118
* Call this method to get singleton
1219
*/
13-
public static function instance(): bool|static
20+
public static function instance(): static
1421
{
15-
static $instance = false;
16-
if ($instance === false) {
17-
$instance = new static();
22+
$class = static::class;
23+
24+
if (!isset(self::$instances[$class])) {
25+
self::$instances[$class] = new static();
1826
}
1927

20-
return $instance;
28+
return self::$instances[$class];
2129
}
2230

2331
/**

src/App.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
defined('ABSPATH') || exit;
66

7+
use Psr\Container\ContainerInterface;
8+
use Psr\Log\LoggerInterface;
79
use StarterKit\Base\Constants;
810
use StarterKit\Base\Hooks;
9-
use StarterKit\Handlers\CLI\CLI;
1011
use StarterKit\Error\ErrorHandler;
11-
use Psr\Container\ContainerInterface;
12-
use Psr\Log\LoggerInterface;
12+
use StarterKit\Handlers\CLI\CLI;
1313
use Throwable;
1414

1515
/**

0 commit comments

Comments
 (0)