• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pmvc-plugin / controller / 87d58475-cd82-4c94-86e7-de7a6990a78f

09 Sep 2023 06:57AM UTC coverage: 63.089% (+0.1%) from 62.987%
87d58475-cd82-4c94-86e7-de7a6990a78f

Pull #39

circleci

StyleCIBot
Apply fixes from StyleCI
Pull Request #39: Apply fixes from StyleCI

61 of 61 new or added lines in 3 files covered. (100.0%)

388 of 615 relevant lines covered (63.09%)

3.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

61.01
/src/ActionForward.php
1
<?php
2
/**
3
 * PMVC.
4
 *
5
 * PHP version 5
6
 *
7
 * @category CategoryName
8
 *
9
 * @package PMVC
10
 *
11
 * @author  Hill <hill@kimo.com>
12
 * @license http://opensource.org/licenses/MIT MIT
13
 *
14
 * @version GIT: <git_id>
15
 *
16
 * @link https://packagist.org/packages/pmvc/pmvc
17
 */
18

19
namespace PMVC;
20

21
/**
22
 * PMVC ActionForward.
23
 *
24
 * !!!!!!!!!!!!!!!
25
 * !! important !!
26
 * !!!!!!!!!!!!!!!
27
 *
28
 * If you change view after get forward,
29
 * you need reget forward again.
30
 *
31
 * @category CategoryName
32
 *
33
 * @package PMVC
34
 *
35
 * @author  Hill <hill@kimo.com>
36
 * @license http://opensource.org/licenses/MIT MIT
37
 *
38
 * @link https://packagist.org/packages/pmvc/pmvc
39
 */
40
class ActionForward extends HashMap
41
{
42
    /**
43
     * VIEW.
44
     *
45
     * @const VIEW
46
     */
47
    const VIEW = 'view';
48

49
    /**
50
     * Path.
51
     *
52
     * @var string
53
     */
54
    private $_path;
55

56
    /**
57
     * Type.
58
     *
59
     * @var string
60
     */
61
    private $_type;
62

63
    /**
64
     * Header.
65
     *
66
     * @var array
67
     */
68
    private $_header = [];
69

70
    /**
71
     * Body.
72
     *
73
     * @var array
74
     */
75
    private $_body = [];
76

77
    /**
78
     * Client redirect.
79
     *
80
     * @var bool
81
     */
82
    private $_isClientRedirect = false;
83

84
    /**
85
     * View.
86
     *
87
     * @var object
88
     */
89
    private $_view;
90

91
    /**
92
     * Name (setup on ActionMappings).
93
     *
94
     * @var string
95
     */
96
    public $name;
97

98
    /**
99
     * Next action.
100
     *
101
     * @var string
102
     */
103
    public $action;
104

105
    /**
106
     * ActionForward.
107
     *
108
     * @param array $forward forward
109
     */
110
    public function __construct($forward)
111
    {
112
        parent::__construct();
6✔
113
        $this->setPath($forward[_PATH]);
6✔
114
        $this->setHeader($forward[_HEADER]);
6✔
115
        callPlugin(
6✔
116
            'dispatcher',
6✔
117
            'notify',
6✔
118
            [
6✔
119
                Event\WILL_SET_VIEW,
6✔
120
            ]
6✔
121
        );
6✔
122
        $this->_setType($forward[_TYPE]);
6✔
123

124
        // assign value
125
        $this->action = $forward[_ACTION];
6✔
126
    }
127

128
    /**
129
     * Destruct.
130
     *
131
     * @return void
132
     */
133
    public function __destruct()
134
    {
135
        dev(
6✔
136
            function () {
6✔
137
                return [
×
138
                    'name'     => $this->name,
×
139
                    'action'   => $this->action,
×
140
                    'header'   => $this->getHeader(),
×
141
                    'type'     => $this->getType(),
×
142
                    'path'     => $this->getPath(),
×
143
                    'viewData' => $this->get(),
×
144
                ];
×
145
            },
6✔
146
            'view'
6✔
147
        );
6✔
148
    }
149

150
    /**
151
     * Set header.
152
     *
153
     * @param array $v value
154
     *
155
     * @return mixed
156
     */
157
    public function cleanHeader($v = [])
158
    {
159
        return clean($this->_header, $v);
×
160
    }
161

162
    /**
163
     * Get header.
164
     *
165
     * @return array header
166
     */
167
    public function &getHeader()
168
    {
169
        return $this->_header;
2✔
170
    }
171

172
    /**
173
     * Set header.
174
     *
175
     * @param array $v value
176
     *
177
     * @return mixed
178
     */
179
    public function setHeader($v)
180
    {
181
        if (empty($v)) {
6✔
182
            return;
6✔
183
        }
184

185
        return set($this->_header, toArray($v));
×
186
    }
187

188
    /**
189
     * Process Header.
190
     *
191
     * @return void
192
     */
193
    private function _processHeader()
194
    {
195
        $headers = $this->getHeader();
2✔
196
        if (empty($headers)) {
2✔
197
            return;
2✔
198
        }
199
        callPlugin(
×
200
            option('get', _ROUTER),
×
201
            'processHeader',
×
202
            [
×
203
                $headers,
×
204
            ]
×
205
        );
×
206
        $this->cleanHeader();
×
207
    }
208

209
    /**
210
     * Set type.
211
     *
212
     * @param string $type type
213
     *
214
     * @return void
215
     */
216
    private function _setType($type = null)
217
    {
218
        if (self::VIEW === $type || 'redirect' === $type) {
6✔
219
            $c = plug('controller');
4✔
220

221
            /*
222
             * Get custom engine from .env.
223
             *
224
             * syntax: view_engine_[app-name]
225
             *
226
             * such as.
227
             * view_engine_sitemap=xml
228
             */
229
            $appViewEngine = value(
4✔
230
                $c,
4✔
231
                [
4✔
232
                    self::VIEW,
4✔
233
                    'engine',
4✔
234
                    $c->getApp(),
4✔
235
                ]
4✔
236
            );
4✔
237
            if ($appViewEngine) {
4✔
238
                $c[_VIEW_ENGINE] = $appViewEngine;
×
239
            }
240
            $this->_view = plug(self::VIEW);
4✔
241
        }
242
        $this->_type = $type;
6✔
243
    }
244

245
    /**
246
     * Get type.
247
     *
248
     * @return string
249
     */
250
    public function getType()
251
    {
252
        return $this->_type;
3✔
253
    }
254

255
    /**
256
     * Get the path of the ActionForward.
257
     *
258
     * @param bool $bMerge merge or not
259
     *
260
     * @return string
261
     */
262
    public function getPath($bMerge = false)
263
    {
264
        $path = $this->_path;
3✔
265
        if ($bMerge) {
3✔
266
            $path = $this->buildCommand(
×
267
                $path,
×
268
                $this->get()
×
269
            );
×
270
        }
271

272
        return $path;
3✔
273
    }
274

275
    /**
276
     * Build URL from parse_url.
277
     *
278
     * @param string $url    default url
279
     * @param array  $params url overwrite params
280
     *
281
     * @return string
282
     */
283
    public function buildCommand($url, $params)
284
    {
285
        return callPlugin(
×
286
            option('get', _ROUTER),
×
287
            __FUNCTION__,
×
288
            [
×
289
                $url,
×
290
                $params,
×
291
            ]
×
292
        );
×
293
    }
294

295
    /**
296
     * Set the path of the ActionForward.
297
     *
298
     * @param string $path path
299
     *
300
     * @return void
301
     */
302
    public function setPath($path)
303
    {
304
        $this->_path = $path;
6✔
305
    }
306

307
    /**
308
     * Append.
309
     *
310
     * @param array $arr merge array
311
     *
312
     * @return array
313
     */
314
    public function append(array $arr)
315
    {
316
        if (self::VIEW === $this->_type) {
1✔
317
            return $this->_view->append($arr);
1✔
318
        } else {
319
            return $this[[]] = $arr;
×
320
        }
321
    }
322

323
    /**
324
     * Set ActionFored key and value.
325
     *
326
     * @param string $k key
327
     * @param string $v value
328
     *
329
     * @return bool
330
     */
331
    public function set($k, $v = null)
332
    {
333
        if (self::VIEW === $this->_type) {
4✔
334
            return $this->_view->set($k, $v);
3✔
335
        } else {
336
            return set($this->_body, $k, $v);
1✔
337
        }
338
    }
339

340
    /**
341
     * Get.
342
     *
343
     * @param mixed $k       key
344
     * @param mixed $default default
345
     *
346
     * @return mixed
347
     */
348
    public function get($k = null, $default = null)
349
    {
350
        if (self::VIEW === $this->_type) {
2✔
351
            return $this->_view->get($k, $default);
1✔
352
        } else {
353
            return get($this->_body, $k, $default);
1✔
354
        }
355
    }
356

357
    /**
358
     * Set client redirect.
359
     *
360
     * @param string $type clinet redirect type [href|replace|false]
361
     *
362
     * @return string isClientRedirect
363
     */
364
    public function setClientRedirect($type)
365
    {
366
        switch ($type) {
367
            case 'href':
×
368
            case 'replace':
×
369
                $this->_isClientRedirect = $type;
×
370
                break;
×
371
            default:
372
                $this->_isClientRedirect = false;
×
373
        }
374

375
        return $this->_isClientRedirect;
×
376
    }
377

378
    /**
379
     * Process View.
380
     *
381
     * @return $this
382
     */
383
    private function _processView()
384
    {
385
        /**
386
         * Do you face a wrong view object?
387
         *
388
         * !! Important !!
389
         *
390
         * Please remeber if you need change view,
391
         * - Option 1. Must change it before create ActionForward instance.
392
         * - Option 2. Or just create a new one.
393
         */
394
        $view = $this->_view;
2✔
395

396
        if ('redirect' !== $this->_type) {
2✔
397
            $path = $this->getPath();
2✔
398
            if (is_null($path)) {
2✔
399
                $path = $this->name;
×
400
            }
401
            $view->setThemePath($path);
2✔
402
        } else {
403
            $view->prepend(get($this));
×
404
        }
405

406
        if (exists(_RUN_APP, 'plugin')) {
2✔
407
            $run = plug(_RUN_APP);
×
408
            $keepForward = $run[_FORWARD];
×
409
            if (!is_null($keepForward) && count($keepForward)) {
×
410
                $view->prepend(get($keepForward));
×
411
                $run[_FORWARD] = new HashMap();
×
412
                $keepForward = null;
×
413
                unset($keepForward);
×
414
            }
415
        }
416

417
        callPlugin(
2✔
418
            'dispatcher',
2✔
419
            'notify',
2✔
420
            [
2✔
421
                Event\WILL_PROCESS_HEADER, true,
2✔
422
            ]
2✔
423
        );
2✔
424
        $c = plug('controller');
2✔
425
        $appTemplateDir = value(
2✔
426
            $c,
2✔
427
            [
2✔
428
                'template',
2✔
429
                'dir',
2✔
430
                $c->getApp(),
2✔
431
            ],
2✔
432
            $c[_TEMPLATE_DIR]
2✔
433
        );
2✔
434
        // Put after WILL_PROCESS_HEADER event for get all
435
        // view_config_helper values.
436
        $view->setThemeFolder(
2✔
437
            $appTemplateDir
2✔
438
        );
2✔
439

440
        // <!-- Get header after $view->setThemeFolder.
441
        if (isset($view['headers'])) {
2✔
442
            $this->setHeader($view['headers']);
×
443
            unset($view['headers']);
×
444
        }
445
        $this->_processHeader();
2✔
446
        // -->
447

448
        callPlugin(
2✔
449
            'dispatcher',
2✔
450
            'notify',
2✔
451
            [
2✔
452
                Event\WILL_PROCESS_VIEW, true,
2✔
453
            ]
2✔
454
        );
2✔
455

456
        return $view->process();
2✔
457
    }
458

459
    /**
460
     * Execute ActionForward.
461
     *
462
     * @return mixed
463
     */
464
    public function go()
465
    {
466
        switch ($this->getType()) {
3✔
467
            case 'redirect':
3✔
468
                $this->_processHeader();
×
469
                $path = $this->getPath(true);
×
470
                if (!empty($this->_isClientRedirect)) {
×
471
                    $this['clientRedirectTo'] = $path;
×
472
                    $this['clientRedirectType'] = $this->_isClientRedirect;
×
473
                }
474
                callPlugin(
×
475
                    option('get', _ROUTER),
×
476
                    'go',
×
477
                    [
×
478
                        $path,
×
479
                        $this->_isClientRedirect,
×
480
                    ]
×
481
                );
×
482
            case self::VIEW:
483
                return $this->_processView();
2✔
484
            case 'action':
1✔
485
            default:
486
                if (exists(_RUN_APP, 'plugin')) {
1✔
487
                    $run = plug(_RUN_APP);
1✔
488
                    $keepForward = $run[_FORWARD];
1✔
489
                    if (is_null($keepForward)) {
1✔
490
                        $keepForward = new HashMap();
1✔
491
                        $run[_FORWARD] = $keepForward;
1✔
492
                    }
493
                    $keepForward[[]] = $this->get();
1✔
494
                }
495

496
                return $this;
1✔
497
        }
498
    }
499
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc