Skip to content

YahnisElsts/mini-wp-hook-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mini WP Hook Parser

This is a basic parser that extracts hook documentation from WordPress source code. It is based on WP Parser. Mainly intended for my personal use.

Usage

use YahnisElsts\MiniWpHookParser\Parser;

Parser::extractAndWriteToFile( 
    '/path/to/wordpress', 
    '/path/to/output.json' 
);

More advanced usage:

use YahnisElsts\MiniWpHookParser\Parser;
$wordpressRoot = '/path/to/wordpress';

//Find all PHP files, potentially excluding some.
$files = Parser::findSourceFiles(
    $wordpressRoot, 
    ['/excluded-dir/', 'excluded-file.php']
);

//Extract documented hooks from the files.
$hooks = Parser::parseFiles($files);

//Convert the hook details to associative arrays.
$exported = Parser::exportHooks(
    $hooks, 
    $wordpressRoot //Used to convert absolute paths to relative paths.
);

Why not just use WP Parser?

At the time of writing, WP Parser was inconvenient to use because it's implemented as a WordPress plugin. I wanted something that I could use in a standalone PHP project.

I also tried WP Hooks Generator, but it triggered a large number of warnings and completely failed to parse some files. This could be because it had some very old dependencies, and I was trying to run it on PHP 8.1.

In the end, it was easier to just write my own, stripped-down version of WP Parser.

Known Issues

  • Namespaces: Name resolution will not work because the parser makes no attempt to detect the namespace of the file it's parsing. This should be fine for now as most of WordPress core is in the global namespace, but it may become an issue in the future.
  • PHPDoc: Nested tags in @param descriptions will not be parsed and will be treated as regular text instead.

Credits

This project was heavily inspired by WP Parser and reuses some of its code.

About

Hook documentation parser

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages