Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
89.13% covered (warning)
89.13%
41 / 46
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
CrudRemove
89.13% covered (warning)
89.13%
41 / 46
66.67% covered (warning)
66.67%
2 / 3
15.29
0.00% covered (danger)
0.00%
0 / 1
 handle
87.18% covered (warning)
87.18%
34 / 39
0.00% covered (danger)
0.00%
0 / 1
13.36
 getOptions
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getArguments
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace San\Crud\Commands;
4
5use Illuminate\Console\Command;
6use San\Crud\Generators\ControllerGen;
7use San\Crud\Generators\ModelGen;
8use San\Crud\Generators\PolicyGen;
9use San\Crud\Generators\RouteGen;
10use San\Crud\Generators\Templates;
11use San\Crud\Generators\ViewGen;
12use Symfony\Component\Console\Input\InputArgument;
13use Symfony\Component\Console\Input\InputOption;
14
15class CrudRemove extends CrudBase {
16    /**
17     * The name and signature of the console command.
18     *
19     * @var string
20     */
21    protected $name = 'crud:remove';
22
23    /**
24     * The console command description.
25     *
26     * @var string
27     */
28    protected $description = 'Removes the generated CRUD files for table';
29
30    /**
31     * Execute the console command.
32     *
33     * @return int
34     */
35    public function handle() {
36        $tables = $this->getTables();
37
38        $cgen = new ControllerGen($tables);
39        $mgen = new ModelGen($tables);
40        $rgen = new RouteGen($tables);
41        $vgen = new ViewGen($tables);
42        $pgen = new PolicyGen($tables);
43
44        $blanks = [
45            'controller' => $cgen->getControllerName(),
46            'model'      => $mgen->getModelName(),
47            'route'      => $rgen->getRouteName(),
48            'policy'     => $pgen->getPolicyName(),
49        ];
50
51        $files = new Templates($this->getTemplateDir());
52        $cssFramework = 'bootstrap';
53        $interactive = $this->option('interactive');
54
55        if ($zipFile = $this->option('backup')) {
56            if (!class_exists('ZipArchive')) {
57                $this->error('ZipArchive class not found. Please install php-zip extension.');
58                return COMMAND::FAILURE;
59            }
60
61            $zip = new \ZipArchive();
62            $result = $zip->open($zipFile, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
63
64            if (($result !== TRUE) || !is_writable(dirname($zipFile))) {
65                $this->error('Cannot create zip file: ' . $zipFile);
66                return COMMAND::FAILURE;
67            }
68        }
69
70        foreach ($this->getStubTypes($cssFramework) as $type) {
71            $stubs = $files->getStubs($type);
72
73            foreach ($stubs as $stub) {
74                $dest = $files->getDest($type, $stub, $blanks, $rgen->getRouteUrl());
75
76                if (file_exists($dest)) {
77                    if ($interactive && !$this->confirm("Delete $dest?")) {
78                        continue;
79                    }
80
81                    if (!empty($zip)) {
82                        $zip->addFromString(file_get_contents($dest), sprintf('%s/%s', $type, basename($dest)));
83                    }
84
85                    $this->info("Removing file $dest");
86
87                    unlink($dest);
88                }
89            }
90        }
91
92        if (!empty($zip) && ($zip->numFiles > 0)) {
93            $zip->close();
94            $this->info(sprintf("Backup created at: %s", realpath($zipFile)));
95        }
96
97        return Command::SUCCESS;
98    }
99
100    protected function getOptions() {
101        return [
102            ['backup', 'b', InputOption::VALUE_REQUIRED, 'Name of zip file to backup files before removing'],
103            ['interactive', 'i', InputOption::VALUE_NONE, 'Ask for confirmation before removing each file'],
104        ];
105    }
106
107    /**
108     * {@inheritdoc}
109     */
110    protected function getArguments() {
111        return [
112            ['table', InputArgument::REQUIRED, 'Remove all generated files for the given tables'],
113        ];
114    }
115}