Completed
Pull Request — master (#38)
by Anton
12:38
created

Application::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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