-
Notifications
You must be signed in to change notification settings - Fork 613
Expand file tree
/
Copy pathModel.php
More file actions
163 lines (145 loc) · 5.32 KB
/
Copy pathModel.php
File metadata and controls
163 lines (145 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
declare(strict_types=1);
/**
* This file is part of the Phalcon Developer Tools.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Phalcon\DevTools\Commands\Builtin;
use Phalcon\Config\Config;
use Phalcon\Config\Adapter\Ini as ConfigIni;
use Phalcon\DevTools\Builder\Component\Model as ModelBuilder;
use Phalcon\DevTools\Builder\Exception\BuilderException;
use Phalcon\DevTools\Commands\Command;
use Phalcon\DevTools\Script\Color;
use Phalcon\DevTools\Utils;
use Phalcon\Text;
/**
* Model Command
*
* Create a model from command line
*/
class Model extends Command
{
/**
* {@inheritdoc}
*
* @return array
*/
public function getPossibleParams(): array
{
return [
'name=s' => 'Table name',
'schema=s' => 'Name of the schema [optional]',
'config=s' => 'Configuration file [optional]',
'namespace=s' => "Model's namespace [optional]",
'get-set' => 'Attributes will be protected and have setters/getters [optional]',
'extends=s' => 'Model extends the class name supplied [optional]',
'excludefields=l' => 'Excludes fields defined in a comma separated list [optional]',
'doc' => 'Helps to improve code completion on IDEs [optional]',
'directory=s' => 'Base path on which project is located [optional]',
'output=s' => 'Folder where models are located [optional]',
'force' => 'Rewrite the model [optional]',
'camelize' => 'Properties is in camelCase [optional]',
'trace' => 'Shows the trace of the framework in case of exception [optional]',
'mapcolumn' => 'Get some code for map columns [optional]',
'abstract' => 'Abstract Model [optional]',
'annotate' => 'Annotate Attributes [optional]',
'help' => 'Shows this help [optional]',
];
}
/**
* {@inheritdoc}
*
* @param array $parameters
* @throws BuilderException
*/
public function run(array $parameters): void
{
$name = $this->getOption(['name', 1]);
$className = Utils::camelize(isset($parameters[1]) ? $parameters[1] : $name, '_-');
$modelBuilder = new ModelBuilder(
[
'name' => $name,
'schema' => $this->getOption('schema'),
'config' => $this->getConfigObject(),
'className' => $className,
'fileName' => Text::uncamelize($className),
'genSettersGetters' => $this->isReceivedOption('get-set'),
'genDocMethods' => $this->isReceivedOption('doc'),
'namespace' => $this->getOption('namespace'),
'directory' => $this->getOption('directory'),
'modelsDir' => $this->getOption('output'),
'extends' => $this->getOption('extends'),
'excludeFields' => $this->getOption('excludefields'),
'camelize' => $this->isReceivedOption('camelize'),
'force' => $this->isReceivedOption('force'),
'mapColumn' => $this->isReceivedOption('mapcolumn'),
'abstract' => $this->isReceivedOption('abstract'),
'annotate' => $this->isReceivedOption('annotate')
]
);
$modelBuilder->build();
}
/**
* {@inheritdoc}
*
* @return array
*/
public function getCommands(): array
{
return ['model', 'create-model'];
}
/**
* {@inheritdoc}
*
* @return void
*/
public function getHelp(): void
{
print Color::head('Help:') . PHP_EOL;
print Color::colorize(' Creates a model') . PHP_EOL . PHP_EOL;
print Color::head('Usage:') . PHP_EOL;
print Color::colorize(' model [table-name] [options]', Color::FG_GREEN) . PHP_EOL . PHP_EOL;
print Color::head('Arguments:') . PHP_EOL;
print Color::colorize(' help', Color::FG_GREEN);
print Color::colorize("\tShows this help text") . PHP_EOL . PHP_EOL;
$this->printParameters($this->getPossibleParams());
}
/**
* {@inheritdoc}
*
* @return int
*/
public function getRequiredParams(): int
{
return 1;
}
/**
* Get Config object
*
* @return Config
* @throws BuilderException
*/
protected function getConfigObject(): Config
{
if (!$this->isReceivedOption('config')) {
return $this->path->getConfig();
}
$configPath = $this->getOption('config');
if (false == $this->path->isAbsolutePath($this->getOption('config'))) {
$configPath = $this->path->getRootPath() . $this->getOption('config');
}
if (preg_match('/.*(:?\.ini)(?:\s)?$/i', $configPath)) {
return new ConfigIni($configPath);
}
$config = include $configPath;
if (is_array($config)) {
return new Config($config);
}
return $config;
}
}