2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * PHP time limit management.
21 * @copyright 2013 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
28 * Utility class to manage PHP time limit.
30 class core_php_time_limit
{
32 * @var int Current end time of time limit (-1 if not set)
34 protected static $currentend = -1;
37 * @var array Data for unit testing
39 protected static $unittestdata = array();
42 * Sets the PHP time limit to a number of seconds from now.
44 * This function will always extend the time limit (in other words, if the time
45 * limit has already been set further in the future, it will do nothing).
47 * In order to support front-end servers which may time out silently if no
48 * output is displayed, you should ideally only call this function if you expect
49 * some output to be displayed at the same time. (I.e. if you call this function
50 * each time around a loop, also display some output each time around the loop,
51 * such as a progress bar update.)
53 * @param int $newlimit Limit in seconds from now (0 = infinite)
55 public static function raise($newlimit = 0) {
58 // Special behaviour in unit tests so that we can check the value.
60 self
::$unittestdata[] = $newlimit;
63 // If the time limit has already been set to 'infinite', ignore. Also do
64 // nothing in CLI scripts (including unit testing) which are set to
65 // infinite by default.
66 if (self
::$currentend === 0 || CLI_SCRIPT
) {
70 // Maximum time limit can be set in config. This can be useful for front-end
71 // server systems; if the front-end server has a timeout without receiving
72 // data, it's helpful to set this timeout lower to ensure that a suitable
74 if (!empty($CFG->maxtimelimit
)) {
75 $realtimeout = max(1, $CFG->maxtimelimit
);
76 if ($newlimit === 0) {
77 $newlimit = $realtimeout;
79 $newlimit = min($newlimit, $realtimeout);
83 // If new time limit is infinite, just set that.
84 if ($newlimit === 0) {
85 self
::$currentend = 0;
90 // Calculate time limits to make sure it's longer than previous.
92 $newend = $now +
$newlimit;
93 if (self
::$currentend !== -1 && self
::$currentend > $newend) {
94 // Existing time limit is already longer, so do nothing.
98 // Set time limit and update current value.
99 @set_time_limit
($newlimit);
100 self
::$currentend = $newend;
104 * For unit testing, returns an array of the values set during test.
106 * @return array Array of values set
108 public static function get_and_clear_unit_test_data() {
109 $data = self
::$unittestdata;
110 self
::$unittestdata = array();