|
1
|
|
|
<?php |
|
2
|
|
|
namespace WebPConvert\Serve; |
|
3
|
|
|
|
|
4
|
|
|
use WebPConvert\Options\Options; |
|
5
|
|
|
use WebPConvert\Options\StringOption; |
|
6
|
|
|
use WebPConvert\Serve\Header; |
|
7
|
|
|
use WebPConvert\Serve\Report; |
|
8
|
|
|
use WebPConvert\Serve\ServeConvertedWeb; |
|
9
|
|
|
use WebPConvert\Serve\Exceptions\ServeFailedException; |
|
10
|
|
|
use WebPConvert\Exceptions\WebPConvertException; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Serve a converted webp image and handle errors. |
|
14
|
|
|
* |
|
15
|
|
|
* @package WebPConvert |
|
16
|
|
|
* @author Bjørn Rosell <[email protected]> |
|
17
|
|
|
* @since Class available since Release 2.0.0 |
|
18
|
|
|
*/ |
|
19
|
|
|
class ServeConvertedWebPWithErrorHandling |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Process options. |
|
24
|
|
|
* |
|
25
|
|
|
* @throws \WebPConvert\Options\Exceptions\InvalidOptionTypeException If the type of an option is invalid |
|
26
|
|
|
* @throws \WebPConvert\Options\Exceptions\InvalidOptionValueException If the value of an option is invalid |
|
27
|
|
|
* @param array $options |
|
28
|
|
|
*/ |
|
29
|
|
|
private static function processOptions($options) |
|
30
|
|
|
{ |
|
31
|
|
|
$options2 = new Options(); |
|
32
|
|
|
$options2->addOptions( |
|
33
|
|
|
new StringOption('fail', 'original', ['original', '404', 'throw', 'report']), |
|
34
|
|
|
new StringOption('fail-when-fail-fails', 'throw', ['original', '404', 'throw', 'report']) |
|
35
|
|
|
); |
|
36
|
|
|
foreach ($options as $optionId => $optionValue) { |
|
37
|
|
|
$options2->setOrCreateOption($optionId, $optionValue); |
|
38
|
|
|
} |
|
39
|
|
|
$options2->check(); |
|
40
|
|
|
return $options2->getOptions(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Add headers for preventing caching. |
|
45
|
|
|
* |
|
46
|
|
|
* @return void |
|
47
|
|
|
*/ |
|
48
|
|
|
private static function addHeadersPreventingCaching() |
|
49
|
|
|
{ |
|
50
|
|
|
Header::setHeader("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); |
|
51
|
|
|
Header::addHeader("Cache-Control: post-check=0, pre-check=0"); |
|
52
|
|
|
Header::setHeader("Pragma: no-cache"); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Perform fail action. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $fail Action to perform (original | 404 | report) |
|
59
|
|
|
* @param string $failIfFailFails Action to perform if $fail action fails |
|
60
|
|
|
* @param string $source path to source file |
|
61
|
|
|
* @param string $destination path to destination |
|
62
|
|
|
* @param array $options (optional) options for serving/converting |
|
63
|
|
|
* @param \Exception $e exception that was thrown when trying to serve |
|
64
|
|
|
* @param string $serveClass (optional) Full class name to a class that has a serveOriginal() method |
|
65
|
|
|
* @return void |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function performFailAction($fail, $failIfFailFails, $source, $destination, $options, $e, $serveClass) |
|
68
|
|
|
{ |
|
69
|
|
|
self::addHeadersPreventingCaching(); |
|
70
|
|
|
|
|
71
|
|
|
//Header::addLogHeader('Failure'); |
|
72
|
|
|
Header::addLogHeader('Performing fail action: ' . $fail); |
|
73
|
|
|
|
|
74
|
|
|
switch ($fail) { |
|
75
|
|
|
case 'original': |
|
76
|
|
|
try { |
|
77
|
|
|
//ServeConvertedWebP::serveOriginal($source, $options); |
|
78
|
|
|
call_user_func($serveClass . '::serveOriginal', $source, $options); |
|
79
|
|
|
} catch (\Exception $e) { |
|
80
|
|
|
self::performFailAction($failIfFailFails, '404', $source, $destination, $options, $e, $serveClass); |
|
81
|
|
|
} |
|
82
|
|
|
break; |
|
83
|
|
|
|
|
84
|
|
|
case '404': |
|
85
|
|
|
$protocol = isset($_SERVER["SERVER_PROTOCOL"]) ? $_SERVER["SERVER_PROTOCOL"] : 'HTTP/1.0'; |
|
86
|
|
|
Header::setHeader($protocol . " 404 Not Found"); |
|
87
|
|
|
break; |
|
88
|
|
|
|
|
89
|
|
|
case 'report': |
|
90
|
|
|
$options['show-report'] = true; |
|
91
|
|
|
Report::convertAndReport($source, $destination, $options); |
|
92
|
|
|
break; |
|
93
|
|
|
|
|
94
|
|
|
case 'throw': |
|
95
|
|
|
throw $e; |
|
96
|
|
|
break; |
|
97
|
|
|
|
|
98
|
|
|
case 'report-as-image': |
|
99
|
|
|
// TODO: Implement or discard ? |
|
100
|
|
|
break; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Serve webp image and handle errors as specified in the 'fail' option. |
|
106
|
|
|
* |
|
107
|
|
|
* This method basically wraps ServeConvertedWebP:serve in order to provide exception handling. |
|
108
|
|
|
* The error handling is set with the 'fail' option and can be either '404', 'original' or 'report'. |
|
109
|
|
|
* If set to '404', errors results in 404 Not Found headers being issued. If set to 'original', an |
|
110
|
|
|
* error results in the original being served. |
|
111
|
|
|
* Look up the ServeConvertedWebP:serve method to learn more. |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $source path to source file |
|
114
|
|
|
* @param string $destination path to destination |
|
115
|
|
|
* @param array $options (optional) options for serving/converting |
|
116
|
|
|
* Supported options: |
|
117
|
|
|
* - 'fail' => (string) Action to take on failure (404 | original | report | throw). |
|
118
|
|
|
* "404" or "throw" is recommended for development and "original" is recommended for production. |
|
119
|
|
|
* Default: 'original'. |
|
120
|
|
|
* - 'fail-when-fail-fails' => (string) Action to take if fail action also fails. Default: '404'. |
|
121
|
|
|
* - All options supported by WebPConvert::convert() |
|
122
|
|
|
* - All options supported by ServeFile::serve() |
|
123
|
|
|
* - All options supported by DecideWhatToServe::decide) |
|
124
|
|
|
* @param \WebPConvert\Loggers\BaseLogger $logger (optional) |
|
125
|
|
|
* @param string $serveClass (optional) Full class name to a class that has a serve() method and a |
|
126
|
|
|
* serveOriginal() method |
|
127
|
|
|
* @return void |
|
128
|
|
|
*/ |
|
129
|
|
|
public static function serve( |
|
130
|
|
|
$source, |
|
131
|
|
|
$destination, |
|
132
|
|
|
$options = [], |
|
133
|
|
|
$logger = null, |
|
134
|
|
|
$serveClass = '\\WebPConvert\\Serve\\ServeConvertedWebP' |
|
135
|
|
|
) { |
|
136
|
|
|
$options = self::processOptions($options); |
|
137
|
|
|
|
|
138
|
|
|
try { |
|
139
|
|
|
//ServeConvertedWebP::serve($source, $destination, $options, $logger); |
|
140
|
|
|
call_user_func($serveClass . '::serve', $source, $destination, $options, $logger); |
|
141
|
|
|
} catch (\Exception $e) { |
|
142
|
|
|
if ($e instanceof \WebPConvert\Exceptions\WebPConvertException) { |
|
143
|
|
|
Header::addLogHeader($e->getShortMessage(), $logger); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
self::performFailAction( |
|
147
|
|
|
$options['fail'], |
|
148
|
|
|
$options['fail-when-fail-fails'], |
|
149
|
|
|
$source, |
|
150
|
|
|
$destination, |
|
151
|
|
|
$options, |
|
152
|
|
|
$e, |
|
153
|
|
|
$serveClass |
|
154
|
|
|
); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|