|
| 1 | +<?php |
| 2 | + |
| 3 | + namespace JetRails\Varnish\Console\Command\Vcl; |
| 4 | + |
| 5 | + use Exception; |
| 6 | + use JetRails\Varnish\Helper\VclGenerator; |
| 7 | + use Magento\PageCache\Console\Command\GenerateVclCommand; |
| 8 | + use Magento\PageCache\Model\Varnish\VclTemplateLocator; |
| 9 | + use Magento\PageCache\Model\VclTemplateLocatorInterface; |
| 10 | + use Magento\PageCache\Model\VclGeneratorInterfaceFactory; |
| 11 | + use Magento\Framework\Filesystem\DriverPool; |
| 12 | + use Magento\Framework\Filesystem\File\WriteFactory; |
| 13 | + use Magento\Framework\App\Config\ScopeConfigInterface; |
| 14 | + use Magento\Framework\Serialize\Serializer\Json; |
| 15 | + use Magento\Framework\Console\Cli; |
| 16 | + use Symfony\Component\Console\Command\Command; |
| 17 | + use Symfony\Component\Console\Input\InputInterface; |
| 18 | + use Symfony\Component\Console\Input\InputOption; |
| 19 | + use Symfony\Component\Console\Output\OutputInterface; |
| 20 | + |
| 21 | + /** |
| 22 | + * @version 2.0.2 |
| 23 | + * @package JetRails® Varnish |
| 24 | + * @author Rafael Grigorian - JetRails® |
| 25 | + * @copyright JetRails®, all rights reserved |
| 26 | + * @license The JetRails License (SEE LICENSE IN LICENSE.md) |
| 27 | + */ |
| 28 | + class GenerateCustom extends Command { |
| 29 | + |
| 30 | + protected $vclTemplateLocator; |
| 31 | + protected $vclGeneratorFactory; |
| 32 | + protected $writeFactory; |
| 33 | + protected $generator; |
| 34 | + |
| 35 | + const EXPORT_VERSION_OPTION = "export-version"; |
| 36 | + const OUTPUT_FILE_OPTION = "output-file"; |
| 37 | + |
| 38 | + public function __construct ( |
| 39 | + VclTemplateLocatorInterface $vclTemplateLocator, |
| 40 | + VclGeneratorInterfaceFactory $vclGeneratorFactory, |
| 41 | + WriteFactory $writeFactory, |
| 42 | + VclGenerator $generator |
| 43 | + ) { |
| 44 | + parent::__construct (); |
| 45 | + $this->vclTemplateLocator = $vclTemplateLocator; |
| 46 | + $this->vclGeneratorFactory = $vclGeneratorFactory; |
| 47 | + $this->writeFactory = $writeFactory; |
| 48 | + $this->generator = $generator; |
| 49 | + } |
| 50 | + |
| 51 | + protected function configure () { |
| 52 | + $this->setName ("varnish:vcl:generate-custom") |
| 53 | + ->setDescription ("Generates Varnish companion VCL and echos it to the command line") |
| 54 | + ->setDefinition ( $this->getOptionList () ); |
| 55 | + } |
| 56 | + |
| 57 | + protected function execute ( InputInterface $input, OutputInterface $output ) { |
| 58 | + try { |
| 59 | + $outputFile = $input->getOption ( self::OUTPUT_FILE_OPTION ); |
| 60 | + $varnishVersion = $input->getOption ( self::EXPORT_VERSION_OPTION ); |
| 61 | + $vcl = $this->vclTemplateLocator->getTemplate ( $varnishVersion ); |
| 62 | + $vcl = $this->generator->generateCustom ( $vcl ); |
| 63 | + if ( $outputFile ) { |
| 64 | + $writer = $this->writeFactory->create ( $outputFile, DriverPool::FILE, "w+" ); |
| 65 | + $writer->write ( $vcl ); |
| 66 | + $writer->close (); |
| 67 | + } |
| 68 | + else { |
| 69 | + $output->writeln ( $vcl ); |
| 70 | + } |
| 71 | + return Cli::RETURN_SUCCESS; |
| 72 | + } |
| 73 | + catch ( Exception $e ) { |
| 74 | + $output->writeln ("<error>" . $e->getMessage () . "</error>"); |
| 75 | + if ($output->getVerbosity () >= OutputInterface::VERBOSITY_VERBOSE ) { |
| 76 | + $output->writeln ( $e->getTraceAsString () ); |
| 77 | + } |
| 78 | + return Cli::RETURN_FAILURE; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private function getOptionList () { |
| 83 | + return [ |
| 84 | + new InputOption( |
| 85 | + self::EXPORT_VERSION_OPTION, |
| 86 | + null, |
| 87 | + InputOption::VALUE_REQUIRED, |
| 88 | + 'The version of Varnish file', |
| 89 | + VclTemplateLocator::VARNISH_SUPPORTED_VERSION_4 |
| 90 | + ), |
| 91 | + new InputOption( |
| 92 | + self::OUTPUT_FILE_OPTION, |
| 93 | + null, |
| 94 | + InputOption::VALUE_REQUIRED, |
| 95 | + 'Path to the file to write vcl' |
| 96 | + ), |
| 97 | + ]; |
| 98 | + } |
| 99 | + |
| 100 | + } |
0 commit comments