Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.06% covered (success)
97.06%
33 / 34
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
NonTabRecordActionCommand
97.06% covered (success)
97.06%
33 / 34
66.67% covered (warning)
66.67%
2 / 3
7
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 configure
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 execute
95.24% covered (success)
95.24%
20 / 21
0.00% covered (danger)
0.00%
0 / 1
5
1<?php
2
3/**
4 * Console command: Generate non-tab record action route.
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\Generate;
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;
36use VuFindConsole\Generator\GeneratorTools;
37
38/**
39 * Console command: Generate non-tab record action route.
40 *
41 * @category VuFind
42 * @package  Console
43 * @author   Demian Katz <demian.katz@villanova.edu>
44 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
45 * @link     https://vufind.org/wiki/development Wiki
46 */
47#[AsCommand(
48    name: 'generate/nontabrecordaction',
49    description: 'Non-tab record action route generator'
50)]
51class NonTabRecordActionCommand extends AbstractCommand
52{
53    /**
54     * Main framework configuration
55     *
56     * @var array
57     */
58    protected $mainConfig;
59
60    /**
61     * Constructor
62     *
63     * @param GeneratorTools $tools      Generator tools
64     * @param array          $mainConfig Main framework configuration
65     * @param string|null    $name       The name of the command; passing null
66     * means it must be set in configure()
67     */
68    public function __construct(
69        GeneratorTools $tools,
70        array $mainConfig,
71        $name = null
72    ) {
73        $this->mainConfig = $mainConfig;
74        parent::__construct($tools, $name);
75    }
76
77    /**
78     * Configure the command.
79     *
80     * @return void
81     */
82    protected function configure()
83    {
84        $this
85            ->setHelp('Adds routes for a non-tab record action.')
86            ->addArgument(
87                'action',
88                InputArgument::REQUIRED,
89                'new action to add'
90            )->addArgument(
91                'target_module',
92                InputArgument::REQUIRED,
93                'the module where the new routes will be generated'
94            );
95    }
96
97    /**
98     * Run the command.
99     *
100     * @param InputInterface  $input  Input object
101     * @param OutputInterface $output Output object
102     *
103     * @return int 0 for success
104     */
105    protected function execute(InputInterface $input, OutputInterface $output)
106    {
107        $action = $input->getArgument('action');
108        $module = $input->getArgument('target_module');
109
110        $this->generatorTools->setOutputInterface($output);
111
112        // Create backup of configuration
113        $configPath = $this->generatorTools->getModuleConfigPath($module);
114        $this->generatorTools->backUpFile($configPath);
115
116        // Append the routes
117        $config = include $configPath;
118        foreach ($this->mainConfig['router']['routes'] as $key => $val) {
119            if (
120                isset($val['options']['route'])
121                && str_ends_with($val['options']['route'], '[:id[/[:tab]]]')
122            ) {
123                $newRoute = $key . '-' . strtolower($action);
124                if (isset($this->mainConfig['router']['routes'][$newRoute])) {
125                    $output->writeln($newRoute . ' already exists; skipping.');
126                } else {
127                    $val['options']['route'] = str_replace(
128                        '[:id[/[:tab]]]',
129                        "[:id]/$action",
130                        $val['options']['route']
131                    );
132                    $val['options']['defaults']['action'] = $action;
133                    $config['router']['routes'][$newRoute] = $val;
134                }
135            }
136        }
137
138        // Write updated configuration
139        $this->generatorTools->writeModuleConfig($configPath, $config);
140        return 0;
141    }
142}