chandler-11 The Singleton Pattern Must Be a Class

Closes #11.
This commit is contained in:
Ilya Bakhlin 2022-01-01 18:49:27 +01:00
parent 3ed115a9ed
commit b457218e26
3 changed files with 73 additions and 14 deletions

29
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "0aa468953e3be7c5754eadbc5f3c6234",
"content-hash": "ec942d4b143e30d41b04837c5d8c09cf",
"packages": [
{
"name": "doctrine/lexer",
@ -2130,16 +2130,16 @@
},
{
"name": "symfony/translation",
"version": "v5.4.1",
"version": "v5.4.2",
"source": {
"type": "git",
"url": "https://github.com/symfony/translation.git",
"reference": "8c82cd35ed861236138d5ae1c78c0c7ebcd62107"
"reference": "ff8bb2107b6a549dc3c5dd9c498dcc82c9c098ca"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/translation/zipball/8c82cd35ed861236138d5ae1c78c0c7ebcd62107",
"reference": "8c82cd35ed861236138d5ae1c78c0c7ebcd62107",
"url": "https://api.github.com/repos/symfony/translation/zipball/ff8bb2107b6a549dc3c5dd9c498dcc82c9c098ca",
"reference": "ff8bb2107b6a549dc3c5dd9c498dcc82c9c098ca",
"shasum": ""
},
"require": {
@ -2207,7 +2207,7 @@
"description": "Provides tools to internationalize your application",
"homepage": "https://symfony.com",
"support": {
"source": "https://github.com/symfony/translation/tree/v5.4.1"
"source": "https://github.com/symfony/translation/tree/v5.4.2"
},
"funding": [
{
@ -2223,7 +2223,7 @@
"type": "tidelift"
}
],
"time": "2021-12-05T20:33:52+00:00"
"time": "2021-12-25T19:45:36+00:00"
},
{
"name": "symfony/translation-contracts",
@ -2305,16 +2305,16 @@
},
{
"name": "symfony/yaml",
"version": "v5.4.0",
"version": "v5.4.2",
"source": {
"type": "git",
"url": "https://github.com/symfony/yaml.git",
"reference": "034ccc0994f1ae3f7499fa5b1f2e75d5e7a94efc"
"reference": "b9eb163846a61bb32dfc147f7859e274fab38b58"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/yaml/zipball/034ccc0994f1ae3f7499fa5b1f2e75d5e7a94efc",
"reference": "034ccc0994f1ae3f7499fa5b1f2e75d5e7a94efc",
"url": "https://api.github.com/repos/symfony/yaml/zipball/b9eb163846a61bb32dfc147f7859e274fab38b58",
"reference": "b9eb163846a61bb32dfc147f7859e274fab38b58",
"shasum": ""
},
"require": {
@ -2360,7 +2360,7 @@
"description": "Loads and dumps YAML files",
"homepage": "https://symfony.com",
"support": {
"source": "https://github.com/symfony/yaml/tree/v5.4.0"
"source": "https://github.com/symfony/yaml/tree/v5.4.2"
},
"funding": [
{
@ -2376,7 +2376,7 @@
"type": "tidelift"
}
],
"time": "2021-11-28T15:25:38+00:00"
"time": "2021-12-16T21:58:21+00:00"
},
{
"name": "tracy/tracy",
@ -4510,7 +4510,8 @@
"prefer-stable": false,
"prefer-lowest": false,
"platform": {
"php": "^7.3"
"ext-yaml": "*",
"php": "^7.4"
},
"platform-dev": [],
"plugin-api-version": "2.2.0"

42
src/Classes/Singleton.php Normal file
View file

@ -0,0 +1,42 @@
<?php
declare(strict_types = 1);
namespace Chandler\Classes;
use Chandler\Interfaces\Singleton as SingletonInterface;
/**
* @package Chandler\Classes
*/
abstract class Singleton implements SingletonInterface
{
/**
* @var array
*/
private static array $instances = [];
/**
* TODO: Add a description.
*
* @return void
*/
final private function __clone() {}
/**
* TODO: Add a description.
*/
abstract protected function __construct();
/**
* @return static
*/
public static function getInstance(): self
{
if (array_key_exists(static::class, self::$instances)) {
return self::$instances[static::class];
} else {
return self::$instances[static::class] = new static();
}
}
}

View file

@ -0,0 +1,16 @@
<?php
declare(strict_types = 1);
namespace Chandler\Interfaces;
/**
* @package Chandler\Interfaces
*/
interface Singleton
{
/**
* @return static
*/
public static function getInstance(): self;
}