From 58e02e0924d02ca1ff708371949bdd1f09c214b1 Mon Sep 17 00:00:00 2001 From: Yury Pikhtarev Date: Mon, 23 Jun 2025 01:13:46 +0400 Subject: [PATCH] feat: add application container and event classes for user and torrent uploads - Introduced a new Container class extending the Illuminate Container for application-specific functionality. - Added TorrentUploaded and UserRegistered event classes to handle torrent uploads and user registrations, respectively. - Implemented methods for retrieving service instances and checking service existence in the Container. - Established a structured approach for event data encapsulation with readonly properties and getter methods. This update enhances the application's architecture by integrating a custom container and event-driven design, improving maintainability and extensibility. --- app/Container/Container.php | 36 +++++++++++++++++++ app/Events/TorrentUploaded.php | 66 ++++++++++++++++++++++++++++++++++ app/Events/UserRegistered.php | 57 +++++++++++++++++++++++++++++ app/Exceptions/.keep | 0 app/Exceptions/Handler.php | 48 +++++++++++++++++++++++++ 5 files changed, 207 insertions(+) create mode 100644 app/Container/Container.php create mode 100644 app/Events/TorrentUploaded.php create mode 100644 app/Events/UserRegistered.php delete mode 100644 app/Exceptions/.keep create mode 100644 app/Exceptions/Handler.php diff --git a/app/Container/Container.php b/app/Container/Container.php new file mode 100644 index 000000000..bd3e1fff6 --- /dev/null +++ b/app/Container/Container.php @@ -0,0 +1,36 @@ +resolve($id); + } + + /** + * Check if a service exists in the container + */ + public function has(string $id): bool + { + return $this->bound($id); + } +} diff --git a/app/Events/TorrentUploaded.php b/app/Events/TorrentUploaded.php new file mode 100644 index 000000000..1727b6a35 --- /dev/null +++ b/app/Events/TorrentUploaded.php @@ -0,0 +1,66 @@ +torrentId; + } + + /** + * Get the uploader user ID + */ + public function getUploaderId(): int + { + return $this->uploaderId; + } + + /** + * Get the torrent name + */ + public function getTorrentName(): string + { + return $this->torrentName; + } + + /** + * Get the torrent size in bytes + */ + public function getSize(): int + { + return $this->size; + } + + /** + * Get the upload timestamp + */ + public function getUploadedAt(): DateTimeInterface + { + return $this->uploadedAt; + } +} diff --git a/app/Events/UserRegistered.php b/app/Events/UserRegistered.php new file mode 100644 index 000000000..3298e518c --- /dev/null +++ b/app/Events/UserRegistered.php @@ -0,0 +1,57 @@ +userId; + } + + /** + * Get the username + */ + public function getUsername(): string + { + return $this->username; + } + + /** + * Get the email + */ + public function getEmail(): string + { + return $this->email; + } + + /** + * Get the registration timestamp + */ + public function getRegisteredAt(): DateTimeInterface + { + return $this->registeredAt; + } +} diff --git a/app/Exceptions/.keep b/app/Exceptions/.keep deleted file mode 100644 index e69de29bb..000000000 diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php new file mode 100644 index 000000000..9f5d96458 --- /dev/null +++ b/app/Exceptions/Handler.php @@ -0,0 +1,48 @@ +getMessage() . ' in ' . $exception->getFile() . ':' . $exception->getLine()); + + // You can add custom handling here + } + + /** + * Render an exception for HTTP response + */ + public function render(Throwable $exception): string + { + if (php_sapi_name() === 'cli') { + return $exception->getMessage() . "\n"; + } + + // Return JSON for API requests or HTML for web + $isJson = isset($_SERVER['HTTP_ACCEPT']) && str_contains($_SERVER['HTTP_ACCEPT'], 'application/json'); + + if ($isJson) { + return json_encode([ + 'error' => $exception->getMessage(), + 'code' => $exception->getCode() + ]); + } + + return '

Error

' . htmlspecialchars($exception->getMessage()) . '

'; + } +}