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

RemoteProvider.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Bundle\EzPublishCoreBundle\Imagine\PlaceholderProvider;
8
9 use eZ\Bundle\EzPublishCoreBundle\Imagine\PlaceholderProvider;
10 use eZ\Publish\Core\FieldType\Image\Value as ImageValue;
11 use RuntimeException;
12 use Symfony\Component\OptionsResolver\OptionsResolver;
13
17 class RemoteProvider implements PlaceholderProvider
18 {
22 public function getPlaceholder(ImageValue $value, array $options = []): string
23 {
24 $options = $this->resolveOptions($options);
25
26 $path = $this->getTemporaryPath();
27 $placeholderUrl = $this->getPlaceholderUrl($options['url_pattern'], $value);
28
29 try {
30 $handler = curl_init();
31
32 curl_setopt_array($handler, [
33 CURLOPT_URL => $placeholderUrl,
34 CURLOPT_FILE => fopen($path, 'wb'),
35 CURLOPT_TIMEOUT => $options['timeout'],
36 CURLOPT_FAILONERROR => true,
37 ]);
38
39 if (curl_exec($handler) === false) {
40 throw new RuntimeException("Unable to download placeholder for {$value->id} ($placeholderUrl): " . curl_error($handler));
41 }
42 } finally {
43 curl_close($handler);
44 }
45
46 return $path;
47 }
48
49 private function getPlaceholderUrl(string $urlPattern, ImageValue $value): string
50 {
51 return strtr($urlPattern, [
52 '%id%' => $value->id,
53 '%width%' => $value->width,
54 '%height%' => $value->height,
55 ]);
56 }
57
58 private function getTemporaryPath(): string
59 {
60 return stream_get_meta_data(tmpfile())['uri'];
61 }
62
63 private function resolveOptions(array $options): array
64 {
65 $resolver = new OptionsResolver();
66 $resolver->setDefaults([
67 'url_pattern' => '',
68 'timeout' => 5,
69 ]);
70 $resolver->setAllowedTypes('url_pattern', 'string');
71 $resolver->setAllowedTypes('timeout', 'int');
72
73 return $resolver->resolve($options);
74 }
75 }
eZ\Bundle\EzPublishCoreBundle\Imagine\PlaceholderProvider\RemoteProvider
Remote placeholder provider e.g.
Definition: RemoteProvider.php:18
eZ\Bundle\EzPublishCoreBundle\Imagine\PlaceholderProvider\RemoteProvider\getPlaceholderUrl
getPlaceholderUrl(string $urlPattern, ImageValue $value)
Definition: RemoteProvider.php:49
eZ\Bundle\EzPublishCoreBundle\Imagine\PlaceholderProvider\RemoteProvider\getTemporaryPath
getTemporaryPath()
Definition: RemoteProvider.php:58
eZ\Bundle\EzPublishCoreBundle\Imagine\PlaceholderProvider\RemoteProvider\resolveOptions
resolveOptions(array $options)
Definition: RemoteProvider.php:63
eZ\Bundle\EzPublishCoreBundle\Imagine\PlaceholderProvider\RemoteProvider\getPlaceholder
getPlaceholder(ImageValue $value, array $options=[])
{Provides a placeholder image path for a given Image FieldType value.string Path to placeholder}
Definition: RemoteProvider.php:22
eZ\Publish\API\Repository\Values\Content\Query\Criterion\Value
Struct that stores extra value information for a Criterion object.
Definition: API/Repository/Values/Content/Query/Criterion/Value.php:13
eZ\Bundle\EzPublishCoreBundle\Imagine\PlaceholderProvider
Definition: PlaceholderProvider.php:12
eZ\Bundle\EzPublishCoreBundle\Imagine\PlaceholderProvider
Definition: GenericProvider.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.