1: <?php
2:
3: namespace PHPixie\Template\Renderer;
4:
5: class Context
6: {
7: protected $extensions;
8: protected $renderer;
9: protected $resolver;
10: protected $template;
11: protected $data;
12:
13: protected $layout;
14: protected $childContent;
15:
16: protected $variables;
17: protected $extensionMethods;
18:
19: protected $blocks = array();
20: protected $blockStack = array();
21:
22: public function __construct($extensions, $renderer, $resolver, $template, $data)
23: {
24: $this->extensions = $extensions;
25: $this->renderer = $renderer;
26: $this->resolver = $resolver;
27: $this->template = $template;
28: $this->data = $data;
29: }
30:
31: public function renderer()
32: {
33: return $this->renderer;
34: }
35:
36: public function resolver()
37: {
38: return $this->resolver;
39: }
40:
41: public function template()
42: {
43: return $this->template;
44: }
45:
46: public function data()
47: {
48: return $this->data;
49: }
50:
51: public function setLayout($name)
52: {
53: $this->layout = $name;
54: }
55:
56: public function layout()
57: {
58: return $this->layout;
59: }
60:
61: public function childContent()
62: {
63: return $this->childContent;
64: }
65:
66: public function processRender($content)
67: {
68: $this->assertBlocksClosed();
69:
70: $this->template = $this->layout;
71: $this->layout = null;
72: $this->childContent = $content;
73: }
74:
75: public function variables()
76: {
77: return array_merge(
78: $this->extensions->aliases(),
79: $this->data->get(null, array())
80: );
81: }
82:
83: public function extension($name)
84: {
85: return $this->extensions->get($name);
86: }
87:
88: public function callExtensionMethod($method, $params)
89: {
90: $method = $this->extensions->getMethod($method);
91: return call_user_func_array($method, $params);
92: }
93:
94: public function blockExists($name)
95: {
96: return array_key_exists($name, $this->blocks);
97: }
98:
99: public function block($name)
100: {
101: if($this->blockExists($name)) {
102: return $this->blocks[$name];
103: }
104:
105: return null;
106: }
107:
108: public function startBlock($name, $prepend = false)
109: {
110: array_push($this->blockStack, array($name, $prepend));
111: }
112:
113: public function endBlock($content)
114: {
115: if(empty($this->blockStack)) {
116: throw new \PHPixie\Template\Exception("endBlock() called too many times in '{$this->template}'");
117: }
118:
119: list($name, $prepend) = array_pop($this->blockStack);
120:
121: if($this->blockExists($name)) {
122: if($prepend) {
123: $content = $content.$this->blocks[$name];
124:
125: }else{
126: $content = $this->blocks[$name].$content;
127:
128: }
129: }
130:
131: $this->blocks[$name] = $content;
132: }
133:
134: protected function assertBlocksClosed()
135: {
136: if(!empty($this->blockStack)) {
137: throw new \PHPixie\Template\Exception("Not all open blocks have been closed in '{$this->template}'");
138: }
139: }
140: }