/
Runner.php
191 lines (168 loc) · 6.01 KB
/
Runner.php
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
/**
* Run Action.
*
* PHP versions 5.3
*
* @package Laiz
* @author Satoshi Nishimura <nishim314@gmail.com>
* @copyright 2009-2010 Satoshi Nishimura
*/
namespace laiz\action;
use \laiz\parser\Aggregate as ConfigParser; // ==check== TODO: YAML
use \laiz\lib\aggregate\laiz\action\Configurables;
use \laiz\lib\aggregate\laiz\action\Components;
/**
* Class of Building and Running Action.
*
* @package Laiz
* @author Satoshi Nishimura <nishim314@gmail.com>
*/
class Runner
{
const GLOBAL_CONFIG = 'laiz';
private $container;
private $parser;
private $debugLoopCount = 0;
public function __construct(ConfigParser $parser
, Configurables $configurables
, Components $components
, Response $res)
{
$this->parser = $parser;
$this->configurables = $configurables;
$this->components = $components;
$this->response = $res;
}
public function run($actionName)
{
$this->debugLoopCount++;
if ($this->debugLoopCount > 100){
trigger_error('Action redirection loop.', E_USER_ERROR);
return;
}
$opts = $this->parseOpts($actionName);
$configs = $this->getConfigs($opts['actionName']);
return $this->runRoutine($configs, $opts);
}
public function parseOpts($actionName)
{
$match = false;
foreach ($this->configurables as $obj){
if ($obj->match($actionName)){
$componentAction = $obj->convertActionClassName($actionName);
$methodName = $obj->getExecutionMethod();
$templateDir = $obj->getTemplateDir();
$templateName = $obj->convertTemplateName($actionName);
$match = true;
break;
}
// sure match Configurable_Default last.
}
$opts = array('actionName' => $componentAction,
'methodName' => $methodName,
'templateDir' => $templateDir,
'templateName' => $templateName);
return $opts;
}
private function clean()
{
$this->response->clean();
return $this;
}
/**
* Run action components.
*
* @param Array $configs get from ini file
* @param Array $opts get from Configurable for action and view
*/
private function runRoutine($configs, $opts)
{
$ret = null;
// setting components configs
if (isset($configs['view']['*']))
$opts['templateName'] = $configs['view']['*'];
$coConfigs = $configs;
$coConfigs['action'] = $configs;
$coConfigs['action']['actionName'] = $opts['actionName'];
$coConfigs['action']['methodName'] = $opts['methodName'];
$coConfigs['initializer'] = $configs;
$coConfigs['viewconfigure'] = array();
$coConfigs['viewconfigure']['templateDir'] = $opts['templateDir'];
$coConfigs['viewconfigure']['templateName'] = $opts['templateName'];
$coConfigs['viewrunner'] = array();
foreach ($this->components as $component){
// unrecognized class name
if (!preg_match('/([a-zA-Z_]+)$/', get_class($component), $matches))
continue;
$sectionName = str_replace('Component_', '', $matches[1]);
$sectionName = strtolower($sectionName);
// is not set in ini file
if (!isset($coConfigs[$sectionName]))
continue;
$ret = $component->run($coConfigs[$sectionName]);
if ($ret){
/*
* parsing return value
*/
if ($ret === 'none:')
return;
// action redirect
if (preg_match('/^redirect:/', $ret)){
$redirect = str_replace('redirect:', '', $ret);
header("Location: $redirect");
return;
}else if (preg_match('/^action:/', $ret)){
return $this->clean()->run(str_replace('action:', '', $ret));
}else if (isset($configs['result'][$ret])){
return $this->clean()->run($configs['result'][$ret]);
}
// change view
else if (isset($configs['view'][$ret])){
$coConfigs['viewconfigure']['templateName']
= $configs['view'][$ret];
}else{
// ini file configuration error
trigger_error("Not found [result] or [view] settings by return value [$ret]");
}
}
}
}
public function getConfigs($actionName)
{
$configFile = str_replace('\\', '/', $actionName);
$configFile = str_replace('_', '/', $configFile);
$tokens = explode('/', $configFile);
$path = '';
$configs = array();
foreach ($tokens as $key => $token){
$path = $path . $token;
if ($key == count($tokens) - 1)
$file = $path;
else
$file = $path . '/' . self::GLOBAL_CONFIG;
$path .= '/';
$configs = $this->mergeIniFile(ltrim($file, '/'), $configs);
}
return $configs;
}
private function mergeIniFile($iniFile, $configs)
{
$newConfigs = $this->parser->parse($iniFile);
foreach ($newConfigs as $section => $value){
if ($section === 'include'){
foreach ($value as $includeFile){
// override:
// global < upper section < include < under section
$configs = $this->mergeIniFile($includeFile, $configs);
}
continue;
}
if (isset($configs[$section]))
$configs[$section] = array_merge($configs[$section], $value);
else
$configs[$section] = $value;
}
return $configs;
}
}