Adds php-di for dependency injection instead of using singletons

main
Tiger 2024-02-23 23:23:52 +01:00
parent fcee1f89d4
commit 792c3939ab
7 changed files with 276 additions and 32 deletions

View File

@ -1,7 +1,8 @@
{
"require": {
"openswoole/core": "22.1.5",
"openswoole/ide-helper": "~22.0.1"
"openswoole/ide-helper": "~22.0.1",
"php-di/php-di": "^7.0"
},
"autoload": {
"psr-4": {

243
composer.lock generated
View File

@ -4,8 +4,68 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "2425e97b911ce8e0f56be03367977c06",
"content-hash": "6765f1cecaa25edf73c7e92ee597b240",
"packages": [
{
"name": "laravel/serializable-closure",
"version": "v1.3.3",
"source": {
"type": "git",
"url": "https://github.com/laravel/serializable-closure.git",
"reference": "3dbf8a8e914634c48d389c1234552666b3d43754"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/laravel/serializable-closure/zipball/3dbf8a8e914634c48d389c1234552666b3d43754",
"reference": "3dbf8a8e914634c48d389c1234552666b3d43754",
"shasum": ""
},
"require": {
"php": "^7.3|^8.0"
},
"require-dev": {
"nesbot/carbon": "^2.61",
"pestphp/pest": "^1.21.3",
"phpstan/phpstan": "^1.8.2",
"symfony/var-dumper": "^5.4.11"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
}
},
"autoload": {
"psr-4": {
"Laravel\\SerializableClosure\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Taylor Otwell",
"email": "taylor@laravel.com"
},
{
"name": "Nuno Maduro",
"email": "nuno@laravel.com"
}
],
"description": "Laravel Serializable Closure provides an easy and secure way to serialize closures in PHP.",
"keywords": [
"closure",
"laravel",
"serializable"
],
"support": {
"issues": "https://github.com/laravel/serializable-closure/issues",
"source": "https://github.com/laravel/serializable-closure"
},
"time": "2023-11-08T14:08:06+00:00"
},
{
"name": "openswoole/core",
"version": "22.1.5",
@ -116,6 +176,187 @@
},
"time": "2023-02-05T22:27:37+00:00"
},
{
"name": "php-di/invoker",
"version": "2.3.4",
"source": {
"type": "git",
"url": "https://github.com/PHP-DI/Invoker.git",
"reference": "33234b32dafa8eb69202f950a1fc92055ed76a86"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/PHP-DI/Invoker/zipball/33234b32dafa8eb69202f950a1fc92055ed76a86",
"reference": "33234b32dafa8eb69202f950a1fc92055ed76a86",
"shasum": ""
},
"require": {
"php": ">=7.3",
"psr/container": "^1.0|^2.0"
},
"require-dev": {
"athletic/athletic": "~0.1.8",
"mnapoli/hard-mode": "~0.3.0",
"phpunit/phpunit": "^9.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Invoker\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "Generic and extensible callable invoker",
"homepage": "https://github.com/PHP-DI/Invoker",
"keywords": [
"callable",
"dependency",
"dependency-injection",
"injection",
"invoke",
"invoker"
],
"support": {
"issues": "https://github.com/PHP-DI/Invoker/issues",
"source": "https://github.com/PHP-DI/Invoker/tree/2.3.4"
},
"funding": [
{
"url": "https://github.com/mnapoli",
"type": "github"
}
],
"time": "2023-09-08T09:24:21+00:00"
},
{
"name": "php-di/php-di",
"version": "7.0.6",
"source": {
"type": "git",
"url": "https://github.com/PHP-DI/PHP-DI.git",
"reference": "8097948a89f6ec782839b3e958432f427cac37fd"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/PHP-DI/PHP-DI/zipball/8097948a89f6ec782839b3e958432f427cac37fd",
"reference": "8097948a89f6ec782839b3e958432f427cac37fd",
"shasum": ""
},
"require": {
"laravel/serializable-closure": "^1.0",
"php": ">=8.0",
"php-di/invoker": "^2.0",
"psr/container": "^1.1 || ^2.0"
},
"provide": {
"psr/container-implementation": "^1.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3",
"friendsofphp/proxy-manager-lts": "^1",
"mnapoli/phpunit-easymock": "^1.3",
"phpunit/phpunit": "^9.5",
"vimeo/psalm": "^4.6"
},
"suggest": {
"friendsofphp/proxy-manager-lts": "Install it if you want to use lazy injection (version ^1)"
},
"type": "library",
"autoload": {
"files": [
"src/functions.php"
],
"psr-4": {
"DI\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "The dependency injection container for humans",
"homepage": "https://php-di.org/",
"keywords": [
"PSR-11",
"container",
"container-interop",
"dependency injection",
"di",
"ioc",
"psr11"
],
"support": {
"issues": "https://github.com/PHP-DI/PHP-DI/issues",
"source": "https://github.com/PHP-DI/PHP-DI/tree/7.0.6"
},
"funding": [
{
"url": "https://github.com/mnapoli",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/php-di/php-di",
"type": "tidelift"
}
],
"time": "2023-11-02T10:04:50+00:00"
},
{
"name": "psr/container",
"version": "2.0.2",
"source": {
"type": "git",
"url": "https://github.com/php-fig/container.git",
"reference": "c71ecc56dfe541dbd90c5360474fbc405f8d5963"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/container/zipball/c71ecc56dfe541dbd90c5360474fbc405f8d5963",
"reference": "c71ecc56dfe541dbd90c5360474fbc405f8d5963",
"shasum": ""
},
"require": {
"php": ">=7.4.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Container\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "https://www.php-fig.org/"
}
],
"description": "Common Container Interface (PHP FIG PSR-11)",
"homepage": "https://github.com/php-fig/container",
"keywords": [
"PSR-11",
"container",
"container-interface",
"container-interop",
"psr"
],
"support": {
"issues": "https://github.com/php-fig/container/issues",
"source": "https://github.com/php-fig/container/tree/2.0.2"
},
"time": "2021-11-05T16:47:00+00:00"
},
{
"name": "psr/http-message",
"version": "2.0",

View File

@ -2,6 +2,17 @@
require_once 'vendor/autoload.php';
use DI\ContainerBuilder;
use Emulator\Core\Logging\Logger;
use Emulator\HabboEnvironment;
HabboEnvironment::getInstance()->initialize();
$containerBuilder = new ContainerBuilder();
$containerBuilder->useAutowiring(true);
$containerBuilder->enableCompilation(dirname(__DIR__, 1) . '/var/cache/php-di');
try {
$container = $containerBuilder->build();
$container->get(HabboEnvironment::class)->initialize();
} catch (Exception $ex) {
Logger::fatal($ex->getTraceAsString());
}

View File

@ -6,21 +6,15 @@ use Emulator\Core\Logging\Logger;
use Emulator\Messages\MessageHandler;
use Emulator\Network\Game\GameNetworkServer;
class HabboEnvironment {
private static ?HabboEnvironment $instance = null;
readonly class HabboEnvironment {
public function __construct(private MessageHandler $messageHandler, private GameNetworkServer $gameNetworkServer) {
}
public function initialize(): void {
Logger::info("Starting up [b]HabboEmulator[/b] (PHP Edition) for [r]$_SERVER[USER][/r]");
MessageHandler::getInstance();
$this->messageHandler->initialize();
GameNetworkServer::getInstance()->start(2096);
}
public static function getInstance(): HabboEnvironment {
if (self::$instance === null)
self::$instance = new self();
return self::$instance;
$this->gameNetworkServer->start(2096);
}
}

View File

@ -11,13 +11,17 @@ use Emulator\Network\Game\Sessions\Session;
use SplObjectStorage;
class MessageHandler {
private static ?MessageHandler $instance = null;
private SplObjectStorage $handlers;
public function __construct() {
$this->handlers = new SplObjectStorage();
}
public function initialize(): void {
$this->handlers[Header::ReleaseVersion] = new ReleaseVersionMessageEvent();
$this->handlers[Header::SecurityTicket] = new SecurityTicketMessageEvent();
Logger::info("Loaded " . count($this->handlers) . " message handlers");
}
public function handlePacket(Session $session, ClientMessage $request): void {
@ -31,11 +35,4 @@ class MessageHandler {
Logger::warn("[{$request->getHeader()}] - Unregistered!");
}
}
public static function getInstance(): MessageHandler {
if (self::$instance === null)
self::$instance = new self();
return self::$instance;
}
}

View File

@ -12,8 +12,10 @@ use OpenSwoole\Http\Request;
use OpenSwoole\WebSocket\Frame;
class GameNetworkServer {
private static ?GameNetworkServer $instance = null;
private array $sessions;
public function __construct(private readonly MessageHandler $messageHandler) {
$this->sessions = [];
}
public function start(int $port): void {
$server = new Server("127.0.0.1", $port);
@ -25,14 +27,6 @@ class GameNetworkServer {
$server->start();
}
public static function getInstance(): GameNetworkServer {
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
function onStart(Server $server): void {
Logger::info("Websocket server is started at $server->host:$server->port");
}
@ -58,7 +52,7 @@ class GameNetworkServer {
$length = ByteEncoding::getInt32(array_slice($data, 1, 4));
$packet = array_slice($data, 4, $length);
MessageHandler::getInstance()->handlePacket($this->sessions[$frame->fd], new ClientMessage($packet));
$this->messageHandler->handlePacket($this->sessions[$frame->fd], new ClientMessage($packet));
}
}
}

6
var/cache/php-di/.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
# .gitignore sample
# Ignore all files in this dir...
*
# ... except for this one.
!.gitignore