From c2323c4c8947b177d5f024ad423805113ec6d9c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sat, 27 Jun 2020 00:22:01 +0200 Subject: [PATCH] MAPG-89 add command that can link views from command line --- mapg | 1 + src/Cli/LinkViewCommand.php | 69 +++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 src/Cli/LinkViewCommand.php diff --git a/mapg b/mapg index 6409a95..8acfda2 100755 --- a/mapg +++ b/mapg @@ -7,5 +7,6 @@ $app = new Symfony\Component\Console\Application('MapGuesser Console', ''); $app->add(new MapGuesser\Cli\DatabaseMigration()); $app->add(new MapGuesser\Cli\AddUserCommand()); +$app->add(new MapGuesser\Cli\LinkViewCommand()); $app->run(); diff --git a/src/Cli/LinkViewCommand.php b/src/Cli/LinkViewCommand.php new file mode 100644 index 0000000..cccd74e --- /dev/null +++ b/src/Cli/LinkViewCommand.php @@ -0,0 +1,69 @@ +setName('view:link') + ->setDescription('Linking of views.') + ->addArgument('view', InputArgument::OPTIONAL, 'View file to be linked.'); + } + + public function execute(InputInterface $input, OutputInterface $output): int + { + $views = []; + + $view = $input->getArgument('view'); + + if ($view !== null) { + $views[] = $view; + } else { + $folder = ROOT . '/views'; + $folderLength = strlen($folder) + 1; + + $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($folder, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST); + + foreach ($iterator as $file) { + if ($file->isDir() || $file->getExtension() !== 'php') { + continue; + } + + $view = substr($file->getPath(), $folderLength) . '/' . $file->getBasename('.php'); + + if (strpos($view, 'templates') === 0) { + continue; + } + + $views[] = $view; + } + } + + try { + foreach ($views as $view) { + $generator = new Linker($view); + $generator->generate(); + } + } catch (\Exception $e) { + $output->writeln('Linking view(s) failed!'); + $output->writeln(''); + + $output->writeln((string) $e); + $output->writeln(''); + + return 1; + } + + $output->writeln('View(s) successfully linked!'); + + return 0; + } +}