Skip to content

Data model generation integration with entity manager #461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
#parse("PHP File Header.php")
declare(strict_types=1);

#if (${NAMESPACE})
namespace ${NAMESPACE};
Expand Down Expand Up @@ -28,15 +29,20 @@ interface ${NAME}
#set ($propertyLowerCamel = $propertyData.get(4))

/**
* @return $propertyType
* Getter for $propertyUpperCamel.
*
* @return $propertyType|null
*/
public function get$propertyUpperCamel();
public function get$propertyUpperCamel(): ?$propertyType;

/**
* @param $propertyType $$propertyLowerCamel
* @return $this
* Setter for $propertyUpperCamel.
*
* @param $propertyType|null $$propertyLowerCamel
*
* @return void
*/
public function set$propertyUpperCamel($$propertyLowerCamel);
public function set$propertyUpperCamel(?$propertyType $$propertyLowerCamel): void;
#end
#end
}
51 changes: 47 additions & 4 deletions resources/fileTemplates/internal/Magento Data Model.php.ft
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
#parse("PHP File Header.php")
declare(strict_types=1);

#if (${NAMESPACE})
namespace ${NAMESPACE};
Expand All @@ -9,33 +10,75 @@
#foreach ($use in $uses.split(","))
use $use;
#end
#if (${HASINTERFACE} == "true")
#set ($hasInterface = true)
#else
#set ($hasInterface = false)
#end

class ${NAME} #if (${EXTENDS})extends ${EXTENDS} #end #if (${IMPLEMENTS})implements ${IMPLEMENTS}#end
class ${NAME} #if (${EXTENDS})extends ${EXTENDS} #end #if (${IMPLEMENTS} && $hasInterface)implements ${IMPLEMENTS}#end
{
#if (${PROPERTIES} && !$hasInterface)
/**
* String constants for property names
*/
#set ($properties = ${PROPERTIES})
#foreach ($property in $properties.split(","))
#set ($propertyData = $property.split(";"))
#set ($propertyUpperSnake = $propertyData.get(0))
#set ($propertyLowerSnake = $propertyData.get(1))
const $propertyUpperSnake = "$propertyLowerSnake";
#end

#end
#if (${PROPERTIES})
#set ($properties = ${PROPERTIES})
#foreach ($property in $properties.split(","))
#set ($propertyData = $property.split(";"))
#set ($propertyUpperSnake = $propertyData.get(0))
#set ($propertyType = $propertyData.get(2))
#set ($propertyUpperCamel = $propertyData.get(3))
#set ($propertyLowerCamel = $propertyData.get(4))
#if(!($foreach.first))

#end
#if ($hasInterface)
/**
* @inheritDoc
*/
public function get$propertyUpperCamel()
#else
/**
* Getter for $propertyUpperCamel.
*
* @return $propertyType|null
*/
#end
public function get$propertyUpperCamel(): ?$propertyType
{
#if($propertyType == 'string')
return $this->getData(self::$propertyUpperSnake);
#{else}
return $this->getData(self::$propertyUpperSnake) === null ? null
: ($propertyType) $this->getData(self::$propertyUpperSnake);
#end
}

#if ($hasInterface)
/**
* @inheritDoc
*/
public function set$propertyUpperCamel($$propertyLowerCamel)
#else
/**
* Setter for $propertyUpperCamel.
*
* @param $propertyType|null $$propertyLowerCamel
*
* @return void
*/
#end
public function set$propertyUpperCamel(?$propertyType $$propertyLowerCamel): void
{
return $this->setData(self::$propertyUpperSnake, $$propertyLowerCamel);
$this->setData(self::$propertyUpperSnake, $$propertyLowerCamel);
}
#end
#end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,97 @@ public class DataModelData {
private final String fqn;
private final String interfaceFQN;
private final String properties;
private final boolean withInterface;

/**
* Constructor.
*
* @param namespace String
* @param name String
* @param moduleName String
* @param fqn String
* @param interfaceFQN String
* @param properties String
* @param hasInterface boolean
*/
public DataModelData(
final String namespace,
final String name,
final String moduleName,
final String fqn,
final String interfaceFQN,
final String properties
final String properties,
final boolean hasInterface
) {
this.namespace = namespace;
this.name = name;
this.moduleName = moduleName;
this.fqn = fqn;
this.interfaceFQN = interfaceFQN;
this.properties = properties;
this.withInterface = hasInterface;
}

/**
* Get Namespace.
*
* @return String
*/
public String getNamespace() {
return namespace;
}

/**
* Get Name.
*
* @return String
*/
public String getName() {
return name;
}

/**
* Get module name.
*
* @return String
*/
public String getModuleName() {
return moduleName;
}

/**
* Get FQN.
*
* @return String
*/
public String getFQN() {
return fqn;
}

/**
* Get Interface FQN.
*
* @return String
*/
public String getInterfaceFQN() {
return interfaceFQN;
}

/**
* Get Properties.
*
* @return String
*/
public String getProperties() {
return properties;
}

/**
* Check if model has interface.
*
* @return boolean
*/
public boolean hasInterface() {
return withInterface;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.magento.idea.magento2plugin.actions.generation.dialog.NewDataModelDialog">
<grid id="cbd77" binding="contentPanel" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="cbd77" binding="contentPanel" layout-manager="GridLayoutManager" row-count="4" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<xy x="48" y="54" width="463" height="423"/>
<xy x="48" y="54" width="618" height="423"/>
</constraints>
<properties>
<preferredSize width="500" height="600"/>
Expand Down Expand Up @@ -41,7 +41,7 @@
<grid id="ebe15" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="5" left="5" bottom="5" right="5"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="empty" title="Properties" title-justification="2" title-position="2"/>
Expand Down Expand Up @@ -89,7 +89,7 @@
<grid id="94766" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
Expand Down Expand Up @@ -127,6 +127,15 @@
</grid>
</children>
</grid>
<component id="530db" class="javax.swing.JCheckBox" binding="createInterface">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<selected value="true"/>
<text value="Create Interface"/>
</properties>
</component>
</children>
</grid>
</form>
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public class NewDataModelDialog extends AbstractDialog {
private JButton buttonCancel;
private JTable propertyTable;
private JButton addProperty;
private JCheckBox createInterface;

@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
message = {NotEmptyRule.MESSAGE, MODEL_NAME})
Expand Down Expand Up @@ -136,9 +137,12 @@ private void onOK() {
if (validateFormFields()) {
buildNamespaces();
formatProperties();
generateModelInterfaceFile();
generateModelFile();
generatePreference();

if (isInterfaceShouldBeCreated()) {
generateModelInterfaceFile();
generatePreference();
}
this.setVisible(false);
}
}
Expand Down Expand Up @@ -205,7 +209,8 @@ private void generateModelFile() {
getModuleName(),
getModelFQN(),
getInterfaceFQN(),
getProperties()
getProperties(),
isInterfaceShouldBeCreated()
)).generate(NewDataModelAction.ACTION_NAME, true);
}

Expand Down Expand Up @@ -250,6 +255,10 @@ private void formatProperties() {
}
}

private boolean isInterfaceShouldBeCreated() {
return createInterface.isSelected();
}

private String getModuleName() {
return moduleName;
}
Expand Down
Loading