|
1
|
|
|
<?php |
|
2
|
|
|
namespace WebPConvert\Serve; |
|
3
|
|
|
|
|
4
|
|
|
//use WebPConvert\Serve\Report; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Decide what to serve based on options, file sizes and file modification dates. |
|
8
|
|
|
* |
|
9
|
|
|
* @package WebPConvert |
|
10
|
|
|
* @author Bjørn Rosell <[email protected]> |
|
11
|
|
|
* @since Class available since Release 2.0.0 |
|
12
|
|
|
*/ |
|
13
|
|
|
class DecideWhatToServe |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
public static $defaultOptions = [ |
|
17
|
|
|
'reconvert' => false, |
|
18
|
|
|
'serve-original' => false, |
|
19
|
|
|
'show-report' => false, |
|
20
|
|
|
]; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Decides what to serve. |
|
24
|
|
|
* |
|
25
|
|
|
* It both decides what to serve and supplies the reason behind. |
|
26
|
|
|
* The possible combinations are: |
|
27
|
|
|
* |
|
28
|
|
|
* - "destination" (serve existing converted image at the destination path) |
|
29
|
|
|
* - "no-reason-not-to" |
|
30
|
|
|
* - "source" |
|
31
|
|
|
* - "explicitly-told-to" |
|
32
|
|
|
* - "source-lighter" |
|
33
|
|
|
* - "fresh-conversion" (note: this may still fail) |
|
34
|
|
|
* - "explicitly-told-to" |
|
35
|
|
|
* - "source-modified" |
|
36
|
|
|
* - "no-existing" |
|
37
|
|
|
* - "fail" |
|
38
|
|
|
* - "Missing destination argument" |
|
39
|
|
|
* - "critical-fail" (a failure where the source file cannot be served) |
|
40
|
|
|
* - "missing-source-argument" |
|
41
|
|
|
* - "source-not-found" |
|
42
|
|
|
* - "report" |
|
43
|
|
|
* |
|
44
|
|
|
* @return array Three items: what to serve (id), why to serve (id) and suggested message |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function decide($source, $destination, $options) |
|
47
|
|
|
{ |
|
48
|
|
|
$options = array_merge(self::$defaultOptions, $options); |
|
49
|
|
|
|
|
50
|
|
|
if ($options['show-report']) { |
|
51
|
|
|
return ['report', 'explicitly-told-to', 'Serving report (explicitly told to)']; |
|
52
|
|
|
} |
|
53
|
|
|
if ($options['serve-original']) { |
|
54
|
|
|
return ['source', 'explicitly-told-to', 'Serving original image (was explicitly told to)']; |
|
55
|
|
|
} |
|
56
|
|
|
if ($options['reconvert']) { |
|
57
|
|
|
return ['fresh-conversion', 'explicitly-told-to', 'Serving fresh conversion (was explicitly told to)']; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if (@file_exists($destination)) { |
|
61
|
|
|
// Reconvert if existing conversion is older than the original |
|
62
|
|
|
$timestampSource = @filemtime($source); |
|
63
|
|
|
$timestampDestination = @filemtime($destination); |
|
64
|
|
|
if (($timestampSource !== false) && |
|
65
|
|
|
($timestampDestination !== false) && |
|
66
|
|
|
($timestampSource > $timestampDestination)) { |
|
67
|
|
|
return [ |
|
68
|
|
|
'fresh-conversion', |
|
69
|
|
|
'source-modified', |
|
70
|
|
|
'Serving fresh conversion ' . |
|
71
|
|
|
'(the existing conversion is discarded because original has been modified since then)' |
|
72
|
|
|
]; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// Serve source if it is smaller than destination |
|
76
|
|
|
$filesizeDestination = @filesize($destination); |
|
77
|
|
|
$filesizeSource = @filesize($source); |
|
78
|
|
|
if (($filesizeSource !== false) && |
|
79
|
|
|
($filesizeDestination !== false) && |
|
80
|
|
|
($filesizeDestination > $filesizeSource)) { |
|
81
|
|
|
return [ |
|
82
|
|
|
'source', |
|
83
|
|
|
'source-lighter', |
|
84
|
|
|
'Serving original image (it is smaller than the already converted)' |
|
85
|
|
|
]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
// Destination exists, and there is no reason left not to serve it |
|
89
|
|
|
return ['destination', 'no-reason-not-to', 'Serving existing conversion']; |
|
90
|
|
|
} else { |
|
91
|
|
|
return [ |
|
92
|
|
|
'fresh-conversion', |
|
93
|
|
|
'no-existing', |
|
94
|
|
|
'Serving fresh conversion (there were no existing conversion to serve)' |
|
95
|
|
|
]; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|