Processor.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * Turn ECML markup into text via plugin hooks
  4. *
  5. * @access private
  6. */
  7. class Elgg_Ecml_Processor {
  8. /**
  9. * @var Elgg_Ecml_Tokenizer
  10. */
  11. protected $tokenizer;
  12. /**
  13. * @param Elgg_Ecml_Tokenizer $tokenizer
  14. */
  15. public function __construct(Elgg_Ecml_Tokenizer $tokenizer) {
  16. $this->tokenizer = $tokenizer;
  17. }
  18. /**
  19. * @param string $text
  20. * @param array $context info to pass to the plugin hooks
  21. *
  22. * @return string
  23. *
  24. * @throws Exception
  25. */
  26. public function process($text, $context = array()) {
  27. $tokens = $this->tokenizer->getTokens($text);
  28. // allow processors that might need to see all the tokens at once
  29. $tokens = elgg_trigger_plugin_hook("prepare:tokens", "ecml", $context, $tokens);
  30. if (!is_array($tokens)) {
  31. throw new Exception(elgg_echo('ecml:Exception:InvalidTokenList'));
  32. }
  33. // process tokens in isolation
  34. $output = '';
  35. foreach ($tokens as $token) {
  36. if (is_string($token)) {
  37. $output .= $token;
  38. } elseif ($token instanceof Elgg_Ecml_Token) {
  39. /* @var Elgg_Ecml_Token $token */
  40. if ($token->isText) {
  41. $output .= $token->content;
  42. } else {
  43. $params = array_merge($context, array(
  44. 'keyword' => $token->keyword,
  45. 'attributes' => $token->attrs,
  46. ));
  47. $output .= elgg_trigger_plugin_hook("render:{$token->keyword}", "ecml", $params, $token->content);
  48. }
  49. } else {
  50. throw new Exception(elgg_echo('ecml:Exception:InvalidToken'));
  51. }
  52. }
  53. return $output;
  54. }
  55. }