Skip to content

Commit

Permalink
Merge pull request #44 from SauravKumar-Webkul/master
Browse files Browse the repository at this point in the history
Master Dev Merge
  • Loading branch information
webkul authored May 13, 2024
2 parents 566d31e + 3784b19 commit 5a87f2a
Show file tree
Hide file tree
Showing 24 changed files with 89 additions and 92 deletions.
48 changes: 0 additions & 48 deletions .vscode/launch.json

This file was deleted.

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
9 changes: 5 additions & 4 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 @@ -52,8 +52,6 @@ public function __construct(
$this->validators = $validators;
$this->optionsPool = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Webkul\CodeGenerator\Model\OptionsPool::class);
$state = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\App\State::class);
$state->setAreaCode("adminhtml");
parent::__construct();
}

Expand Down Expand Up @@ -861,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
47 changes: 41 additions & 6 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 @@ -144,7 +153,7 @@ public function createApiClass($dir, $data, $columns)
$column['type']
)]),
new Tag\ReturnTag([
'datatype' => $nameSpace.'\\'.$data['name'].'Interface',
'datatype' => '\\'.$nameSpace.'\\'.$data['name'].'Interface',
]),
],
]),
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 @@ -299,7 +309,7 @@ public function createModelClass($dir, $data, $columns)
'tags' => [
new Tag\ParamTag($camelCase, [$this->helper->getReturnType($column['type'])]),
new Tag\ReturnTag([
'datatype' => $nameSpace.'\\'.$data['name'].'Interface',
'datatype' => '\\'.$nameSpace.'\\'.$data['name'].'Interface',
]),
],
]),
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/NewModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Webkul Software.
*
* @package Webkul_CodeGenerator
* @author Mahesh Singh
* @author Webkul
*/

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

namespace Webkul\CodeGenerator\Model\Generate\NewModule;
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/Generate/Shipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Webkul Software.
*
* @package Webkul_CodeGenerator
* @author Mahesh Singh
* @author Webkul
*/

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

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

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

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
Loading

0 comments on commit 5a87f2a

Please sign in to comment.