Skip to content

Commit

Permalink
Add Container
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed May 1, 2024
1 parent af4fb27 commit ed02006
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 16 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@
"nunomaduro/termwind": "^1.15 || ^2",
"nyholm/psr7": "^1.8",
"php-http/message": "^1.15",
"psr/container": "^1.1 || ^2.0",
"psr/http-message": "^1.1 || ^2",
"symfony/console": "^6.4 || ^7",
"symfony/var-dumper": "^6.3 || ^7"
"symfony/var-dumper": "^6.3 || ^7",
"yiisoft/injector": "^1.2"
},
"require-dev": {
"dereuromark/composer-prefer-lowest": "^0.1.10",
Expand Down
7 changes: 5 additions & 2 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

namespace Buggregator\Trap;

use Buggregator\Trap\Config\SocketServer;
use Buggregator\Trap\Config\Server\SocketServer;
use Buggregator\Trap\Handler\Http\Handler\Websocket;
use Buggregator\Trap\Handler\Http\Middleware;
use Buggregator\Trap\Proto\Buffer;
use Buggregator\Trap\Service\Container;
use Buggregator\Trap\Socket\Client;
use Buggregator\Trap\Socket\Server;
use Buggregator\Trap\Socket\SocketStream;
Expand All @@ -31,17 +32,19 @@ final class Application implements Processable, Cancellable, Destroyable

private readonly Buffer $buffer;
private bool $cancelled = false;
private readonly Logger $logger;

/**
* @param SocketServer[] $map
* @param Sender[] $senders
*/
public function __construct(
private readonly Container $container,
array $map = [],
private readonly Logger $logger = new Logger(),
private array $senders = [],
bool $withFrontend = true,
) {
$this->logger = $this->container->get(Logger::class);
$this->buffer = new Buffer(bufferSize: 10485760, timer: 0.1);

$inspector = new Inspector(
Expand Down
17 changes: 10 additions & 7 deletions src/Command/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
namespace Buggregator\Trap\Command;

use Buggregator\Trap\Application;
use Buggregator\Trap\Config\SocketServer;
use Buggregator\Trap\Config\Server\SocketServer;
use Buggregator\Trap\Info;
use Buggregator\Trap\Logger;
use Buggregator\Trap\Sender;
use Buggregator\Trap\Service\Container;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\SignalableCommandInterface;
Expand Down Expand Up @@ -82,13 +83,15 @@ protected function execute(

$registry = $this->createRegistry($output);

$this->app = new Application(
$servers,
new Logger($output),
senders: $registry->getSenders($senders),
withFrontend: $input->getOption('ui') !== false,
);
$container = new Container();
$container->set(new Logger($output));
$container->set($container->make(Application::class, [
'map' => $servers,
'senders' => $registry->getSenders($senders),
'withFrontend' => $input->getOption('ui') !== false,
]));

$this->app = $container->get(Application::class);
$this->app->run();
} catch (\Throwable $e) {
if ($output->isVerbose()) {
Expand Down
2 changes: 1 addition & 1 deletion src/Config/Frontend.php → src/Config/Server/Frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Buggregator\Trap\Config;
namespace Buggregator\Trap\Config\Server;

use Buggregator\Trap\Service\Config\XPath;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Buggregator\Trap\Config\Frontend;
namespace Buggregator\Trap\Config\Server\Frontend;

use Buggregator\Trap\Service\Config\XPath;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Buggregator\Trap\Config;
namespace Buggregator\Trap\Config\Server;

/**
* @internal
Expand Down
2 changes: 1 addition & 1 deletion src/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class Info
{
public const NAME = 'Buggregator Trap';
public const VERSION = '1.4.6';
public const VERSION = '1.6.0';
public const LOGO_CLI_COLOR = <<<CONSOLE
\e[44;97;1m \e[0m
\e[44;97;1m ▄█▀ ▀█▄ \e[0m
Expand Down
2 changes: 1 addition & 1 deletion src/Sender/Frontend/EventStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Buggregator\Trap\Sender\Frontend;

use Buggregator\Trap\Config\Frontend\EventStorage as Config;
use Buggregator\Trap\Config\Server\Frontend\EventStorage as Config;
use Countable;
use IteratorAggregate;

Expand Down
115 changes: 115 additions & 0 deletions src/Service/Container.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

declare(strict_types=1);

namespace Buggregator\Trap\Service;

use Buggregator\Trap\Destroyable;
use Buggregator\Trap\Service\Config\ConfigLoader;
use Psr\Container\ContainerInterface;
use Yiisoft\Injector\Injector;

/**
* Simple Trap container.
*
* @internal
*/
final class Container implements ContainerInterface, Destroyable
{
/** @var array<class-string, object> */
private array $cache = [];

/** @var array<class-string, callable(Container): object> */
private array $factory = [];

private readonly Injector $injector;

/**
* @psalm-suppress PropertyTypeCoercion
*/
public function __construct()
{
$this->injector = (new Injector($this))->withCacheReflections(false);
$this->cache[Injector::class] = $this->injector;
$this->cache[self::class] = $this;
$this->cache[ContainerInterface::class] = $this;
}

/**
* @template T of object
* @param class-string<T> $id
* @return T

Check failure on line 41 in src/Service/Container.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.2, OS ubuntu-latest)

InvalidReturnType

src/Service/Container.php:41:16: InvalidReturnType: The declared return type 'T:fn-buggregator\trap\service\container::get as object' for Buggregator\Trap\Service\Container::get is incorrect, got '(T:fn-buggregator\trap\service\container::get as object)|object' (see https://psalm.dev/011)
*
* @psalm-suppress MoreSpecificImplementedParamType
*/
public function get(string $id): object
{
/** @psalm-suppress InvalidReturnStatement */
return $this->cache[$id] ??= $this->make($id);
}

/**
* @param class-string $id
*
* @psalm-suppress MoreSpecificImplementedParamType
*/
public function has(string $id): bool
{
return \array_key_exists($id, $this->cache) || \array_key_exists($id, $this->factory);
}

/**
* @template T of object
* @param T $service
* @param class-string<T>|null $id
*/
public function set(object $service, ?string $id = null): void
{
\assert($id === null || $service instanceof $id, "Service must be instance of {$id}.");
$this->cache[$id ?? \get_class($service)] = $service;
}

/**
* Create an object of the specified class without caching.
*
* @template T
* @param class-string<T> $class
* @return T
*/
public function make(string $class, array $arguments = []): object
{
$result = \array_key_exists($class, $this->factory)
? ($this->factory[$class])($this)
: $this->injector->make($class, $arguments);

\assert($result instanceof $class, "Created object must be instance of {$class}.");

// Detect Trap related types

// Configs
if (\str_starts_with($class, 'Buggregator\\Trap\\Config\\')) {
// Hydrate config
$configLoader = $this->get(ConfigLoader::class);
$configLoader->hidrate($result);
}

return $result;
}

/**
* Declare a factory for the specified class.
*
* @template T of object
* @param class-string<T> $id
* @param (callable(Container): T) $callback
*/
public function bind(string $id, callable $callback): void
{
$this->factory[$id] = $callback;
}

public function destroy(): void
{
unset($this->cache, $this->factory, $this->injector);
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Sender/Frontend/EventsStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Buggregator\Trap\Tests\Unit\Sender\Frontend;

use Buggregator\Trap\Config\Frontend\EventStorage as Config;
use Buggregator\Trap\Config\Server\Frontend\EventStorage as Config;
use Buggregator\Trap\Sender\Frontend\Event;
use Buggregator\Trap\Sender\Frontend\EventStorage;
use Buggregator\Trap\Support\Uuid;
Expand Down

0 comments on commit ed02006

Please sign in to comment.