ArjanSchouten / HtmlMinifier

Html minifier written in PHP
MIT License
22 stars 1 forks source link

Change API of PlaceholderContainer #5

Open ArjanSchouten opened 7 years ago

ArjanSchouten commented 7 years ago

The PlaceholderContainer has a bad API.

Current:

$content = '<html>
<body><pre>Content which should be replaced</pre></body>
</html>';
$placeholderContainer = new PlaceholderContainer();
$placeholder = $placeholderContainer->addPlaceholder('Content which should be replaced');
$result = str_replace('Content which should be replaced', $placeholder, $content);

Desired:

$content = '<html>
<body><pre>Content which should be replaced</pre></body>
</html>';
$placeholderContainer = new PlaceholderContainer();
$result = $placeholderContainer->addPlaceholder('Content which should be replaced', $content);

In lots of cases the API is used for doing something like this which makes sense:

return preg_replace_callback($pattern, function ($match) use ($placeholderContainer) {
     return $placeholderContainer->addPlaceholder($match[1]);
}, $contents);

The original addPlaceholder function should be renamed to createPlaceholder() with the same method definition.