diff --git a/src/ApiGenerator.php b/src/ApiGenerator.php index 5422403..db4e7c5 100644 --- a/src/ApiGenerator.php +++ b/src/ApiGenerator.php @@ -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" diff --git a/src/Command/Controller/ControllerCommand.php b/src/Command/Controller/ControllerCommand.php index 26cf428..8d4d3c8 100644 --- a/src/Command/Controller/ControllerCommand.php +++ b/src/Command/Controller/ControllerCommand.php @@ -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( @@ -98,7 +95,10 @@ 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');"; @@ -106,40 +106,55 @@ private function createController(string $name): void } $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( @@ -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( @@ -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; } } diff --git a/src/Command/Cron/CronCommand.php b/src/Command/Cron/CronCommand.php index 6e78695..b77aaa2 100644 --- a/src/Command/Cron/CronCommand.php +++ b/src/Command/Cron/CronCommand.php @@ -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; diff --git a/src/Command/Cron/CronJobCommand.php b/src/Command/Cron/CronJobCommand.php index bd35d21..48dcb91 100644 --- a/src/Command/Cron/CronJobCommand.php +++ b/src/Command/Cron/CronJobCommand.php @@ -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)) { @@ -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')) { @@ -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 = <<job($name::JOB_NAME) @@ -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"); } } diff --git a/src/Command/Init/InitCommand.php b/src/Command/Init/InitCommand.php index 28daff1..7ab51c8 100644 --- a/src/Command/Init/InitCommand.php +++ b/src/Command/Init/InitCommand.php @@ -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; diff --git a/src/Command/Migration/MigrationCommand.php b/src/Command/Migration/MigrationCommand.php index ad090b2..b48933d 100644 --- a/src/Command/Migration/MigrationCommand.php +++ b/src/Command/Migration/MigrationCommand.php @@ -33,32 +33,37 @@ protected function execute(InputInterface $input, OutputInterface $output): int errorMessage: 'Inválido, debe empezar por mayúscula y solo puede contener letras, números y guiones bajos.' ); - $this->createMigration($name); - - return Command::SUCCESS; + return $this->createMigration($name) ? Command::SUCCESS : Command::FAILURE; } - private function createMigration(string $name): void + private function createMigration(string $name): bool { $folder = 'Migration/'; - Utils::createFolder($folder); + if (false === Utils::createFolder($folder)) { + return false; + } $fileName = $folder . $name . '.php'; if (file_exists($fileName)) { Utils::echo("* La migración " . $name . " YA EXISTE.\n"); - return; + return false; } $samplePath = dirname(__DIR__, 3) . "/samples/Migration.php.sample"; - $sample = file_get_contents($samplePath); - + $sample = Utils::readFile($samplePath); + if ($sample === false) { + return false; + } + $migrationNameConst = strtolower(preg_replace('/(? OK.\n"); $newContentUse = InitEditor::addUse('use FacturaScripts\Core\Migrations;'); @@ -70,5 +75,7 @@ private function createMigration(string $name): void if ($newContentFunc) { InitEditor::setInitContent($newContentFunc); } + + return true; } } diff --git a/src/Command/Mod/ModCommand.php b/src/Command/Mod/ModCommand.php index 2067e82..3a501d1 100644 --- a/src/Command/Mod/ModCommand.php +++ b/src/Command/Mod/ModCommand.php @@ -74,12 +74,19 @@ private function createMod(string $name, string $sampleName, string $useClass, s return Command::FAILURE; } - Utils::createFolder($dir); + if (false === Utils::createFolder($dir)) { + return Command::FAILURE; + } $samplePath = dirname(__DIR__, 3) . "/samples/" . $sampleName; - $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"); // Escribir en Init.php diff --git a/src/Command/Model/ModelCommand.php b/src/Command/Model/ModelCommand.php index ca964f9..1da1f80 100644 --- a/src/Command/Model/ModelCommand.php +++ b/src/Command/Model/ModelCommand.php @@ -53,14 +53,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int } $fields = Column::askMulti(); - FileGenerator::createModelByFields($fileName, $tableName, $fields, $name, Utils::getNamespace()); + if (!FileGenerator::createModelByFields($fileName, $tableName, $fields, $name, Utils::getNamespace())) { + return Command::FAILURE; + } Utils::echo('* ' . $fileName . " -> OK.\n"); $tablePath = Utils::isCoreFolder() ? 'Core/Table/' : 'Table/'; $tableFilename = $tablePath . $tableName . '.xml'; Utils::createFolder($tablePath); if (false === file_exists($tableFilename)) { - FileGenerator::createTableXmlByFields($tableFilename, $tableName, $fields); + if (!FileGenerator::createTableXmlByFields($tableFilename, $tableName, $fields)) { + return Command::FAILURE; + } Utils::echo('* ' . $tableFilename . " -> OK.\n"); } else { Utils::echo("\n" . '* ' . $tableFilename . " YA EXISTE"); @@ -104,13 +108,18 @@ 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; + } $template = str_replace( ['[[NAME_SPACE]]', '[[MODEL_NAME]]', '[[MENU]]'], [Utils::getNamespace(), $modelName, $menu], $sample ); - file_put_contents($fileName, $template); + if (!Utils::writeFile($fileName, $template)) { + return; + } Utils::echo('* ' . $fileName . " -> OK.\n"); $xmlPath = Utils::isCoreFolder() ? 'Core/XMLView/' : 'XMLView/'; @@ -121,7 +130,9 @@ private function createEditController(string $modelName, array $fields): void return; } - FileGenerator::createXMLViewByFields($xmlFilename, $fields, 'edit'); + if (!FileGenerator::createXMLViewByFields($xmlFilename, $fields, 'edit')) { + return; + } Utils::echo('* ' . $xmlFilename . " -> OK.\n"); } @@ -159,13 +170,18 @@ private function createListController(string $modelName, array $fields): void } $samplePath = dirname(__DIR__, 3) . "/samples/ListController.php.sample"; - $sample = file_get_contents($samplePath); + $sample = Utils::readFile($samplePath); + if ($sample === false) { + return; + } $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; + } Utils::echo('* ' . $fileName . " -> OK.\n"); $xmlPath = Utils::isCoreFolder() ? 'Core/XMLView/' : 'XMLView/'; @@ -176,7 +192,9 @@ private function createListController(string $modelName, array $fields): void return; } - FileGenerator::createXMLViewByFields($xmlFilename, $fields, 'list'); + if (!FileGenerator::createXMLViewByFields($xmlFilename, $fields, 'list')) { + return; + } Utils::echo('* ' . $xmlFilename . " -> OK.\n"); } } diff --git a/src/Command/Plugin/PluginCommand.php b/src/Command/Plugin/PluginCommand.php index 19c0030..7224447 100644 --- a/src/Command/Plugin/PluginCommand.php +++ b/src/Command/Plugin/PluginCommand.php @@ -73,10 +73,14 @@ 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 createInit(): void @@ -88,9 +92,13 @@ private function createInit(): void } $samplePath = dirname(__DIR__, 3) . "/samples/Init.php.sample"; - $sample = file_get_contents($samplePath); + $sample = Utils::readFile($samplePath); + if ($sample === false) { + return; + } $template = str_replace('[[NAME]]', Utils::findPluginName(), $sample); - file_put_contents($fileName, $template); - Utils::echo('* ' . $fileName . " -> OK.\n"); + if (Utils::writeFile($fileName, $template)) { + Utils::echo('* ' . $fileName . " -> OK.\n"); + } } } diff --git a/src/Command/Test/TestCommand.php b/src/Command/Test/TestCommand.php index b6fa02b..aedba40 100644 --- a/src/Command/Test/TestCommand.php +++ b/src/Command/Test/TestCommand.php @@ -35,7 +35,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int $filePath = 'Test/main/'; $fileName = $filePath . $name . '.php'; - Utils::createFolder($filePath); + if (false === Utils::createFolder($filePath)) { + return Command::FAILURE; + } if (file_exists($fileName)) { Utils::echo("* El test " . $name . " YA EXISTE.\n"); return Command::FAILURE; @@ -44,16 +46,26 @@ protected function execute(InputInterface $input, OutputInterface $output): int $txtFile = $filePath . 'install-plugins.txt'; if (false === file_exists($txtFile)) { // Creamos el fichero install-plugins.txt con el nombre del plugin - $ini = parse_ini_file('facturascripts.ini'); - file_put_contents($txtFile, $ini['name'] ?? ''); + $ini = Utils::parseIniFile('facturascripts.ini'); + if ($ini === false) { + return Command::FAILURE; + } + if (!Utils::writeFile($txtFile, $ini['name'] ?? '')) { + return Command::FAILURE; + } Utils::echo('* ' . $txtFile . " -> OK.\n"); } $samplePath = dirname(__DIR__, 3) . "/samples/Test.php.sample"; - $sample = file_get_contents($samplePath); + $sample = Utils::readFile($samplePath); + if ($sample === false) { + return Command::FAILURE; + } $nameSpace = Utils::getNamespace() . '\\' . str_replace('/', '\\', substr($filePath, 0, -1)); $template = str_replace(['[[NAME_SPACE]]', '[[NAME]]'], [$nameSpace, $name], $sample); - file_put_contents($fileName, $template); + if (!Utils::writeFile($fileName, $template)) { + return Command::FAILURE; + } Utils::echo('* ' . $fileName . " -> OK.\n"); return Command::SUCCESS; diff --git a/src/Command/View/ViewCommand.php b/src/Command/View/ViewCommand.php index 5ec0fef..0131634 100644 --- a/src/Command/View/ViewCommand.php +++ b/src/Command/View/ViewCommand.php @@ -57,7 +57,9 @@ private function createView(string $name): int $fileName = $viewPath . $name . '.html.twig'; - Utils::createFolder($viewPath); + if (false === Utils::createFolder($viewPath)) { + return Command::FAILURE; + } if (file_exists($fileName)) { Utils::echo("* La vista " . $fileName . " YA EXISTE.\n"); @@ -65,9 +67,14 @@ private function createView(string $name): int } $samplePath = dirname(__DIR__, 3) . "/samples/View.html.twig.sample"; - $sample = file_get_contents($samplePath); + $sample = Utils::readFile($samplePath); + if ($sample === false) { + return Command::FAILURE; + } - file_put_contents($fileName, $sample); + if (!Utils::writeFile($fileName, $sample)) { + return Command::FAILURE; + } Utils::echo('* ' . $fileName . " -> OK.\n"); diff --git a/src/Command/Worker/WorkerCommand.php b/src/Command/Worker/WorkerCommand.php index f3eec41..8b4bbc6 100644 --- a/src/Command/Worker/WorkerCommand.php +++ b/src/Command/Worker/WorkerCommand.php @@ -38,16 +38,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int $filePath = 'Worker/'; $fileName = $filePath . $name . '.php'; - Utils::createFolder($filePath); + if (false === Utils::createFolder($filePath)) { + return Command::FAILURE; + } if (file_exists($fileName)) { Utils::echo("* El worker " . $name . " YA EXISTE.\n"); return Command::FAILURE; } $samplePath = dirname(__DIR__, 3) . "/samples/Worker.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"); $options = multiselect( diff --git a/src/FileGenerator.php b/src/FileGenerator.php index 25ff1e3..18c3bcf 100644 --- a/src/FileGenerator.php +++ b/src/FileGenerator.php @@ -26,8 +26,11 @@ public static function createGithubActionRelease(): void return; } - $template = file_get_contents(__DIR__ . '/../samples/github-action-release.yml.sample'); - if (file_put_contents($filePath, $template)) { + $template = Utils::readFile(__DIR__ . '/../samples/github-action-release.yml.sample'); + if ($template === false) { + return; + } + if (Utils::writeFile($filePath, $template)) { Utils::echo('* ' . $filePath . self::OK); } } @@ -56,9 +59,12 @@ public static function createGithubActionTest(): void return; } - $template = file_get_contents(__DIR__ . '/../samples/github-action-test.yml.sample'); + $template = Utils::readFile(__DIR__ . '/../samples/github-action-test.yml.sample'); + if ($template === false) { + return; + } $content = str_replace('$$NOMBRE-DEL-PLUGIN$$', $pluginName, $template); - if (file_put_contents($filePath, $content)) { + if (Utils::writeFile($filePath, $content)) { Utils::echo('* ' . $filePath . self::OK); } } @@ -71,9 +77,13 @@ public static function createGitIgnore(): void return; } - $template = file_get_contents(__DIR__ . "/../samples/gitignore.sample"); - file_put_contents($fileName, $template); - Utils::echo('* ' . $fileName . self::OK); + $template = Utils::readFile(__DIR__ . "/../samples/gitignore.sample"); + if ($template === false) { + return; + } + if (Utils::writeFile($fileName, $template)) { + Utils::echo('* ' . $fileName . self::OK); + } } public static function createIni(string $name): void @@ -84,10 +94,14 @@ public static function createIni(string $name): void return; } - $sample = file_get_contents(__DIR__ . "/../samples/facturascripts.ini.sample"); + $sample = Utils::readFile(__DIR__ . "/../samples/facturascripts.ini.sample"); + if ($sample === false) { + return; + } $template = str_replace('[[NAME]]', $name, $sample); - file_put_contents($fileName, $template); - Utils::echo('* ' . $fileName . self::OK); + if (Utils::writeFile($fileName, $template)) { + Utils::echo('* ' . $fileName . self::OK); + } } /** @@ -96,9 +110,9 @@ public static function createIni(string $name): void * @param Column[] $fields * @param string $name * @param string $namespace - * @return void + * @return bool */ - public static function createModelByFields(string $fileName, string $tableName, array $fields, string $name, string $namespace): void + public static function createModelByFields(string $fileName, string $tableName, array $fields, string $name, string $namespace): bool { $properties = ''; $primaryColumn = ''; @@ -157,16 +171,16 @@ public static function createModelByFields(string $fileName, string $tableName, $sample .= "}\n"; - file_put_contents($fileName, $sample); + return Utils::writeFile($fileName, $sample); } /** * @param string $tableFilename * @param string $tableName * @param Column[] $fields - * @return void + * @return bool */ - public static function createTableXmlByFields(string $tableFilename, string $tableName, array $fields): void + public static function createTableXmlByFields(string $tableFilename, string $tableName, array $fields): bool { $columns = ''; $constraints = ''; @@ -178,7 +192,7 @@ public static function createTableXmlByFields(string $tableFilename, string $tab $sample = '' . "\n" . "\n" . $columns . $constraints . "
"; - file_put_contents($tableFilename, $sample); + return Utils::writeFile($tableFilename, $sample); } /** @@ -186,9 +200,9 @@ public static function createTableXmlByFields(string $tableFilename, string $tab * @param Column[] $fields * @param string $type * @param bool $extension - * @return void + * @return bool */ - public static function createXMLViewByFields(string $xmlFilename, array $fields, string $type, bool $extension = false): void + public static function createXMLViewByFields(string $xmlFilename, array $fields, string $type, bool $extension = false): bool { if (empty($fields)) { $fields = Column::askMulti($extension); @@ -241,12 +255,12 @@ public static function createXMLViewByFields(string $xmlFilename, array $fields, break; default: // No es ninguna de las opciones de antes - return; + return false; } $sample .= " \n" . ""; - file_put_contents($xmlFilename, $sample); + return Utils::writeFile($xmlFilename, $sample); } } diff --git a/src/RunTests.php b/src/RunTests.php index e291dbb..3d1a439 100644 --- a/src/RunTests.php +++ b/src/RunTests.php @@ -86,8 +86,13 @@ public static function run(?string $fs_folder = null): void } $foundTests = false; + $testEntries = scandir('Test/'); + if ($testEntries === false) { + Utils::echo("* No se pudo leer la carpeta Test/.\n"); + return; + } // recorremos las carpetas dentro de Test - foreach (scandir('Test/') as $item) { + foreach ($testEntries as $item) { if ($item === '.' || $item === '..') { continue; } diff --git a/src/UpdateTranslations.php b/src/UpdateTranslations.php index 67b21ff..f0e9108 100644 --- a/src/UpdateTranslations.php +++ b/src/UpdateTranslations.php @@ -11,12 +11,14 @@ class UpdateTranslations public static function create(string $name): void { + if (!Utils::createFolder($name . '/Translation')) { + return; + } foreach (explode(',', self::TRANSLATIONS) as $filename) { - file_put_contents( - $name . '/Translation/' . $filename . '.json', - '{"' . strtolower($name) . '": "' . $name . '"}' - ); - Utils::echo('* ' . $name . '/Translation/' . $filename . ".json -> OK.\n"); + $path = $name . '/Translation/' . $filename . '.json'; + if (Utils::writeFile($path, '{"' . strtolower($name) . '": "' . $name . '"}')) { + Utils::echo('* ' . $path . " -> OK.\n"); + } } } @@ -24,12 +26,19 @@ public static function run(): void { if (Utils::isPluginFolder()) { $folder = 'Translation/'; - Utils::createFolder($folder); - $ini = parse_ini_file('facturascripts.ini'); + if (false === Utils::createFolder($folder)) { + return; + } + $ini = Utils::parseIniFile('facturascripts.ini'); + if ($ini === false) { + return; + } $project = $ini['name'] ?? ''; } elseif (Utils::isCoreFolder()) { $folder = 'Core/Translation/'; - Utils::createFolder($folder); + if (false === Utils::createFolder($folder)) { + return; + } $project = 'CORE'; } else { Utils::echo("* Esta no es la carpeta raíz del plugin.\n"); @@ -42,6 +51,7 @@ public static function run(): void } // descargamos el json de facturascripts.com + $errores = 0; foreach (explode(',', self::TRANSLATIONS) as $filename) { // esperamos medio segundo entre peticiones usleep(500000); @@ -49,13 +59,23 @@ public static function run(): void Utils::echo("D " . $folder . $filename . ".json"); $url = "https://facturascripts.com/EditLanguage?action=json&project=" . $project . "&code=" . $filename; $json = file_get_contents($url); + if ($json === false) { + Utils::echo(" - ERROR de red\n"); + $errores++; + continue; + } if (!empty($json) && strlen($json) > 10) { - file_put_contents($folder . $filename . '.json', $json); + if (!Utils::writeFile($folder . $filename . '.json', $json)) { + $errores++; + } Utils::echo("\n"); continue; } Utils::echo(" - vacío\n"); } + if ($errores > 0) { + Utils::echo("* $errores traducciones no se pudieron descargar.\n"); + } } } diff --git a/src/Utils.php b/src/Utils.php index aaf3e83..0abd4e5 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -34,10 +34,55 @@ public static function createFolder(string $path): bool return false; } + public static function readFile(string $path): string|false + { + if (!file_exists($path)) { + self::echo("* ERROR: Archivo no encontrado: $path\n"); + return false; + } + + $content = file_get_contents($path); + if ($content === false) { + self::echo("* ERROR: Sin permisos de lectura: $path\n"); + return false; + } + + return $content; + } + + public static function writeFile(string $path, string $content): bool + { + if (false === file_put_contents($path, $content)) { + self::echo("* ERROR: No se pudo escribir: $path\n"); + return false; + } + + return true; + } + + public static function parseIniFile(string $path): array|false + { + if (!file_exists($path)) { + self::echo("* ERROR: Archivo no encontrado: $path\n"); + return false; + } + + $ini = parse_ini_file($path); + if ($ini === false) { + self::echo("* ERROR: No se pudo leer o parsear: $path\n"); + return false; + } + + return $ini; + } + public static function findPluginName(): string { if (self::isPluginFolder()) { - $ini = parse_ini_file('facturascripts.ini'); + $ini = self::parseIniFile('facturascripts.ini'); + if ($ini === false) { + return ''; + } return $ini['name'] ?? ''; } @@ -56,7 +101,10 @@ public static function getNamespace(): string } if (self::isPluginFolder()) { - $ini = parse_ini_file('facturascripts.ini'); + $ini = self::parseIniFile('facturascripts.ini'); + if ($ini === false) { + return ''; + } return 'Plugins\\' . ($ini['name'] ?? ''); } diff --git a/src/ZipGenerator.php b/src/ZipGenerator.php index eb13f5d..6b457e7 100644 --- a/src/ZipGenerator.php +++ b/src/ZipGenerator.php @@ -18,7 +18,10 @@ public static function generate(): void return; } - $ini = parse_ini_file('facturascripts.ini'); + $ini = Utils::parseIniFile('facturascripts.ini'); + if ($ini === false) { + return; + } $pluginName = $ini['name'] ?? ''; if (empty($pluginName)) { Utils::echo("* No se ha encontrado el nombre del plugin.\n");