Skip to content

Commit

Permalink
session usage encapsulated in the SessionStorage & NativeSession classes
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 20, 2021
1 parent 7552d0d commit d7c79e5
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 12 deletions.
28 changes: 27 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 SessionStorage */
private static $sessionStorage;


/**
* Static class - cannot be instantiated.
Expand Down Expand Up @@ -231,6 +234,8 @@ public static function enable($mode = null, ?string $logDirectory = null, $email
'Dumper/Value',
'Logger/FireLogger',
'Logger/Logger',
'Session/SessionStorage',
'Session/NativeSession',
'Helpers',
] as $path) {
require_once dirname(__DIR__) . "/$path.php";
Expand Down Expand Up @@ -456,13 +461,34 @@ 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 DeferredContent);
: new DevelopmentStrategy(self::getBar(), self::getBlueScreen(), new DeferredContent(self::getSessionStorage()));
}

return self::$strategy[self::$productionMode];
}


public static function setSessionStorage(SessionStorage $storage): void
{
if (self::$sessionStorage) {
throw new \Exception('Storage is already set.');
}

self::$sessionStorage = $storage;
}


/** @internal */
public static function getSessionStorage(): SessionStorage
{
if (!self::$sessionStorage) {
self::$sessionStorage = new NativeSession;
}

return self::$sessionStorage;
}


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


Expand Down
24 changes: 13 additions & 11 deletions src/Tracy/Debugger/DeferredContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,26 @@
*/
final class DeferredContent
{
/** @var SessionStorage */
private $sessionStorage;

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

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


public function __construct()
public function __construct(SessionStorage $sessionStorage)
{
$this->sessionStorage = $sessionStorage;
$this->requestId = $_SERVER['HTTP_X_TRACY_AJAX'] ?? Helpers::createId();
}


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


Expand All @@ -42,7 +46,7 @@ public function getRequestId(): string

public function &getItems(string $key): array
{
$items = &$_SESSION['_tracy'][$key];
$items = &$this->sessionStorage->getData()[$key];
$items = (array) $items;
return $items;
}
Expand Down Expand Up @@ -76,7 +80,7 @@ public function sendAssets(): bool
return true;
}

$this->useSession = session_status() === PHP_SESSION_ACTIVE;
$this->useSession = $this->sessionStorage->isAvailable();
if (!$this->useSession) {
return false;
}
Expand Down Expand Up @@ -142,13 +146,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->sessionStorage->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: 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 SessionStorage
{
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/SessionStorage.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 SessionStorage
{
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/SessionStorage.php';
require __DIR__ . '/Tracy/Session/NativeSession.php';
require __DIR__ . '/Tracy/Helpers.php';
require __DIR__ . '/Tracy/functions.php';

0 comments on commit d7c79e5

Please sign in to comment.