Completed
Push — master ( 550846...8c8483 )
by Anton
09:53
created

Application::getDefaultInputDefinition()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 13
nc 1
nop 0
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
    public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
30
    {
31
        parent::__construct($name, $version);
32
33
        $this->registerCommands();
34
    }
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
    protected function getDefaultInputDefinition()
58
    {
59
        return new InputDefinition(
60
            [
61
                new InputArgument('command', InputArgument::REQUIRED, 'The command to execute'),
62
                new InputOption(
63
                    '--env',
64
                    '-e',
65
                    InputOption::VALUE_REQUIRED,
66
                    'The environment to be used',
67
                    getenv('BLUZ_ENV') ?: 'dev'
68
                ),
69
                new InputOption('--help', '-h', InputOption::VALUE_NONE, 'Display this help message'),
70
                new InputOption('--quiet', '-q', InputOption::VALUE_NONE, 'Do not output any message'),
71
                new InputOption('--verbose', '-v', InputOption::VALUE_NONE, 'Increase verbosity of messages'),
72
                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
    protected function registerCommands()
83
    {
84
        $this->addCommands(
85
            [
86
                new Command\MagicCommand,
87
                new Command\Generate\ModuleCommand,
88
                new Command\Generate\ControllerCommand,
89
                new Command\Generate\ModelCommand,
90
                new Command\Generate\CrudCommand,
91
                new Command\Generate\GridCommand,
92
                new Command\Generate\RestCommand,
93
                new Command\Server\StartCommand,
94
                new Command\Server\StopCommand,
95
                new Command\Server\StatusCommand,
96
            ]
97
        );
98
    }
99
100
    /**
101
     * Returns the path to the directory with bluzman application
102
     *
103
     * @return string
104
     */
105
    public function getWorkingPath()
106
    {
107
        return getcwd();
108
    }
109
110
    /**
111
     * Get Module path
112
     *
113
     * @param  string $name
114
     * @return string
115
     */
116
    public function getModulePath($name)
117
    {
118
        return $this->getWorkingPath() . DIRECTORY_SEPARATOR
119
            . 'application' . DIRECTORY_SEPARATOR
120
            . 'modules' . DIRECTORY_SEPARATOR
121
            . $name;
122
    }
123
124
    /**
125
     * Get Model path
126
     *
127
     * @param  string $name
128
     * @return string
129
     */
130
    public function getModelPath($name)
131
    {
132
        return $this->getWorkingPath() . DIRECTORY_SEPARATOR
133
            . 'application' . DIRECTORY_SEPARATOR
134
            . 'models' . DIRECTORY_SEPARATOR
135
            . $name;
136
    }
137
138
    /**
139
     * @param  string $name
140
     * @return bool
141
     */
142
    public function isModuleExists($name)
143
    {
144
        return is_dir($this->getModulePath($name));
145
    }
146
147
    /**
148
     * @param  string $name
149
     * @return bool
150
     */
151
    public function isModelExists($name)
152
    {
153
        return is_dir($this->getModelPath($name));
154
    }
155
}
156