Skip to content

Commit

Permalink
Merge pull request #46 from webkul/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
webkul committed May 13, 2024
2 parents da33720 + 5a87f2a commit b88be8e
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Api/GenerateInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Webkul Software.
*
* @package Webkul_CodeGenerator
* @author Ashutosh Srivastva
* @author Webkul Software Pvt Ltd
*/

namespace Webkul\CodeGenerator\Api;
Expand Down
2 changes: 1 addition & 1 deletion Api/ValidatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Webkul Software.
*
* @package Webkul_CodeGenerator
* @author Ashutosh Srivastva
* @author Webkul Software Pvt Ltd
*/

namespace Webkul\CodeGenerator\Api;
Expand Down
7 changes: 5 additions & 2 deletions Console/Command/Generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Webkul Software.
*
* @package Webkul_CodeGenerator
* @author Ashutosh Srivastva
* @author Webkul Software Pvt Ltd
*/

namespace Webkul\CodeGenerator\Console\Command;
Expand Down Expand Up @@ -859,7 +859,10 @@ public function addFormField($input, $output, $questionHelper)
}

if (!$input->getOption('field_type')) {
$question = new Question('<question>Enter Field Type (input/select/multiselect/imageUploader):</question> ', 'input');
$question = new Question(
'<question>Enter Field Type (input/select/multiselect/imageUploader):</question> ',
'input'
);
$this->addNotEmptyValidator($question);
$input->setOption(
"field_type",
Expand Down
43 changes: 39 additions & 4 deletions Model/Generate/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Webkul Software.
*
* @package Webkul_CodeGenerator
* @author Ashutosh Srivastva
* @author Webkul Software Pvt Ltd
*/

namespace Webkul\CodeGenerator\Model\Generate;
Expand All @@ -17,6 +17,8 @@
use Laminas\Code\Generator\ParameterGenerator;
use Magento\Framework\Setup\Declaration\Schema\Declaration\ReaderComposite;
use Webkul\CodeGenerator\Model\Helper;
use Magento\Framework\Simplexml\Config;
use Webkul\CodeGenerator\Model\XmlGeneratorFactory;

/**
* Generate Model.php
Expand All @@ -38,10 +40,12 @@ class Model implements GenerateInterface
* __construct function
*
* @param ReaderComposite $readerComposite
* @param XmlGeneratorFactory $xmlGenerator
* @param Helper $helper
*/
public function __construct(
ReaderComposite $readerComposite,
protected XmlGeneratorFactory $xmlGenerator,
Helper $helper
) {
$this->readerComposite = $readerComposite;
Expand Down Expand Up @@ -89,10 +93,15 @@ public function execute($data)
$apiDataDirPath = $apiDataDirPath.DIRECTORY_SEPARATOR.'Data'
);

$this->helper->createDirectory(
$etcDirPath = $path.DIRECTORY_SEPARATOR.'etc'
);

$this->createApiClass($apiDataDirPath, $data, $columns);
$this->createModelClass($modelDirPath, $data, $columns);
$this->createResourceModelClass($rModelDirPath, $data, $identityColumn);
$this->createCollectionClass($collectionDirPath, $data, $identityColumn);
$this->addDiXmlData($etcDirPath, $data);

return ['status' => 'success', 'message' => "model generated successfully"];
}
Expand Down Expand Up @@ -205,8 +214,8 @@ public function createModelClass($dir, $data, $columns)
{
$moduleNamespace = explode('_', $data['module']);
$nameSpace = $moduleNamespace[0].'\\'.$moduleNamespace[1].'\\Model';
$parentClass = "Magento\\Framework\\Model\\AbstractModel";
$parentInterface = "Magento\\Framework\\DataObject\\IdentityInterface";
$parentClass = "Magento".'\\'."Framework\\Model\\AbstractModel";
$parentInterface = "Magento".'\\'."Framework\\DataObject\\IdentityInterface";
$apiInterface = $moduleNamespace[0].'\\'.$moduleNamespace[1].'\\Api\\Data\\'.$data['name'].'Interface';
$resourceClass = '\\'.$nameSpace.'\\ResourceModel\\'.$data['name'];
$modelClass = new ClassGenerator();
Expand All @@ -229,7 +238,8 @@ public function createModelClass($dir, $data, $columns)
// MethodGenerator::fromArray([
// 'name' => 'load',
// 'parameters' => ['id', 'field'],
// 'body' => 'if ($id === null) {'. "\n". 'return $this->noRouteReasons();'. "\n". '}'. "\n". 'return parent::load($id, $field);',
// 'body' => 'if ($id === null) {'. "\n". 'return $this->noRouteReasons();'. "\n". '}'.
// "\n". 'return parent::load($id, $field);',
// 'docblock' => DocBlockGenerator::fromArray([
// 'shortDescription' => 'load model',
// 'longDescription' => "",
Expand Down Expand Up @@ -466,4 +476,29 @@ public function createCollectionClass($collectionDirPath, $data, $identityColumn
$file->generate()
);
}

/**
* Add di xml data
*
* @param string $etcDirPath
* @param array $data
* @return void
*/
public function addDiXmlData($etcDirPath, $data)
{
$moduleName = $data['module'];
$data['model-class'] = str_replace('_', '\\', $moduleName).'\\'.'Model'.'\\'.$data['name'];
$data['api-class'] = str_replace('_', '\\', $moduleName).'\\'.'Api'.'\\'.$data['name'].'Interface';
$diXmlFile = $this->helper->getDiXmlFile($etcDirPath, $data);
$xmlObj = new Config($diXmlFile);
$diXml = $xmlObj->getNode();
$typeNode = $this->xmlGenerator->create()->addXmlNode(
$diXml,
'preference',
'',
['for' => $data['api-class'], 'type' => $data['model-class']]
);
$xmlData = $this->xmlGenerator->create()->formatXml($diXml->asXml());
$this->helper->saveFile($diXmlFile, $xmlData);
}
}
2 changes: 1 addition & 1 deletion Model/Generate/Model/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Webkul Software.
*
* @package Webkul_CodeGenerator
* @author Ashutosh Srivastva
* @author Webkul Software Pvt Ltd
*/

namespace Webkul\CodeGenerator\Model\Generate\Model;
Expand Down
2 changes: 1 addition & 1 deletion Model/Generate/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Webkul Software.
*
* @package Webkul_CodeGenerator
* @author Ashutosh Srivastva
* @author Webkul Software Pvt Ltd
*/

namespace Webkul\CodeGenerator\Model\Generate;
Expand Down
8 changes: 4 additions & 4 deletions Model/Generate/Repository/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Webkul Software.
*
* @package Webkul_CodeGenerator
* @author Ashutosh Srivastva
* @author Webkul Software Pvt Ltd
*/

namespace Webkul\CodeGenerator\Model\Generate\Repository;
Expand All @@ -21,9 +21,9 @@ public function validate($data)
$module = $data['module'];
$type = $data['type'];
$name = $data['name'];
$path = $data['path']??null;
$modelClass = $data['model-class']??null;
$collectionClass = $data['collection-class']??null;
$path = $data['path'] ?? null;
$modelClass = $data['model-class'] ?? null;
$collectionClass = $data['collection-class'] ?? null;
$response = [];
$response['type'] = $type;
if ($module) {
Expand Down
2 changes: 1 addition & 1 deletion Model/GeneratorPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Webkul Software.
*
* @package Webkul_CodeGenerator
* @author Ashutosh Srivastva
* @author Webkul Software Pvt Ltd
*/

namespace Webkul\CodeGenerator\Model;
Expand Down
30 changes: 20 additions & 10 deletions Model/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Webkul Software.
*
* @package Webkul_CodeGenerator
* @author Ashutosh Srivastva
* @author Webkul Software Pvt Ltd
*/

namespace Webkul\CodeGenerator\Model;
Expand All @@ -15,6 +15,16 @@

class Helper
{
/**
* __construct function
*
* @param \Magento\Framework\Filesystem\Driver\File $driverFile
*/
public function __construct(
protected \Magento\Framework\Filesystem\Driver\File $driverFile
) {
}

/**
* Save File
*
Expand All @@ -24,10 +34,7 @@ class Helper
*/
public function saveFile($path, $content)
{
file_put_contents(
$path,
$content
);
$this->driverFile->filePutContents($path, $content);
}

/**
Expand All @@ -46,7 +53,6 @@ public function getHeadDocBlock($moduleName)
new Tag\GenericTag('author', 'Webkul'),
new Tag\GenericTag('copyright', 'Copyright (c) Webkul Software Private Limited (https://webkul.com)'),
new Tag\LicenseTag('https://store.webkul.com/license.html', '')

],
]);
}
Expand Down Expand Up @@ -80,8 +86,8 @@ public function getReturnType($type = 'default')
*/
public function createDirectory($dirPath, $permission = 0777)
{
if (!is_dir($dirPath)) {
mkdir($dirPath, $permission, true);
if (!$this->driverFile->isDirectory($dirPath)) {
$this->driverFile->createDirectory($dirPath, $permission);
}
}

Expand All @@ -93,7 +99,11 @@ public function createDirectory($dirPath, $permission = 0777)
*/
public function getTemplatesFiles($template)
{
return file_get_contents(dirname(dirname(__FILE__)). DIRECTORY_SEPARATOR. $template);
return $this->driverFile->fileGetContents(
$this->driverFile->getParentDirectory(
$this->driverFile->getParentDirectory(__FILE__)
). DIRECTORY_SEPARATOR. $template
);
}

/**
Expand All @@ -108,7 +118,7 @@ public function getTemplatesFiles($template)
public function loadTemplateFile($path, $fileName, $templatePath, $replace = [])
{
$filePath = $path.DIRECTORY_SEPARATOR.$fileName;
if (!file_exists($filePath)) {
if (!$this->driverFile->isExists($filePath)) {
$data = $this->getTemplatesFiles($templatePath);
if (!empty($replace) && is_array($replace)) {
foreach ($replace as $find => $value) {
Expand Down
2 changes: 1 addition & 1 deletion Model/OptionsPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Webkul Software.
*
* @package Webkul_CodeGenerator
* @author Ashutosh Srivastva
* @author Webkul Software Pvt Ltd
*/

namespace Webkul\CodeGenerator\Model;
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "webkul/code-generator",
"description": "module lets you generate models, controller, helpers in seconds so that you can focus more on application logic ",
"type": "magento2-module",
"version": "2.0.7",
"version": "2.0.8",
"license": [
"proprietary"
],
Expand Down
2 changes: 1 addition & 1 deletion etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Webkul Software.
*
* @package Webkul_CodeGenerator
* @author Ashutosh Srivastva
* @author Webkul Software Pvt Ltd
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Webkul Software.
*
* @package Webkul_CodeGenerator
* @author Ashutosh Srivastva
* @author Webkul Software Pvt Ltd
*/ -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Webkul_CodeGenerator">
Expand Down
2 changes: 1 addition & 1 deletion registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Webkul Software.
*
* @package Webkul_CodeGenerator
* @author Ashutosh Srivastva
* @author Webkul Software Pvt Ltd
*/

\Magento\Framework\Component\ComponentRegistrar::register(
Expand Down

0 comments on commit b88be8e

Please sign in to comment.