Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
RequestTimeout
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
4 / 4
6
100.00% covered (success)
100.00%
1 / 1
 singleton
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 setInstance
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 factory
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 createCriticalSectionProvider
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 enterCriticalSection
n/a
0 / 0
n/a
0 / 0
0
 exitCriticalSection
n/a
0 / 0
n/a
0 / 0
0
 setWallTimeLimit
n/a
0 / 0
n/a
0 / 0
0
 getWallTimeRemaining
n/a
0 / 0
n/a
0 / 0
0
 getWallTimeLimit
n/a
0 / 0
n/a
0 / 0
0
1<?php
2
3namespace Wikimedia\RequestTimeout;
4
5use Wikimedia\RequestTimeout\Detail\BasicRequestTimeout;
6use Wikimedia\RequestTimeout\Detail\ExcimerRequestTimeout;
7
8abstract class RequestTimeout {
9    /** @var RequestTimeout|null The singleton instance */
10    private static $instance;
11
12    /**
13     * Get a singleton instance of RequestTimeout. If the Excimer extension is
14     * loaded, this will return a fully functional implementation. If it is
15     * not loaded, a fallback implementation will be returned.
16     *
17     * @return RequestTimeout
18     */
19    public static function singleton() {
20        if ( !self::$instance ) {
21            self::$instance = self::factory();
22        }
23        return self::$instance;
24    }
25
26    /**
27     * Set the instance to be returned by singleton(), or null to clear the
28     * instance so that it will be recreated.
29     *
30     * @param RequestTimeout|null $instance
31     */
32    public static function setInstance( ?RequestTimeout $instance ) {
33        self::$instance = $instance;
34    }
35
36    /**
37     * Create a new instance of RequestTimeout. If the Excimer extension is
38     * loaded, this will return a fully functional implementation. If it is
39     * not loaded, a fallback implementation will be returned.
40     *
41     * @return RequestTimeout
42     */
43    public static function factory() {
44        if ( extension_loaded( 'excimer' ) ) {
45            return new ExcimerRequestTimeout;
46        } else {
47            // @codeCoverageIgnoreStart
48            return new BasicRequestTimeout;
49            // @codeCoverageIgnoreEnd
50        }
51    }
52
53    /**
54     * Create a CriticalSectionProvider with the specified configuration.
55     *
56     * @param float $emergencyLimit The emergency timeout in seconds
57     * @param callable|null $emergencyCallback A callback to call when the
58     *   emergency timeout expires. If null, an exception will be thrown.
59     * @param callable|null $implicitExitCallback A callback to call if a
60     *   critical section scope is exited implicitly, rather than by calling
61     *   exit().
62     * @return CriticalSectionProvider
63     */
64    public function createCriticalSectionProvider(
65        $emergencyLimit, $emergencyCallback = null, $implicitExitCallback = null
66    ) {
67        return new CriticalSectionProvider( $this, $emergencyLimit,
68            $emergencyCallback, $implicitExitCallback );
69    }
70
71    /**
72     * @internal For use by CriticalSectionProvider
73     *
74     * @param string $name
75     * @param float $emergencyLimit
76     * @param callable|null $emergencyCallback
77     * @return int
78     */
79    abstract public function enterCriticalSection( $name, $emergencyLimit, $emergencyCallback );
80
81    /**
82     * @internal For use by CriticalSectionProvider
83     *
84     * @throws TimeoutException
85     * @param int $id
86     */
87    abstract public function exitCriticalSection( $id );
88
89    /**
90     * Set the wall time limit. If excimer is available, an exception will be
91     * thrown after the specified number of seconds.
92     *
93     * If excimer is not available, this falls back to set_time_limit(), which
94     * causes a fatal error after the CPU time (not wall clock time) exceeds
95     * the given number of seconds, which is rounded to an integer.
96     *
97     * A time limit of INF or 0 is interpreted as no limit.
98     *
99     * @param float $limit The limit in seconds
100     */
101    abstract public function setWallTimeLimit( $limit );
102
103    /**
104     * Get the amount of time remaining of the limit. If there is no limit, INF
105     * will be returned.
106     *
107     * @return float
108     */
109    abstract public function getWallTimeRemaining();
110
111    /**
112     * Get the current wall time limit, or INF if there is no limit
113     *
114     * @return float
115     */
116    abstract public function getWallTimeLimit();
117}