Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ManagementPhpEvaluateLogic
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 5
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 startup
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
2
 validateImpl
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 executeImpl
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
12
 evalStatement
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\App\Models\Domain\Page\Management;
6
7use PeServer\App\Models\AuditLog;
8use PeServer\App\Models\Domain\Page\PageLogicBase;
9use PeServer\Core\Mvc\LogicCallMode;
10use PeServer\Core\Mvc\LogicParameter;
11use PeServer\Core\OutputBuffer;
12use PeServer\Core\Text;
13use Throwable;
14
15class ManagementPhpEvaluateLogic extends PageLogicBase
16{
17    public function __construct(LogicParameter $parameter)
18    {
19        parent::__construct($parameter);
20    }
21
22    protected function startup(LogicCallMode $callMode): void
23    {
24        $this->registerParameterKeys([
25            'php_statement',
26            'executed',
27            'execute_statement',
28            'result',
29            'result_is_string',
30            'output',
31            'output_is_string',
32        ], true);
33        $this->setValue('executed', false);
34        $this->setValue('execute_statement', null);
35        $this->setValue('result', null);
36        $this->setValue('result_is_string', false);
37        $this->setValue('output', null);
38        $this->setValue('output_is_string', false);
39    }
40
41    protected function validateImpl(LogicCallMode $callMode): void
42    {
43        if ($callMode === LogicCallMode::Initialize) {
44            return;
45        }
46
47        $this->validation('php_statement', function (string $key, string $value) {
48            $this->validator->isNotWhiteSpace($key, $value);
49        });
50    }
51
52    protected function executeImpl(LogicCallMode $callMode): void
53    {
54        if ($callMode === LogicCallMode::Initialize) {
55            return;
56        }
57
58        $phpStatement = $this->getRequest('php_statement');
59        $executeStatement = $phpStatement;
60
61        $result = null;
62
63        $this->setValue('executed', true);
64        $this->setValue('execute_statement', $executeStatement);
65
66        $output = Text::EMPTY;
67
68        try {
69            $output = OutputBuffer::get(function () use ($executeStatement, &$result) {
70                $result = $this->evalStatement($executeStatement);
71            });
72            $this->setValue('output', $output);
73            $this->setValue('output_is_string', !$output->hasNull());
74        } catch (Throwable $ex) {
75            $this->setValue('output', $ex);
76            $output = (string)$ex;
77        }
78        $this->setValue('result', $result);
79        $this->setValue('result_is_string', is_string($result));
80
81        $this->writeAuditLogCurrentUser(AuditLog::ADMINISTRATOR_EXECUTE_PHP, [
82            'php' => $executeStatement,
83            'output' => Text::dump($output),
84        ]);
85    }
86
87    /**
88     * PHP文を実行。
89     *
90     * @param string $statement
91     * @return mixed
92     * @SuppressWarnings(PHPMD.EvalExpression)
93     */
94    private function evalStatement(string $statement): mixed
95    {
96        return eval($statement);
97    }
98}