1: <?php
2:
3: /**
4: * Texy! - human-readable text to HTML converter.
5: *
6: * @copyright Copyright (c) 2004, 2010 David Grudl
7: * @license GNU GENERAL PUBLIC LICENSE version 2 or 3
8: * @link http://texy.info
9: * @package Texy
10: */
11:
12:
13:
14: /**
15: * Around advice handlers.
16: *
17: * @copyright Copyright (c) 2004, 2010 David Grudl
18: * @package Texy
19: */
20: final class TexyHandlerInvocation extends TexyObject
21: {
22: /** @var array of callbacks */
23: private $handlers;
24:
25: /** @var int callback counter */
26: private $pos;
27:
28: /** @var array */
29: private $args;
30:
31: /** @var TexyParser */
32: private $parser;
33:
34:
35:
36: /**
37: * @param array array of callbacks
38: * @param TexyParser
39: * @param array arguments
40: */
41: public function __construct($handlers, TexyParser $parser, $args)
42: {
43: $this->handlers = $handlers;
44: $this->pos = count($handlers);
45: $this->parser = $parser;
46: array_unshift($args, $this);
47: $this->args = $args;
48: }
49:
50:
51:
52: /**
53: * @param mixed
54: * @return mixed
55: */
56: public function proceed()
57: {
58: if ($this->pos === 0) {
59: throw new InvalidStateException('No more handlers.');
60: }
61:
62: if (func_num_args()) {
63: $this->args = func_get_args();
64: array_unshift($this->args, $this);
65: }
66:
67: $this->pos--;
68: $res = call_user_func_array($this->handlers[$this->pos], $this->args);
69: if ($res === NULL) {
70: throw new UnexpectedValueException("Invalid value returned from handler '" . print_r($this->handlers[$this->pos], TRUE) . "'.");
71: }
72: return $res;
73: }
74:
75:
76:
77: /**
78: * @return TexyParser
79: */
80: public function getParser()
81: {
82: return $this->parser;
83: }
84:
85:
86:
87: /**
88: * @return Texy
89: */
90: public function getTexy()
91: {
92: return $this->parser->getTexy();
93: }
94:
95:
96:
97: /**
98: * PHP garbage collector helper.
99: */
100: public function free()
101: {
102: $this->handlers = $this->parser = $this->args = NULL;
103: }
104:
105: }
106: