1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
use Symfony\Component\Console\Input\InputOption; |
5
|
|
|
|
6
|
|
|
class MakeFormCommand extends MakeCommand |
|
|
|
|
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* @var string |
10
|
|
|
*/ |
11
|
|
|
protected $name = 'make:form'; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $description = 'Create a new Form class and optional template'; |
17
|
|
|
|
18
|
|
|
protected function writeFile($target, $class) |
19
|
|
|
{ |
20
|
|
|
parent::writeFile($target, $class); |
21
|
|
|
|
22
|
|
|
if ($controller = $this->option('controller')) { |
23
|
|
|
$filePath = $this->findControllerFilePath($controller); |
24
|
|
|
if($filePath !== false) { |
25
|
|
|
$this->addFormMethodToController($controller, $class); |
|
|
|
|
26
|
|
|
$content = file_get_contents($filePath); |
27
|
|
|
|
28
|
|
|
$content = $this->addFormMethodToController($content, $class); |
29
|
|
|
$this->info($class . ' method added to ' , $controller); |
|
|
|
|
30
|
|
|
$content = $this->addAllowedActionsToController($content, $class); |
31
|
|
|
$this->warn('Adding allowed_actions not supported yet'); |
32
|
|
|
|
33
|
|
|
file_put_contents($filePath, $content); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $content |
40
|
|
|
* @param string $formClass |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
protected function addFormMethodToController($content, $formClass) |
44
|
|
|
{ |
45
|
|
|
if(!Str::contains($content, "function $formClass(")) { |
46
|
|
|
$methodStub = $this->getFormMethodStub($formClass); |
47
|
|
|
$replacement = $methodStub . "\n}"; |
48
|
|
|
if($methodStub) { |
|
|
|
|
49
|
|
|
$content = Str::replaceLast('}', $replacement, $content); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
return $content; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $content |
57
|
|
|
* @param string $formClass |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
protected function addAllowedActionsToController($content, $formClass) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$allowedActions = 'private static $allowed_actions'; |
63
|
|
|
|
64
|
|
|
if(!Str::contains($content, $allowedActions)) { |
|
|
|
|
65
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $content; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param $controller |
73
|
|
|
* @return bool|string |
74
|
|
|
*/ |
75
|
|
|
protected function findControllerFilePath($controller) |
76
|
|
|
{ |
77
|
|
|
$controller = strtolower($controller); |
78
|
|
|
$classes = SS_ClassLoader::instance()->getManifest()->getClasses(); |
79
|
|
|
$filePath = isset($classes[$controller]) ? $classes[$controller] : ''; |
80
|
|
|
|
81
|
|
|
if(!is_file($filePath)) { |
82
|
|
|
$this->error("$filePath does not exist"); |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $filePath; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function getFormMethodStub($formClass) |
90
|
|
|
{ |
91
|
|
|
$file = $this->getStubFilePath('ControllerFormMethods'); |
92
|
|
|
|
93
|
|
|
if(is_file($file)) { |
94
|
|
|
return str_replace('DummyClass', $formClass, file_get_contents($file)); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
|
|
protected function getOptions() |
102
|
|
|
{ |
103
|
|
|
$options = parent::getOptions(); |
104
|
|
|
$options[] = ['template', 't', InputOption::VALUE_NONE, 'Create a custom template for this Form']; |
105
|
|
|
$options[] = ['controller', 'C', InputOption::VALUE_REQUIRED, 'Add FormMethod and allowed actions to the given Controller']; |
106
|
|
|
return $options; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.