Skip to content

Commit

Permalink
session usage encapsulated in the SessionHandler & NativeSession classes
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 10, 2021
1 parent b1c405e commit 3b0b22e
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 15 deletions.
31 changes: 17 additions & 14 deletions src/Tracy/Debugger/AjaxHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,26 @@
*/
final class AjaxHandler
{
/** @var SessionHandler */
private $sessionHandler;

/** @var string */
private $id;

/** @var bool */
private $useSession = false;


public function __construct()
public function __construct(SessionHandler $sessionHandler)
{
$this->sessionHandler = $sessionHandler;
$this->id = $_SERVER['HTTP_X_TRACY_AJAX'] ?? Helpers::createId();
}


public function isAvailable(): bool
{
return $this->useSession && session_status() === PHP_SESSION_ACTIVE;
return $this->useSession && $this->sessionHandler->isAvailable();
}


Expand All @@ -42,14 +46,14 @@ public function getId(): string

public function &getItems(string $key)
{
return $_SESSION['_tracy'][$key];
return $this->sessionHandler->getData()[$key];
}


public function addCalling(string $method, $argument)
{
$argument = json_encode($argument, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE);
$item = &$_SESSION['_tracy']['calling'][$this->id];
$item = &$this->sessionHandler->getData()['calling'][$this->id];
$item = ['code' => ($item['code'] ?? '') . "$method($argument);\n", 'time' => time()];
}

Expand All @@ -66,7 +70,7 @@ public function sendAssets(): bool
return true;
}

$this->useSession = session_status() === PHP_SESSION_ACTIVE;
$this->useSession = $this->sessionHandler->isAvailable();
if (!$this->useSession) {
return false;
}
Expand All @@ -81,8 +85,9 @@ public function sendAssets(): bool
if (!$ajax) {
$this->sendJsCss();
}
echo $this->getItems('calling')[$id]['code'] ?? '';
unset($_SESSION['_tracy']['calling'][$id]);
$data = &$this->sessionHandler->getData()['calling'];
echo $data[$id]['code'] ?? '';
unset($data[$id]);
return true;
}

Expand Down Expand Up @@ -130,13 +135,11 @@ private function sendJsCss(): void

public function clean(): void
{
if (isset($_SESSION['_tracy'])) {
foreach ($_SESSION['_tracy'] as &$items) {
$items = array_slice((array) $items, -10, null, true);
$items = array_filter($items, function ($item) {
return isset($item['time']) && $item['time'] > time() - 60;
});
}
foreach ($this->sessionHandler->getData() as &$items) {
$items = array_slice((array) $items, -10, null, true);
$items = array_filter($items, function ($item) {
return isset($item['time']) && $item['time'] > time() - 60;
});
}
}
}
26 changes: 25 additions & 1 deletion src/Tracy/Debugger/Debugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ class Debugger
/** @var array{DevelopmentStrategy, ProductionStrategy} */
private static $strategy;

/** @var SessionHandler */
private static $sessionHandler;


/**
* Static class - cannot be instantiated.
Expand Down Expand Up @@ -226,6 +229,8 @@ public static function enable($mode = null, string $logDirectory = null, $email
'Dumper/Value',
'Logger/FireLogger',
'Logger/Logger',
'Session/SessionHandler',
'Session/NativeSession',
'Helpers',
] as $path) {
require_once dirname(__DIR__) . "/$path.php";
Expand Down Expand Up @@ -450,12 +455,31 @@ public static function getStrategy()
if (empty(self::$strategy[self::$productionMode])) {
self::$strategy[self::$productionMode] = self::$productionMode
? new ProductionStrategy
: new DevelopmentStrategy(self::getBar(), self::getBlueScreen(), new AjaxHandler);
: new DevelopmentStrategy(self::getBar(), self::getBlueScreen(), new AjaxHandler(self::getSessionHandler()));
}
return self::$strategy[self::$productionMode];
}


public static function setSessionHandler(SessionHandler $storage): void
{
if (self::$sessionHandler) {
throw new \Exception('Storage is already set.');
}
self::$sessionHandler = $storage;
}


/** @internal */
public static function getSessionHandler(): SessionHandler
{
if (!self::$sessionHandler) {
self::$sessionHandler = new NativeSession;
}
return self::$sessionHandler;
}


/********************* useful tools ****************d*g**/


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

/**
* This file is part of the Tracy (https://tracy.nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Tracy;


class NativeSession implements SessionHandler
{
public function isAvailable(): bool
{
return session_status() === PHP_SESSION_ACTIVE;
}


public function &getData(): array
{
settype($_SESSION['_tracy'], 'array');
return $_SESSION['_tracy'];
}
}
18 changes: 18 additions & 0 deletions src/Tracy/Session/SessionHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/**
* This file is part of the Tracy (https://tracy.nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Tracy;


interface SessionHandler
{
function isAvailable(): bool;

function &getData(): array;
}
2 changes: 2 additions & 0 deletions src/tracy.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@
require __DIR__ . '/Tracy/Debugger/DevelopmentStrategy.php';
require __DIR__ . '/Tracy/Debugger/ProductionStrategy.php';
require __DIR__ . '/Tracy/OutputDebugger/OutputDebugger.php';
require __DIR__ . '/Tracy/Session/SessionHandler.php';
require __DIR__ . '/Tracy/Session/NativeSession.php';
require __DIR__ . '/Tracy/Helpers.php';
require __DIR__ . '/Tracy/functions.php';

0 comments on commit 3b0b22e

Please sign in to comment.