From 8836e17fd3e3a9b6f912483bcdaffa78b39d4e62 Mon Sep 17 00:00:00 2001 From: Hamid Ghorashi Date: Fri, 7 Aug 2020 17:57:21 +0430 Subject: [PATCH] Add input option to convert documents to PDF. --- CHANGELOG.md | 4 ++ config/application.config.php | 3 +- src/Application/Command/ConvertCommand.php | 26 -------- src/Application/Command/PreviewCommand.php | 28 ++++----- src/Application/Service/PreviewService.php | 28 +++++++++ src/Core/Command.php | 69 ++++++++++++++++++++++ src/Core/ConsoleApplication.php | 12 ++++ 7 files changed, 126 insertions(+), 44 deletions(-) create mode 100644 CHANGELOG.md delete mode 100644 src/Application/Command/ConvertCommand.php create mode 100644 src/Core/Command.php diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..9e7bf24 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,4 @@ +# Changelog + +## v1.0.0 (05/08/2020) +*No changelog for this release.* diff --git a/config/application.config.php b/config/application.config.php index 7bdd35e..2025f6c 100644 --- a/config/application.config.php +++ b/config/application.config.php @@ -4,7 +4,7 @@ return [ // App info 'AppName' => 'PreviewMaker', - 'Version' => '1.0.0', + 'Version' => '1.1.0', // App settings 'settings' => require __DIR__ . "/settings.config.php", @@ -14,7 +14,6 @@ // Commands 'commands' => [ - // Command\ConvertCommand::class, Command\PreviewCommand::class, ], ]; \ No newline at end of file diff --git a/src/Application/Command/ConvertCommand.php b/src/Application/Command/ConvertCommand.php deleted file mode 100644 index 279294f..0000000 --- a/src/Application/Command/ConvertCommand.php +++ /dev/null @@ -1,26 +0,0 @@ -setName('convert') - ->setDescription('Prints Hello-World!') - ->setHelp('Demonstration of custom commands created by Symfony Console component.') - ->addArgument('username', InputArgument::REQUIRED, 'Pass the username.'); - } - - protected function execute(InputInterface $input, OutputInterface $output) - { - $service = $this->getApplication()->getService(ffmpegService::class); - - $output->writeln(sprintf('Hello World!, %s', $input->getArgument('username'))); - } -} diff --git a/src/Application/Command/PreviewCommand.php b/src/Application/Command/PreviewCommand.php index b28c12d..5284dcc 100644 --- a/src/Application/Command/PreviewCommand.php +++ b/src/Application/Command/PreviewCommand.php @@ -1,9 +1,8 @@ setName('preview') ->setDescription('This creates a preview image from an input file.') - ->setHelp('Demonstration of custom commands created by Symfony Console component.') - ->addArgument('filepath', InputArgument::REQUIRED, 'Path to the file you want to make preview from') + ->setHelp('This command is to create an image or pdf preview from an input file.') + ->addArgument('inputFile', InputArgument::REQUIRED, 'Path to the file you want to make preview from') + ->addOption('pdf', null, InputOption::VALUE_NONE, 'Convert given document to pdf') ->addOption('output', 'o', InputOption::VALUE_REQUIRED, 'Path to output file'); } protected function execute(InputInterface $input, OutputInterface $output) { - $filePath = $input->getArgument('filepath'); - $service = $this->getPreviewService()->setFile($filePath); + $inputFile = $input->getArgument('inputFile'); + $service = $this->getPreviewService()->setFile($inputFile); - $startTime = microtime(true); - $file = $service->preview($input->getOption('output')); - $endTime = microtime(true); + if ($input->getOption('pdf')) { + $file = $service->toPdf($input->getOption('output')); + } else { + $file = $service->preview($input->getOption('output')); + } - $output->writeln(''); - $output->writeln(sprintf('File name: %s', $file->getName())); - $output->writeln(sprintf('Directory: %s', $file->getDir())); - $output->writeln(sprintf('Preview: %s', $file->getPath())); - $output->writeln(''); - $output->writeln(sprintf('Execution time: %s sec', number_format($endTime - $startTime, 5))); - $output->writeln(''); + $this->writeSummary($output, $file); } private function getPreviewService(): PreviewService diff --git a/src/Application/Service/PreviewService.php b/src/Application/Service/PreviewService.php index 9fb63ee..d187b60 100644 --- a/src/Application/Service/PreviewService.php +++ b/src/Application/Service/PreviewService.php @@ -74,6 +74,7 @@ public function getFile(): InputFile /** * Create an image preview from the given file * + * @param string $output * @return InputFile * @throws RuntimeException */ @@ -99,4 +100,31 @@ public function preview($output = null) throw new RuntimeException(sprintf("Error: Unsupported file type! The mime type %s is not supported.", $this->file->getMetadata()->getMimeType())); } + + /** + * Create a PDF preview from the given file + * + * @param string $output + * @return void + */ + public function toPdf($output = null) + { + if (!$this->file instanceof InputFile) { + throw new RuntimeException("Error: No file has been selected."); + } + + $output = !$output && isset($this->options['TmpDir']) ? $this->options['TmpDir'] : $output; + + if (!$output) { + throw new RuntimeException("Error: Invalid output path."); + } + + $strategy = new Strategy\DocumentStrategy($this->file); + + if ($strategy->match()) { + return $strategy->toPdf($output); + } + + throw new RuntimeException(sprintf("Error: Unable to convert given file to PDF. The mimetype %s is not supported.", $this->file->getMetadata()->getMimeType())); + } } diff --git a/src/Core/Command.php b/src/Core/Command.php new file mode 100644 index 0000000..7f8bd86 --- /dev/null +++ b/src/Core/Command.php @@ -0,0 +1,69 @@ +startTime = microtime(true); + $result = parent::run($input, $output); + + return $result; + } + + /** + * Calculate commmand execution time in seconds + * + * @return float + */ + protected function getExecutionTime() + { + $this->endTime = microtime(true); + return number_format($this->endTime - $this->startTime, 5); + } + + /** + * Write a file summary to the output + * + * @param mixed $output + * @param mixed $file + * @return void + */ + public function writeSummary(OutputInterface $output, FileInterface $file) + { + $output->writeln(''); + $output->writeln(sprintf('File name: %s', $file->getName())); + $output->writeln(sprintf('Directory: %s', $file->getDir())); + $output->writeln(sprintf('Preview: %s', $file->getPath())); + $output->writeln(''); + $output->writeln(sprintf('Execution time: %s sec', $this->getExecutionTime())); + $output->writeln(''); + } +} diff --git a/src/Core/ConsoleApplication.php b/src/Core/ConsoleApplication.php index 7d49446..8ac1e91 100644 --- a/src/Core/ConsoleApplication.php +++ b/src/Core/ConsoleApplication.php @@ -89,6 +89,18 @@ private function checkRequirements() return $this; } + /** + * Registers a new command. + * + * @param string $name The command name + * + * @return Command The newly created command + */ + public function register($name) + { + return $this->add(new Command($name)); + } + /** * Register available commands *