From d752cd09c6cf36dc2025b43b6bd86147e1300cf6 Mon Sep 17 00:00:00 2001 From: Tyler Cosgrove Date: Thu, 31 Mar 2016 19:29:20 -0400 Subject: [PATCH] Error handling try-catch --- .../Console/Cli/DevTools/DevToolsCommand.php | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/system/src/Grav/Console/Cli/DevTools/DevToolsCommand.php b/system/src/Grav/Console/Cli/DevTools/DevToolsCommand.php index 57e52c45b..f652f1bfe 100644 --- a/system/src/Grav/Console/Cli/DevTools/DevToolsCommand.php +++ b/system/src/Grav/Console/Cli/DevTools/DevToolsCommand.php @@ -88,7 +88,12 @@ class DevToolsCommand extends ConsoleCommand $componentFolder = $this->locator->findResource($type . 's://') . DS . $folderName; //Copy All files to component folder - Folder::copy($templateFolder, $componentFolder); + try { + Folder::copy($templateFolder, $componentFolder); + } catch (\Exception $e) { + $this->output->writeln("" . $e->getMessage() . ""); + return false; + } //Add Twig vars and templates then initialize $this->twig->twig_vars['component'] = $this->component; @@ -97,17 +102,26 @@ class DevToolsCommand extends ConsoleCommand //Get all templates of component then process each with twig and save $templates = Folder::all($componentFolder); - foreach($templates as $templateFile) { - if (Utils::endsWith($templateFile, '.twig') && !Utils::endsWith($templateFile, '.html.twig')) { - $content = $this->twig->processTemplate($templateFile); - $file = File::instance($componentFolder . DS . str_replace('.twig', '', $templateFile)); - $file->content($content); - $file->save(); - - //Delete twig template - $file = File::instance($componentFolder . DS . $templateFile); - $file->delete(); + + try { + foreach($templates as $templateFile) { + if (Utils::endsWith($templateFile, '.twig') && !Utils::endsWith($templateFile, '.html.twig')) { + $content = $this->twig->processTemplate($templateFile); + $file = File::instance($componentFolder . DS . str_replace('.twig', '', $templateFile)); + $file->content($content); + $file->save(); + + //Delete twig template + $file = File::instance($componentFolder . DS . $templateFile); + $file->delete(); + } } + } catch (\Exception $e) { + $this->output->writeln("" . $e->getMessage() . ""); + $this->output->writeln("Rolling back..."); + Folder::delete($componentFolder); + $this->output->writeln($type . "creation failed!"); + return false; } rename($componentFolder . DS . $type . '.php', $componentFolder . DS . $this->inflector->hyphenize($name) . '.php');