1441 |
ariadna |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace tool_generator\output;
|
|
|
18 |
use templatable;
|
|
|
19 |
use tool_generator\local\testscenario\runner;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Class stepsinformation
|
|
|
23 |
*
|
|
|
24 |
* @package tool_generator
|
|
|
25 |
* @copyright 2024 Ferran Recio <ferran@moodle.com>
|
|
|
26 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
27 |
*/
|
|
|
28 |
class stepsinformation implements templatable, \renderable {
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Constructor.
|
|
|
32 |
*
|
|
|
33 |
* @param runner $runner the scenario runner
|
|
|
34 |
*/
|
|
|
35 |
public function __construct(
|
|
|
36 |
/** @var runner the runner instance. */
|
|
|
37 |
public runner $runner
|
|
|
38 |
) {
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Export this data so it can be used as the context for a mustache template (core/inplace_editable).
|
|
|
43 |
*
|
|
|
44 |
* @param \renderer_base $output typically, the renderer that's calling this function
|
|
|
45 |
* @return \stdClass data context for a mustache template
|
|
|
46 |
*/
|
|
|
47 |
public function export_for_template(\renderer_base $output): \stdClass {
|
|
|
48 |
$steps = [];
|
|
|
49 |
$validsteps = $this->runner->get_valid_steps();
|
|
|
50 |
foreach ($validsteps as $step) {
|
|
|
51 |
$steps[] = (object)[
|
|
|
52 |
'given' => $this->highlight_params($step->given),
|
|
|
53 |
'example' => $step->example,
|
|
|
54 |
];
|
|
|
55 |
}
|
|
|
56 |
return (object) [
|
|
|
57 |
'steps' => $steps,
|
|
|
58 |
];
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* Highlight the parameters in a step.
|
|
|
63 |
*
|
|
|
64 |
* @param string $step the step to highlight
|
|
|
65 |
* @return string the step with the parameters highlighted
|
|
|
66 |
*/
|
|
|
67 |
private function highlight_params(string $step): string {
|
|
|
68 |
$step = htmlentities($step);
|
|
|
69 |
|
|
|
70 |
// Highlight params starting with ":".
|
|
|
71 |
$step = preg_replace('/:(\w+)/', '<strong>$0</strong>', $step);
|
|
|
72 |
|
|
|
73 |
// Highlight params enclosed in "(?P<...>)".
|
|
|
74 |
$step = preg_replace('/\(\?P<(\w+)>((?:[^"]|\\")*)\)/', '<strong>$0</strong>', $step);
|
|
|
75 |
|
|
|
76 |
return $step;
|
|
|
77 |
}
|
|
|
78 |
}
|