1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Milkmeowo\Framework\Repository\Generators\Commands; |
4
|
|
|
|
5
|
|
|
use File; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Milkmeowo\Framework\Repository\Generators\BindingsGenerator; |
10
|
|
|
use Milkmeowo\Framework\Repository\Generators\Exceptions\FileAlreadyExistsException; |
11
|
|
|
|
12
|
|
|
class BindingsCommand extends Command |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The name of command. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $name = 'starter:bindings'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The description of command. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $description = 'Add repository bindings to service provider.'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The type of class being generated. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $type = 'Bindings'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Execute the command. |
37
|
|
|
* |
38
|
|
|
* @return null|false |
39
|
|
|
*/ |
40
|
|
|
public function fire() |
41
|
|
|
{ |
42
|
|
|
try { |
43
|
|
|
$bindingGenerator = new BindingsGenerator([ |
44
|
|
|
'name' => $this->argument('name'), |
45
|
|
|
'force' => $this->option('force'), |
46
|
|
|
]); |
47
|
|
|
// generate repository service provider |
48
|
|
|
if (! file_exists($bindingGenerator->getPath())) { |
49
|
|
|
$this->call('make:provider', [ |
50
|
|
|
'name' => $bindingGenerator->getConfigGeneratorClassPath($bindingGenerator->getPathConfigNode()), |
51
|
|
|
]); |
52
|
|
|
// placeholder to mark the place in file where to prepend repository bindings |
53
|
|
|
$provider = File::get($bindingGenerator->getPath()); |
54
|
|
|
File::put($bindingGenerator->getPath(), vsprintf(str_replace('//', '%s', $provider), [ |
55
|
|
|
'//', |
56
|
|
|
$bindingGenerator->bindPlaceholder, |
57
|
|
|
])); |
58
|
|
|
} |
59
|
|
|
$bindingGenerator->run(); |
60
|
|
|
$this->info($this->type.' created successfully.'); |
61
|
|
|
} catch (FileAlreadyExistsException $e) { |
62
|
|
|
$this->error($this->type.' already exists!'); |
63
|
|
|
|
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* The array of command arguments. |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
public function getArguments() |
74
|
|
|
{ |
75
|
|
|
return [ |
76
|
|
|
[ |
77
|
|
|
'name', |
78
|
|
|
InputArgument::REQUIRED, |
79
|
|
|
'The name of model for which the controller is being generated.', |
80
|
|
|
null, |
81
|
|
|
], |
82
|
|
|
]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* The array of command options. |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
public function getOptions() |
91
|
|
|
{ |
92
|
|
|
return [ |
93
|
|
|
[ |
94
|
|
|
'force', |
95
|
|
|
'f', |
96
|
|
|
InputOption::VALUE_NONE, |
97
|
|
|
'Force the creation if file already exists.', |
98
|
|
|
null, |
99
|
|
|
], |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|