|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Shlinkio\Shlink\CLI\Command\ShortUrl; |
|
5
|
|
|
|
|
6
|
|
|
use Shlinkio\Shlink\Common\Exception\PreviewGenerationException; |
|
7
|
|
|
use Shlinkio\Shlink\Common\Service\PreviewGeneratorInterface; |
|
8
|
|
|
use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface; |
|
9
|
|
|
use Symfony\Component\Console\Command\Command; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
13
|
|
|
use Zend\I18n\Translator\TranslatorInterface; |
|
14
|
|
|
use function sprintf; |
|
15
|
|
|
|
|
16
|
|
|
class GeneratePreviewCommand extends Command |
|
17
|
|
|
{ |
|
18
|
|
|
public const NAME = 'short-url:process-previews'; |
|
19
|
|
|
private const ALIASES = ['shortcode:process-previews', 'short-code:process-previews']; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var PreviewGeneratorInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $previewGenerator; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var TranslatorInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $translator; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var ShortUrlServiceInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $shortUrlService; |
|
33
|
|
|
|
|
34
|
2 |
|
public function __construct( |
|
35
|
|
|
ShortUrlServiceInterface $shortUrlService, |
|
36
|
|
|
PreviewGeneratorInterface $previewGenerator, |
|
37
|
|
|
TranslatorInterface $translator |
|
38
|
|
|
) { |
|
39
|
2 |
|
$this->shortUrlService = $shortUrlService; |
|
40
|
2 |
|
$this->previewGenerator = $previewGenerator; |
|
41
|
2 |
|
$this->translator = $translator; |
|
42
|
2 |
|
parent::__construct(null); |
|
43
|
2 |
|
} |
|
44
|
|
|
|
|
45
|
2 |
|
protected function configure(): void |
|
46
|
|
|
{ |
|
47
|
|
|
$this |
|
48
|
2 |
|
->setName(self::NAME) |
|
49
|
2 |
|
->setAliases(self::ALIASES) |
|
50
|
2 |
|
->setDescription( |
|
51
|
2 |
|
$this->translator->translate( |
|
52
|
2 |
|
'Processes and generates the previews for every URL, improving performance for later web requests.' |
|
53
|
|
|
) |
|
54
|
|
|
); |
|
55
|
2 |
|
} |
|
56
|
|
|
|
|
57
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output): void |
|
58
|
|
|
{ |
|
59
|
2 |
|
$page = 1; |
|
60
|
|
|
do { |
|
61
|
2 |
|
$shortUrls = $this->shortUrlService->listShortUrls($page); |
|
62
|
2 |
|
$page += 1; |
|
63
|
|
|
|
|
64
|
2 |
|
foreach ($shortUrls as $shortUrl) { |
|
65
|
2 |
|
$this->processUrl($shortUrl->getOriginalUrl(), $output); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
2 |
|
} while ($page <= $shortUrls->count()); |
|
68
|
|
|
|
|
69
|
2 |
|
(new SymfonyStyle($input, $output))->success($this->translator->translate('Finished processing all URLs')); |
|
70
|
2 |
|
} |
|
71
|
|
|
|
|
72
|
2 |
|
private function processUrl($url, OutputInterface $output): void |
|
73
|
|
|
{ |
|
74
|
|
|
try { |
|
75
|
2 |
|
$output->write(sprintf($this->translator->translate('Processing URL %s...'), $url)); |
|
76
|
2 |
|
$this->previewGenerator->generatePreview($url); |
|
77
|
1 |
|
$output->writeln($this->translator->translate(' <info>Success!</info>')); |
|
78
|
1 |
|
} catch (PreviewGenerationException $e) { |
|
79
|
1 |
|
$output->writeln(' <error>' . $this->translator->translate('Error') . '</error>'); |
|
80
|
1 |
|
if ($output->isVerbose()) { |
|
81
|
|
|
$this->getApplication()->renderException($e, $output); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
2 |
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.