[FEATURE] Introduce native support for Symfony Console
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Command / LockBackendCommand.php
1 <?php
2 namespace TYPO3\CMS\Backend\Command;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use Symfony\Component\Console\Command\Command;
17 use Symfony\Component\Console\Input\InputArgument;
18 use Symfony\Component\Console\Input\InputInterface;
19 use Symfony\Component\Console\Output\OutputInterface;
20 use Symfony\Component\Console\Style\SymfonyStyle;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22
23 /**
24 * Core function for locking and unlocking the TYPO3 Backend
25 */
26 class LockBackendCommand extends Command
27 {
28 /**
29 * Configure the command by defining the name, options and arguments
30 */
31 protected function configure()
32 {
33 if ($this->getName() === 'backend:unlock') {
34 $this
35 ->setDescription('Unlock the TYPO3 Backend');
36 } else {
37 $this
38 ->setDescription('Lock the TYPO3 Backend')
39 ->addArgument(
40 'redirect',
41 InputArgument::OPTIONAL,
42 'If set, then the TYPO3 Backend will redirect to the locking state (only used when locking the TYPO3 Backend'
43 );
44 }
45 }
46
47 /**
48 * Executes the command for adding or removing the lock file
49 * @param InputInterface $input
50 * @param OutputInterface $output
51 * @return void
52 */
53 protected function execute(InputInterface $input, OutputInterface $output)
54 {
55 $io = new SymfonyStyle($input, $output);
56 $io->title($this->getDescription());
57 if ($this->getName() === 'backend:unlock') {
58 $this->unlock($io);
59 } else {
60 $this->lock($io, $input);
61 }
62 }
63
64 /**
65 * Unlock the TYPO3 Backend by removing the lock file
66 *
67 * @param SymfonyStyle $io
68 */
69 protected function unlock(SymfonyStyle $io)
70 {
71 $lockFile = $this->getLockFileName();
72 if (@is_file($lockFile)) {
73 unlink($lockFile);
74 if (@is_file($lockFile)) {
75 $io->caution('Could not remove lock file "' . $lockFile . '"!');
76 } else {
77 $io->success('Removed lock file "' . $lockFile . '".');
78 }
79 } else {
80 $io->note('No lock file "' . $lockFile . '" was found.' . LF . 'Hence no lock can be removed.');
81 }
82 }
83
84 /**
85 * Lock the TYPO3 Backend
86 *
87 * @param SymfonyStyle $io
88 * @param InputInterface $input
89 */
90 protected function lock(SymfonyStyle $io, InputInterface $input)
91 {
92 $lockFile = $this->getLockFileName();
93 if (@is_file($lockFile)) {
94 $io->note('A lock file already exists. Overwriting it.');
95 }
96 $output = 'Wrote lock file to "' . $lockFile . '"';
97 if ($input->getArgument('redirect')) {
98 $lockFileContent = $input->getArgument('redirect');
99 $output .= LF . 'with content "' . $lockFileContent . '".';
100 } else {
101 $lockFileContent = '';
102 $output .= '.';
103 }
104 GeneralUtility::writeFile($lockFile, $lockFileContent);
105 $io->success($output);
106 }
107
108 /**
109 * Location of the file name
110 *
111 * @return string
112 */
113 protected function getLockFileName()
114 {
115 return PATH_typo3conf . 'LOCK_BACKEND';
116 }
117 }