1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\MediaBundle\Command; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
7
|
|
|
use \ImagickException; |
8
|
|
|
use Kunstmaan\MediaBundle\Helper\Transformer\PdfTransformer; |
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @final since 5.1 |
15
|
|
|
* NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages |
16
|
|
|
*/ |
17
|
|
|
class CreatePdfPreviewCommand extends ContainerAwareCommand |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var EntityManager |
21
|
|
|
*/ |
22
|
|
|
private $em; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var PdfTransformer |
26
|
|
|
*/ |
27
|
|
|
private $pdfTransformer; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $rootDir; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
private $enablePdfPreview; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param EntityManagerInterface|null $em |
41
|
|
|
* @param PdfTransformer|null $mediaManager |
|
|
|
|
42
|
|
|
*/ |
43
|
|
View Code Duplication |
public function __construct(/* EntityManagerInterface */ $em = null, /* PdfTransformer */ $pdfTransformer = null, $rootDir = null, $enablePdfPreview = null) |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
parent::__construct(); |
46
|
|
|
|
47
|
|
|
if (!$em instanceof EntityManagerInterface) { |
48
|
|
|
@trigger_error(sprintf('Passing a command name as the first argument of "%s" is deprecated since version symfony 3.4 and will be removed in symfony 4.0. If the command was registered by convention, make it a service instead. ', __METHOD__), E_USER_DEPRECATED); |
|
|
|
|
49
|
|
|
|
50
|
|
|
$this->setName(null === $em ? 'kuma:media:create-pdf-previews' : $em); |
51
|
|
|
|
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->em = $em; |
|
|
|
|
56
|
|
|
$this->pdfTransformer = $pdfTransformer; |
57
|
|
|
$this->rootDir = $rootDir; |
58
|
|
|
$this->enablePdfPreview = $enablePdfPreview; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function configure() |
62
|
|
|
{ |
63
|
|
|
parent::configure(); |
64
|
|
|
|
65
|
|
|
$this |
66
|
|
|
->setName('kuma:media:create-pdf-previews') |
67
|
|
|
->setDescription('Create preview images for PDFs that have already been uploaded') |
68
|
|
|
->setHelp( |
69
|
|
|
"The <info>kuma:media:create-pdf-previews</info> command can be used to create preview images for PDFs that have already been uploaded." |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
74
|
|
|
{ |
75
|
|
|
if (null === $this->em) { |
76
|
|
|
$this->em = $this->getContainer()->get('doctrine.orm.entity_manager'); |
77
|
|
|
$this->pdfTransformer = $this->getContainer()->get('kunstmaan_media.pdf_transformer'); |
78
|
|
|
$this->rootDir = $this->getContainer()->get('kernel')->getRootDir(); |
79
|
|
|
$this->enablePdfPreview = $this->getContainer()->getParameter('kunstmaan_media.enable_pdf_preview'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$output->writeln('Creating PDF preview images...'); |
83
|
|
|
$webPath = realpath($this->rootDir . '/../web') . DIRECTORY_SEPARATOR; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @var EntityManager |
87
|
|
|
*/ |
88
|
|
|
$medias = $this->em->getRepository('KunstmaanMediaBundle:Media')->findBy( |
89
|
|
|
array('contentType' => 'application/pdf', 'deleted' => false) |
90
|
|
|
); |
91
|
|
|
/** @var Media $media */ |
92
|
|
|
foreach ($medias as $media) { |
93
|
|
|
try |
94
|
|
|
{ |
95
|
|
|
$this->pdfTransformer->apply($webPath . $media->getUrl()); |
96
|
|
|
} |
97
|
|
|
catch(ImagickException $e) |
98
|
|
|
{ |
99
|
|
|
$output->writeln('<comment>'.$e->getMessage().'</comment>'); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$output->writeln('<info>PDF preview images have been created.</info>'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Checks whether the command is enabled or not in the current environment. |
108
|
|
|
* |
109
|
|
|
* Override this to check for x or y and return false if the command can not |
110
|
|
|
* run properly under the current conditions. |
111
|
|
|
* |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
public function isEnabled() |
115
|
|
|
{ |
116
|
|
|
if (null === $this->enablePdfPreview) { |
117
|
|
|
$this->enablePdfPreview = $this->getContainer()->getParameter('kunstmaan_media.enable_pdf_preview'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this->enablePdfPreview; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.