setName('dependencies:add-template') ->setDescription('Adds a Twig template dependency to the specification of a class.') ->addArgument('coid-target', InputArgument::REQUIRED, 'The COID of the target class.') ->addArgument('key', InputArgument::REQUIRED, 'The key for dependency injection.') ->addArgument('filename', InputArgument::REQUIRED, 'The filename for the Twig template.') ->addOption('upload', null, InputOption::VALUE_NONE, 'Calls "cloudobjects" to actually upload the template file to CloudObjects.'); parent::configure(); } protected function execute(InputInterface $input, OutputInterface $output): int { $this->requireCloudObjectsCLI(); $this->parse($input->getArgument('coid-target')); $this->dependencyPrecheck(); // Check filename $filename = $input->getArgument('filename'); if (!file_exists($filename)) throw new \Exception("File not found: ".$filename); // Upload file if requested if ($input->getOption('upload')) { $output->writeln("Calling cloudobjects for file upload ..."); passthru("cloudobjects attachment:put ".(string)$this->coid." ".$filename); } // Add dependency $this->addDependency( $input->getArgument('key'), 'coid://phpmae.dev/TwigTemplateDependency', [ 'coid://phpmae.dev/usesAttachedTwigFile' => [ [ 'type' => 'uri', 'value' => "file:///".basename($filename) ] ] ], $input, $output ); // Print documentation $key = $input->getArgument('key'); $output->writeln(""); $output->writeln("Use your Twig template dependency:"); $output->writeln(""); $output->writeln("1) Make sure you have access to the dependency injection container by adding the container to your class constructor."); $output->writeln("2) Request the template from the container using the key \"".$key."\"."); $output->writeln(""); $output->writeln(" private \$".$key."Template;"); $output->writeln(""); $output->writeln(" public function __construct(\Psr\Container\ContainerInterface \$container) {"); $output->writeln(" \$this->".$key."Template = \$container->get('".$key."');"); $output->writeln(" }"); $output->writeln(""); $output->writeln("3) Render your template by calling \$this->".$key."Template->render(\$context)."); $output->writeln(""); return Command::SUCCESS; } }