Completed
Push — master ( 926f1e...80db42 )
by Anton
10:25 queued 07:09
created

Application::getDefaultInputDefinition()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 13
cts 13
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 13
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link https://github.com/bluzphp/bluzman
5
 */
6
7
namespace Bluzman\Application;
8
9
use Bluz\Config\Config;
10
use Bluz\Proxy;
11
use Bluzman\Command;
12
use Symfony\Component\Console;
13
use Symfony\Component\Console\Input\InputDefinition;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Input\InputArgument;
16
17
/**
18
 * @package Bluzman\Application
19
 *
20
 * @author Pavel Machekhin
21
 * @created 2013-11-28 12:31
22
 */
23
class Application extends Console\Application
24
{
25
    /**
26
     * @param string $name
27
     * @param string $version
28
     */
29 14
    public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
30
    {
31 14
        parent::__construct($name, $version);
32
33 14
        $this->registerCommands();
34 14
    }
35
36
    /**
37
     * init
38
     *
39
     * @return void
40
     */
41
    public function init()
42
    {
43
        // Init Bluz config
44
        $config = new Config();
45
        $config->setPath(PATH_APPLICATION);
46
        $config->setEnvironment(BLUZ_ENV);
47
        $config->init();
48
49
        Proxy\Config::setInstance($config);
50
    }
51
52
    /**
53
     * Removed some commands from default input definition.
54
     *
55
     * @return InputDefinition An InputDefinition instance
56
     */
57 14
    protected function getDefaultInputDefinition()
58
    {
59 14
        return new InputDefinition(
60
            [
61 14
                new InputArgument('command', InputArgument::REQUIRED, 'The command to execute'),
62 14
                new InputOption(
63 14
                    '--env',
64 14
                    '-e',
65 14
                    InputOption::VALUE_REQUIRED,
66 14
                    'The environment to be used',
67 14
                    getenv('BLUZ_ENV') ?: 'dev'
68
                ),
69 14
                new InputOption('--help', '-h', InputOption::VALUE_NONE, 'Display this help message'),
70 14
                new InputOption('--quiet', '-q', InputOption::VALUE_NONE, 'Do not output any message'),
71 14
                new InputOption('--verbose', '-v', InputOption::VALUE_NONE, 'Increase verbosity of messages'),
72 14
                new InputOption('--version', '-V', InputOption::VALUE_NONE, 'Display this application version')
73
            ]
74
        );
75
    }
76
77
    /**
78
     * Register Bluzman commands
79
     *
80
     * @todo Find a way to do this automatically
81
     */
82 14
    protected function registerCommands()
83
    {
84 14
        $this->addCommands(
85
            [
86 14
                new Command\MagicCommand,
87 14
                new Command\Db\CreateCommand,
88 14
                new Command\Db\MigrateCommand,
89 14
                new Command\Db\RollbackCommand,
90 14
                new Command\Db\StatusCommand,
91 14
                new Command\Db\SeedCreateCommand,
92 14
                new Command\Db\SeedRunCommand,
93 14
                new Command\Generate\ModuleCommand,
94 14
                new Command\Generate\ControllerCommand,
95 14
                new Command\Generate\ModelCommand,
96 14
                new Command\Generate\CrudCommand,
97 14
                new Command\Generate\GridCommand,
98 14
                new Command\Generate\RestCommand,
99 14
                new Command\Server\StartCommand,
100 14
                new Command\Server\StopCommand,
101 14
                new Command\Server\StatusCommand,
102
            ]
103
        );
104 14
    }
105
106
    /**
107
     * Returns the path to the directory with bluzman application
108
     *
109
     * @return string
110
     */
111
    public function getWorkingPath()
112
    {
113
        return getcwd();
114
    }
115
116
    /**
117
     * Get Module path
118
     *
119
     * @param  string $name
120
     * @return string
121
     */
122 3
    public function getModulePath($name)
123
    {
124 3
        return $this->getWorkingPath() . DIRECTORY_SEPARATOR
125 3
            . 'application' . DIRECTORY_SEPARATOR
126 3
            . 'modules' . DIRECTORY_SEPARATOR
127 3
            . $name;
128
    }
129
130
    /**
131
     * Get Model path
132
     *
133
     * @param  string $name
134
     * @return string
135
     */
136 4
    public function getModelPath($name)
137
    {
138 4
        return $this->getWorkingPath() . DIRECTORY_SEPARATOR
139 4
            . 'application' . DIRECTORY_SEPARATOR
140 4
            . 'models' . DIRECTORY_SEPARATOR
141 4
            . $name;
142
    }
143
144
    /**
145
     * @param  string $name
146
     * @return bool
147
     */
148 2
    public function isModuleExists($name)
149
    {
150 2
        return is_dir($this->getModulePath($name));
151
    }
152
153
    /**
154
     * @param  string $name
155
     * @return bool
156
     */
157 3
    public function isModelExists($name)
158
    {
159 3
        return is_dir($this->getModelPath($name));
160
    }
161
}
162