|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BitBag\SyliusCmsPlugin\Controller; |
|
6
|
|
|
|
|
7
|
|
|
use BitBag\SyliusCmsPlugin\Entity\MediaInterface; |
|
8
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration; |
|
9
|
|
|
use Sylius\Component\Resource\Model\ResourceInterface; |
|
10
|
|
|
use Symfony\Component\Form\FormInterface; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\File\File; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
13
|
|
|
use Webmozart\Assert\Assert; |
|
14
|
|
|
|
|
15
|
|
|
trait ResourceDataProcessingTrait |
|
16
|
|
|
{ |
|
17
|
|
|
private function getResourceInterface(Request $request): object |
|
18
|
|
|
{ |
|
19
|
|
|
return null !== $request->get('id') && $this->repository->find($request->get('id')) ? |
|
20
|
|
|
$this->repository->find($request->get('id')) : |
|
21
|
|
|
$this->factory->createNew(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
private function getFormForResource(RequestConfiguration $configuration, ResourceInterface $resource): FormInterface |
|
25
|
|
|
{ |
|
26
|
|
|
return $this->resourceFormFactory->create($configuration, $resource); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
private function getRequestConfiguration(Request $request): RequestConfiguration |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->requestConfigurationFactory->create($this->metadata, $request); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
private function setResourceMediaPath(MediaInterface $media): void |
|
35
|
|
|
{ |
|
36
|
|
|
if (null === $media->getPath()) { |
|
37
|
|
|
return; |
|
38
|
|
|
} |
|
39
|
|
|
Assert::notNull($media->getMimeType()); |
|
40
|
|
|
Assert::notNull($media->getType()); |
|
41
|
|
|
if (1 === preg_match("/image\//", $media->getMimeType()) && 'image' === $media->getType()) { |
|
|
|
|
|
|
42
|
|
|
$this->setPathForImageMediaType($media); |
|
43
|
|
|
} else { |
|
44
|
|
|
$this->setPathForNonImageMediaType($media); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private function setPathForImageMediaType(MediaInterface $media): void |
|
49
|
|
|
{ |
|
50
|
|
|
Assert::string($media->getPath()); |
|
51
|
|
|
if (!$this->cacheManager->isStored($media->getPath(), $this::FILTER)) { |
|
|
|
|
|
|
52
|
|
|
$this->cacheManager->store($this->dataManager->find($this::FILTER, $media->getPath()), $media->getPath(), $this::FILTER); |
|
53
|
|
|
} |
|
54
|
|
|
$resolvedPath = $this->cacheManager->resolve($media->getPath(), $this::FILTER); |
|
55
|
|
|
$fileContents = file_get_contents($resolvedPath); |
|
56
|
|
|
Assert::string($fileContents); |
|
57
|
|
|
$this->setFileContentsAsMediaPath($media, $fileContents); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
private function setPathForNonImageMediaType(MediaInterface $media): void |
|
61
|
|
|
{ |
|
62
|
|
|
$mediaPath = $this->getMediaPathIfNotNull($media); |
|
63
|
|
|
$file = new File($mediaPath); |
|
64
|
|
|
$fileContents = file_get_contents($file->getPathname()); |
|
65
|
|
|
Assert::string($fileContents); |
|
66
|
|
|
$this->setFileContentsAsMediaPath($media, $fileContents); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
private function setFileContentsAsMediaPath(MediaInterface $media, string $fileContents): void |
|
70
|
|
|
{ |
|
71
|
|
|
$base64Content = base64_encode($fileContents); |
|
72
|
|
|
$path = 'data:' . $media->getMimeType() . ';base64, ' . $base64Content; |
|
73
|
|
|
$media->setPath($path); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
private function getMediaPathIfNotNull(MediaInterface $media): string |
|
77
|
|
|
{ |
|
78
|
|
|
Assert::string($media->getPath()); |
|
79
|
|
|
Assert::string($this->getParameter('sylius_core.public_dir')); |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
return $this->getParameter('sylius_core.public_dir') . $media->getPath(); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|