-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommandMakeCommand.php
More file actions
executable file
·160 lines (133 loc) · 3.28 KB
/
CommandMakeCommand.php
File metadata and controls
executable file
·160 lines (133 loc) · 3.28 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
<?php namespace Illuminate\Foundation\Console;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class CommandMakeCommand extends Command {
protected Filesystem $files;
/**
* The console command name.
*
* @var string
*/
protected $name = 'command:make';
/**
* The console command description.
*
* @var string
*/
protected $description = "Create a new Artisan command";
/**
* Create a new command creator command.
*
* @param \Illuminate\Filesystem\Filesystem $files
* @return void
*/
public function __construct(Filesystem $files)
{
parent::__construct();
$this->files = $files;
}
/**
* Execute the console command.
*
* @return int
*/
public function fire()
{
$path = $this->getPath();
$stub = $this->files->get(__DIR__.'/stubs/command.stub');
// We'll grab the class name to determine the file name. Since applications are
// typically using the PSR-0 standards we can safely assume the classes name
// will correspond to what the actual file should be stored as on storage.
$file = $path.'/'.$this->input->getArgument('name').'.php';
$this->writeCommand($file, $stub);
return 0;
}
/**
* Write the finished command file to disk.
*
* @param string $file
* @param string $stub
* @return void
*/
protected function writeCommand($file, $stub)
{
if ( ! file_exists($file))
{
$this->files->put($file, $this->formatStub($stub));
$this->info('Command created successfully.');
}
else
{
$this->error('Command already exists!');
}
}
/**
* Format the command class stub.
*
* @param string $stub
* @return string
*/
protected function formatStub($stub)
{
$stub = str_replace('{{class}}', $this->input->getArgument('name'), $stub);
if ( ! is_null($this->option('command')))
{
$stub = str_replace('command:name', $this->option('command'), $stub);
}
return $this->addNamespace($stub);
}
/**
* Add the proper namespace to the command.
*
* @param string $stub
* @return string
*/
protected function addNamespace($stub)
{
if ( ! is_null($namespace = $this->input->getOption('namespace')))
{
return str_replace('{{namespace}}', ' namespace '.$namespace.';', $stub);
}
return str_replace('{{namespace}}', '', $stub);
}
/**
* Get the path where the command should be stored.
*
* @return string
*/
protected function getPath()
{
$path = $this->input->getOption('path');
if (is_null($path))
{
return $this->laravel['path'].'/commands';
}
return $this->laravel['path.base'].'/'.$path;
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return array(
array('name', InputArgument::REQUIRED, 'The name of the command.'),
);
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return array(
array('command', null, InputOption::VALUE_OPTIONAL, 'The terminal command that should be assigned.', null),
array('path', null, InputOption::VALUE_OPTIONAL, 'The path where the command should be stored.', null),
array('namespace', null, InputOption::VALUE_OPTIONAL, 'The command namespace.', null),
);
}
}