Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.89% covered (warning)
88.89%
16 / 18
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
DeleteCommand
88.89% covered (warning)
88.89%
16 / 18
33.33% covered (danger)
33.33%
1 / 3
7.07
0.00% covered (danger)
0.00%
0 / 1
 configure
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 writeFileToDisk
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
5.03
1<?php
2
3/**
4 * Language command: add string using template.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2020.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Console
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development Wiki
28 */
29
30namespace VuFindConsole\Command\Language;
31
32use Symfony\Component\Console\Attribute\AsCommand;
33use Symfony\Component\Console\Input\InputArgument;
34use Symfony\Component\Console\Input\InputInterface;
35use Symfony\Component\Console\Output\OutputInterface;
36
37/**
38 * Language command: add string using template.
39 *
40 * @category VuFind
41 * @package  Console
42 * @author   Demian Katz <demian.katz@villanova.edu>
43 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
44 * @link     https://vufind.org/wiki/development Wiki
45 */
46#[AsCommand(
47    name: 'language/delete',
48    description: 'Delete string tool'
49)]
50class DeleteCommand extends AbstractCommand
51{
52    /**
53     * Configure the command.
54     *
55     * @return void
56     */
57    protected function configure()
58    {
59        $this
60            ->setHelp(
61                'Removes a language string from all files'
62            )->addArgument(
63                'target',
64                InputArgument::REQUIRED,
65                "the target key to remove (may include 'textdomain::' prefix)"
66            );
67    }
68
69    /**
70     * Write file contents to disk.
71     *
72     * @param string $filename Filename
73     * @param string $content  Content
74     *
75     * @return bool
76     */
77    protected function writeFileToDisk($filename, $content)
78    {
79        return file_put_contents($filename, $content);
80    }
81
82    /**
83     * Run the command.
84     *
85     * @param InputInterface  $input  Input object
86     * @param OutputInterface $output Output object
87     *
88     * @return int 0 for success
89     */
90    protected function execute(InputInterface $input, OutputInterface $output)
91    {
92        $target = $input->getArgument('target');
93
94        [$domain, $key] = $this->extractTextDomain($target);
95        $target = $key . ' = "';
96
97        if (!($dir = $this->getLangDir($output, $domain))) {
98            return 1;
99        }
100        $callback = function ($full) use ($output, $target) {
101            $lines = file($full);
102            $out = '';
103            $found = false;
104            foreach ($lines as $line) {
105                if (!str_starts_with($line, $target)) {
106                    $out .= $line;
107                } else {
108                    $found = true;
109                }
110            }
111            if ($found) {
112                $this->writeFileToDisk($full, $out);
113                $this->normalizer->normalizeFile($full);
114            } else {
115                $output->writeln('Source key not found.');
116            }
117        };
118        $this->processDirectory($dir, $callback, [$output, 'writeln']);
119
120        return 0;
121    }
122}