mirror of
https://github.com/torrentpier/torrentpier
synced 2025-08-21 22:03:49 -07:00
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.
This commit is contained in:
parent
314ceacbe7
commit
58e02e0924
5 changed files with 207 additions and 0 deletions
36
app/Container/Container.php
Normal file
36
app/Container/Container.php
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Container;
|
||||||
|
|
||||||
|
use Illuminate\Container\Container as IlluminateContainer;
|
||||||
|
use Illuminate\Contracts\Container\BindingResolutionException;
|
||||||
|
use Illuminate\Contracts\Container\CircularDependencyException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Application Container
|
||||||
|
*
|
||||||
|
* Extends Illuminate Container with application-specific functionality
|
||||||
|
*/
|
||||||
|
class Container extends IlluminateContainer
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Get a service from the container
|
||||||
|
*
|
||||||
|
* @throws BindingResolutionException When the service cannot be resolved
|
||||||
|
* @throws CircularDependencyException
|
||||||
|
*/
|
||||||
|
public function get(string $id): mixed
|
||||||
|
{
|
||||||
|
return $this->resolve($id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a service exists in the container
|
||||||
|
*/
|
||||||
|
public function has(string $id): bool
|
||||||
|
{
|
||||||
|
return $this->bound($id);
|
||||||
|
}
|
||||||
|
}
|
66
app/Events/TorrentUploaded.php
Normal file
66
app/Events/TorrentUploaded.php
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Events;
|
||||||
|
|
||||||
|
use DateTimeInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event fired when a new torrent is uploaded
|
||||||
|
*/
|
||||||
|
readonly class TorrentUploaded
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Create a new event instance
|
||||||
|
*/
|
||||||
|
public function __construct(
|
||||||
|
public int $torrentId,
|
||||||
|
public int $uploaderId,
|
||||||
|
public string $torrentName,
|
||||||
|
public int $size,
|
||||||
|
public DateTimeInterface $uploadedAt
|
||||||
|
)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the torrent ID
|
||||||
|
*/
|
||||||
|
public function getTorrentId(): int
|
||||||
|
{
|
||||||
|
return $this->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;
|
||||||
|
}
|
||||||
|
}
|
57
app/Events/UserRegistered.php
Normal file
57
app/Events/UserRegistered.php
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Events;
|
||||||
|
|
||||||
|
use DateTimeInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event fired when a new user is registered
|
||||||
|
*/
|
||||||
|
readonly class UserRegistered
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Create a new event instance
|
||||||
|
*/
|
||||||
|
public function __construct(
|
||||||
|
public int $userId,
|
||||||
|
public string $username,
|
||||||
|
public string $email,
|
||||||
|
public DateTimeInterface $registeredAt
|
||||||
|
)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the user ID
|
||||||
|
*/
|
||||||
|
public function getUserId(): int
|
||||||
|
{
|
||||||
|
return $this->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;
|
||||||
|
}
|
||||||
|
}
|
48
app/Exceptions/Handler.php
Normal file
48
app/Exceptions/Handler.php
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Exceptions;
|
||||||
|
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Application Exception Handler
|
||||||
|
*
|
||||||
|
* Handles all uncaught exceptions in the application
|
||||||
|
*/
|
||||||
|
class Handler
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Handle an uncaught exception
|
||||||
|
*/
|
||||||
|
public function handle(Throwable $exception): void
|
||||||
|
{
|
||||||
|
// Log the exception
|
||||||
|
error_log($exception->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 '<h1>Error</h1><p>' . htmlspecialchars($exception->getMessage()) . '</p>';
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue