Skip to content

Commit

Permalink
Merge branch 'master' into maintenance
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk authored Jan 28, 2024
2 parents 922e0fc + a7c15c3 commit fe1a61a
Show file tree
Hide file tree
Showing 17 changed files with 259 additions and 62 deletions.
23 changes: 23 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!--- Note to EXTERNAL Contributors -->
<!-- Thanks for opening a PR!
If it is a significant code change, please **make sure there is an open issue** for this.
We work best with you when we have accepted the idea first before you code. -->

<!--- For ALL Contributors 👇 -->

## What was changed
<!-- Describe what has changed in this PR -->

## Why?
<!-- Tell your future self why have you made these changes -->

## Checklist
<!--- add/delete as needed --->

- Closes #<!-- add issue number here -->
- How was this tested:
<!--- Please describe how you tested your changes/how we can test them -->
<!-- - Added autotests -->
- Any docs updates needed?
<!--- Provide a part of the documentation that should be updated
or a link to related PR in the documentation repository -->
46 changes: 28 additions & 18 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
# BSD-3 License
# BSD 3-Clause

Copyright (c) 2023–2024, Buggregator. All rights reserved.
Copyright (c) 2023, Buggregator.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Buggregator nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of Buggregator nor the names the copyright holder nor the names of its contributors may be used to
endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ To install Buggregator Trap in your PHP application, add the package as a dev de
to your project using Composer:

```bash
composer require --dev buggregator/trap
composer require --dev buggregator/trap -W
```

[![PHP](https://img.shields.io/packagist/php-v/buggregator/trap.svg?style=flat-square&logo=php)](https://packagist.org/packages/buggregator/trap)
Expand Down Expand Up @@ -102,6 +102,12 @@ trap($animal)->once()->if($var instanceof Animal\Cat);

---

> [!TIP]
> Feature in development:
> add the flag `--ui` to rise the web interface of the Buggregator Server without docker.
![trap-ui](https://github.com/buggregator/trap/assets/4152481/1ccc2c85-2f81-4b62-8ae7-49ee76380674)
---

We care about the quality of our products' codebase and strive to provide the best user experience.
Buggregator Trap has passed the Proof of Concept stage and is now an important part of the Buggregator ecosystem.
We have big plans for the development of the entire ecosystem and we would be delighted if you join us on this journey.
Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"description": "A simple and powerful tool for debugging PHP applications.",
"homepage": "https://buggregator.dev/",
"keywords": [
"debug", "cli", "console", "sentry", "smtp", "dump", "binary dump", "websockets", "server", "helper"
"debug", "cli", "console", "sentry", "smtp", "dump", "binary dump", "websockets", "server", "helper", "dev"
],
"authors": [
{
Expand Down Expand Up @@ -56,11 +56,11 @@
"php": ">=8.1",
"ext-sockets": "*",
"clue/stream-filter": "^1.6",
"nunomaduro/termwind": "^1.15",
"nunomaduro/termwind": "^1.15 || ^2",
"nyholm/psr7": "^1.8",
"php-http/message": "^1.15",
"psr/http-message": "^1.1 || ^2",
"symfony/console": "^5.4 || ^6 || ^7",
"symfony/console": "^6.4 || ^7",
"symfony/var-dumper": "^6.3 || ^7"
},
"require-dev": {
Expand All @@ -73,6 +73,7 @@
"symfony/polyfill-php72": "*",
"symfony/polyfill-php73": "*",
"symfony/polyfill-php74": "*",
"symfony/polyfill-php80": "*"
"symfony/polyfill-php80": "*",
"symfony/polyfill-php81": "*"
}
}
35 changes: 31 additions & 4 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* @internal
*/
final class Application implements Processable
final class Application implements Processable, Cancellable, Destroyable
{
/** @var Processable[] */
private array $processors = [];
Expand All @@ -30,6 +30,7 @@ final class Application implements Processable
private array $fibers = [];

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

/**
* @param SocketServer[] $map
Expand Down Expand Up @@ -115,6 +116,31 @@ public function process(array $senders = []): void
}
}

public function destroy(): void
{
foreach ([...$this->servers, ...$this->processors] as $instance) {
if ($instance instanceof Destroyable) {
$instance->destroy();
}
}

$this->servers = [];
$this->processors = [];
$this->fibers = [];
}

public function cancel(): void
{
$this->cancelled = true;
$this->fibers[] = new Fiber(
function () {
foreach ($this->servers as $server) {
$server->cancel();
}
}
);
}

/**
* @param Sender[] $senders
*/
Expand All @@ -131,8 +157,9 @@ private function sendBuffer(array $senders = []): void

private function createServer(SocketServer $config, Inspector $inspector): Server
{
$clientInflector = static function (Client $client, int $id) use ($inspector): Client {
// Logger::debug('New client connected %d', $id);
$logger = $this->logger;
$clientInflector = static function (Client $client, int $id) use ($inspector, $logger): Client {
$logger->debug('Client %d connected', $id);
$inspector->addStream(SocketStream::create($client, $id));
return $client;
};
Expand Down Expand Up @@ -161,7 +188,7 @@ public function prepareServerFiber(SocketServer $config, Inspector $inspector, L
$logger->error("Can't create TCP socket on port $config->port.");
(new Timer(1.0))->wait();
}
} while (true);
} while (!$this->cancelled);
});
}

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

declare(strict_types=1);

namespace Buggregator\Trap;

/**
* The implementation can be canceled safely.
*
* @internal
*/
interface Cancellable
{
public function cancel(): void;
}
2 changes: 2 additions & 0 deletions src/Client/TrapHandle/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ final class Dumper

public static function dump(mixed $var, string|int|null $label = null, int $depth = 0): mixed
{
/** @psalm-suppress RiskyTruthyFalsyComparison */
return (self::$handler ??= self::registerHandler())($var, empty($label) ? null : (string)$label, $depth);
}

Expand Down Expand Up @@ -72,6 +73,7 @@ public static function setDumper(?DataDumperInterface $dumper = null): Closure
* @return Closure(mixed, string|null, int): mixed
*
* @author Nicolas Grekas <p@tchwork.com>
* @psalm-suppress RiskyTruthyFalsyComparison
*/
private static function registerHandler(): Closure
{
Expand Down
40 changes: 37 additions & 3 deletions src/Command/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Buggregator\Trap\Sender;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\SignalableCommandInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -24,8 +25,11 @@
name: 'run',
description: 'Run application',
)]
final class Run extends Command
final class Run extends Command implements SignalableCommandInterface
{
private ?Application $app = null;
private bool $cancelled = false;

public function configure(): void
{
$this->addOption('port', 'p', InputOption::VALUE_OPTIONAL, 'Port to listen', 9912);
Expand Down Expand Up @@ -54,14 +58,14 @@ protected function execute(

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

$app = new Application(
$this->app = new Application(
[new SocketServer($port)],
new Logger($output),
senders: $registry->getSenders($senders),
withFrontend: $input->getOption('ui') !== false,
);

$app->run();
$this->app->run();
} catch (\Throwable $e) {
if ($output->isVerbose()) {
// Write colorful exception (title, message, stacktrace)
Expand Down Expand Up @@ -93,4 +97,34 @@ public function createRegistry(OutputInterface $output): Sender\SenderRegistry

return $registry;
}

public function getSubscribedSignals(): array
{
$result = [];
\defined('SIGINT') and $result[] = \SIGINT;
\defined('SIGTERM') and $result[] = \SIGTERM;

return $result;
}

public function handleSignal(int $signal, int|false $previousExitCode = 0): int|false
{
if (\defined('SIGINT') && $signal === \SIGINT) {
if ($this->cancelled) {
// Force exit
$this->app?->destroy();
return $signal;
}

$this->app?->cancel();
$this->cancelled = true;
}

if (\defined('SIGTERM') && $signal === \SIGTERM) {
$this->app?->destroy();
return $signal;
}

return false;
}
}
15 changes: 15 additions & 0 deletions src/Destroyable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Buggregator\Trap;

/**
* Should be used to destroy objects and free resources.
*
* @internal
*/
interface Destroyable
{
public function destroy(): void;
}
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.3.1';
public const VERSION = '1.3.3';
public const LOGO_CLI_COLOR = <<<CONSOLE
\e[44;97;1m \e[0m
\e[44;97;1m ▄█▀ ▀█▄ \e[0m
Expand Down
4 changes: 3 additions & 1 deletion src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ public function exception(Throwable $e, ?string $header = null, bool $important
$r .= "In \033[3;32m" . $e->getFile() . ':' . $e->getLine() . "\033[0m\n";
// Print stack trace using gray
$r .= "Stack trace:\n";
$r .= "\033[90m" . $e->getTraceAsString() . "\033[0m\n";
// Limit stacktrace to 5 lines
$stack = \explode("\n", $e->getTraceAsString());
$r .= "\033[90m" . implode("\n", \array_slice($stack, 0, \min(5, \count($stack)))) . "\033[0m\n";
$r .= "\n";
$this->echo($r, !$important);
}
Expand Down
3 changes: 3 additions & 0 deletions src/Sender/Console/Renderer/Sentry/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public static function render(OutputInterface $output, mixed $exceptions): void

/**
* Renders the trace of the exception.
*
* @psalm-suppress RiskyTruthyFalsyComparison
*/
private static function renderTrace(OutputInterface $output, array $frames, bool $verbose = false): void
{
Expand All @@ -82,6 +84,7 @@ private static function renderTrace(OutputInterface $output, array $frames, bool
$file = $getValue($frame, 'filename');
$line = $getValue($frame, 'lineno', null);
$class = $getValue($frame, 'class');
/** @psalm-suppress RiskyTruthyFalsyComparison */
$class = empty($class) ? '' : $class . '::';
$function = $getValue($frame, 'function');

Expand Down
3 changes: 3 additions & 0 deletions src/Sender/Console/Renderer/VarDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public function __construct(
) {
}

/**
* @psalm-suppress RiskyTruthyFalsyComparison
*/
public function describe(OutputInterface $output, Data $data, array $context, int $clientId): void
{
Common::renderHeader1($output, 'DUMP');
Expand Down
Loading

0 comments on commit fe1a61a

Please sign in to comment.