1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
use Symfony\Component\Console\Input\InputOption; |
5
|
|
|
|
6
|
|
|
class MakeFormCommand extends AbstractMakeCommand |
|
|
|
|
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
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
protected function addFormMethodToController($content, $formClass) |
45
|
|
|
{ |
46
|
|
|
if (!Str::contains($content, "function $formClass(")) { |
47
|
|
|
$methodStub = $this->getFormMethodStub($formClass); |
48
|
|
|
$replacement = $methodStub."\n}"; |
49
|
|
|
if ($methodStub) { |
|
|
|
|
50
|
|
|
$content = Str::replaceLast('}', $replacement, $content); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $content; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $content |
59
|
|
|
* @param string $formClass |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
protected function addAllowedActionsToController($content, $formClass) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$allowedActions = 'private static $allowed_actions'; |
66
|
|
|
|
67
|
|
|
if (!Str::contains($content, $allowedActions)) { |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $content; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param $controller |
75
|
|
|
* |
76
|
|
|
* @return bool|string |
77
|
|
|
*/ |
78
|
|
|
protected function findControllerFilePath($controller) |
79
|
|
|
{ |
80
|
|
|
$controller = strtolower($controller); |
81
|
|
|
$classes = SS_ClassLoader::instance()->getManifest()->getClasses(); |
82
|
|
|
$filePath = isset($classes[$controller]) ? $classes[$controller] : ''; |
83
|
|
|
|
84
|
|
|
if (!is_file($filePath)) { |
85
|
|
|
$this->error("$filePath does not exist"); |
86
|
|
|
|
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $filePath; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
protected function getFormMethodStub($formClass) |
94
|
|
|
{ |
95
|
|
|
$file = $this->getStubFilePath('ControllerFormMethods'); |
96
|
|
|
|
97
|
|
|
if (is_file($file)) { |
98
|
|
|
return str_replace('DummyClass', $formClass, file_get_contents($file)); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
protected function getOptions() |
106
|
|
|
{ |
107
|
|
|
$options = parent::getOptions(); |
108
|
|
|
$options[] = ['template', 't', InputOption::VALUE_NONE, 'Create a custom template for this Form']; |
109
|
|
|
$options[] = ['controller', 'C', InputOption::VALUE_REQUIRED, 'Add FormMethod and allowed actions to the given Controller']; |
110
|
|
|
|
111
|
|
|
return $options; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
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.