Debugging hooks?

When I use hooks I see that something is not right because the panel loads forever. How can I debug it to see what goes wrong?

The code below is all I have in a plugin. It work outside the hook but not inside it. I need a way to figure out what’s going on when it’s inside the hook.

<?php
class test {
	public static $root;
	public static $folder;
	public static $page;

	public static function saveRevision($page) {
		self::$page = $page;
		self::createFolder();
	}

	public static function createFolder() {
		self::$root = kirby()->roots()->index();
		self::$folder = 'revisions';
		$modified = date( 'Y-m-d@H-i-s', self::$page->modified() );
		$uri = self::$page->id();

		$path = self::$root . DS . self::$folder . DS . $uri . DS . '.revision' . DS . $modified;
		
		$has_dir = self::makeDir($path);
		#echo $has_dir;
	}

	public static function makeDir($path) {
		return mkdir($path, 0777, true);
	}
}
#$page = page();
#test::saveRevision($page);

kirby()->hook('panel.page.create', function($page) {
	test::saveRevision($page);
});

If you see what is wrong with it, it’s a bonus, but it would be great to be able to have a way to debug it myself.

You can uncomment the comments to try it outside the hook.

Update

I found out that the script does work. I was looking at the wrong place. I also found a way to debug it.

function debug_hook() {
	$dir = kirby()->roots()->index() . DS . 'panel.page.create.txt';
	file_put_contents($dir, time());
}

Then I can put the function to see if the script has come so far…

debug_hook();
1 Like

While debugging with echo, vardump and more complex stuff like writing to text files works in many cases, it can easily become very tedious; or might even not work as in your example.

I thoroughly recommend getting familiar with a real debugger, such as XDebug. If you have no programming background or have never worked with an IDE there will be some initial effort to set everything up and understand the core concepts. Breakpoints, Step Into, Step Over, Run to Line… There are many many things that can make your life so much easier!

Essentially, XDebug is a PHP extension that enables you to ‘communicate’ with your PHP server while it’s executing your code. This enables you to pause the server at any (predefined) point of your code, a so-called breakpoint. Then, you can inspect all variables that are defined at that point in the code, and take a closer look at the contents of your arrays, objects and scalars. When you’re ready to continue the execution, you have four main options:

  1. Step over. This will execute the next function (usually the next line of code) and stop right after.
  2. Step into. This stops on the first line of code of the next function.
  3. Step out. This completes the execution of the current function, and stops right after.
  4. Run to a given line or till the end.

Especially when combined with an IDE this should allow you to easily find out why your hook is not being triggered, and save you a lot of time compared to endless trial-end-error debugging with var_dump.

XDebug integrations exist for all major IDEs (Eclipse, PHPStorm, …) and some more ‘basic’ editors (Sublime, Atom, …).

2 Likes

Thanks! I’m a programmer, but maybe not so hardcore on the server side. I’m keeping myself close to the surface.

Maybe I’ll try your suggestion. Right now I’ve built a panel form field that work as a log output.