Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
arashjafari committed Nov 20, 2021
0 parents commit ec584ff
Show file tree
Hide file tree
Showing 13 changed files with 804 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.lock
phpunit.xml
vendor/
104 changes: 104 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Dump Plus

With DumpPlus, you can view the content of any variable or object in many different formats:

* syntax highlighting
* Browsers console log output
* Via Javascript alert() function
* Default var_dump() output
* Command-line interface output
* Simple format
* JSON format

## Installation
Use Composer to install the library:
```
$ composer require arashjafari/dump-plus
```

## Screenshots

Screenshot of DumpPlus::pretty($colors);
![pretty](./examples/pretty.png "Screenshot of DumpPlus::pretty")

Screenshot of DumpPlus::console($colors);
![console](./examples/console.png "Screenshot of DumpPlus::console")

Screenshot of DumpPlus::alert($colors);
![alert](./examples/alert.png "Screenshot of DumpPlus::alert")


## Example

```php

use ArashJafari\DumpPlus\DumpPlus;

// Sample array to dump
$colors = ['key1' => 'red', 'key2' => 'blue'];

// Default dump output
DumpPlus::dump($colors);

// Dump with syntax highlighting
DumpPlus::pretty($colors);

// Dump in simple format
DumpPlus::simple($colors);

// Dump for CLI output
DumpPlus::cli($colors);

// Dump in browser console
DumpPlus::console($colors);

// Dump in Javascript alert!
DumpPlus::alert($colors);

// Dump in JSON format
DumpPlus::json($colors);

```

Calling with multiple arguments:

```php
use ArashJafari\DumpPlus\DumpPlus;

$msg = "Hello World!";
$colors = ['key1' => 'red', 'key2' => 'blue'];

DumpPlus::dump($msg, $colors);
```

Calling with functions instead of DumpPlus methods:

```php

$colors = ['key1' => 'red', 'key2' => 'blue'];

dp($colors); // DumpPlus::dump($colors);
dpd($colors); // DumpPlus::dump($colors); die();

dp_pretty($colors); // DumpPlus::pretty($colors);
dpd_pretty($colors); // DumpPlus::pretty($colors); die();

dp_simple($colors); // DumpPlus::simple($colors);
dpd_simple($colors); // DumpPlus::simple($colors); die();

dp_cli($colors); // DumpPlus::cli($colors);
dpd_cli($colors); // DumpPlus::cli($colors); die();

dp_console($colors); // DumpPlus::console($colors);
dpd_console($colors); // DumpPlus::console($colors); die();

dp_alert($colors); // DumpPlus::alert($colors);
dpd_alert($colors); // DumpPlus::alert($colors); die();

dp_json($colors); // DumpPlus::json($colors);
dpd_json($colors); // DumpPlus::json($colors); die();
```

## License

The DumpPlus is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).
25 changes: 25 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "arashjafari/dump-plus",
"description": "Provides variety of formats for dumps information about variables or objects",
"keywords": ["dump_plus", "dump-plus", "print_r", "var_dump", "debug", "better", "pretty", "print", "dump", "cli", "console.log", "alert"],
"homepage": "https://github.com/arashjafari/dump-plus",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Arash Jafari",
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"require": {
"php": ">=5.4.0"
},
"autoload": {
"files": [ "./src/functions.php" ],
"psr-4":{
"ArashJafari\\DumpPlus\\": "./src/"

}
}
}
Binary file added examples/alert.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/console.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/pretty.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
141 changes: 141 additions & 0 deletions src/DumpPlus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

namespace ArashJafari\DumpPlus;

class DumpPlus
{
/**
* Dumps information about a variable.
*
* @param mixed $value The variable you want to export.
* @param mixed ...$values [optional]
* @return void
*/
public static function dump(mixed $value, ...$values): void
{
Output::default(
Helpers::groupTitle($value, debug_backtrace()),
Helpers::varDumpContent($value)
);

foreach ($values as $val) {
static::dump($val);
}
}

/**
* Dumps information about a variable in a simple format.
*
* @param mixed $value The variable you want to export.
* @param mixed ...$values [optional]
* @return void
*/
public static function simple(mixed $value, ...$values): void
{
Output::default(
Helpers::groupTitle($value, debug_backtrace()),
Helpers::printRContent($value)
);

foreach ($values as $val) {
static::simple($val);
}
}

/**
* Dumps information about a variable in a highlighted format.
*
* @param mixed $value The variable you want to export.
* @param mixed ...$values [optional]
* @return void
*/
public static function pretty(mixed $value, ...$values): void
{
Output::pretty(
Helpers::groupTitle($value, debug_backtrace()),
Helpers::varDumpContent($value)
);

foreach ($values as $val) {
static::pretty($val);
}
}

/**
* Dumps information about a variable in CLI.
*
* @param mixed $value The variable you want to export.
* @param mixed ...$values [optional]
* @return void
*/
public static function cli(mixed $value, ...$values): void
{
Output::cli(
Helpers::groupTitle($value, debug_backtrace()),
Helpers::varDumpContent($value)
);

foreach ($values as $val) {
static::cli($val);
}
}

/**
* Dumps information about a variable in a javascript alert.
*
* @param mixed $value The variable you want to export.
* @param mixed ...$values [optional]
* @return void
*/
public static function alert(mixed $value, ...$values): void
{
Output::alert(
Helpers::groupTitle($value, debug_backtrace(), true),
Helpers::printRContent($value)
);

foreach ($values as $val) {
static::alert($val);
}
}

/**
* Dumps information about a variable in JSON format.
*
* @param mixed $value The variable you want to export.
* @param mixed ...$values [optional]
* @return void
*/
public static function json(mixed $value, ...$values): void
{
header("Content-Type: application/json; charset=UTF-8");

Output::json(
Helpers::groupTitle($value, debug_backtrace()),
Helpers::printRContent($value)
);

foreach ($values as $val) {
static::json($val);
}
}

/**
* Dumps information about a variable in browser console.
*
* @param mixed $value The variable you want to export.
* @param mixed ...$values [optional]
* @return void
*/
public static function console(mixed $value, ...$values): void
{
Output::console(
Helpers::groupTitle($value, debug_backtrace(), true),
Helpers::printRContent($value)
);

foreach ($values as $val) {
static::console($val);
}
}
}
29 changes: 29 additions & 0 deletions src/FileInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace ArashJafari\DumpPlus;

class FileInfo
{
/**
* Get file path and line number of the caller.
*
* @param array $backtrace
* @return string
*/
public static function getFileAndLine(array $backtrace, bool $shortTitle): string
{
$return = '';

if (isset($backtrace[0]['file'])) {
$return .= $shortTitle
? basename($backtrace[0]['file'])
: $backtrace[0]['file'];
}

if (isset($backtrace[0]['line'])) {
$return .= ':' . $backtrace[0]['line'];
}

return $return;
}
}
Loading

0 comments on commit ec584ff

Please sign in to comment.