Skip to content
Merged
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
82 changes: 67 additions & 15 deletions src/PrestashopConsole/Command/Module/Generate/ControllerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ class ControllerCommand extends Command
/** @var Filesystem */
protected $_fileSystem;

/** @var array Files created during execution */
protected $_createdFiles = [];

/** @var array Directories created during execution */
protected $_createdDirectories = [];

protected function configure(): void
{
$this
Expand All @@ -68,6 +74,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->_controllerType = $input->getArgument('controllerType');
$this->_template = $input->getOption('template');
$this->_fileSystem = new Filesystem();
$this->_createdFiles = [];
$this->_createdDirectories = [];

if (!is_dir(_PS_MODULE_DIR_ . $this->_moduleName)) {
$output->writeln('<error>Module not exists</error>');
Expand Down Expand Up @@ -104,18 +112,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$defaultContent = str_replace('{controllerClass}', $controllerClass, $defaultContent);

$controllerPath = _PS_MODULE_DIR_ . $this->_moduleName . '/controllers/' . $this->_controllerType . '/' . strtolower($this->_controllerName) . '.php';

try {
$this->_fileSystem->dumpFile(
_PS_MODULE_DIR_ . $this->_moduleName . '/controllers/' . $this->_controllerType . '/' . strtolower($this->_controllerName) . '.php',
$defaultContent
);
$this->_fileSystem->dumpFile($controllerPath, $defaultContent);
$this->_createdFiles[] = $this->_getRelativePath($controllerPath);
} catch (IOException $e) {
$output->writeln('<error>Unable to creat controller directories</error>');

return self::RESPONSE_ERROR;
}

$output->writeln('<info>Controller ' . $this->_controllerName . ' created with sucess');
$output->writeln('<info>Controller ' . $this->_controllerName . ' created with success</info>');
$this->_displayCreatedFiles($output);

return self::RESPONSE_SUCCESS;
}
Expand All @@ -131,16 +140,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
*/
protected function _createDirectories(): void
{
if (!$this->_fileSystem->exists(_PS_MODULE_DIR_ . $this->_moduleName . '/controllers/admin')) {
$this->_fileSystem->mkdir(_PS_MODULE_DIR_ . $this->_moduleName . '/controllers/admin', 0775);
$adminDir = _PS_MODULE_DIR_ . $this->_moduleName . '/controllers/admin';
if (!$this->_fileSystem->exists($adminDir)) {
$this->_fileSystem->mkdir($adminDir, 0775);
$this->_createdDirectories[] = $this->_getRelativePath($adminDir);
}

if (!$this->_fileSystem->exists(_PS_MODULE_DIR_ . $this->_moduleName . '/controllers/front')) {
$this->_fileSystem->mkdir(_PS_MODULE_DIR_ . $this->_moduleName . '/controllers/front', 0775);
$frontDir = _PS_MODULE_DIR_ . $this->_moduleName . '/controllers/front';
if (!$this->_fileSystem->exists($frontDir)) {
$this->_fileSystem->mkdir($frontDir, 0775);
$this->_createdDirectories[] = $this->_getRelativePath($frontDir);
}

if (!$this->_fileSystem->exists(_PS_MODULE_DIR_ . $this->_moduleName . '/views/templates/front')) {
$this->_fileSystem->mkdir(_PS_MODULE_DIR_ . $this->_moduleName . '/views/templates/front', 0775);
$templatesDir = _PS_MODULE_DIR_ . $this->_moduleName . '/views/templates/front';
if (!$this->_fileSystem->exists($templatesDir)) {
$this->_fileSystem->mkdir($templatesDir, 0775);
$this->_createdDirectories[] = $this->_getRelativePath($templatesDir);
}

/*$indexCommand = $this->getApplication()->find('dev:add-index-files');
Expand Down Expand Up @@ -220,9 +235,46 @@ protected function _generateTemplate(): void
<p>Controller template generated by PrestashopConsole to edit</p>
{/block}
';
$this->_fileSystem->dumpFile(
_PS_MODULE_DIR_ . $this->_moduleName . '/views/templates/front/' . $this->_controllerName . '.tpl',
$defaultTemplateContent
);
$templatePath = _PS_MODULE_DIR_ . $this->_moduleName . '/views/templates/front/' . $this->_controllerName . '.tpl';
$this->_fileSystem->dumpFile($templatePath, $defaultTemplateContent);
$this->_createdFiles[] = $this->_getRelativePath($templatePath);
}

/**
* Get relative path from PrestaShop root
*
* @param string $absolutePath
*
* @return string
*/
protected function _getRelativePath(string $absolutePath): string
{
return str_replace(_PS_ROOT_DIR_ . '/', '', $absolutePath);
}

/**
* Display created files and directories
*
* @param OutputInterface $output
*
* @return void
*/
protected function _displayCreatedFiles(OutputInterface $output): void
{
if (!empty($this->_createdDirectories)) {
$output->writeln('');
$output->writeln('<comment>Created directories:</comment>');
foreach ($this->_createdDirectories as $dir) {
$output->writeln(' - ' . $dir);
}
}

if (!empty($this->_createdFiles)) {
$output->writeln('');
$output->writeln('<comment>Created files:</comment>');
foreach ($this->_createdFiles as $file) {
$output->writeln(' - ' . $file);
}
}
}
}