|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Sonata Project package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Sonata\MediaBundle\Command; |
|
15
|
|
|
|
|
16
|
|
|
use Sonata\MediaBundle\Provider\MediaProviderInterface; |
|
17
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
|
18
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
19
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
20
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
21
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* This command can be used to re-generate the thumbnails for all uploaded medias. |
|
25
|
|
|
* |
|
26
|
|
|
* Useful if you have existing media content and added new formats. |
|
27
|
|
|
*/ |
|
28
|
|
|
/** |
|
29
|
|
|
* @final since sonata-project/media-bundle 3.21.0 |
|
30
|
|
|
*/ |
|
31
|
|
|
class RefreshMetadataCommand extends BaseCommand |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var bool |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $quiet = false; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var OutputInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $output; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var InputInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
private $input; |
|
47
|
|
|
|
|
48
|
|
|
public function configure(): void |
|
49
|
|
|
{ |
|
50
|
|
|
$this->setName('sonata:media:refresh-metadata') |
|
51
|
|
|
->setDescription('Refresh meta information') |
|
52
|
|
|
->setDefinition( |
|
53
|
|
|
[ |
|
54
|
|
|
new InputArgument('providerName', InputArgument::OPTIONAL, 'The provider'), |
|
55
|
|
|
new InputArgument('context', InputArgument::OPTIONAL, 'The context'), |
|
56
|
|
|
] |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function execute(InputInterface $input, OutputInterface $output): int |
|
61
|
|
|
{ |
|
62
|
|
|
$this->quiet = $input->getOption('quiet'); |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
$this->input = $input; |
|
65
|
|
|
$this->output = $output; |
|
66
|
|
|
|
|
67
|
|
|
$provider = $this->getProvider(); |
|
68
|
|
|
$context = $this->getContext(); |
|
69
|
|
|
|
|
70
|
|
|
$medias = $this->getMediaManager()->findBy([ |
|
71
|
|
|
'providerName' => $provider->getName(), |
|
72
|
|
|
'context' => $context, |
|
73
|
|
|
]); |
|
74
|
|
|
|
|
75
|
|
|
$this->log(sprintf( |
|
76
|
|
|
'Loaded %s medias for generating thumbs (provider: %s, context: %s)', |
|
77
|
|
|
\count($medias), |
|
78
|
|
|
$provider->getName(), |
|
79
|
|
|
$context |
|
80
|
|
|
)); |
|
81
|
|
|
|
|
82
|
|
|
foreach ($medias as $media) { |
|
83
|
|
|
$this->log('Refresh media '.$media->getName().' - '.$media->getId()); |
|
84
|
|
|
|
|
85
|
|
|
try { |
|
86
|
|
|
$provider->updateMetadata($media, false); |
|
87
|
|
|
} catch (\Exception $e) { |
|
88
|
|
|
$this->log(sprintf('<error>Unable to update metadata, media: %s - %s </error>', $media->getId(), $e->getMessage())); |
|
89
|
|
|
|
|
90
|
|
|
continue; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
try { |
|
94
|
|
|
$this->getMediaManager()->save($media); |
|
95
|
|
|
} catch (\Exception $e) { |
|
96
|
|
|
$this->log(sprintf('<error>Unable saving media, media: %s - %s </error>', $media->getId(), $e->getMessage())); |
|
97
|
|
|
|
|
98
|
|
|
continue; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$this->log('Done!'); |
|
103
|
|
|
|
|
104
|
|
|
return 0; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Write a message to the output. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $message |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function log($message): void |
|
113
|
|
|
{ |
|
114
|
|
|
if (false === $this->quiet) { |
|
115
|
|
|
$this->output->writeln($message); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
private function getProvider(): MediaProviderInterface |
|
120
|
|
|
{ |
|
121
|
|
|
$providerName = $this->input->getArgument('providerName'); |
|
122
|
|
|
|
|
123
|
|
|
if (null === $providerName) { |
|
124
|
|
|
$providerName = $this->getQuestionHelper()->ask( |
|
125
|
|
|
$this->input, |
|
126
|
|
|
$this->output, |
|
127
|
|
|
new ChoiceQuestion('Please select the provider', array_keys($this->getMediaPool()->getProviders())) |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $this->getMediaPool()->getProvider($providerName); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
private function getContext(): string |
|
135
|
|
|
{ |
|
136
|
|
|
$context = $this->input->getArgument('context'); |
|
137
|
|
|
|
|
138
|
|
|
if (null === $context) { |
|
139
|
|
|
$context = $this->getQuestionHelper()->ask( |
|
140
|
|
|
$this->input, |
|
141
|
|
|
$this->output, |
|
142
|
|
|
new ChoiceQuestion('Please select the context', array_keys($this->getMediaPool()->getContexts())) |
|
143
|
|
|
); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return $context; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
private function getQuestionHelper(): QuestionHelper |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->getHelper('question'); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.