Issues (57)

core/console/model_console.php (1 issue)

1
<?php
2
/**
3
 * KumbiaPHP web & app Framework
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.
9
 *
10
 * @category   Kumbia
11
 * @package    Console
12
 *
13
 * @copyright  Copyright (c) 2005 - 2023 KumbiaPHP Team (http://www.kumbiaphp.com)
14
 * @license    https://github.com/KumbiaPHP/KumbiaPHP/blob/master/LICENSE   New BSD License
15
 */
16
17
/**
18
 * Consola para manejar modelos.
19
 *
20
 * @category   Kumbia
21
 * @package    Console
22
 */
23
class ModelConsole
24
{
25
    /**
26
     * Comando de consola para crear un modelo.
27
     *
28
     * @param array  $params parametros nombrados de la consola
29
     * @param string $model  modelo
30
     * @throw KumbiaException
31
     */
32
    public function create($params, $model)
33
    {
34
        // nombre de archivo
35
        $file = APP_PATH.'models';
36
37
        // obtiene el path
38
        $path = explode('/', trim($model, '/'));
39
40
        // obtiene el nombre de modelo
41
        $model_name = array_pop($path);
42
43
        if (count($path)) {
44
            $dir = implode('/', $path);
45
            $file .= "/$dir";
46
            if (!is_dir($file) && !FileUtil::mkdir($file)) {
47
                throw new KumbiaException("No se ha logrado crear el directorio \"$file\"");
48
            }
49
        }
50
        $file .= "/$model_name.php";
51
52
        // si no existe o se sobreescribe
53
        if (!is_file($file) ||
54
            Console::input('El modelo existe, desea sobrescribirlo? (s/n): ', array('s', 'n')) == 's') {
55
            // nombre de clase
56
            $class = Util::camelcase($model_name);
57
58
            // codigo de modelo
59
            ob_start();
60
            include __DIR__.'/generators/model.php';
61
            $code = '<?php'.PHP_EOL.ob_get_clean();
62
63
            // genera el archivo
64
            if (file_put_contents($file, $code)) {
65
                echo "-> Creado modelo $model_name en: $file".PHP_EOL;
66
            } else {
67
                throw new KumbiaException("No se ha logrado crear el archivo \"$file\"");
68
            }
69
        }
70
    }
71
72
    /**
73
     * Comando de consola para eliminar un modelo.
74
     *
75
     * @param array  $params parametros nombrados de la consola
76
     * @param string $model  modelo
77
     * @throw KumbiaException
78
     */
79
    public function delete($params, $model)
0 ignored issues
show
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

79
    public function delete(/** @scrutinizer ignore-unused */ $params, $model)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
    {
81
        // nombre de archivo
82
        $file = APP_PATH.'models/'.trim($model, '/');
83
84
        // si es un directorio
85
        if (is_dir($file)) {
86
            $success = FileUtil::rmdir($file);
87
        } else {
88
            // entonces es un archivo
89
            $file = "$file.php";
90
            $success = unlink($file);
91
        }
92
93
        // mensaje
94
        if ($success) {
95
            echo "-> Eliminado: $file".PHP_EOL;
96
        } else {
97
            throw new KumbiaException("No se ha logrado eliminar \"$file\"");
98
        }
99
    }
100
}
101