Skip to content

Commit 9e47887

Browse files
committed
added two ready to use event handlers for handling raw shortcodes and returning content only from shortcode results
1 parent 66a452f commit 9e47887

5 files changed

Lines changed: 92 additions & 38 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
* introduced events handling in `Processor` with events above,
1111
* added `Processor::withEventContainer()` to configure event handlers,
1212
* added `ProcessedShortcode::hasAncestor()` to detect if shortcode has any parent with given name,
13-
* introduced `ReplacedShortcode` which represents parsed shortcode data with replacement returned from its handler.
13+
* introduced `ReplacedShortcode` which represents parsed shortcode data with replacement returned from its handler,
14+
* introduced ready to use event handlers classes:
15+
* `FilterRawEventHandler` allows to automatically configure shortcodes that should not have their content processed,
16+
* `ReplaceJoinEventHandler` discards the parent shortcode content and returns only concatenated replacements.
1417

1518
* v0.5.3 (26.01.2016)
1619
* massive performance improvements in RegularParser,
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
namespace Thunder\Shortcode\EventHandler;
3+
4+
use Thunder\Shortcode\Event\FilterShortcodesEvent;
5+
6+
/**
7+
* @author Tomasz Kowalczyk <tomasz@kowalczyk.cc>
8+
*/
9+
final class FilterRawEventHandler
10+
{
11+
/** @var string[] */
12+
private $names = array();
13+
14+
public function __construct(array $names)
15+
{
16+
foreach($names as $name) {
17+
if(false === is_string($name)) {
18+
throw new \InvalidArgumentException('Expected array of strings!');
19+
}
20+
21+
$this->names[] = $name;
22+
}
23+
}
24+
25+
public function __invoke(FilterShortcodesEvent $event)
26+
{
27+
$parent = $event->getParent();
28+
if($parent && in_array($parent->getName(), $this->names)) {
29+
$event->setShortcodes(array());
30+
31+
return;
32+
}
33+
}
34+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
namespace Thunder\Shortcode\EventHandler;
3+
4+
use Thunder\Shortcode\Event\ReplaceShortcodesEvent;
5+
6+
/**
7+
* @author Tomasz Kowalczyk <tomasz@kowalczyk.cc>
8+
*/
9+
final class ReplaceJoinEventHandler
10+
{
11+
/** @var string[] */
12+
private $names = array();
13+
14+
public function __construct(array $names)
15+
{
16+
foreach($names as $name) {
17+
if(false === is_string($name)) {
18+
throw new \InvalidArgumentException('Expected array of strings!');
19+
}
20+
21+
$this->names[] = $name;
22+
}
23+
}
24+
25+
public function __invoke(ReplaceShortcodesEvent $event)
26+
{
27+
if($event->getShortcode() && in_array($event->getShortcode()->getName(), $this->names)) {
28+
$replaces = array();
29+
foreach($event->getReplacements() as $r) {
30+
$replaces[] = $r->getReplacement();
31+
}
32+
33+
$event->setResult(implode('', $replaces));
34+
}
35+
}
36+
}

‎src/Shortcode/ProcessedShortcode.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Thunder\Shortcode\Shortcode;
33

4+
use Thunder\Shortcode\Parser\ParserInterface;
45
use Thunder\Shortcode\Processor\ProcessorContext;
56
use Thunder\Shortcode\Processor\ProcessorInterface;
67

‎tests/EventsTest.php‎

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
use Thunder\Shortcode\Event\ReplaceShortcodesEvent;
55
use Thunder\Shortcode\EventContainer\EventContainer;
6-
use Thunder\Shortcode\Event\FilterShortcodesEvent;
6+
use Thunder\Shortcode\EventHandler\FilterRawEventHandler;
7+
use Thunder\Shortcode\EventHandler\ReplaceJoinEventHandler;
78
use Thunder\Shortcode\Events;
89
use Thunder\Shortcode\HandlerContainer\HandlerContainer;
910
use Thunder\Shortcode\Parser\RegularParser;
@@ -17,26 +18,6 @@
1718
*/
1819
final class EventsTest extends \PHPUnit_Framework_TestCase
1920
{
20-
public function testFilterShortcodes()
21-
{
22-
$handlers = new HandlerContainer();
23-
$handlers->add('root', function(ShortcodeInterface $s) { return 'root['.$s->getContent().']'; });
24-
$handlers->add('yes', function(ShortcodeInterface $s) { return 'yes['.$s->getContent().']'; });
25-
$handlers->add('no', function(ShortcodeInterface $s) { return 'nope'; });
26-
27-
$events = new EventContainer();
28-
$events->addListener(Events::FILTER_SHORTCODES, function(FilterShortcodesEvent $event) {
29-
$event->setShortcodes(array_filter($event->getShortcodes(), function(ShortcodeInterface $s) {
30-
return $s->getName() !== 'no';
31-
}));
32-
});
33-
34-
$processor = new Processor(new RegularParser(), $handlers);
35-
$processor = $processor->withEventContainer($events);
36-
37-
$this->assertSame('x root[ yes[ yes[] ] yes[ [no /] ] ] y', $processor->process('x [root] [yes] [yes/] [/yes] [yes] [no /] [/yes] [/root] y'));
38-
}
39-
4021
public function testRaw()
4122
{
4223
$times = 0;
@@ -46,12 +27,7 @@ public function testRaw()
4627
$handlers->add('c', function(ShortcodeInterface $s) use(&$times) { ++$times; return $s->getContent(); });
4728

4829
$events = new EventContainer();
49-
$events->addListener(Events::FILTER_SHORTCODES, function(FilterShortcodesEvent $event) {
50-
$parent = $event->getParent();
51-
if($parent && ($parent->getName() === 'raw' || $parent->hasAncestor('raw'))) {
52-
$event->setShortcodes(array());
53-
}
54-
});
30+
$events->addListener(Events::FILTER_SHORTCODES, new FilterRawEventHandler(array('raw')));
5531

5632
$processor = new Processor(new RegularParser(), $handlers);
5733
$processor = $processor->withEventContainer($events);
@@ -69,20 +45,12 @@ public function testStripContentOutsideShortcodes()
6945
$handlers->add('root', function(ProcessedShortcode $s) { return 'root['.$s->getContent().']'; });
7046

7147
$events = new EventContainer();
72-
$events->addListener(Events::REPLACE_SHORTCODES, function(ReplaceShortcodesEvent $event) {
73-
if($event->getShortcode() && 'root' === $event->getShortcode()->getName()) {
74-
$replaces = array();
75-
foreach($event->getReplacements() as $r) {
76-
$replaces[] = $r->getReplacement();
77-
}
78-
$event->setResult(implode('', $replaces));
79-
}
80-
});
48+
$events->addListener(Events::REPLACE_SHORTCODES, new ReplaceJoinEventHandler(array('root')));
8149

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

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

8856
public function testDefaultApplier()
@@ -114,4 +82,16 @@ public function testExceptionOnHandlerForUnknownEvent()
11482
$this->setExpectedException('InvalidArgumentException');
11583
$events->addListener('invalid', function() {});
11684
}
85+
86+
public function testInvalidFilterRawShortcodesNames()
87+
{
88+
$this->setExpectedException('InvalidArgumentException');
89+
new FilterRawEventHandler(array(new \stdClass()));
90+
}
91+
92+
public function testInvalidReplaceJoinNames()
93+
{
94+
$this->setExpectedException('InvalidArgumentException');
95+
new ReplaceJoinEventHandler(array(new \stdClass()));
96+
}
11797
}

0 commit comments

Comments
 (0)