Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

HTTPHandler.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Bundle\EzPublishCoreBundle\URLChecker\Handler;
8
9 use eZ\Publish\API\Repository\Values\URL\URL;
10 use Symfony\Component\OptionsResolver\OptionsResolver;
11
12 class HTTPHandler extends AbstractURLHandler
13 {
19 public function validate(array $urls)
20 {
21 if (!$this->options['enabled']) {
22 return;
23 }
24
25 $master = curl_multi_init();
26 $handlers = [];
27
28 // Batch size can't be larger then number of urls
29 $batchSize = min(count($urls), $this->options['batch_size']);
30 for ($i = 0; $i < $batchSize; ++$i) {
31 curl_multi_add_handle($master, $this->createCurlHandlerForUrl($urls[$i], $handlers));
32 }
33
34 do {
35 while (($execrun = curl_multi_exec($master, $running)) == CURLM_CALL_MULTI_PERFORM);
36
37 if ($execrun != CURLM_OK) {
38 break;
39 }
40
41 while ($done = curl_multi_info_read($master)) {
42 $handler = $done['handle'];
43
44 $this->doValidate($handlers[(int)$handler], $handler);
45
46 if ($i < count($urls)) {
47 curl_multi_add_handle($master, $this->createCurlHandlerForUrl($urls[$i], $handlers));
48 ++$i;
49 }
50
51 curl_multi_remove_handle($master, $handler);
52 curl_close($handler);
53 }
54 } while ($running);
55
56 curl_multi_close($master);
57 }
58
62 protected function getOptionsResolver()
63 {
64 $resolver = new OptionsResolver();
65 $resolver->setDefaults([
66 'enabled' => true,
67 'timeout' => 10,
68 'connection_timeout' => 5,
69 'batch_size' => 10,
70 'ignore_certificate' => false,
71 ]);
72
73 $resolver->setAllowedTypes('enabled', 'bool');
74 $resolver->setAllowedTypes('timeout', 'int');
75 $resolver->setAllowedTypes('connection_timeout', 'int');
76 $resolver->setAllowedTypes('batch_size', 'int');
77 $resolver->setAllowedTypes('ignore_certificate', 'bool');
78
79 return $resolver;
80 }
81
90 private function createCurlHandlerForUrl(URL $url, array &$handlers)
91 {
92 $handler = curl_init();
93
94 curl_setopt_array($handler, [
95 CURLOPT_URL => $url->url,
96 CURLOPT_RETURNTRANSFER => false,
97 CURLOPT_FOLLOWLOCATION => true,
98 CURLOPT_CONNECTTIMEOUT => $this->options['connection_timeout'],
99 CURLOPT_TIMEOUT => $this->options['timeout'],
100 CURLOPT_FAILONERROR => true,
101 CURLOPT_NOBODY => true,
102 ]);
103
104 if ($this->options['ignore_certificate']) {
105 curl_setopt_array($handler, [
106 CURLOPT_SSL_VERIFYPEER => false,
107 CURLOPT_SSL_VERIFYHOST => false,
108 ]);
109 }
110
111 $handlers[(int)$handler] = $url;
112
113 return $handler;
114 }
115
122 private function doValidate(URL $url, $handler)
123 {
124 $this->setUrlStatus($url, $this->isSuccessful(curl_getinfo($handler, CURLINFO_HTTP_CODE)));
125 }
126
127 private function isSuccessful($statusCode)
128 {
129 return $statusCode >= 200 && $statusCode < 300;
130 }
131 }
eZ\Bundle\EzPublishCoreBundle\URLChecker\Handler\AbstractURLHandler
Definition: AbstractURLHandler.php:18
eZ\Bundle\EzPublishCoreBundle\URLChecker\Handler\AbstractURLHandler\setUrlStatus
setUrlStatus(URL $url, $isValid)
Sets URL status.
Definition: AbstractURLHandler.php:59
eZ\Bundle\EzPublishCoreBundle\URLChecker\Handler\HTTPHandler
Definition: HTTPHandler.php:13
eZ\Bundle\EzPublishCoreBundle\URLChecker\Handler\HTTPHandler\validate
validate(array $urls)
{Validates given list of URLs.}
Definition: HTTPHandler.php:19
eZ\Bundle\EzPublishCoreBundle\URLChecker\Handler\HTTPHandler\isSuccessful
isSuccessful($statusCode)
Definition: HTTPHandler.php:127
eZ\Bundle\EzPublishCoreBundle\URLChecker\Handler\HTTPHandler\getOptionsResolver
getOptionsResolver()
{Returns options resolver.\Symfony\Component\OptionsResolver\OptionsResolver}
Definition: HTTPHandler.php:62
eZ\Bundle\EzPublishCoreBundle\URLChecker\Handler\HTTPHandler\doValidate
doValidate(URL $url, $handler)
Validate single response.
Definition: HTTPHandler.php:122
eZ\Bundle\EzPublishCoreBundle\URLChecker\Handler\HTTPHandler\createCurlHandlerForUrl
createCurlHandlerForUrl(URL $url, array &$handlers)
Initialize and return a cURL session for given URL.
Definition: HTTPHandler.php:90
eZ\Publish\API\Repository\Values\URL\URL
Definition: API/Repository/Values/URL/URL.php:12
eZ\Bundle\EzPublishCoreBundle\URLChecker\Handler
Definition: AbstractURLHandler.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.