Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileSession: set greater gc probability #585

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/coding-style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: 8.0
php-version: 8.1
coverage: none

- run: composer create-project nette/code-checker temp/code-checker ^3 --no-progress
Expand All @@ -24,7 +24,7 @@ jobs:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: 8.0
php-version: 8.1
coverage: none

- run: composer create-project nette/coding-standard temp/coding-standard ^3 --no-progress
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: 8.0
php-version: 8.1
coverage: none

- run: composer install --no-progress --prefer-dist
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
php: ['8.0', '8.1', '8.2', '8.3', '8.4']
php: ['8.1', '8.2', '8.3', '8.4']
sapi: ['php', 'php-cgi']

fail-fast: false
Expand Down Expand Up @@ -41,7 +41,7 @@ jobs:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: 8.0
php-version: 8.1
extensions: ds
coverage: none

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
],
"require": {
"php": "8.0 - 8.4",
"php": "8.1 - 8.4",
"ext-session": "*",
"ext-json": "*"
},
Expand Down Expand Up @@ -43,7 +43,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "2.10-dev"
"dev-master": "3.0-dev"
}
}
}
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ composer require tracy/tracy

Alternatively, you can download the whole package or [tracy.phar](https://github.com/nette/tracy/releases) file.

Tracy is compatible with PHP 8.0 to 8.4.
Tracy is compatible with PHP 8.1 to 8.4.

 <!---->

Expand Down Expand Up @@ -456,5 +456,6 @@ This is a list of unofficial integrations to other frameworks and CMS:
- [Slim Framework](https://github.com/runcmf/runtracy)
- Symfony framework: [kutny/tracy-bundle](https://github.com/kutny/tracy-bundle), [VasekPurchart/Tracy-Blue-Screen-Bundle](https://github.com/VasekPurchart/Tracy-Blue-Screen-Bundle)
- [Wordpress](https://github.com/ktstudio/WP-Tracy)
- [Joomla! CMS](https://n3t.bitbucket.io/extension/n3t-debug/)

... feel free to be famous, create an integration for your favourite platform!
72 changes: 1 addition & 71 deletions src/Bridges/Nette/Bridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,95 +9,25 @@

namespace Tracy\Bridges\Nette;

use Latte;
use Nette;
use Tracy;
use Tracy\BlueScreen;
use Tracy\Helpers;


/**
* Bridge for NEON & Latte.
* Bridge for NEON.
*/
class Bridge
{
public static function initialize(): void
{
$blueScreen = Tracy\Debugger::getBlueScreen();
if (!class_exists(Latte\Bridges\Tracy\BlueScreenPanel::class)) {
$blueScreen->addPanel([self::class, 'renderLatteError']);
$blueScreen->addAction([self::class, 'renderLatteUnknownMacro']);
$blueScreen->addFileGenerator(fn(string $file) => substr($file, -6) === '.latte'
? "{block content}\n\$END\$"
: null);
Tracy\Debugger::addSourceMapper([self::class, 'mapLatteSourceCode']);
}

$blueScreen->addAction([self::class, 'renderMemberAccessException']);
$blueScreen->addPanel([self::class, 'renderNeonError']);
}


public static function renderLatteError(?\Throwable $e): ?array
{
if ($e instanceof Latte\CompileException && $e->sourceName) {
return [
'tab' => 'Template',
'panel' => (preg_match('#\n|\?#', $e->sourceName)
? ''
: '<p>'
. (@is_file($e->sourceName) // @ - may trigger error
? '<b>File:</b> ' . Helpers::editorLink($e->sourceName, $e->sourceLine)
: '<b>' . htmlspecialchars($e->sourceName . ($e->sourceLine ? ':' . $e->sourceLine : '')) . '</b>')
. '</p>')
. BlueScreen::highlightFile($e->sourceCode, $e->sourceLine, php: false),
];
}

return null;
}


public static function renderLatteUnknownMacro(?\Throwable $e): ?array
{
if (
$e instanceof Latte\CompileException
&& $e->sourceName
&& @is_file($e->sourceName) // @ - may trigger error
&& (preg_match('#Unknown macro (\{\w+)\}, did you mean (\{\w+)\}\?#A', $e->getMessage(), $m)
|| preg_match('#Unknown attribute (n:\w+), did you mean (n:\w+)\?#A', $e->getMessage(), $m))
) {
return [
'link' => Helpers::editorUri($e->sourceName, $e->sourceLine, 'fix', $m[1], $m[2]),
'label' => 'fix it',
];
}

return null;
}


/** @return array{file: string, line: int, label: string, active: bool} */
public static function mapLatteSourceCode(string $file, int $line): ?array
{
if (!strpos($file, '.latte--')) {
return null;
}

$lines = file($file);
if (
!preg_match('#^/(?:\*\*|/) source: (\S+\.latte)#m', implode('', array_slice($lines, 0, 10)), $m)
|| !@is_file($m[1]) // @ - may trigger error
) {
return null;
}

$file = $m[1];
$line = $line && preg_match('#/\* line (\d+) \*/#', $lines[$line - 1], $m) ? (int) $m[1] : 0;
return ['file' => $file, 'line' => $line, 'label' => 'Latte', 'active' => true];
}


public static function renderMemberAccessException(?\Throwable $e): ?array
{
if (!$e instanceof Nette\MemberAccessException && !$e instanceof \LogicException) {
Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/Nette/TracyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@
}
}

if ($this->config->netteMailer && $builder->getByType(Nette\Mail\IMailer::class)) {

Check failure on line 118 in src/Bridges/Nette/TracyExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan

Cannot access property $netteMailer on array|object.
$params = [];
$params['fromEmail'] = $this->config->fromEmail;

Check failure on line 120 in src/Bridges/Nette/TracyExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan

Cannot access property $fromEmail on array|object.
if (class_exists(Nette\Http\Request::class)) {
$params['host'] = new Statement('$this->getByType(?, false)\?->getUrl()->getHost()', [Nette\Http\Request::class]);
}
Expand All @@ -128,9 +128,9 @@
}

if ($this->debugMode) {
foreach ($this->config->bar as $item) {

Check failure on line 131 in src/Bridges/Nette/TracyExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan

Cannot access property $bar on array|object.
if (is_string($item) && substr($item, 0, 1) === '@') {
$item = new Statement(['@' . $builder::THIS_CONTAINER, 'getService'], [substr($item, 1)]);
$item = new Statement(['@' . $builder::ThisContainer, 'getService'], [substr($item, 1)]);
} elseif (is_string($item)) {
$item = new Statement($item);
}
Expand All @@ -151,7 +151,7 @@
}
}

foreach ($this->config->blueScreen as $item) {

Check failure on line 154 in src/Bridges/Nette/TracyExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan

Cannot access property $blueScreen on array|object.
$initialize->addBody($builder->formatPhp(
'$this->getService(?)->addPanel(?);',
Nette\DI\Helpers::filterArguments([$this->prefix('blueScreen'), $item]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if (empty($data)) {
}
?>
<style class="tracy-debug">
#tracy-debug .tracy-ErrorTab {
#tracy-debug .tracy-WarningsTab {
display: block;
background: #D51616;
color: white;
Expand All @@ -19,7 +19,7 @@ if (empty($data)) {
padding: 1px .4em;
}
</style>
<span class="tracy-ErrorTab">
<span class="tracy-WarningsTab">
<svg viewBox="0 0 2048 2048"><path fill="#fff" d="M1152 1503v-190q0-14-9.5-23.5t-22.5-9.5h-192q-13 0-22.5 9.5t-9.5 23.5v190q0 14 9.5 23.5t22.5 9.5h192q13 0 22.5-9.5t9.5-23.5zm-2-374l18-459q0-12-10-19-13-11-24-11h-220q-11 0-24 11-10 7-10 21l17 457q0 10 10 16.5t24 6.5h185q14 0 23.5-6.5t10.5-16.5zm-14-934l768 1408q35 63-2 126-17 29-46.5 46t-63.5 17h-1536q-34 0-63.5-17t-46.5-46q-37-63-2-126l768-1408q17-31 47-49t65-18 65 18 47 49z"/>
</svg><span class="tracy-label"><?= $sum = array_sum($data), $sum > 1 ? ' warnings' : ' warning' ?></span>
</span>
4 changes: 2 additions & 2 deletions src/Tracy/Debugger/Debugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
class Debugger
{
public const Version = '2.10.8';
public const Version = '3.0-dev';

/** server modes for Debugger::enable() */
public const
Expand Down Expand Up @@ -408,7 +408,7 @@ public static function getBar(): Bar
self::$bar = new Bar;
self::$bar->addPanel($info = new DefaultBarPanel('info'), 'Tracy:info');
$info->cpuUsage = self::$cpuUsage;
self::$bar->addPanel(new DefaultBarPanel('errors'), 'Tracy:errors'); // filled by errorHandler()
self::$bar->addPanel(new DefaultBarPanel('warnings'), 'Tracy:warnings'); // filled by errorHandler()
}

return self::$bar;
Expand Down
2 changes: 1 addition & 1 deletion src/Tracy/Debugger/DevelopmentStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function handleError(
}

$message = Helpers::errorTypeToString($severity) . ': ' . Helpers::improveError($message);
$count = &$this->bar->getPanel('Tracy:errors')->data["$file|$line|$message"];
$count = &$this->bar->getPanel('Tracy:warnings')->data["$file|$line|$message"];

if (!$count++ && !Helpers::isHtmlMode() && !Helpers::isAjax()) {
echo "\n$message in $file on line $line\n";
Expand Down
2 changes: 1 addition & 1 deletion src/Tracy/Debugger/ProductionStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function handleError(
$err = 'PHP ' . Helpers::errorTypeToString($severity) . ': ' . Helpers::improveError($message) . " in $file:$line";
}

Debugger::tryLog($err, Debugger::ERROR);
Debugger::tryLog($err, Debugger::WARNING);
}


Expand Down
39 changes: 14 additions & 25 deletions src/Tracy/Logger/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,22 @@
*/
class Logger implements ILogger
{
/** @var string|null name of the directory where errors should be logged */
public $directory;
/** name of the directory where errors should be logged */
public ?string $directory = null;

/** @var string|array|null email or emails to which send error notifications */
public $email;
/** email or emails to which send error notifications */
public string|array|null $email = null;

/** @var string|null sender of email notifications */
public $fromEmail;
/** sender of email notifications */
public ?string $fromEmail = null;

/** @var mixed interval for sending email is 2 days */
public $emailSnooze = '2 days';
/** interval for sending email is 2 days */
public mixed $emailSnooze = '2 days';

/** @var callable handler for sending emails */
public $mailer;

/** @var BlueScreen|null */
private $blueScreen;
private ?BlueScreen $blueScreen = null;


public function __construct(?string $directory, string|array|null $email = null, ?BlueScreen $blueScreen = null)
Expand Down Expand Up @@ -78,10 +77,7 @@ public function log(mixed $message, string $level = self::INFO)
}


/**
* @param mixed $message
*/
public static function formatMessage($message): string
public static function formatMessage(mixed $message): string
{
if ($message instanceof \Throwable) {
foreach (Helpers::getExceptionChain($message) as $exception) {
Expand All @@ -101,10 +97,7 @@ public static function formatMessage($message): string
}


/**
* @param mixed $message
*/
public static function formatLogLine($message, ?string $exceptionFile = null): string
public static function formatLogLine(mixed $message, ?string $exceptionFile = null): string
{
return implode(' ', [
date('[Y-m-d H-i-s]'),
Expand All @@ -127,7 +120,7 @@ public function getExceptionFile(\Throwable $exception, string $level = self::EX
];
}

$hash = substr(md5(serialize($data)), 0, 10);
$hash = substr(hash('xxh128', serialize($data)), 0, 10);
$dir = strtr($this->directory . '/', '\\/', DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR);
foreach (new \DirectoryIterator($this->directory) as $file) {
if (strpos($file->getBasename(), $hash)) {
Expand All @@ -152,10 +145,7 @@ protected function logException(\Throwable $exception, ?string $file = null): st
}


/**
* @param mixed $message
*/
protected function sendEmail($message): void
protected function sendEmail(mixed $message): void
{
$snooze = is_numeric($this->emailSnooze)
? $this->emailSnooze
Expand All @@ -174,10 +164,9 @@ protected function sendEmail($message): void

/**
* Default mailer.
* @param mixed $message
* @internal
*/
public function defaultMailer($message, string $email): void
public function defaultMailer(mixed $message, string $email): void
{
$host = preg_replace('#[^\w.-]+#', '', $_SERVER['SERVER_NAME'] ?? php_uname('n'));
$parts = str_replace(
Expand Down
2 changes: 1 addition & 1 deletion src/Tracy/Session/FileSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class FileSession implements SessionStorage
public string $cookieName = 'tracy-session';

/** probability that the clean() routine is started */
public float $gcProbability = 0.001;
public float $gcProbability = 0.01;
private string $dir;

/** @var resource */
Expand Down
Loading