nochso / writeme

WRITEME to README. Help create and maintain READMEs using placeholders.
Other
0 stars 0 forks source link

Refactor around Placeholder::apply, Converter, etc. #18

Closed nochso closed 8 years ago

nochso commented 8 years ago

This is a work in progress.

As of now I've added a class Call that returns the first call to any placeholder or a more specific method of any placeholder.

See tests for how it's supposed to behave.


This new class should help with doing the following, because the Converter class is kind of.. dirty and does too much.

Instead of calling apply on each Placeholder and letting the Placeholder handle too much (checking if there's work to do..), I'd like something like this:

$call = Call::extractFirstCall($document);
while ($call !== null) {
    // Find the placeholder that is responsible for this call:
    $ph = $this->getPlaceholderByIdentifier($call->getIdentifier());
    // Call the placeholder with the Call object. The placeholder decides what to do with
    // the Document using the Call object for method name and parameters.
    $ph->call($call, $document);
    // The document MUST have modified the document, i.e. replaced `@something@`
    // with whatever..
    // Now we look for the next call until there are no more..
    $call = Call::extractFirstCall($document);
}

The above code (or similar) would then replace this: https://github.com/nochso/writeme/blob/8d8b88fe449a2b35ce1256e681ed04f1db887144/src/Converter.php#L41-L65

That way Placeholders only do their work and do not have to check if they have work to do: "we call you"


Example how a placeholder might work:

// Simple example like @dice 1,6@ for rolling a random number..
class Dice implements Placeholder {
    public function call(Call $call, Document $doc) {
        // No method specified..
        if ($call->getMethod() === null) {
            $params = $call->getParameters();
            // Let some other class handle the replacement.
            // Simply pass the replacement string to it
            Converter::replaceCall($call, rand($params[0], $params[1]));
        }
    }
}