Skip to content

Commit

Permalink
Revised and extended.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdavenport committed Aug 28, 2016
1 parent c00a2cd commit dabe6c2
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 60 deletions.
50 changes: 41 additions & 9 deletions libraries/joomla/string/parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
*
* Code syntax:
* $parser = (new JStringParser())
* ->registerToken('a', function(JStringTokenInterface $token, $content) { return 'replacement'; }, true)
* ->registerToken('b', function(JStringTokenInterface $token, $content) { return 'replacement'; }, false)
* ->register('a', function(JStringTokenInterface $token, $content) { return 'replacement'; }, true)
* ->register('b', function(JStringTokenInterface $token, $content) { return 'replacement'; }, false)
* ;
* $output = $parser->translate($content);
*
Expand Down Expand Up @@ -76,7 +76,7 @@ class JStringParser
* If there are characters before the next "real" token, return them in a TokenString object.
* If there are no further "real" tokens, return the remaining characters in a TokenString object.
*
* @return TokenInterface | null
* @return JStringToken | null
*
* @since __DEPLOY_VERSION__
*/
Expand Down Expand Up @@ -143,7 +143,7 @@ private function getNextToken()
return new JStringTokenEnd($tokenDefn);
}

return new JStringTokenBegin($tokenDefn, $tokenParams == '' ? array() : explode($this->paramSeparator, $tokenParams));
return new JStringTokenBegin($tokenName, $tokenDefn, $tokenParams == '' ? array() : explode($this->paramSeparator, $tokenParams));
}
while (true);

Expand Down Expand Up @@ -258,17 +258,49 @@ private function parseToken()
/**
* Register the definition of a token.
*
* @param string $name A token name to look for.
* @param callable $callback A callable that will return the replacement string.
* @param boolean $simple True for a simple token; false for a block token.
* @param string $name Token name.
* @param JStringTokenDefinition $definition Token definition.
*
* @return This object for method chaining.
*
* @since __DEPLOY_VERSION__
*/
public function registerToken($name, callable $callback, $simple = true)
public function register($name, JStringTokenDefinition $definition)
{
$this->tokens[JString::strtolower($name)] = new JStringTokenDefinition($name, $callback, $simple);
$this->tokens[JString::strtolower($name)] = $definition;

return $this;
}

/**
* Is token registered?
*
* @param string $name A token name to check.
*
* @return boolean
*
* @since __DEPLOY_VERSION__
*/
public function isRegistered($name)
{
return isset($this->tokens[JString::strtolower($name)]);
}

/**
* Unregister the definition of a token.
*
* @param string $name A token name to look for.
*
* @return This object for method chaining.
*
* @since __DEPLOY_VERSION__
*/
public function unregister($name)
{
if ($this->isRegistered($name))
{
unset($this->tokens[JString::strtolower($name)]);
}

return $this;
}
Expand Down
9 changes: 8 additions & 1 deletion libraries/joomla/string/token.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
*/
abstract class JStringToken
{
/**
* Name of the token..
*
* @since __DEPLOY_VERSION__
*/
protected $name = '';

/**
* Token definition object.
*
Expand Down Expand Up @@ -46,7 +53,7 @@ abstract class JStringToken
*/
public function getName()
{
return is_null($this->tokenDefinition) ? '' : $this->tokenDefinition->name;
return $this->name;
}

/**
Expand Down
20 changes: 17 additions & 3 deletions libraries/joomla/string/token/begin.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ class JStringTokenBegin extends JStringToken
/**
* Constructor.
*
* @param string $name Name of the token.
* @param JStringTokenDefinition $tokenDefinition Token definition object.
* @param array $params String representing parameters.
*
* @since __DEPLOY_VERSION__
*/
public function __construct(JStringTokenDefinition $tokenDefinition, array $params)
public function __construct($name, JStringTokenDefinition $tokenDefinition, array $params)
{
$this->name = $name;
$this->tokenDefinition = $tokenDefinition;
$this->params = $params;
}
Expand All @@ -41,9 +43,21 @@ public function __construct(JStringTokenDefinition $tokenDefinition, array $para
*/
public function getValue($content = '')
{
$value = $this->tokenDefinition->bound;
$callback = $this->tokenDefinition->callback;
$layout = $this->tokenDefinition->layout;

return $callback($this, $content);
if (is_callable($callback))
{
$value = $callback($this, $content, $value);
}

if ($layout instanceof JLayoutFile)
{
$value = $layout->render($value);
}

return $value;
}

/**
Expand All @@ -55,7 +69,7 @@ public function getValue($content = '')
*/
public function isSimple()
{
return (boolean) $this->tokenDefinition->simple;
return (boolean) $this->tokenDefinition->isSimple();
}
}

31 changes: 31 additions & 0 deletions libraries/joomla/string/token/block.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* @package Joomla.Platform
* @subpackage String
*
* @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

defined('_JEXEC') or die;

/**
* Block-type token type definition.
*
* @since __DEPLOY_VERSION__
*/
class JStringTokenBlock extends JStringTokenDefinition
{
/**
* Is this token simple or the beginning of a block?
*
* @return boolean
*
* @since __DEPLOY_VERSION__
*/
public function isSimple()
{
return false;
}
}

65 changes: 49 additions & 16 deletions libraries/joomla/string/token/definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,47 +14,80 @@
*
* @since __DEPLOY_VERSION__
*/
class JStringTokenDefinition
abstract class JStringTokenDefinition
{
/**
* Name of the token. Example "loadposition".
* Bound variable.
*
* @since __DEPLOY_VERSION__
*/
public $name = '';
public $bound = null;

/**
* Function that will be called to translate the token.
* Callback.
*
* @since __DEPLOY_VERSION__
*/
public $callback = null;

/**
* Flag which is set to indicate that this token is simple.
*
* A simple token looks like {name} and will be replaced in its entirety.
* A block token has a matching end block token which looks like {/name}.
* The begin and end block tokens and everything in between will be replaced.
* JLayoutFile object.
*
* @since __DEPLOY_VERSION__
*/
public $simple = true;
public $layout = null;

/**
* Constructor.
*
* @param string $name Token name.
* @param callable $callback Callable which will return the replacement string.
* @param boolean $simple True if token is simple; false if token is block.
* @param mixed $bound An optional value or callable to bind to the token.
*
* @since __DEPLOY_VERSION__
*/
public function __construct($bound = null)
{
$this->bound = $bound;
}

/**
* Assign a callback function.
*
* @param callable $callback An optional callback function.
*
* @return This object for method chaining.
*
* @since __DEPLOY_VERSION__
*/
public function __construct($name, callable $callback, $simple = true)
public function callback(callable $callback = null)
{
$this->name = JString::strtolower($name);
$this->callback = $callback;
$this->simple = (boolean) $simple;

return $this;
}

/**
* Is this token simple or the beginning of a block?
*
* @return boolean
*
* @since __DEPLOY_VERSION__
*/
abstract public function isSimple();

/**
* Assign a layout function.
*
* @param JLayout $layout An optional layout to bind to the token.
*
* @return This object for method chaining.
*
* @since __DEPLOY_VERSION__
*/
public function layout(JLayout $layout = null)
{
$this->layout = $layout;

return $this;
}
}

12 changes: 0 additions & 12 deletions libraries/joomla/string/token/end.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,5 @@ public function __construct(JStringTokenDefinition $tokenDefinition)
{
$this->tokenDefinition = $tokenDefinition;
}

/**
* Return the name of the token.
*
* @return string
*
* @since __DEPLOY_VERSION__
*/
public function getName()
{
return $this->tokenDefinition->name;
}
}

31 changes: 31 additions & 0 deletions libraries/joomla/string/token/simple.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* @package Joomla.Platform
* @subpackage String
*
* @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

defined('_JEXEC') or die;

/**
* Simple token type definition.
*
* @since __DEPLOY_VERSION__
*/
class JStringTokenSimple extends JStringTokenDefinition
{
/**
* Is this token simple or the beginning of a block?
*
* @return boolean
*
* @since __DEPLOY_VERSION__
*/
public function isSimple()
{
return true;
}
}

42 changes: 23 additions & 19 deletions plugins/content/loadmodule/loadmodule.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,35 @@ public function onContentPrepare($context, &$article, &$params, $page = 0)

// Register the loadposition token.
// Syntax: {loadposition <module-position>[,<style>]}
$parser->registerToken(
$parser->register(
'loadposition',
function(JStringToken $token)
{
$tokenParams = $token->getParams();
$position = trim($tokenParams[0]);
$style = isset($tokenParams[1]) ? trim($tokenParams[1]) : $this->params->def('style', 'none');

return addcslashes($this->_load($position, $style), '\\$');
}
(new JStringTokenSimple)->callback(
function(JStringToken $token)
{
$tokenParams = $token->getParams();
$position = trim($tokenParams[0]);
$style = isset($tokenParams[1]) ? trim($tokenParams[1]) : $this->params->def('style', 'none');

return addcslashes($this->_load($position, $style), '\\$');
}
)
);

// Register the loadmodule token.
// Syntax: {loadmodule <module-type>[,<module-title>[,<style>]]}
$parser->registerToken(
$parser->register(
'loadmodule',
function(JStringToken $token)
{
$tokenParams = $token->getParams();
$moduleName = trim($tokenParams[0]);
$moduleTitle = isset($tokenParams[1]) ? htmlspecialchars_decode(trim($tokenParams[1])) : '';
$style = isset($tokenParams[2]) ? trim($tokenParams[2]) : $this->params->def('style', 'none');

return addcslashes($this->_loadmod($moduleName, $moduleTitle, $style), '\\$');
}
(new JStringTokenSimple)->callback(
function(JStringToken $token)
{
$tokenParams = $token->getParams();
$moduleName = trim($tokenParams[0]);
$moduleTitle = isset($tokenParams[1]) ? htmlspecialchars_decode(trim($tokenParams[1])) : '';
$style = isset($tokenParams[2]) ? trim($tokenParams[2]) : $this->params->def('style', 'none');

return addcslashes($this->_loadmod($moduleName, $moduleTitle, $style), '\\$');
}
)
);

// Parse and translate the content.
Expand Down

0 comments on commit dabe6c2

Please sign in to comment.