-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathActionChain.php
More file actions
147 lines (126 loc) · 4.27 KB
/
ActionChain.php
File metadata and controls
147 lines (126 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/*
* This file has its roots as part of the Mojavi package which was
* Copyright (c) 2003 Sean Kerr. It has been incorporated into this
* derivative work under the terms of the LGPL V2.1.
* (http://www.gnu.org/licenses/lgpl-2.1.html)
*/
namespace Xmf\Xadr;
/**
* An ActionChain allows execution of multiple actions and retrieving
* the rendered results from that execution. Potential uses include
* incoporating information from external Action implementations.
*
* @category Xmf\Xadr\ActionChain
* @package Xmf
* @author Richard Griffith <richard@geekwright.com>
* @author Sean Kerr <skerr@mojavi.org>
* @copyright 2013-2015 XOOPS Project (http://xoops.org)
* @copyright 2003 Sean Kerr
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
* @link http://xoops.org
*/
class ActionChain extends ContextAware
{
/**
* An associative array of actions.
*
* @var array
*/
protected $actions = array();
/**
* Whether or not to preserve request parameters while actions are being
* executed.
*
* @var boolean
*/
protected $preserve = false;
/**
* Execute all registered actions.
*
* _This method should never be called manually._
*
* @return void
*/
public function execute()
{
$keys = array_keys($this->actions);
$count = count($keys);
// retrieve current render mode
$renderMode = $this->controller()->getRenderMode();
// force all actions at this point to render to variable
$this->controller()->setRenderMode(Xadr::RENDER_VARIABLE);
for ($i = 0; $i < $count; $i++) {
$action =& $this->actions[$keys[$i]];
if ($this->preserve && $action['params'] != null) {
$originalParams = $this->request()->parameters()->getArrayCopy();
}
if ($action['params'] != null) {
$this->request()->parameters()->setMerge($action['params']);
}
// execute/forward the action and retrieve rendered result
$this->controller()->forward($action['unit'], $action['action']);
// retrieve renderer for action
$renderer =& $this->request()->attributes()->get('org.mojavi.renderer');
// did the action render a view?
if ($renderer !== null) {
// retrieve rendered result
$action['result'] = $renderer->fetchResult();
// clear rendered result
$renderer->clearResult();
// remove renderer
$this->request()->attributes()->remove('org.mojavi.renderer');
}
if (isset($originalParams)) {
$this->request()->parameters()->exchangeArray($originalParams);
unset($originalParams);
}
}
// put the old rendermode back
$this->controller()->setRenderMode($renderMode);
}
/**
* Fetch the result of an executed action.
*
* @param string $regName An action registration name.
*
* @return string A rendered view, if the given action exists and did render
* a view, otherwise NULL.
*/
public function & fetchResult($regName)
{
if (isset($this->actions[$regName]['result'])) {
return $this->actions[$regName]['result'];
}
$null = null;
return $null;
}
/**
* Register an action with the chain.
*
* @param string $regName An action registration name.
* @param string $unitName A unit name.
* @param string $actName An action name.
* @param array|null $params Associative array of temporary request
* parameters.
*
* @return void
*/
public function register($regName, $unitName, $actName, $params = null)
{
$this->actions[$regName]['action'] = $actName;
$this->actions[$regName]['unit'] = $unitName;
$this->actions[$regName]['params'] = $params;
}
/**
* Set the parameter preservation status.
*
* @param bool $preserve A preservation status.
*
* @return void
*/
public function setPreserve($preserve)
{
$this->preserve = $preserve;
}
}