1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\MediaBundle\Command; |
4
|
|
|
|
5
|
|
|
use Kunstmaan\MediaBundle\Helper\MediaManager; |
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @final since 5.1 |
14
|
|
|
* NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages |
15
|
|
|
*/ |
16
|
|
|
class CleanDeletedMediaCommand extends ContainerAwareCommand |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var EntityManager |
20
|
|
|
*/ |
21
|
|
|
private $em; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var MediaManager |
25
|
|
|
*/ |
26
|
|
|
private $mediaManager; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param EntityManagerInterface|null $em |
30
|
|
|
* @param MediaManager|null $mediaManager |
31
|
|
|
*/ |
32
|
|
|
public function __construct(/* EntityManagerInterface */ $em = null, /* MediaManager */ $mediaManager = null) |
33
|
|
|
{ |
34
|
|
|
parent::__construct(); |
35
|
|
|
|
36
|
|
|
if (!$em instanceof EntityManagerInterface) { |
|
|
|
|
37
|
|
|
@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); |
|
|
|
|
38
|
|
|
|
39
|
|
|
$this->setName(null === $em ? 'kuma:media:clean-deleted-media' : $em); |
40
|
|
|
|
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->em = $em; |
|
|
|
|
45
|
|
|
$this->mediaManager = $mediaManager; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function configure() |
49
|
|
|
{ |
50
|
|
|
parent::configure(); |
51
|
|
|
|
52
|
|
|
$this |
53
|
|
|
->setName('kuma:media:clean-deleted-media') |
54
|
|
|
->setDescription('Throw away all files from the file system that have been deleted in the database') |
55
|
|
|
->setHelp( |
56
|
|
|
"The <info>kuma:media:clean-deleted-media</info> command can be used to clean up your file system after having deleted Media items using the backend." |
57
|
|
|
) |
58
|
|
|
->addOption( |
59
|
|
|
'force', |
60
|
|
|
'f', |
61
|
|
|
InputOption::VALUE_NONE, |
62
|
|
|
'If set does not prompt the user if he is certain he wants to remove Media' |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
67
|
|
|
{ |
68
|
|
View Code Duplication |
if (null === $this->em) { |
|
|
|
|
69
|
|
|
$this->em = $this->getContainer()->get('doctrine.orm.entity_manager'); |
70
|
|
|
$this->mediaManager = $this->getContainer()->get('kunstmaan_media.media_manager'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($input->getOption('force') !== true) { |
74
|
|
|
$helper = $this->getHelper('question'); |
75
|
|
|
$question = new ConfirmationQuestion('<question>Are you sure you want to remove all deleted Media from the file system?</question> ', false); |
76
|
|
|
|
77
|
|
|
if (!$helper->ask($input, $output, $question)) { |
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$output->writeln('<info>Removing all Media from the file system that have their status set to deleted in the database.</info>'); |
83
|
|
|
|
84
|
|
|
$medias = $this->em->getRepository('KunstmaanMediaBundle:Media')->findAllDeleted(); |
85
|
|
|
try { |
86
|
|
|
$this->em->beginTransaction(); |
87
|
|
|
foreach ($medias as $media) { |
88
|
|
|
$this->mediaManager->removeMedia($media); |
89
|
|
|
} |
90
|
|
|
$this->em->flush(); |
91
|
|
|
$this->em->commit(); |
92
|
|
|
$output->writeln('<info>All Media flagged as deleted, have now been removed from the file system.<info>'); |
93
|
|
|
} catch (\Exception $e) { |
94
|
|
|
$this->em->rollback(); |
95
|
|
|
$output->writeln('An error occured while trying to delete Media from the file system:'); |
96
|
|
|
$output->writeln('<error>'. $e->getMessage() . '</error>'); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.