-
Notifications
You must be signed in to change notification settings - Fork 613
Expand file tree
/
Copy pathScaffold.php
More file actions
144 lines (127 loc) · 4.52 KB
/
Copy pathScaffold.php
File metadata and controls
144 lines (127 loc) · 4.52 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
<?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\DevTools\Builder\Component\Scaffold as ScaffoldBuilder;
use Phalcon\DevTools\Builder\Exception\BuilderException;
use Phalcon\DevTools\Commands\Command;
use Phalcon\DevTools\Commands\CommandsException;
use Phalcon\DevTools\Script\Color;
/**
* Scaffold Command
*
* Scaffold a controller, model and view for a database table
*/
class Scaffold extends Command
{
/**
* {@inheritdoc}
*
* @return array
*/
public function getPossibleParams(): array
{
return [
'table-name=s' => 'Table used as base to generate the scaffold',
'schema=s' => 'Name of the schema [optional]',
'config=s' => 'Configuration file [optional]',
'get-set' => 'Attributes will be protected and have setters/getters. [optional]',
'directory=s' => 'Base path on which project was created [optional]',
'template-path=s' => 'Specify a template path [optional]',
'template-engine=s' => 'Define the template engine, default phtml (phtml, volt) [optional]',
'force' => 'Forces to rewrite generated code if they already exists [optional]',
'trace' => 'Shows the trace of the framework in case of exception [optional]',
'ns-models=s' => "Model's namespace [optional]",
'ns-controllers=s' => "Controller's namespace [optional]",
'help' => 'Shows this help [optional]',
];
}
/**
* {@inheritdoc}
*
* @param array $parameters
* @return mixed
* @throws CommandsException
* @throws BuilderException
*/
public function run(array $parameters)
{
$name = $this->getOption(['table-name', 1]);
$templatePath = $this->getOption(['template-path'], null, TEMPLATE_PATH);
$schema = $this->getOption('schema');
$templateEngine = $this->getOption(['template-engine'], null, "phtml");
$path = $this->getDirectoryPath();
$scaffoldBuilder = new ScaffoldBuilder([
'name' => $name,
'schema' => $schema,
'force' => $this->isReceivedOption('force'),
'genSettersGetters' => $this->isReceivedOption('get-set'),
'directory' => $path,
'templatePath' => $templatePath,
'templateEngine' => $templateEngine,
'modelsNamespace' => $this->getOption('ns-models'),
'controllersNamespace' => $this->getOption('ns-controllers'),
'config' => $this->getreceivedOrDefaultConfig($path),
]);
return $scaffoldBuilder->build();
}
/**
* {@inheritdoc}
*
* @return array
*/
public function getCommands(): array
{
return ['scaffold', 'create-scaffold'];
}
/**
* {@inheritdoc}
*
* @return void
*/
public function getHelp(): void
{
print Color::head('Help:') . PHP_EOL;
print Color::colorize(' Creates a scaffold from a database table') . PHP_EOL . PHP_EOL;
print Color::head('Usage:') . PHP_EOL;
print Color::colorize(' scaffold [tableName] [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());
}
protected function getDirectoryPath()
{
$path = $this->isReceivedOption('directory') ? $this->getOption('directory') : '';
return realpath($path) . DIRECTORY_SEPARATOR;
}
/**
* @param string $path
* @return Config
* @throws CommandsException
*/
protected function getreceivedOrDefaultConfig(string $path): Config
{
if ($this->isReceivedOption('config')) {
return $this->loadConfig($path . $this->getOption('config'));
}
return $this->getConfig($path);
}
/**
* {@inheritdoc}
*
* @return integer
*/
public function getRequiredParams(): int
{
return 1;
}
}