Completed
Push — master ( dd5b6c...a2750e )
by Anton
19:24 queued 05:40
created

Application   C

Complexity

Total Complexity 10

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 26

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 26
dl 0
loc 143
ccs 54
cts 63
cp 0.8571
rs 5
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A init() 0 10 1
A getDefaultInputDefinition() 0 19 2
B registerCommands() 0 27 1
A getWorkingPath() 0 4 1
A getModulePath() 0 7 1
A getModelPath() 0 7 1
A isModuleExists() 0 4 1
A isModelExists() 0 4 1
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\TestCommand,
88 14
                new Command\Db\CreateCommand,
89 14
                new Command\Db\MigrateCommand,
90 14
                new Command\Db\RollbackCommand,
91 14
                new Command\Db\StatusCommand,
92 14
                new Command\Db\SeedCreateCommand,
93 14
                new Command\Db\SeedRunCommand,
94 14
                new Command\Generate\ModuleCommand,
95 14
                new Command\Generate\ControllerCommand,
96 14
                new Command\Generate\ModelCommand,
97 14
                new Command\Generate\CrudCommand,
98 14
                new Command\Generate\GridCommand,
99 14
                new Command\Generate\RestCommand,
100 14
                new Command\Module\InstallCommand,
101 14
                new Command\Module\ListCommand,
102 14
                new Command\Module\RemoveCommand,
103 14
                new Command\Server\StartCommand,
104 14
                new Command\Server\StopCommand,
105 14
                new Command\Server\StatusCommand,
106
            ]
107
        );
108 14
    }
109
110
    /**
111
     * Returns the path to the directory with bluzman application
112
     *
113
     * @return string
114
     */
115
    public function getWorkingPath()
116
    {
117
        return getcwd();
118
    }
119
120
    /**
121
     * Get Module path
122
     *
123
     * @param  string $name
124
     * @return string
125
     */
126 3
    public function getModulePath($name)
127
    {
128 3
        return $this->getWorkingPath() . DIRECTORY_SEPARATOR
129 3
            . 'application' . DIRECTORY_SEPARATOR
130 3
            . 'modules' . DIRECTORY_SEPARATOR
131 3
            . $name;
132
    }
133
134
    /**
135
     * Get Model path
136
     *
137
     * @param  string $name
138
     * @return string
139
     */
140 4
    public function getModelPath($name)
141
    {
142 4
        return $this->getWorkingPath() . DIRECTORY_SEPARATOR
143 4
            . 'application' . DIRECTORY_SEPARATOR
144 4
            . 'models' . DIRECTORY_SEPARATOR
145 4
            . $name;
146
    }
147
148
    /**
149
     * @param  string $name
150
     * @return bool
151
     */
152 2
    public function isModuleExists($name)
153
    {
154 2
        return is_dir($this->getModulePath($name));
155
    }
156
157
    /**
158
     * @param  string $name
159
     * @return bool
160
     */
161 3
    public function isModelExists($name)
162
    {
163 3
        return is_dir($this->getModelPath($name));
164
    }
165
}
166