Skip to content

Commit e1e3e3f

Browse files
committed
Added README.md
1 parent 70de4a0 commit e1e3e3f

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed

README.md

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<!--suppress HtmlDeprecatedAttribute -->
2+
<h1 align="center">PHP Singleton</h1>
3+
4+
<!-- Main Badges -->
5+
<p align="center">
6+
<!-- License: MIT -->
7+
<a href="https://opensource.org/licenses/MIT" target="_blank">
8+
<img alt="License: MIT" src="https://img.shields.io/badge/License-MIT-9C0000.svg?labelColor=ebdbb2&style=flat&logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNCIgaGVpZ2h0PSIxNCI+PHBhdGggdmVjdG9yLWVmZmVjdD0ibm9uLXNjYWxpbmctc3Ryb2tlIiBkPSJNMCAyLjk5NWgxLjI4djguMDFIMHpNMi41NCAzaDEuMjh2NS4zNEgyLjU0em0yLjU1LS4wMDVoMS4yOHY4LjAxSDUuMDl6bTIuNTQuMDA3aDEuMjh2MS4zMzZINy42M3oiIGZpbGw9IiM5YzAwMDAiLz48cGF0aCB2ZWN0b3ItZWZmZWN0PSJub24tc2NhbGluZy1zdHJva2UiIGQ9Ik03LjYzIDUuNjZoMS4yOFYxMUg3LjYzeiIgZmlsbD0iIzdjN2Q3ZSIvPjxwYXRoIHZlY3Rvci1lZmZlY3Q9Im5vbi1zY2FsaW5nLXN0cm9rZSIgZD0iTTEwLjE3NyAzLjAwMmgzLjgyNnYxLjMzNmgtMy44MjZ6bS4wMDMgMi42NThoMS4yOFYxMWgtMS4yOHoiIGZpbGw9IiM5YzAwMDAiLz48L3N2Zz4="/>
9+
</a>
10+
11+
<!-- Twitter: @WalterWoshid -->
12+
<a href="https://twitter.com/WalterWoshid" target="_blank">
13+
<img alt="Twitter: @WalterWoshid" src="https://img.shields.io/badge/@WalterWoshid-Twitter?labelColor=ebdbb2&style=flat&logo=twitter&logoColor=458588&color=458588&label=Twitter"/>
14+
</a>
15+
16+
<!-- PHP: >=8.0 -->
17+
<a href="https://www.php.net" target="_blank">
18+
<img alt="PHP: >=8.0" src="https://img.shields.io/badge/PHP->=8.0-4C5789.svg?labelColor=ebdbb2&style=flat&logo=php&logoColor=4C5789"/>
19+
</a>
20+
21+
<!-- Packagist -->
22+
<a href="https://packagist.org/packages/okapi/singleton" target="_blank">
23+
<img alt="Packagist" src="https://img.shields.io/packagist/v/okapi/singleton?label=Packagist&labelColor=ebdbb2&style=flat&color=fe8019&logo=packagist"/>
24+
</a>
25+
26+
<!-- Build -->
27+
<!--suppress HtmlUnknownTarget -->
28+
<a href="../../actions/workflows/tests.yml" target="_blank">
29+
<img alt="Build" src="https://img.shields.io/github/actions/workflow/status/okapi-web/php-singleton/tests.yml?label=Build&labelColor=ebdbb2&style=flat&logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNiIgdmlld0JveD0iMCAwIDUxMiA1MTIiIGhlaWdodD0iMTYiPjxwYXRoIGZpbGw9IiM2YWFiMjAiIGQ9Ik0zMy45MTQgNDEzLjYxMmgxNDkuNTV2MjcuNTk1SDI3LjQ5NGMtMjYuMzQ4IDAtMzQuMTM2LTEzLjE5NC0yMS43MjktMzQuMzFMMTM3LjkxIDE4Ny43NTNWNjEuOTc1aC0yNi4wNzVjLTE5LjUwNCAwLTE5LjUwNC0yNy41OTUgMC0yNy41OTVoMTg5LjkzYzE5LjUwNSAwIDE5LjUwNSAyNy41OTUgMCAyNy41OTVIMjc1LjY5djEzMi44MjhoLTI3Ljk2M1Y2MS45NzVoLTgxLjg1NHYxMzIuODI4TDMzLjkxNCA0MTMuNjEyem0xMzUuNi0xNjkuMTg3TDg0LjY5MiAzODYuNTc0aDcwLjYwMWwxMDQuMzc1LTExMi45MDctMTUuNTgyLTI5LjI0MmgtNzQuNTd6bTE0NS45OTYgOS43ODNMMjA5LjUgMzY3LjUwNmwxMDYuMDEgMTEwLjI4NiAzMy41MzgtMzMuNTM4LTgwLjY1LTc2Ljc0OCA4MC42NS03OS43Ni0zMy41MzgtMzMuNTM4em01Ni45NDMgMzMuNTM3IDgwLjY1IDc5Ljc2LTgwLjY1IDc2Ljc1IDMzLjUzOCAzMy41MzdMNTEyIDM2Ny41MDYgNDA1Ljk5IDI1NC4yMDhsLTMzLjUzNyAzMy41Mzd6Ii8+PC9zdmc+">
30+
</a>
31+
</p>
32+
33+
<!-- Coverage -->
34+
<p align="center">
35+
<!-- Coverage - PHP 8.0 -->
36+
<a href="https://app.codecov.io/gh/okapi-web/php-singleton/flags" target="_blank">
37+
<img alt="Coverage - PHP 8.0" src="https://img.shields.io/codecov/c/github/okapi-web/php-singleton?flag=os-ubuntu-latest_php-8.0&label=Coverage - PHP 8.0&labelColor=ebdbb2&style=flat&logo=codecov&logoColor=FFC107&color=FFC107"/>
38+
</a>
39+
40+
<!-- Coverage - PHP 8.1 -->
41+
<a href="https://app.codecov.io/gh/okapi-web/php-singleton/flags" target="_blank">
42+
<img alt="Coverage - PHP 8.1" src="https://img.shields.io/codecov/c/github/okapi-web/php-singleton?flag=os-ubuntu-latest_php-8.1&label=Coverage - PHP 8.1&labelColor=ebdbb2&style=flat&logo=codecov&logoColor=FFC107&color=FFC107"/>
43+
</a>
44+
45+
<!-- Coverage - PHP 8.2 -->
46+
<a href="https://app.codecov.io/gh/okapi-web/php-singleton/flags" target="_blank">
47+
<img alt="Coverage - PHP 8.2" src="https://img.shields.io/codecov/c/github/okapi-web/php-singleton?flag=os-ubuntu-latest_php-8.2&label=Coverage - PHP 8.2&labelColor=ebdbb2&style=flat&logo=codecov&logoColor=FFC107&color=FFC107"/>
48+
</a>
49+
</p>
50+
51+
<h2 align="center">PHP Singleton is a PHP library that allows you to create singleton classes with a trait.</h2>
52+
53+
54+
55+
## Installation
56+
57+
```shell
58+
composer require okapi/singleton
59+
```
60+
61+
62+
63+
# Usage
64+
65+
```php
66+
<?php
67+
68+
use Okapi\Singleton\Singleton;
69+
70+
class GovernmentOfUSA
71+
{
72+
// Add the singleton trait
73+
use Singleton;
74+
75+
/**
76+
* Function to register the singleton.
77+
*
78+
* This function and the "initialized" methods are completely optional.
79+
*
80+
* Can be static or non-static.
81+
*/
82+
public static function register(): void
83+
{
84+
// Get instance
85+
$instance = self::getInstance();
86+
87+
// For non-static just use $this
88+
89+
// Make sure the instance is only registered once
90+
$instance->ensureNotAlreadyInitialized();
91+
92+
// Do something
93+
// ...
94+
95+
// Mark the instance as initialized
96+
$instance->setInitialized();
97+
}
98+
99+
/**
100+
* Custom function
101+
*/
102+
public function takeOverTheWorld(): void
103+
{
104+
// Make sure the instance is initialized
105+
$this->ensureAlreadyInitialized();
106+
107+
// Do something
108+
// ...
109+
}
110+
}
111+
112+
// Other file
113+
114+
// Register the singleton
115+
GovernmentOfUSA::register();
116+
117+
// Take over the world
118+
$instance = GovernmentOfUSA::getInstance();
119+
$instance->takeOverTheWorld();
120+
// or
121+
GovernmentOfUSA::getInstance()->takeOverTheWorld();
122+
```
123+
124+
125+
126+
## Testing
127+
128+
- Run `composer run-script test`<br>
129+
or
130+
- Run `composer run-script test-coverage`
131+
132+
133+
134+
## Show your support
135+
136+
Give a ⭐ if this project helped you!
137+
138+
139+
140+
## 📝 License
141+
142+
Copyright © 2023 [Valentin Wotschel](https://github.com/WalterWoshid).<br>
143+
This project is [MIT](https://opensource.org/licenses/MIT) licensed.

0 commit comments

Comments
 (0)