Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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 core\task;
18
 
19
/**
20
 * This file defines a trait to assist with unit tests in tasks.
21
 *
22
 * @package    core
23
 * @copyright  2024 Huong Nguyen <huongnv13@gmail.com>
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
trait task_trait {
27
 
28
    /**
29
     * Helper to execute a particular task.
30
     *
31
     * @param string $taskclass The task class.
32
     */
33
    protected function execute_task(string $taskclass): void {
34
        // Run the scheduled task.
35
        $this->start_output_buffering();
36
        $task = manager::get_scheduled_task($taskclass);
37
        $task->execute();
38
        $this->stop_output_buffering();
39
    }
40
 
41
    /**
42
     * Helper to start output buffering.
43
     */
44
    protected function start_output_buffering(): void {
45
        ob_start();
46
    }
47
 
48
    /**
49
     * Helper to stop output buffering.
50
     *
51
     * @return string|null The output buffer contents or null if output buffering is not active.
52
     */
53
    protected function stop_output_buffering(): ?string {
54
        $output = ob_get_contents();
55
        ob_end_clean();
56
 
57
        return $output;
58
    }
59
}