Skip to content
Closed
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
13 changes: 10 additions & 3 deletions src/ApiGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,17 @@ public static function generate(): void
errorMessage: 'Inválido, debe comenzar con /api/3/ y tener solo letras, números, guiones o barras.'
);

$sample = file_get_contents(dirname(__DIR__, 1) . "/samples/ApiController.php.sample");
$sample = Utils::readFile(dirname(__DIR__, 1) . "/samples/ApiController.php.sample");
if ($sample === false) {
return;
}
$template = str_replace(['[[NAME_SPACE]]', '[[NAME]]'], [Utils::getNamespace(), $name], $sample);
Utils::createFolder('Controller');
file_put_contents($file_path, $template);
if (false === Utils::createFolder('Controller')) {
return;
}
if (!Utils::writeFile($file_path, $template)) {
return;
}
Utils::echo('* ' . $file_path . " -> OK.\n");

$use = "use FacturaScripts\Core\Controller\ApiRoot;\n"
Expand Down
107 changes: 73 additions & 34 deletions src/Command/Controller/ControllerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,36 +52,33 @@ protected function execute(InputInterface $input, OutputInterface $output): int

switch ($option) {
case 'Controller':
$this->createController($modelName);
return Command::SUCCESS;
return $this->createController($modelName) ? Command::SUCCESS : Command::FAILURE;

case 'ListController':
$fields = Column::askMulti();
$this->createListController($modelName, $fields);
return Command::SUCCESS;
return $this->createListController($modelName, $fields) ? Command::SUCCESS : Command::FAILURE;

case 'EditController':
$fields = Column::askMulti();
$this->createEditController($modelName, $fields);
return Command::SUCCESS;
return $this->createEditController($modelName, $fields) ? Command::SUCCESS : Command::FAILURE;
}

Utils::echo("Opción no válida.\n");
return Command::FAILURE;
}

private function createController(string $name): void
private function createController(string $name): bool
{
if (empty($name)) {
Utils::echo("* No introdujo el nombre del controlador.\n");
return;
return false;
}

$filePath = Utils::isCoreFolder() ? 'Core/Controller/' : 'Controller/';
$fileName = $filePath . $name . '.php';
if (file_exists($fileName)) {
Utils::echo("* El controlador " . $name . " YA EXISTE.\n");
return;
return false;
}

$menu = text(
Expand All @@ -98,48 +95,66 @@ private function createController(string $name): void
);

$samplePath = dirname(__DIR__, 3) . "/samples/Controller.php.sample";
$sample = file_get_contents($samplePath);
$sample = Utils::readFile($samplePath);
if ($sample === false) {
return false;
}

if (!$createView) {
$search = "\n\n \$this->view('[[NAME]].html.twig');";
$sample = str_replace($search, '', $sample);
}

$template = str_replace(['[[NAME_SPACE]]', '[[NAME]]', '[[MENU]]'], [Utils::getNamespace(), $name, $menu], $sample);
Utils::createFolder($filePath);
file_put_contents($fileName, $template);
if (false === Utils::createFolder($filePath)) {
return false;
}
if (!Utils::writeFile($fileName, $template)) {
return false;
}
Utils::echo('* ' . $fileName . " -> OK.\n");

if ($createView) {
$viewPath = Utils::isCoreFolder() ? 'Core/View/' : 'View/';
$viewFilename = $viewPath . $name . '.html.twig';
Utils::createFolder($viewPath);
if (false === Utils::createFolder($viewPath)) {
return false;
}
if (file_exists($viewFilename)) {
Utils::echo('* ' . $viewFilename . " YA EXISTE.\n");
return;
return true;
}

$samplePath2 = dirname(__DIR__, 3) . "/samples/View.html.twig.sample";
$sample2 = file_get_contents($samplePath2);
$sample2 = Utils::readFile($samplePath2);
if ($sample2 === false) {
return false;
}
$template2 = str_replace('[[NADA_A_REEMPLAZAR]]', $name, $sample2);
file_put_contents($viewFilename, $template2);
if (!Utils::writeFile($viewFilename, $template2)) {
return false;
}
Utils::echo('* ' . $viewFilename . " -> OK.\n");
}

return true;
}

private function createEditController(string $modelName, array $fields): void
private function createEditController(string $modelName, array $fields): bool
{
if (empty($modelName)) {
Utils::echo('* No introdujo el nombre del EditController');
return;
return false;
}

$filePath = Utils::isCoreFolder() ? 'Core/Controller/' : 'Controller/';
$fileName = $filePath . 'Edit' . $modelName . '.php';
Utils::createFolder($filePath);
if (false === Utils::createFolder($filePath)) {
return false;
}
if (file_exists($fileName)) {
Utils::echo("El controlador " . $fileName . " YA EXISTE.\n");
return;
return false;
}

$menu = text(
Expand All @@ -152,32 +167,43 @@ private function createEditController(string $modelName, array $fields): void
);

$samplePath = dirname(__DIR__, 3) . "/samples/EditController.php.sample";
$sample = file_get_contents($samplePath);
$sample = Utils::readFile($samplePath);
if ($sample === false) {
return false;
}
$template = str_replace(
['[[NAME_SPACE]]', '[[MODEL_NAME]]', '[[MENU]]'],
[Utils::getNamespace(), $modelName, $menu],
$sample
);
file_put_contents($fileName, $template);
if (!Utils::writeFile($fileName, $template)) {
return false;
}
Utils::echo('* ' . $fileName . " -> OK.\n");

$xmlPath = Utils::isCoreFolder() ? 'Core/XMLView/' : 'XMLView/';
$xmlFilename = $xmlPath . 'Edit' . $modelName . '.xml';
Utils::createFolder($xmlPath);
if (false === Utils::createFolder($xmlPath)) {
return false;
}
if (file_exists($xmlFilename)) {
Utils::echo('* ' . $xmlFilename . " YA EXISTE\n");
return;
return true;
}

FileGenerator::createXMLViewByFields($xmlFilename, $fields, 'edit');
if (!FileGenerator::createXMLViewByFields($xmlFilename, $fields, 'edit')) {
return false;
}
Utils::echo('* ' . $xmlFilename . " -> OK.\n");

return true;
}

private function createListController(string $modelName, array $fields): void
private function createListController(string $modelName, array $fields): bool
{
if (empty($modelName)) {
Utils::echo('* No introdujo el nombre del ListController');
return;
return false;
}

$menu = text(
Expand All @@ -200,31 +226,44 @@ private function createListController(string $modelName, array $fields): void

$filePath = Utils::isCoreFolder() ? 'Core/Controller/' : 'Controller/';
$fileName = $filePath . 'List' . $modelName . '.php';
Utils::createFolder($filePath);
if (false === Utils::createFolder($filePath)) {
return false;
}
if (file_exists($fileName)) {
Utils::echo("El controlador " . $fileName . " YA EXISTE.\n");
return;
return false;
}

$samplePath = dirname(__DIR__, 3) . "/samples/ListController.php.sample";
$sample = file_get_contents($samplePath);
$sample = Utils::readFile($samplePath);
if ($sample === false) {
return false;
}
$template = str_replace(
['[[NAME_SPACE]]', '[[MODEL_NAME]]', '[[MENU]]', '[[TITLE]]'],
[Utils::getNamespace(), $modelName, $menu, $title],
$sample
);
file_put_contents($fileName, $template);
if (!Utils::writeFile($fileName, $template)) {
return false;
}
Utils::echo('* ' . $fileName . " -> OK.\n");

$xmlPath = Utils::isCoreFolder() ? 'Core/XMLView/' : 'XMLView/';
$xmlFilename = $xmlPath . 'List' . $modelName . '.xml';
Utils::createFolder($xmlPath);
if (false === Utils::createFolder($xmlPath)) {
return false;
}
if (file_exists($xmlFilename)) {
Utils::echo('* ' . $xmlFilename . " YA EXISTE\n");
return;
return true;
}

FileGenerator::createXMLViewByFields($xmlFilename, $fields, 'list');
if (!FileGenerator::createXMLViewByFields($xmlFilename, $fields, 'list')) {
return false;
}
Utils::echo('* ' . $xmlFilename . " -> OK.\n");

return true;
}
}
9 changes: 7 additions & 2 deletions src/Command/Cron/CronCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$samplePath = dirname(__DIR__, 3) . "/samples/Cron.php.sample";
$sample = file_get_contents($samplePath);
$sample = Utils::readFile($samplePath);
if ($sample === false) {
return Command::FAILURE;
}
$template = str_replace(['[[NAME_SPACE]]', '[[NAME]]'], [Utils::getNamespace(), $name], $sample);
file_put_contents($fileName, $template);
if (!Utils::writeFile($fileName, $template)) {
return Command::FAILURE;
}
Utils::echo('* ' . $fileName . " -> OK.\n");

return Command::SUCCESS;
Expand Down
36 changes: 27 additions & 9 deletions src/Command/Cron/CronJobCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$folder = 'CronJob/';
$plugin = Utils::findPluginName();
Utils::createFolder($folder);
if (false === Utils::createFolder($folder)) {
return Command::FAILURE;
}

$fileName = $folder . $name . '.php';
if (file_exists($fileName)) {
Expand All @@ -42,11 +44,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$samplePath = dirname(__DIR__, 3) . "/samples/CronJob.php.sample";
$sample = file_get_contents($samplePath);
$sample = Utils::readFile($samplePath);
if ($sample === false) {
return Command::FAILURE;
}
$jobName = Utils::kebab($name);
$template = str_replace(['[[NAME_SPACE]]', '[[NAME]]', '[[JOB_NAME]]'], [Utils::getNamespace(), $name, $jobName], $sample);

file_put_contents($fileName, $template);
if (!Utils::writeFile($fileName, $template)) {
return Command::FAILURE;
}
Utils::echo('* ' . $fileName . " -> OK.\n");

if (file_exists('Cron.php')) {
Expand All @@ -68,15 +75,22 @@ private function createCron(string $name): void
}

$samplePath = dirname(__DIR__, 3) . "/samples/Cron.php.sample";
$sample = file_get_contents($samplePath);
$sample = Utils::readFile($samplePath);
if ($sample === false) {
return;
}
$template = str_replace(['[[NAME_SPACE]]', '[[NAME]]'], [Utils::getNamespace(), $name], $sample);
file_put_contents($fileName, $template);
Utils::echo('* ' . $fileName . " -> OK.\n");
if (Utils::writeFile($fileName, $template)) {
Utils::echo('* ' . $fileName . " -> OK.\n");
}
}

private function updateCron(string $name): void
{
$fileStr = file_get_contents('Cron.php');
$fileStr = Utils::readFile('Cron.php');
if ($fileStr === false) {
return;
}
$newJob = <<<END
\n
\$this->job($name::JOB_NAME)
Expand All @@ -92,11 +106,15 @@ private function updateCron(string $name): void
if ($position !== false) {
$position = strpos($fileStr, '{', $position) + 1;
$fileStr = substr_replace($fileStr, $newJob, $position, 0);
file_put_contents('Cron.php', $fileStr);
if (!Utils::writeFile('Cron.php', $fileStr)) {
return;
}
$usePosition = strpos($fileStr, 'use FacturaScripts\Core\Template\CronClass');
$usePosition = strpos($fileStr, ';', $usePosition) + 1;
$fileStr = substr_replace($fileStr, "\nuse FacturaScripts\\$nameSpace\CronJob\\$name;", $usePosition, 0);
file_put_contents('Cron.php', $fileStr);
if (!Utils::writeFile('Cron.php', $fileStr)) {
return;
}
Utils::echo('* Cron.php actualizado' . " -> OK.\n");
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/Command/Init/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$samplePath = dirname(__DIR__, 3) . "/samples/Init.php.sample";
$sample = file_get_contents($samplePath);
$sample = Utils::readFile($samplePath);
if ($sample === false) {
return Command::FAILURE;
}
$template = str_replace('[[NAME]]', Utils::findPluginName(), $sample);
file_put_contents($fileName, $template);
if (!Utils::writeFile($fileName, $template)) {
return Command::FAILURE;
}
Utils::echo('* ' . $fileName . " -> OK.\n");

return Command::SUCCESS;
Expand Down
Loading