Skip to content

Commit ec584ff

Browse files
committed
Initial commit
0 parents  commit ec584ff

File tree

13 files changed

+804
-0
lines changed

13 files changed

+804
-0
lines changed

.gitattributes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.gitattributes export-ignore
4+
/.gitignore export-ignore

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
composer.lock
2+
phpunit.xml
3+
vendor/

README.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Dump Plus
2+
3+
With DumpPlus, you can view the content of any variable or object in many different formats:
4+
5+
* syntax highlighting
6+
* Browsers console log output
7+
* Via Javascript alert() function
8+
* Default var_dump() output
9+
* Command-line interface output
10+
* Simple format
11+
* JSON format
12+
13+
## Installation
14+
Use Composer to install the library:
15+
```
16+
$ composer require arashjafari/dump-plus
17+
```
18+
19+
## Screenshots
20+
21+
Screenshot of DumpPlus::pretty($colors);
22+
![pretty](./examples/pretty.png "Screenshot of DumpPlus::pretty")
23+
24+
Screenshot of DumpPlus::console($colors);
25+
![console](./examples/console.png "Screenshot of DumpPlus::console")
26+
27+
Screenshot of DumpPlus::alert($colors);
28+
![alert](./examples/alert.png "Screenshot of DumpPlus::alert")
29+
30+
31+
## Example
32+
33+
```php
34+
35+
use ArashJafari\DumpPlus\DumpPlus;
36+
37+
// Sample array to dump
38+
$colors = ['key1' => 'red', 'key2' => 'blue'];
39+
40+
// Default dump output
41+
DumpPlus::dump($colors);
42+
43+
// Dump with syntax highlighting
44+
DumpPlus::pretty($colors);
45+
46+
// Dump in simple format
47+
DumpPlus::simple($colors);
48+
49+
// Dump for CLI output
50+
DumpPlus::cli($colors);
51+
52+
// Dump in browser console
53+
DumpPlus::console($colors);
54+
55+
// Dump in Javascript alert!
56+
DumpPlus::alert($colors);
57+
58+
// Dump in JSON format
59+
DumpPlus::json($colors);
60+
61+
```
62+
63+
Calling with multiple arguments:
64+
65+
```php
66+
use ArashJafari\DumpPlus\DumpPlus;
67+
68+
$msg = "Hello World!";
69+
$colors = ['key1' => 'red', 'key2' => 'blue'];
70+
71+
DumpPlus::dump($msg, $colors);
72+
```
73+
74+
Calling with functions instead of DumpPlus methods:
75+
76+
```php
77+
78+
$colors = ['key1' => 'red', 'key2' => 'blue'];
79+
80+
dp($colors); // DumpPlus::dump($colors);
81+
dpd($colors); // DumpPlus::dump($colors); die();
82+
83+
dp_pretty($colors); // DumpPlus::pretty($colors);
84+
dpd_pretty($colors); // DumpPlus::pretty($colors); die();
85+
86+
dp_simple($colors); // DumpPlus::simple($colors);
87+
dpd_simple($colors); // DumpPlus::simple($colors); die();
88+
89+
dp_cli($colors); // DumpPlus::cli($colors);
90+
dpd_cli($colors); // DumpPlus::cli($colors); die();
91+
92+
dp_console($colors); // DumpPlus::console($colors);
93+
dpd_console($colors); // DumpPlus::console($colors); die();
94+
95+
dp_alert($colors); // DumpPlus::alert($colors);
96+
dpd_alert($colors); // DumpPlus::alert($colors); die();
97+
98+
dp_json($colors); // DumpPlus::json($colors);
99+
dpd_json($colors); // DumpPlus::json($colors); die();
100+
```
101+
102+
## License
103+
104+
The DumpPlus is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

composer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "arashjafari/dump-plus",
3+
"description": "Provides variety of formats for dumps information about variables or objects",
4+
"keywords": ["dump_plus", "dump-plus", "print_r", "var_dump", "debug", "better", "pretty", "print", "dump", "cli", "console.log", "alert"],
5+
"homepage": "https://github.com/arashjafari/dump-plus",
6+
"type": "library",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Arash Jafari",
11+
"email": "arashrj@gmail.com"
12+
}
13+
],
14+
"minimum-stability": "dev",
15+
"require": {
16+
"php": ">=5.4.0"
17+
},
18+
"autoload": {
19+
"files": [ "./src/functions.php" ],
20+
"psr-4":{
21+
"ArashJafari\\DumpPlus\\": "./src/"
22+
23+
}
24+
}
25+
}

examples/alert.png

39.7 KB
Loading

examples/console.png

50.3 KB
Loading

examples/pretty.png

29.6 KB
Loading

src/DumpPlus.php

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
namespace ArashJafari\DumpPlus;
4+
5+
class DumpPlus
6+
{
7+
/**
8+
* Dumps information about a variable.
9+
*
10+
* @param mixed $value The variable you want to export.
11+
* @param mixed ...$values [optional]
12+
* @return void
13+
*/
14+
public static function dump(mixed $value, ...$values): void
15+
{
16+
Output::default(
17+
Helpers::groupTitle($value, debug_backtrace()),
18+
Helpers::varDumpContent($value)
19+
);
20+
21+
foreach ($values as $val) {
22+
static::dump($val);
23+
}
24+
}
25+
26+
/**
27+
* Dumps information about a variable in a simple format.
28+
*
29+
* @param mixed $value The variable you want to export.
30+
* @param mixed ...$values [optional]
31+
* @return void
32+
*/
33+
public static function simple(mixed $value, ...$values): void
34+
{
35+
Output::default(
36+
Helpers::groupTitle($value, debug_backtrace()),
37+
Helpers::printRContent($value)
38+
);
39+
40+
foreach ($values as $val) {
41+
static::simple($val);
42+
}
43+
}
44+
45+
/**
46+
* Dumps information about a variable in a highlighted format.
47+
*
48+
* @param mixed $value The variable you want to export.
49+
* @param mixed ...$values [optional]
50+
* @return void
51+
*/
52+
public static function pretty(mixed $value, ...$values): void
53+
{
54+
Output::pretty(
55+
Helpers::groupTitle($value, debug_backtrace()),
56+
Helpers::varDumpContent($value)
57+
);
58+
59+
foreach ($values as $val) {
60+
static::pretty($val);
61+
}
62+
}
63+
64+
/**
65+
* Dumps information about a variable in CLI.
66+
*
67+
* @param mixed $value The variable you want to export.
68+
* @param mixed ...$values [optional]
69+
* @return void
70+
*/
71+
public static function cli(mixed $value, ...$values): void
72+
{
73+
Output::cli(
74+
Helpers::groupTitle($value, debug_backtrace()),
75+
Helpers::varDumpContent($value)
76+
);
77+
78+
foreach ($values as $val) {
79+
static::cli($val);
80+
}
81+
}
82+
83+
/**
84+
* Dumps information about a variable in a javascript alert.
85+
*
86+
* @param mixed $value The variable you want to export.
87+
* @param mixed ...$values [optional]
88+
* @return void
89+
*/
90+
public static function alert(mixed $value, ...$values): void
91+
{
92+
Output::alert(
93+
Helpers::groupTitle($value, debug_backtrace(), true),
94+
Helpers::printRContent($value)
95+
);
96+
97+
foreach ($values as $val) {
98+
static::alert($val);
99+
}
100+
}
101+
102+
/**
103+
* Dumps information about a variable in JSON format.
104+
*
105+
* @param mixed $value The variable you want to export.
106+
* @param mixed ...$values [optional]
107+
* @return void
108+
*/
109+
public static function json(mixed $value, ...$values): void
110+
{
111+
header("Content-Type: application/json; charset=UTF-8");
112+
113+
Output::json(
114+
Helpers::groupTitle($value, debug_backtrace()),
115+
Helpers::printRContent($value)
116+
);
117+
118+
foreach ($values as $val) {
119+
static::json($val);
120+
}
121+
}
122+
123+
/**
124+
* Dumps information about a variable in browser console.
125+
*
126+
* @param mixed $value The variable you want to export.
127+
* @param mixed ...$values [optional]
128+
* @return void
129+
*/
130+
public static function console(mixed $value, ...$values): void
131+
{
132+
Output::console(
133+
Helpers::groupTitle($value, debug_backtrace(), true),
134+
Helpers::printRContent($value)
135+
);
136+
137+
foreach ($values as $val) {
138+
static::console($val);
139+
}
140+
}
141+
}

src/FileInfo.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace ArashJafari\DumpPlus;
4+
5+
class FileInfo
6+
{
7+
/**
8+
* Get file path and line number of the caller.
9+
*
10+
* @param array $backtrace
11+
* @return string
12+
*/
13+
public static function getFileAndLine(array $backtrace, bool $shortTitle): string
14+
{
15+
$return = '';
16+
17+
if (isset($backtrace[0]['file'])) {
18+
$return .= $shortTitle
19+
? basename($backtrace[0]['file'])
20+
: $backtrace[0]['file'];
21+
}
22+
23+
if (isset($backtrace[0]['line'])) {
24+
$return .= ':' . $backtrace[0]['line'];
25+
}
26+
27+
return $return;
28+
}
29+
}

0 commit comments

Comments
 (0)