Skip to content

Commit

Permalink
Remove Redis adapter; Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksagona committed Oct 31, 2023
1 parent 5ff756b commit 4a2b1a5
Show file tree
Hide file tree
Showing 12 changed files with 314 additions and 323 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,10 @@ jobs:
#- name: Validate composer.json and composer.lock
# run: composer validate

- name: Start Redis
uses: supercharge/[email protected]
with:
redis-version: ${{ matrix.redis-version }}

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: redis
ini-values: memory_limit=128M

- name: Cache Composer packages
Expand Down
78 changes: 61 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ pop-debug
* [Storage](#storage)
- [File](#file)
- [Database](#database)
- [Redis](#redis)
* [Formats](#formats)
* [Retrieving](#retrieving)

Overview
--------
Expand Down Expand Up @@ -398,22 +398,6 @@ $debugger->setStorage(new Database($db, 'text', 'my_debug_table'));

[Top](#pop-debug)

### Redis

Store the debugger output into the Redis server cache.

```php
use Pop\Debug\Debugger;
use Pop\Debug\Handler\TimeHandler;
use Pop\Debug\Storage\Redis;

$debugger = new Debugger();
$debugger->addHandler(new TimeHandler());
$debugger->setStorage(new Redis());
```

[Top](#pop-debug)

Formats
-------

Expand Down Expand Up @@ -449,5 +433,65 @@ $fileStorage->setFormat('JSON');
$fileStorage->setFormat('PHP');
```

[Top](#pop-debug)

Retrieving
----------

You can retrieve the stored debug content from the debugger. Calling the `save()` method returns the
request ID generated from that method call.

```php
use Pop\Debug\Debugger;
use Pop\Debug\Handler\MessageHandler;
use Pop\Debug\Storage\File;

$debugger = new Debugger(new MessageHandler(), new MemoryHandler(), new File(__DIR__ . '/logs'));
$debugger['message']->addMessage('Hey! Something happened!');
$requestId = $debugger->save();
```

The auto-generated request ID will look like:

```text
857f0869d00b64db7c9dbdee4194781a
```

From there, you can call `getById` to retrieve stored debug content:

```php
// A wildcard search
print_r($debugger->getById($requestId . '*'));
```

```text
Array
(
[0] => 857f0869d00b64db7c9dbdee4194781a-message.log
)
```
```php
// An exact search by ID
print_r($debugger->getById('857f0869d00b64db7c9dbdee4194781a-message'));
```
```text
1698773755.86070 Hey! Something happened!
```

The method `getByType` is also available to get groups of debug content by type:

```php
print_r($debugger->getByType('message'));
```

```text
Array
(
[0] => 857f0869d00b64db7c9dbdee4194781a-message.log
[1] => 966dc22f1c34489d7d61de295aa008a9-message.log
[2] => f5c21a372ba375bce9b2382f67e3b70d-message.log
)
```

[Top](#pop-debug)
64 changes: 60 additions & 4 deletions src/Debugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,22 +183,78 @@ public function getData(): array
{
$data = [];
foreach ($this->handlers as $name => $handler) {
$data[$name] = ($this->storage->getFormat() === null) ? $handler->prepareAsString() : $handler->prepare();
$data[$name] = ($this->storage->getFormat() == 'TEXT') ? $handler->prepareAsString() : $handler->prepare();
}
return $data;
}

/**
* Save the debug handlers' data to storage
* Get stored request by ID
*
* @param string $id
* @return mixed
*/
public function getById(string $id): mixed
{
return $this->storage->getById($id);
}

/**
* Get stored request by type
*
* @param string $type
* @return mixed
*/
public function getByType(string $type): mixed
{
return $this->storage->getByType($type);
}

/**
* Determine if debug data exists by ID
*
* @param string $id
* @return bool
*/
public function has(string $id): bool
{
return $this->storage->has($id);
}

/**
* Delete debug data by ID
*
* @param string $id
* @return void
*/
public function delete(string $id): void
{
$this->storage->delete($id);
}

/**
* Clear storage
*
* @return void
*/
public function save(): void
public function clear(): void
{
$this->storage->clear();
}

/**
* Save the debug handlers' data to storage
*
* @return string
*/
public function save(): string
{
foreach ($this->handlers as $name => $handler) {
$data = ($this->storage->getFormat() === null) ? $handler->prepareAsString() : $handler->prepare();
$data = ($this->storage->getFormat() == 'TEXT') ? $handler->prepareAsString() : $handler->prepare();
$this->storage->save($this->getRequestId() . '-' . $name, $data);
}

return $this->getRequestId();
}

/**
Expand Down
44 changes: 30 additions & 14 deletions src/Storage/AbstractStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ abstract class AbstractStorage implements StorageInterface

/**
* Storage format (json, php or text)
* @var ?string
* @var string
*/
protected ?string $format = null;
protected string $format = 'TEXT';

/**
* Constructor
Expand All @@ -46,7 +46,7 @@ abstract class AbstractStorage implements StorageInterface
*
* @param ?string $format
*/
public function __construct(?string $format = null)
public function __construct(?string $format = self::TEXT)
{
if ($format !== null) {
$this->setFormat($format);
Expand All @@ -61,17 +61,25 @@ public function __construct(?string $format = null)
*/
public function setFormat(string $format): AbstractStorage
{
switch (strtoupper($format)) {
case self::JSON:
$this->format = self::JSON;
break;
case self::PHP:
$this->format = self::PHP;
}
$this->format = match (strtoupper($format)) {
self::JSON => self::JSON,
self::PHP => self::PHP,
default => self::TEXT,
};

return $this;
}

/**
* Determine if the format is PHP
*
* @return bool
*/
public function isText(): bool
{
return ($this->format == self::TEXT);
}

/**
* Determine if the format is PHP
*
Expand Down Expand Up @@ -112,23 +120,31 @@ public function getFormat(): ?string
abstract public function save(string $id, mixed $value): void;

/**
* Get debug data
* Get debug data by ID
*
* @param string $id
* @return mixed
*/
abstract public function get(string $id): mixed;
abstract public function getById(string $id): mixed;

/**
* Get debug data by type
*
* @param string $type
* @return mixed
*/
abstract public function getByType(string $type): mixed;

/**
* Determine if debug data exists
* Determine if debug data exists by
*
* @param string $id
* @return bool
*/
abstract public function has(string $id): bool;

/**
* Delete debug data
* Delete debug data by id
*
* @param string $id
* @return void
Expand Down
54 changes: 48 additions & 6 deletions src/Storage/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,24 +183,31 @@ public function save(string $id, mixed $value): void
}

/**
* Get debug data
* Get debug data by ID
*
* @param string $id
* @return mixed
*/
public function get(string $id): mixed
public function getById(string $id): mixed
{
$sql = $this->db->createSql();
$placeholder = $sql->getPlaceholder();
$value = false;
$isWildcard = false;

if ($placeholder == ':') {
$placeholder .= 'key';
} else if ($placeholder == '$') {
$placeholder .= '1';
}

$sql->select()->from($this->table)->where('key = ' . $placeholder);
if (str_ends_with($id, '*') || str_ends_with($id, '%')) {
$sql->select()->from($this->table)->where('key LIKE ' . $placeholder);
$id = substr($id, 0, -1) . '%';
$isWildcard = true;
} else {
$sql->select()->from($this->table)->where('key = ' . $placeholder);
}

$this->db->prepare($sql)
->bindParams(['key' => $id])
Expand All @@ -209,15 +216,50 @@ public function get(string $id): mixed
$rows = $this->db->fetchAll();

// If the value is found, return.
if (isset($rows[0]) && isset($rows[0]['value'])) {
if (($isWildcard) && isset($rows[0])) {
$value = $rows;
} else if (isset($rows[0]) && isset($rows[0]['value'])) {
$value = $this->decodeValue($rows[0]['value']);
}

return $value;
}

/**
* Determine if debug data exists
* Get debug data by type
*
* @param string $type
* @return mixed
*/
public function getByType(string $type): mixed
{
$sql = $this->db->createSql();
$placeholder = $sql->getPlaceholder();
$value = false;

if ($placeholder == ':') {
$placeholder .= 'key';
} else if ($placeholder == '$') {
$placeholder .= '1';
}

$sql->select()->from($this->table)->where('key LIKE ' . $placeholder);
$this->db->prepare($sql)
->bindParams(['key' => '%' . $type])
->execute();

$rows = $this->db->fetchAll();

// If the value is found, return.
if (isset($rows[0])) {
$value = $rows;
}

return $value;
}

/**
* Determine if debug data exists by ID
*
* @param string $id
* @return bool
Expand Down Expand Up @@ -245,7 +287,7 @@ public function has(string $id): bool
}

/**
* Delete debug data
* Delete debug data by ID
*
* @param string $id
* @return void
Expand Down
Loading

0 comments on commit 4a2b1a5

Please sign in to comment.