Passed
Pull Request — master (#35)
by Anton
03:28
created

Application::isModelExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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