Skip to content

Commit

Permalink
Fix accessing calls
Browse files Browse the repository at this point in the history
Some plugins need access to the handler (and rewriter) calls. One
example is the do plugin.

This adds some accessing methods to the callWriter objects, ensures
$calls can be accessed and removes some duplicate code by introducing an
abstract base class.
  • Loading branch information
splitbrain committed Jun 2, 2020
1 parent 0884c25 commit 533aca4
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 122 deletions.
39 changes: 39 additions & 0 deletions inc/Parsing/Handler/AbstractRewriter.php
@@ -0,0 +1,39 @@
<?php

namespace dokuwiki\Parsing\Handler;

/**
* Basic implementation of the rewriter interface to be specialized by children
*/
abstract class AbstractRewriter implements ReWriterInterface
{
/** @var CallWriterInterface original CallWriter */
protected $callWriter;

/** @var array[] list of calls */
public $calls = array();

/** @inheritdoc */
public function __construct(CallWriterInterface $callWriter)
{
$this->callWriter = $callWriter;
}

/** @inheritdoc */
public function writeCall($call)
{
$this->calls[] = $call;
}

/** * @inheritdoc */
public function writeCalls($calls)
{
$this->calls = array_merge($this->calls, $calls);
}

/** @inheritDoc */
public function getCallWriter()
{
return $this->callWriter;
}
}
4 changes: 2 additions & 2 deletions inc/Parsing/Handler/CallWriterInterface.php
Expand Up @@ -7,14 +7,14 @@ interface CallWriterInterface
/**
* Add a call to our call list
*
* @param $call the call to be added
* @param array $call the call to be added
*/
public function writeCall($call);

/**
* Append a list of calls to our call list
*
* @param $calls list of calls to be appended
* @param array[] $calls list of calls to be appended
*/
public function writeCalls($calls);

Expand Down
29 changes: 1 addition & 28 deletions inc/Parsing/Handler/Lists.php
Expand Up @@ -2,42 +2,15 @@

namespace dokuwiki\Parsing\Handler;

class Lists implements ReWriterInterface
class Lists extends AbstractRewriter
{

/** @var CallWriterInterface original call writer */
protected $callWriter;

protected $calls = array();
protected $listCalls = array();
protected $listStack = array();

protected $initialDepth = 0;

const NODE = 1;


/** @inheritdoc */
public function __construct(CallWriterInterface $CallWriter)
{
$this->callWriter = $CallWriter;
}

/** @inheritdoc */
public function writeCall($call)
{
$this->calls[] = $call;
}

/**
* @inheritdoc
* Probably not needed but just in case...
*/
public function writeCalls($calls)
{
$this->calls = array_merge($this->calls, $calls);
}

/** @inheritdoc */
public function finalise()
{
Expand Down
15 changes: 7 additions & 8 deletions inc/Parsing/Handler/Nest.php
Expand Up @@ -8,13 +8,8 @@
*
* @author Chris Smith <chris@jalakai.co.uk>
*/
class Nest implements ReWriterInterface
class Nest extends AbstractRewriter
{

/** @var CallWriterInterface original CallWriter */
protected $callWriter;

protected $calls = array();
protected $closingInstruction;

/**
Expand All @@ -26,8 +21,7 @@ class Nest implements ReWriterInterface
*/
public function __construct(CallWriterInterface $CallWriter, $close = "nest_close")
{
$this->callWriter = $CallWriter;

parent::__construct($CallWriter);
$this->closingInstruction = $close;
}

Expand Down Expand Up @@ -69,6 +63,9 @@ public function process()
return $this->callWriter;
}

/**
* @param array $call
*/
protected function addCall($call)
{
$key = count($this->calls);
Expand All @@ -80,4 +77,6 @@ protected function addCall($call)
$this->calls[] = $call;
}
}


}
29 changes: 1 addition & 28 deletions inc/Parsing/Handler/Preformatted.php
Expand Up @@ -2,39 +2,12 @@

namespace dokuwiki\Parsing\Handler;

class Preformatted implements ReWriterInterface
class Preformatted extends AbstractRewriter
{

/** @var CallWriterInterface original call writer */
protected $callWriter;

protected $calls = array();
protected $pos;
protected $text ='';

/**
* @inheritdoc
*/
public function __construct(CallWriterInterface $CallWriter)
{
$this->callWriter = $CallWriter;
}

/** @inheritdoc */
public function writeCall($call)
{
$this->calls[] = $call;
}

/**
* @inheritdoc
* Probably not needed but just in case...
*/
public function writeCalls($calls)
{
$this->calls = array_merge($this->calls, $calls);
}

/** @inheritdoc */
public function finalise()
{
Expand Down
34 changes: 5 additions & 29 deletions inc/Parsing/Handler/Quote.php
Expand Up @@ -2,38 +2,10 @@

namespace dokuwiki\Parsing\Handler;

class Quote implements ReWriterInterface
class Quote extends AbstractRewriter
{

/** @var CallWriterInterface original CallWriter */
protected $callWriter;

protected $calls = array();

protected $quoteCalls = array();

/** @inheritdoc */
public function __construct(CallWriterInterface $CallWriter)
{
$this->callWriter = $CallWriter;
}

/** @inheritdoc */
public function writeCall($call)
{
$this->calls[] = $call;
}

/**
* @inheritdoc
*
* Probably not needed but just in case...
*/
public function writeCalls($calls)
{
$this->calls = array_merge($this->calls, $calls);
}

/** @inheritdoc */
public function finalise()
{
Expand Down Expand Up @@ -101,6 +73,10 @@ public function process()
return $this->callWriter;
}

/**
* @param $marker
* @return int
*/
protected function getDepth($marker)
{
preg_match('/>{1,}/', $marker, $matches);
Expand Down
10 changes: 9 additions & 1 deletion inc/Parsing/Handler/ReWriterInterface.php
Expand Up @@ -5,10 +5,11 @@
/**
* A ReWriter takes over from the orignal call writer and handles all new calls itself until
* the process method is called and control is given back to the original writer.
*
* @property array[] $calls The list of current calls
*/
interface ReWriterInterface extends CallWriterInterface
{

/**
* ReWriterInterface constructor.
*
Expand All @@ -26,4 +27,11 @@ public function __construct(CallWriterInterface $callWriter);
* @return CallWriterInterface the orignal call writer
*/
public function process();

/**
* Accessor for this rewriter's original CallWriter
*
* @return CallWriterInterface
*/
public function getCallWriter();
}
27 changes: 1 addition & 26 deletions inc/Parsing/Handler/Table.php
Expand Up @@ -2,13 +2,9 @@

namespace dokuwiki\Parsing\Handler;

class Table implements ReWriterInterface
class Table extends AbstractRewriter
{

/** @var CallWriterInterface original CallWriter */
protected $callWriter;

protected $calls = array();
protected $tableCalls = array();
protected $maxCols = 0;
protected $maxRows = 1;
Expand All @@ -19,27 +15,6 @@ class Table implements ReWriterInterface
protected $currentRow = array('tableheader' => 0, 'tablecell' => 0);
protected $countTableHeadRows = 0;

/** @inheritdoc */
public function __construct(CallWriterInterface $CallWriter)
{
$this->callWriter = $CallWriter;
}

/** @inheritdoc */
public function writeCall($call)
{
$this->calls[] = $call;
}

/**
* @inheritdoc
* Probably not needed but just in case...
*/
public function writeCalls($calls)
{
$this->calls = array_merge($this->calls, $calls);
}

/** @inheritdoc */
public function finalise()
{
Expand Down
18 changes: 18 additions & 0 deletions inc/parser/handler.php
Expand Up @@ -49,6 +49,24 @@ public function addCall($handler, $args, $pos) {
$this->callWriter->writeCall($call);
}

/**
* Accessor for the current CallWriter
*
* @return CallWriterInterface
*/
public function getCallWriter() {
return $this->callWriter;
}

/**
* Set a new CallWriter
*
* @param CallWriterInterface $callWriter
*/
public function setCallWriter($callWriter) {
$this->callWriter = $callWriter;
}

/** @deprecated 2019-10-31 use addCall() instead */
public function _addCall($handler, $args, $pos) {
dbg_deprecated('addCall');
Expand Down

0 comments on commit 533aca4

Please sign in to comment.