Page MenuHomeDevCentral

D1614.id4122.diff
No OneTemporary

D1614.id4122.diff

diff --git a/src/Debug/Debugger.php b/src/Debug/Debugger.php
new file mode 100644
--- /dev/null
+++ b/src/Debug/Debugger.php
@@ -0,0 +1,32 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\OmniTools\Debug;
+
+class Debugger {
+
+ /**
+ * Prints human-readable information about a variable, wrapped in a <pre> block
+ *
+ * @param mixed $variable the variable to dump
+ */
+ public static function printVariable ($variable) : void {
+ echo "<pre class='debug'>";
+ print_r($variable);
+ echo "</pre>";
+ }
+
+ public static function printVariableAndDie ($variable) : void {
+ static::printVariable($variable);
+ die;
+ }
+
+ ///
+ /// Comfort debug helper to register debug method in global space
+ ///
+
+ public static function register () : void {
+ require_once '_register_to_global_space.php';
+ }
+
+}
diff --git a/src/Debug/_register_to_global_space.php b/src/Debug/_register_to_global_space.php
new file mode 100644
--- /dev/null
+++ b/src/Debug/_register_to_global_space.php
@@ -0,0 +1,17 @@
+<?php
+
+/* This code is intentionnally left in the global namespace. */
+
+use Keruald\OmniTools\Debug\Debugger;
+
+if (!function_exists("dprint_r")) {
+ function dprint_r ($variable) {
+ Debugger::printVariable($variable);
+ }
+}
+
+if (!function_exists("dieprint_r")) {
+ function dieprint_r ($variable) {
+ Debugger::printVariableAndDie($variable);
+ }
+}
diff --git a/tests/Debug/DebuggerTest.php b/tests/Debug/DebuggerTest.php
new file mode 100644
--- /dev/null
+++ b/tests/Debug/DebuggerTest.php
@@ -0,0 +1,27 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\OmniTools\Tests\Debug;
+
+
+use Keruald\OmniTools\Debug\Debugger;
+use PHPUnit\Framework\TestCase;
+
+class DebuggerTest extends TestCase {
+
+ public function testRegister () {
+ $this->assertTestSuiteStateIsValid();
+
+ Debugger::register();
+
+ $this->assertTrue(function_exists("dprint_r"));
+ }
+
+ private function assertTestSuiteStateIsValid() : void {
+ $this->assertFalse(
+ function_exists("dprint_r"),
+ "Configure the test suite so dprint_r isn't in global space first."
+ );
+ }
+
+}

File Metadata

Mime Type
text/plain
Expires
Wed, Jun 18, 13:37 (15 h, 8 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2744193
Default Alt Text
D1614.id4122.diff (2 KB)

Event Timeline