Skip to content

Commit d6a6cca

Browse files
author
Vitaliy
authored
Merge pull request #189 from serhiyzhovnir/issue-57-controller-generation
#Issue-57 Controller generation
2 parents f3ab2a3 + 4e831cd commit d6a6cca

29 files changed

+2655
-275
lines changed

resources/META-INF/plugin.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
<!-- Module file generators -->
5858
<group id="MagentoNewModuleFileGroup" class="com.magento.idea.magento2plugin.actions.groups.NewModuleFileGroup" text="Module File" popup="true">
5959
<action id="MagentoCreateABlock" class="com.magento.idea.magento2plugin.actions.generation.NewBlockAction" />
60+
<action id="MagentoCreateAController" class="com.magento.idea.magento2plugin.actions.generation.NewControllerAction" />
6061
<action id="MagentoCreateACronjob" class="com.magento.idea.magento2plugin.actions.generation.NewCronjobAction" />
6162
<action id="MagentoCreateAViewModel" class="com.magento.idea.magento2plugin.actions.generation.NewViewModelAction" />
6263
<action id="MagentoCreateAGraphQlResolver" class="com.magento.idea.magento2plugin.actions.generation.NewGraphQlResolverAction" />
@@ -163,6 +164,8 @@
163164
<internalFileTemplate name="Magento Cronjob Class"/>
164165
<internalFileTemplate name="Magento Crontab Xml"/>
165166
<internalFileTemplate name="Magento CLI Command"/>
167+
<internalFileTemplate name="Magento Module Controller Backend Class"/>
168+
<internalFileTemplate name="Magento Module Controller Frontend Class"/>
166169
</extensions>
167170

168171
<extensions defaultExtensionNs="com.jetbrains.php">
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
#parse("PHP File Header.php")
3+
#if (${NAMESPACE})
4+
5+
namespace ${NAMESPACE};
6+
#end
7+
8+
#set ($uses = ${USES})
9+
#foreach ($use in $uses.split(","))
10+
use $use;
11+
#end
12+
13+
class ${NAME}#if (${EXTENDS}) extends ${EXTENDS}#end#if (${IMPLEMENTS}) implements ${IMPLEMENTS}#end {
14+
#if (${ACL})
15+
/**
16+
* Authorization level of a basic admin session
17+
*/
18+
const ADMIN_RESOURCE = '${ACL}';
19+
20+
#end
21+
/**
22+
* Execute action based on request and return result
23+
*
24+
* @return ResultInterface|ResponseInterface
25+
* @throws NotFoundException
26+
*/
27+
public function execute()
28+
{
29+
// TODO: Implement execute method.
30+
}
31+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<!--
2+
/*
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
-->
7+
<html>
8+
<body>
9+
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
10+
<tr>
11+
<td>
12+
<font face="verdana" size="-1">
13+
Action classes are extensions of the Action class that a router returns on matched requests.
14+
Each Action should implement Magento\Framework\App\Action\HttpHTTP MethodActionInterface to declare which HTTP request methods it can process.
15+
The <a href="https://github.com/magento/magento2/blob/2.3/lib/internal/Magento/Framework/App/ActionInterface.php#L34">execute()</a> function in these classes contain the logic for dispatching requests.
16+
Action class should return a <a href="https://github.com/magento/magento2/blob/2.3/lib/internal/Magento/Framework/Controller/ResultInterface.php">result</a> object.
17+
Backend Controller allows to restrict an access by ACL.
18+
</font><br>
19+
</td>
20+
</tr>
21+
<tr>
22+
<td><font face="verdana" size="-1">
23+
<a href="https://devdocs.magento.com/guides/v2.3/extension-dev-guide/routing.html#action-class">Read more</a> about Controller Action, including when those can be used.
24+
</font><br>
25+
</td>
26+
</tr>
27+
</table>
28+
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
29+
<tr>
30+
<td colspan="3"><font face="verdana" size="-1">Predefined variables will take the following values:</font></td>
31+
</tr>
32+
<tr>
33+
<td valign="top"><nobr><font face="verdana" size="-2"><b>${NAMESPACE}</b></font></nobr></td>
34+
<td width="10">&nbsp;</td>
35+
<td width="100%" valign="top"><font face="verdana" size="-1">Created PHP Controller Action class namespace.</font></td>
36+
</tr>
37+
<tr>
38+
<td valign="top"><nobr><font face="verdana" size="-2"><b>${NAME}</b></font></nobr></td>
39+
<td width="10">&nbsp;</td>
40+
<td width="100%" valign="top"><font face="verdana" size="-1">PHP class for the Controller Action name.</font></td>
41+
</tr>
42+
<tr>
43+
<td valign="top"><nobr><font face="verdana" size="-2"><b>${EXTENDS}</b></font></nobr></td>
44+
<td width="10">&nbsp;</td>
45+
<td width="100%" valign="top"><font face="verdana" size="-1">Name of PHP class that the Controller Action extends.</font></td>
46+
</tr>
47+
<tr>
48+
<td valign="top"><nobr><font face="verdana" size="-2"><b>${IMPLEMENTS}</b></font></nobr></td>
49+
<td width="10">&nbsp;</td>
50+
<td width="100%" valign="top"><font face="verdana" size="-1">Name of PHP interface that the Controller Action implements.</font></td>
51+
</tr>
52+
<tr>
53+
<td valign="top"><nobr><font face="verdana" size="-2"><b>${USES}</b></font></nobr></td>
54+
<td width="10">&nbsp;</td>
55+
<td width="100%" valign="top"><font face="verdana" size="-1">List of imports separated by comma.</font></td>
56+
</tr>
57+
<tr>
58+
<td valign="top"><nobr><font face="verdana" size="-2"><b>${ACL}</b></font></nobr></td>
59+
<td width="10">&nbsp;</td>
60+
<td width="100%" valign="top"><font face="verdana" size="-1">The name of ACL resource that allows to restrict an access to the backend controller action.</font></td>
61+
</tr>
62+
</table>
63+
</body>
64+
</html>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
#parse("PHP File Header.php")
3+
#if (${NAMESPACE})
4+
5+
namespace ${NAMESPACE};
6+
#end
7+
8+
#set ($uses = ${USES})
9+
#foreach ($use in $uses.split(","))
10+
use $use;
11+
#end
12+
13+
class ${NAME}#if (${EXTENDS}) extends ${EXTENDS}#end#if (${IMPLEMENTS}) implements ${IMPLEMENTS}#end {
14+
/**
15+
* Execute action based on request and return result
16+
*
17+
* @return ResultInterface|ResponseInterface
18+
* @throws NotFoundException
19+
*/
20+
public function execute()
21+
{
22+
// TODO: Implement execute method.
23+
}
24+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<!--
2+
/*
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
-->
7+
<html>
8+
<body>
9+
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
10+
<tr>
11+
<td>
12+
<font face="verdana" size="-1">
13+
Action classes are extensions of the Action class that a router returns on matched requests.
14+
Each Action should implement Magento\Framework\App\Action\HttpHTTP MethodActionInterface to declare which HTTP request methods it can process.
15+
The <a href="https://github.com/magento/magento2/blob/2.3/lib/internal/Magento/Framework/App/ActionInterface.php#L34">execute()</a> function in these classes contain the logic for dispatching requests.
16+
Action class should return a <a href="https://github.com/magento/magento2/blob/2.3/lib/internal/Magento/Framework/Controller/ResultInterface.php">result</a> object.
17+
</font><br>
18+
</td>
19+
</tr>
20+
<tr>
21+
<td><font face="verdana" size="-1">
22+
<a href="https://devdocs.magento.com/guides/v2.3/extension-dev-guide/routing.html#action-class">Read more</a> about Controller Action, including when those can be used.
23+
</font><br>
24+
</td>
25+
</tr>
26+
</table>
27+
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
28+
<tr>
29+
<td colspan="3"><font face="verdana" size="-1">Predefined variables will take the following values:</font></td>
30+
</tr>
31+
<tr>
32+
<td valign="top"><nobr><font face="verdana" size="-2"><b>${NAMESPACE}</b></font></nobr></td>
33+
<td width="10">&nbsp;</td>
34+
<td width="100%" valign="top"><font face="verdana" size="-1">Created PHP Controller Action class namespace.</font></td>
35+
</tr>
36+
<tr>
37+
<td valign="top"><nobr><font face="verdana" size="-2"><b>${NAME}</b></font></nobr></td>
38+
<td width="10">&nbsp;</td>
39+
<td width="100%" valign="top"><font face="verdana" size="-1">PHP class for the Controller Action name.</font></td>
40+
</tr>
41+
<tr>
42+
<td valign="top"><nobr><font face="verdana" size="-2"><b>${EXTENDS}</b></font></nobr></td>
43+
<td width="10">&nbsp;</td>
44+
<td width="100%" valign="top"><font face="verdana" size="-1">Name of PHP class that the Controller Action extends.</font></td>
45+
</tr>
46+
<tr>
47+
<td valign="top"><nobr><font face="verdana" size="-2"><b>${IMPLEMENTS}</b></font></nobr></td>
48+
<td width="10">&nbsp;</td>
49+
<td width="100%" valign="top"><font face="verdana" size="-1">Name of PHP interface that the Controller Action implements.</font></td>
50+
</tr>
51+
<tr>
52+
<td valign="top"><nobr><font face="verdana" size="-2"><b>${USES}</b></font></nobr></td>
53+
<td width="10">&nbsp;</td>
54+
<td width="100%" valign="top"><font face="verdana" size="-1">List of imports separated by comma.</font></td>
55+
</tr>
56+
</table>
57+
</body>
58+
</html>

resources/magento2/common.properties

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ common.module.name=Module Name
44
common.module.version=Version
55
common.module.license=License(s)
66
common.dependencies=Dependencies
7+
common.httpMethod=HTTP Method
8+
common.controller.name=Controller Name
9+
common.controller.inheritAction=Inherit Action Class
10+
common.controller.backend.acl=Admin Resource ACL
11+
common.controller.action=Controller Action
712
common.ok=OK
813
common.cancel=Cancel
914
common.error=Error

resources/magento2/validation.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ validator.alphaNumericCharacters={0} must contain letters and numbers only
44
validator.startWithNumberOrCapitalLetter={0} must start from a number or a capital letter
55
validator.onlyNumbers={0} must contain numbers only
66
validator.identifier={0} must contain letters, numbers, dashes, and underscores only
7+
validator.class.isNotValid={0} is not valid class name
8+
validator.namespace.isNotValid={0} is not valid namespace name
79
validator.directory.isNotValid={0} is not valid
810
validator.module.noSuchModule=No such module {0}
911
validator.file.alreadyExists={0} already exists
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.actions.generation;
7+
8+
import com.intellij.ide.IdeView;
9+
import com.intellij.openapi.actionSystem.AnAction;
10+
import com.intellij.openapi.actionSystem.AnActionEvent;
11+
import com.intellij.openapi.actionSystem.CommonDataKeys;
12+
import com.intellij.openapi.actionSystem.DataContext;
13+
import com.intellij.openapi.actionSystem.LangDataKeys;
14+
import com.intellij.openapi.project.Project;
15+
import com.intellij.psi.PsiDirectory;
16+
import com.magento.idea.magento2plugin.MagentoIcons;
17+
import com.magento.idea.magento2plugin.actions.generation.dialog.NewControllerDialog;
18+
19+
@SuppressWarnings({"PMD.OnlyOneReturn", "PMD.FieldNamingConventions"})
20+
public class NewControllerAction extends AnAction {
21+
public static final String ACTION_NAME = "Magento 2 Controller";
22+
public static final String ACTION_DESCRIPTION = "Create a new Magento 2 controller";
23+
24+
/**
25+
* New controller action constructor.
26+
*/
27+
public NewControllerAction() {
28+
super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
29+
}
30+
31+
@Override
32+
public void actionPerformed(final AnActionEvent event) {
33+
final DataContext dataContext = event.getDataContext();
34+
final IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
35+
36+
if (view == null) {
37+
return;
38+
}
39+
40+
final Project project = CommonDataKeys.PROJECT.getData(dataContext);
41+
if (project == null) {
42+
return;
43+
}
44+
45+
final PsiDirectory directory = view.getOrChooseDirectory();
46+
if (directory == null) {
47+
return;
48+
}
49+
50+
NewControllerDialog.open(project, directory);
51+
}
52+
53+
@Override
54+
public boolean isDumbAware() {
55+
return false;
56+
}
57+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.actions.generation.data;
7+
8+
@SuppressWarnings({"PMD.DataClass"})
9+
public class ControllerFileData {
10+
private final String actionDirectory;
11+
private final String actionClassName;
12+
private final String controllerModule;
13+
private final String controllerArea;
14+
private final String httpMethodName;
15+
private final String acl;
16+
private final Boolean isInheritClass;
17+
private final String namespace;
18+
19+
/**
20+
* Controller data file constructor.
21+
*
22+
* @param actionDirectory String
23+
* @param controllerClassName String
24+
* @param controllerModule String
25+
* @param controllerArea String
26+
* @param httpMethodName String
27+
* @param acl String
28+
* @param isInheritClass Boolean
29+
* @param namespace String
30+
*/
31+
public ControllerFileData(
32+
final String actionDirectory,
33+
final String controllerClassName,
34+
final String controllerModule,
35+
final String controllerArea,
36+
final String httpMethodName,
37+
final String acl,
38+
final Boolean isInheritClass,
39+
final String namespace
40+
) {
41+
this.actionDirectory = actionDirectory;
42+
this.actionClassName = controllerClassName;
43+
this.controllerModule = controllerModule;
44+
this.controllerArea = controllerArea;
45+
this.httpMethodName = httpMethodName;
46+
this.acl = acl;
47+
this.isInheritClass = isInheritClass;
48+
this.namespace = namespace;
49+
}
50+
51+
/**
52+
* Get action directory.
53+
*
54+
* @return String
55+
*/
56+
public String getActionDirectory() {
57+
return actionDirectory;
58+
}
59+
60+
/**
61+
* Get action class name.
62+
*
63+
* @return String
64+
*/
65+
public String getActionClassName() {
66+
return actionClassName;
67+
}
68+
69+
/**
70+
* Get controller module.
71+
*
72+
* @return String
73+
*/
74+
public String getControllerModule() {
75+
return controllerModule;
76+
}
77+
78+
/**
79+
* Get namespace.
80+
*
81+
* @return String
82+
*/
83+
public String getNamespace() {
84+
return namespace;
85+
}
86+
87+
/**
88+
* Get controller area.
89+
*
90+
* @return String
91+
*/
92+
public String getControllerArea() {
93+
return controllerArea;
94+
}
95+
96+
/**
97+
* Get HTTP method name.
98+
*
99+
* @return String
100+
*/
101+
public String getHttpMethodName() {
102+
return httpMethodName;
103+
}
104+
105+
/**
106+
* Get ACL.
107+
*
108+
* @return String
109+
*/
110+
public String getAcl() {
111+
return acl;
112+
}
113+
114+
/**
115+
* Get is inherit class.
116+
*
117+
* @return String
118+
*/
119+
public Boolean getIsInheritClass() {
120+
return isInheritClass;
121+
}
122+
}

0 commit comments

Comments
 (0)