Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Implemented REWRITE_REPLACEMENTS event #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/Event/RewriteReplacementsEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
namespace Thunder\Shortcode\Event;

use Thunder\Shortcode\Shortcode\ReplacedShortcode;
use Thunder\Shortcode\Shortcode\ShortcodeInterface;

/**
* This event is called after gathering shortcode handlers results and just
* before the REPLACE_SHORTCODES event to allow modification of replacements
* before applying them in the source text.
*
* @author Tomasz Kowalczyk <tomasz@kowalczyk.cc>
*/
final class RewriteReplacementsEvent
{
/** @var ReplacedShortcode[] */
private $replacements;
/** @var ReplacedShortcode[] */
private $rewritten = array();
/** @var bool */
private $called = false;

/** @param ReplacedShortcode[] $replacements */
public function __construct(array $replacements)
{
$this->replacements = $replacements;
}

/** @return ReplacedShortcode[] */
public function getReplacements()
{
$this->called = true;

return $this->replacements;
}

/** @return void */
public function addRewritten(ReplacedShortcode $replacement)
{
$this->rewritten[] = $replacement;
}

/** @return ReplacedShortcode[] */
public function getRewritten()
{
return $this->called ? $this->rewritten : $this->replacements;
}
}
32 changes: 32 additions & 0 deletions src/EventHandler/RewriteWrapEventHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
namespace Thunder\Shortcode\EventHandler;

use Thunder\Shortcode\Event\RewriteReplacementsEvent;

/**
* @author Tomasz Kowalczyk <tomasz@kowalczyk.cc>
*/
final class RewriteWrapEventHandler
{
/** @var string */
private $prefix;
/** @var string */
private $suffix;

/**
* @param string $prefix
* @param string $suffix
*/
public function __construct($prefix, $suffix)
{
$this->prefix = $prefix;
$this->suffix = $suffix;
}

public function __invoke(RewriteReplacementsEvent $event)
{
foreach($event->getReplacements() as $replacement) {
$event->addRewritten($replacement->withReplacement($this->prefix.$replacement->getReplacement().$this->suffix));
}
}
}
3 changes: 2 additions & 1 deletion src/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ final class Events
{
const FILTER_SHORTCODES = 'event.filter-shortcodes';
const REPLACE_SHORTCODES = 'event.replace-shortcodes';
const REWRITE_REPLACEMENTS = 'event.rewrite-replacements';

/** @return string[] */
public static function getEvents()
{
return array(static::FILTER_SHORTCODES, static::REPLACE_SHORTCODES);
return array(static::FILTER_SHORTCODES, static::REPLACE_SHORTCODES, static::REWRITE_REPLACEMENTS);
}
}
4 changes: 4 additions & 0 deletions src/Processor/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Thunder\Shortcode\Event\ReplaceShortcodesEvent;
use Thunder\Shortcode\Event\FilterShortcodesEvent;
use Thunder\Shortcode\Event\RewriteReplacementsEvent;
use Thunder\Shortcode\EventContainer\EventContainerInterface;
use Thunder\Shortcode\Events;
use Thunder\Shortcode\HandlerContainer\HandlerContainerInterface as Handlers;
Expand Down Expand Up @@ -122,6 +123,9 @@ private function processIteration($text, ProcessorContext $context, ProcessedSho
$replaces[] = new ReplacedShortcode($shortcode, $replace);
}

$rewriteEvent = new RewriteReplacementsEvent($replaces);
$this->dispatchEvent(Events::REWRITE_REPLACEMENTS, $rewriteEvent);
$replaces = $rewriteEvent->getRewritten();
$applyEvent = new ReplaceShortcodesEvent($text, $replaces, $parent);
$this->dispatchEvent(Events::REPLACE_SHORTCODES, $applyEvent);

Expand Down
13 changes: 13 additions & 0 deletions src/Shortcode/ReplacedShortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ public function __construct(ParsedShortcodeInterface $shortcode, $replacement)
$this->replacement = $replacement;
}

/**
* @param string $replacement
*
* @return self
*/
public function withReplacement($replacement)
{
$base = new Shortcode($this->name, $this->parameters, $this->content, $this->bbCode);
$parsed = new ParsedShortcode($base, $this->text, $this->offset);

return new self($parsed, $replacement);
}

/** @return string */
public function getReplacement()
{
Expand Down
17 changes: 17 additions & 0 deletions tests/EventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Thunder\Shortcode\EventContainer\EventContainer;
use Thunder\Shortcode\EventHandler\FilterRawEventHandler;
use Thunder\Shortcode\EventHandler\ReplaceJoinEventHandler;
use Thunder\Shortcode\EventHandler\RewriteWrapEventHandler;
use Thunder\Shortcode\Events;
use Thunder\Shortcode\HandlerContainer\HandlerContainer;
use Thunder\Shortcode\Parser\RegularParser;
Expand Down Expand Up @@ -76,6 +77,22 @@ public function testDefaultApplier()
$this->assertSame('a root[x name c name y] b', $processor->process('a [root]x [name] c[content] [name /] [/content] y[/root] b'));
}

public function testRewriteReplacements()
{
$handlers = new HandlerContainer();
$handlers->add('name', function(ShortcodeInterface $s) { return $s->getName(); });
$handlers->add('content', function(ShortcodeInterface $s) { return $s->getContent(); });
$handlers->add('root', function(ProcessedShortcode $s) { return 'root['.$s->getContent().']'; });

$events = new EventContainer();
$events->addListener(Events::REWRITE_REPLACEMENTS, new RewriteWrapEventHandler('>', '<'));

$processor = new Processor(new RegularParser(), $handlers);
$processor = $processor->withEventContainer($events);

$this->assertSame('a >root[x >name< c> >name< < y]< b', $processor->process('a [root]x [name] c[content] [name /] [/content] y[/root] b'));
}

public function testExceptionOnHandlerForUnknownEvent()
{
$events = new EventContainer();
Expand Down