php-mf2 icon indicating copy to clipboard operation
php-mf2 copied to clipboard

Improve main parser entry point API and config

Open barnabywalters opened this issue 3 years ago • 0 comments

Currently, the parser has a variety of different entry points:

Mf2\fetch('url');
Mf2\parse('html', 'url');
$p = new Parser('html', 'url');
$p->parse();
$p->parseFromId();

Additionally, there is a variety of different ways of setting various configuration options and feature flags, often just by passing a boolean to a positional argument. Depending on the entry point, a boolean in a particular position might do something completely different.

For v1.0 we could make the most of the possibility for breaking changes to improve the API for better consistency, readability and extensibility.

Inspired by the XRay API, one potential improvement could be replacing the fetch and parse functions with a single parse function which does both depending on its arguments:

Mf2\parse($url); // Fetches and parses the contents of $url
Mf2\parse($url, $html); // Parses $html using $url as a base URL

One potential solution to the config issue would be to allow passing a $config array or object to any of the main entry points, e.g.

$config = [
  'lang' => true,
  'curl_info' => &$curlInfo,
  'json_mode' => true,
  'debug' => true,
  'parse_classic' => true
];

// Used with the existing entry point functions:
Mf2\fetch('url', $config);
Mf2\parse('html', 'url', $config);
$p = new Parser('html', 'url', $config);

// Or, with the fetch/parse change suggested above:
Mf2\parse('url', $config);
Mf2\parse('url', 'html', $config);

The config keys themselves are only suggestions and could likely be improved.

With some type checking, it may even be possible to implement this while maintaining back compatibility with the old APIs.

barnabywalters avatar Feb 23 '22 20:02 barnabywalters