Skip to content

Commit ea5140d

Browse files
author
Vitaliy
authored
Merge pull request #176 from roma-glushko/143-generate-cronjobs
#143 Action/Code Generation. Cron job generation
2 parents 3bad5c8 + c9e897c commit ea5140d

26 files changed

+1789
-0
lines changed

resources/META-INF/plugin.xml

Lines changed: 4 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="MagentoCreateACronjob" class="com.magento.idea.magento2plugin.actions.generation.NewCronjobAction" />
6061
<action id="MagentoCreateAViewModel" class="com.magento.idea.magento2plugin.actions.generation.NewViewModelAction" />
6162
<action id="MagentoCreateAGraphQlResolver" class="com.magento.idea.magento2plugin.actions.generation.NewGraphQlResolverAction" />
6263
<add-to-group group-id="NewGroup" anchor="last"/>
@@ -95,6 +96,7 @@
9596
<fileBasedIndex implementation="com.magento.idea.magento2plugin.stubs.indexes.EventNameIndex" />
9697
<fileBasedIndex implementation="com.magento.idea.magento2plugin.stubs.indexes.VirtualTypeIndex" />
9798
<fileBasedIndex implementation="com.magento.idea.magento2plugin.stubs.indexes.PluginIndex" />
99+
<fileBasedIndex implementation="com.magento.idea.magento2plugin.stubs.indexes.CronGroupIndexer" />
98100
<fileBasedIndex implementation="com.magento.idea.magento2plugin.stubs.indexes.BlockNameIndex" />
99101
<fileBasedIndex implementation="com.magento.idea.magento2plugin.stubs.indexes.ContainerNameIndex" />
100102
<fileBasedIndex implementation="com.magento.idea.magento2plugin.stubs.indexes.WebApiTypeIndex" />
@@ -157,6 +159,8 @@
157159
<internalFileTemplate name="Magento Observer Class"/>
158160
<internalFileTemplate name="Magento Module Events Xml"/>
159161
<internalFileTemplate name="Magento GraphQL Resolver Class"/>
162+
<internalFileTemplate name="Magento Cronjob Class"/>
163+
<internalFileTemplate name="Magento Crontab Xml"/>
160164
</extensions>
161165

162166
<extensions defaultExtensionNs="com.jetbrains.php">
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#if (${CRON_GROUP})
2+
<group id="${CRON_GROUP}">
3+
#end
4+
<job name="${CRONJOB_NAME}" instance="${CRONJOB_INSTANCE}" method="execute">
5+
#if (${CRONJOB_SCHEDULE})
6+
<schedule>${CRONJOB_SCHEDULE}</schedule>
7+
#end
8+
#if (${CRONJOB_SCHEDULE_CONFIG_PATH})
9+
<config_path>${CRONJOB_SCHEDULE_CONFIG_PATH}</config_path>
10+
#end
11+
</job>
12+
#if (${CRON_GROUP})
13+
</group>
14+
#end
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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">Each cronjob should be registered in crontab.xml in order to be executed on schedule.</font>
13+
</td>
14+
</tr>
15+
<tr>
16+
<td>
17+
<font face="verdana" size="-1">
18+
<a href="https://devdocs.magento.com/guides/v2.3/config-guide/cron/custom-cron-tut.html">Configure a custom cron job and cron group (tutorial)</a>
19+
</font>
20+
</td>
21+
</tr>
22+
</table>
23+
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
24+
<tr>
25+
<td colspan="3"><font face="verdana" size="-1">Predefined variables explanation:</font></td>
26+
</tr>
27+
<tr>
28+
<td valign="top"><nobr><font face="verdana" size="-2"><b>${CRON_GROUP}</b></font></nobr></td>
29+
<td width="10">&nbsp;</td>
30+
<td width="100%" valign="top"><font face="verdana" size="-1">A cron group in scope of which the cronjob will be executed.</font></td>
31+
</tr>
32+
<tr>
33+
<td valign="top"><nobr><font face="verdana" size="-2"><b>${CRONJOB_NAME}</b></font></nobr></td>
34+
<td width="10">&nbsp;</td>
35+
<td width="100%" valign="top"><font face="verdana" size="-1">An arbitrary cronjob identifier. Also used to merge the configurations for cronjobs.</font></td>
36+
</tr>
37+
<tr>
38+
<td valign="top"><nobr><font face="verdana" size="-2"><b>${CRONJOB_INSTANCE}</b></font></nobr></td>
39+
<td width="10">&nbsp;</td>
40+
<td width="100%" valign="top"><font face="verdana" size="-1">Classname of the cronjob</font></td>
41+
</tr>
42+
<tr>
43+
<td valign="top"><nobr><font face="verdana" size="-2"><b>${CRONJOB_SCHEDULE}</b></font></nobr></td>
44+
<td width="10">&nbsp;</td>
45+
<td width="100%" valign="top"><font face="verdana" size="-1">The valid fixed cron schedule expression which defines when cronjob is planed to execute</font></td>
46+
</tr>
47+
<tr>
48+
<td valign="top"><nobr><font face="verdana" size="-2"><b>${CRONJOB_SCHEDULE_CONFIG_PATH}</b></font></nobr></td>
49+
<td width="10">&nbsp;</td>
50+
<td width="100%" valign="top"><font face="verdana" size="-1">A valid Magento config path which defines a cron schedule expression which may be configured from Magento Admin</font></td>
51+
</tr>
52+
</table>
53+
</body>
54+
</html>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
#parse("PHP File Header.php")
3+
4+
#if (${NAMESPACE})
5+
6+
namespace ${NAMESPACE};
7+
8+
#end
9+
10+
class ${NAME}
11+
{
12+
/**
13+
* Cronjob Description
14+
*
15+
* @return void
16+
*/
17+
public function execute(): void
18+
{
19+
// todo: implement cronjob logic here
20+
}
21+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!--
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
-->
7+
<html lang="en">
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+
Cronjob class is a component that is being executed on schedule.
14+
It's a plain PHP class which should have one method and should be responsible for single background work.
15+
</font>
16+
</td>
17+
</tr>
18+
<tr>
19+
<td>
20+
<font face="verdana" size="-1">
21+
<a href="https://devdocs.magento.com/guides/v2.3/config-guide/cron/custom-cron-tut.html">Configure a custom cron job and cron group (tutorial)</a>
22+
</font>
23+
</td>
24+
</tr>
25+
</table>
26+
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">Template's variables:</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 cronjob 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 cronjob name.</font></td>
40+
</tr>
41+
</table>
42+
</body>
43+
</html>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
3+
</config>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!--
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
-->
7+
<html lang="en">
8+
<body>
9+
<font face="verdana" size="-1">
10+
<p>
11+
crontab.xml file is the place where cronjobs are declared to adjust the system.
12+
</p>
13+
<p>
14+
Read more about <a href="https://devdocs.magento.com/guides/v2.3/config-guide/cron/custom-cron-ref.html">Custom cron job and cron group reference</a>.
15+
</p>
16+
</font>
17+
18+
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
19+
<tr>
20+
<td colspan="3"><font face="verdana" size="-1">crontab.xml should be defined in the global area</font></td>
21+
</tr>
22+
</table>
23+
</body>
24+
</html>

resources/magento2/validation.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ validator.file.cantBeCreated={0} can't be created
1111
validator.class.alreadyDeclared={0} already declared in the target module
1212
validator.magentoVersionInvalid=Please specify valid Magento version or use 'any' keyword as default
1313
validator.class.targetClassNotFound=Target class is not found. Check the di.xml file
14+
validator.cronSchedule.invalidExpression={0} has invalid cron schedule expression (e.g. * * * * *)
15+
validator.configPath.invalidFormat={0} has invalid config path format (e.g. section/group/field)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
package com.magento.idea.magento2plugin.actions.generation;
6+
7+
import com.intellij.ide.IdeView;
8+
import com.intellij.openapi.actionSystem.*;
9+
import com.intellij.openapi.project.Project;
10+
import com.intellij.psi.PsiDirectory;
11+
import com.magento.idea.magento2plugin.MagentoIcons;
12+
import com.magento.idea.magento2plugin.actions.generation.dialog.NewCronjobDialog;
13+
import com.magento.idea.magento2plugin.indexes.CronGroupIndex;
14+
import org.jetbrains.annotations.NotNull;
15+
16+
public class NewCronjobAction extends AnAction {
17+
public static String ACTION_NAME = "Magento 2 Cronjob";
18+
public static String ACTION_DESCRIPTION = "Create a new Magento 2 cronjob";
19+
20+
NewCronjobAction() {
21+
super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
22+
}
23+
24+
@Override
25+
public void actionPerformed(@NotNull AnActionEvent e) {
26+
DataContext dataContext = e.getDataContext();
27+
IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
28+
29+
if (view == null) {
30+
return;
31+
}
32+
33+
Project project = CommonDataKeys.PROJECT.getData(dataContext);
34+
if (project == null) {
35+
return;
36+
}
37+
38+
PsiDirectory directory = view.getOrChooseDirectory();
39+
if (directory == null) {
40+
return;
41+
}
42+
43+
NewCronjobDialog.open(project, directory);
44+
}
45+
46+
@Override
47+
public boolean isDumbAware() {
48+
return false;
49+
}
50+
}
51+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
package com.magento.idea.magento2plugin.actions.generation.data;
6+
7+
public class CronjobClassData {
8+
private String directory;
9+
private String className;
10+
private String namespace;
11+
private String moduleName;
12+
13+
/**
14+
* @param cronjobClassName
15+
* @param cronjobDirectory
16+
* @param cronjobNamespace
17+
* @param cronjobModule
18+
*/
19+
public CronjobClassData(
20+
String cronjobClassName,
21+
String cronjobDirectory,
22+
String cronjobNamespace,
23+
String cronjobModule
24+
) {
25+
this.className = cronjobClassName;
26+
this.directory = cronjobDirectory;
27+
this.namespace = cronjobNamespace;
28+
this.moduleName = cronjobModule;
29+
}
30+
31+
public String getClassName() {
32+
return className;
33+
}
34+
35+
public void setClassName(String className) {
36+
this.className = className;
37+
}
38+
39+
public String getDirectory() {
40+
return directory;
41+
}
42+
43+
public void setDirectory(String directory) {
44+
this.directory = directory;
45+
}
46+
47+
public String getModuleName() {
48+
return moduleName;
49+
}
50+
51+
public void setModuleName(String moduleName) {
52+
this.moduleName = moduleName;
53+
}
54+
55+
public String getNamespace() {
56+
return namespace;
57+
}
58+
59+
public void setNamespace(String namespace) {
60+
this.namespace = namespace;
61+
}
62+
}

0 commit comments

Comments
 (0)