Skip to content

Commit

Permalink
Merge pull request #7 from myaaghubi/dev
Browse files Browse the repository at this point in the history
1.6
  • Loading branch information
myaaghubi authored Sep 29, 2024
2 parents 9040172 + 5b58869 commit 3afbbc1
Show file tree
Hide file tree
Showing 19 changed files with 372 additions and 89 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Debench
[![Test Debench](https://github.com/myaaghubi/Debench/actions/workflows/ci.yml/badge.svg)](https://github.com/myaaghubi/Debench/actions/workflows/ci.yml) [![Debench Coverage Status](https://coveralls.io/repos/github/myaaghubi/Debench/badge.svg?branch=main)](https://coveralls.io/github/myaaghubi/Debench?branch=main) ![Debench release (latest by date)](https://img.shields.io/github/v/release/myaaghubi/Debench) ![Debench License](https://img.shields.io/github/license/myaaghubi/Debench)

A small debug/benchmark helper for PHP
A small and lightweight debug/benchmark helper for PHP.

![myaaghubi/debench-debench-minimal](screenshot/screenshot-minimal.png)
![myaaghubi/debench-debench-fullsize](screenshot/screenshot-fullsize.png)
Expand All @@ -18,26 +18,26 @@ namespace DEBENCH;

require __DIR__ . '/vendor/autoload.php';

Debench::info('let\'s have the debench');

// call it from your index.php after autoload
// then check the webpage with your browser
// $debench = new Debench(true, 'theme');
Debench::getInstance(true, 'theme');

// for enable() or minimalOnly() you can
// call them even before getInstance
Debench::enable(false);

// for dump(), info(), warning() and error() you can
// call them before getInstance too
Debench::info('let\'s use some memory');
$st = str_repeat("Debench!", 10000);

Debench::info('step one');
// step one
// $debench->newPoint("one");
$st = str_repeat("Debench!", 10000);
Debench::point('one');

$st .= str_repeat("Debench!", 10000);

Debench::info('step two');
// step two
Debench::point("two");
// $debench->newPoint("two");
Debench::point('two');
```
For `minimal` mode:
```php
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
]
},
"require-dev": {
"phpunit/phpunit": "^9.0"
"phpunit/phpunit": "^10.0"
},
"config": {
"platform": {
"php": "8.0"
"php": "8.1"
}
}
}
93 changes: 83 additions & 10 deletions lib/Debench/Debench.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,22 +524,82 @@ private function checkTag(string $tag): bool


/**
* Add a message as an info
* Add an info
*
* @param string $message
* @return void
*/
public static function info(string $message = ''): void
public static function info(string $message): void
{
if (!isset(self::$messages)) {
self::$messages = [];
self::addMessage($message, MessageLevel::INFO);
}


/**
* Add a warning
*
* @param string $message
* @return void
*/
public static function warning(string $message): void
{
self::addMessage($message, MessageLevel::WARNING);
}


/**
* Add an error
*
* @param string $message
* @return void
*/
public static function error(string $message): void
{
self::addMessage($message, MessageLevel::ERROR);
}


/**
* Dump var/s
*
* @param mixed $var/s
* @return void
*/
public static function dump(...$args): void
{
$messages = [];

foreach (func_get_args() as $var) {
ob_start();
var_dump($var);
$dumped = ob_get_clean();
$messages[] = preg_replace("/\n*<small>.*?<\/small>/", "", $dumped, 1);
}

$messageString = implode('', $messages);

self::addMessage($messageString, MessageLevel::DUMP);
}


/**
* Add a message
*
* @param string $message
* @param MessageLevel $level
* @return void
*/
private static function addMessage(string $message, MessageLevel $level): void
{
$lastBT = Utils::getBacktrace()[0];
$path = $lastBT['file'];
$line = $lastBT['line'];

$messageObject = new Message($message, $path, $line);
$messageObject = new Message($message, $level, $path, $line);

if (empty(self::$messages)) {
self::$messages = [];
}

self::$messages[] = $messageObject;
}
Expand All @@ -556,6 +616,17 @@ public static function messages(): array
}


/**
* Clear messages
*
* @return void
*/
public static function clearMessages(): void
{
self::$messages = [];
}


/**
* Add an exception to exceptions array
*
Expand Down Expand Up @@ -675,24 +746,25 @@ private function makeOutput(): string


// ------- logMessages
$logMessage = '';
$logMessage = '<b>Nothing</b> Yet!';

if (!empty(self::messages())) {
$logMessage = '';
}

foreach (self::messages() as $message) {
$file = basename($message->getPath());
$path = str_replace($file, "<b>$file</b>", $message->getPath());

$logMessage .= Template::render(self::$pathUI . '/widget.log.message.htm', [
// "code" => $exception->getCode(),
"level" => strtoupper($message->getLevel()->name()),
"message" => $message->getMessage(),
"path" => $path,
"line" => $message->getLineNumber(),
]);
}

if (empty($logMessage)) {
$logMessage = '<b>Nothing</b> Yet!';
}


// ------- logException
$logException = '';
Expand Down Expand Up @@ -721,6 +793,7 @@ private function makeOutput(): string
'ramUsage' => $this->getRamUsage(true, true),
// 'includedFilesCount' => $this->getLoadedFilesCount(),
'preloadTime' => $this->initPointMS - $this->getRequestTime(),
'pointsCount' => count($this->getCheckPoints()),
'request' => count($_POST) + count($_GET) + count($_COOKIE),
'logPost' => $logPost,
'logGet' => $logGet,
Expand Down
30 changes: 29 additions & 1 deletion lib/Debench/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ class Message
{
/**
* Message Constructor
*
*
* @param string $message
* @param MessageLevel $level
* @param string $path
* @param int $lineNumber
* @return void
*/
public function __construct(
private string $message,
private MessageLevel $level,
private string $path,
private int $lineNumber
) {
Expand Down Expand Up @@ -50,6 +55,29 @@ public function setMessage(string $message): void
}


/**
* Get the level
*
* @return MessageLevel
*/
public function getLevel(): MessageLevel
{
return $this->level;
}


/**
* Set the level
*
* @param MessageLevel $level
* @return void
*/
public function setLevel(MessageLevel $level): void
{
$this->level = $level;
}


/**
* Get the path
*
Expand Down
31 changes: 31 additions & 0 deletions lib/Debench/MessageLevel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

/**
* @package Debench, MessageLevel
* @link http://github.com/myaaghubi/debench Github
* @author Mohammad Yaaghubi <[email protected]>
* @copyright Copyright (c) 2024, Mohammad Yaaghubi
* @license MIT License
*/

namespace DEBENCH;

enum MessageLevel
{
case INFO;
case WARNING;
case ERROR;
case DUMP;

public function name(): string
{
return match ($this) {
MessageLevel::INFO => 'Info',
MessageLevel::WARNING => 'Warning',
MessageLevel::ERROR => 'Error',
MessageLevel::DUMP => 'Dump',
};
}
}
8 changes: 2 additions & 6 deletions lib/Debench/Template.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@ public static function makeUI(string $targetPath): void
// for ui/assets
@mkdir($targetPath, 0777, true);

// for path
if (is_dir($targetPath)) {
// Copy the templates files from ui dir into your webroot dir if files don't match
Utils::copyDir(__DIR__ . '/ui', $targetPath);
}
// Copy the templates files from ui dir into your webroot dir if files don't match
Utils::copyDir(__DIR__ . '/ui', $targetPath);
}


Expand All @@ -52,7 +49,6 @@ public static function render(string $themePath, array $params): string
if (empty(self::$paths[$themePath])) {
if (!file_exists($themePath)) {
throw new \Exception("File '$themePath` doesn't exists!");
return '';
}
self::$paths[$themePath] = file_get_contents($themePath);
}
Expand Down
14 changes: 6 additions & 8 deletions lib/Debench/Utils.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,15 @@ public static function getBacktrace(): array
* @param bool $checkForError
* @return void
*/
public static function copyDir(string $from, string $to, bool $checkForError = true): void
public static function copyDir(string $from, string $to): void
{
if ($checkForError) {
if (!file_exists($from)) {
throw new \Exception("File/Dir source '$from` doesn't exists!");
}
}

// open the source directory
$dir = opendir($from);

if ($dir===false) {
return;
}

@mkdir($to);

// Loop through the files in source directory
Expand All @@ -63,7 +61,7 @@ public static function copyDir(string $from, string $to, bool $checkForError = t
$fileTo = $to . DIRECTORY_SEPARATOR . $file;
if (is_dir($fileFrom)) {
// for sub directory
Utils::copyDir($fileFrom, $fileTo, false);
Utils::copyDir($fileFrom, $fileTo);
} else {
if (!file_exists($fileTo) || filesize($fileFrom) != filesize($fileTo)) {
copy($fileFrom, $fileTo);
Expand Down
Loading

0 comments on commit 3afbbc1

Please sign in to comment.