setName('dependencies:add-class') ->setDescription('Adds a class dependency to the specification of a class.') ->addArgument('coid-target', InputArgument::REQUIRED, 'The COID of the target class.') ->addOption('key', null, InputArgument::OPTIONAL, 'The key for dependency injection. Randomly generated if not provided.', null) ->addArgument('coid-class', InputArgument::REQUIRED, 'The COID of the dependency class.'); parent::configure(); } protected function execute(InputInterface $input, OutputInterface $output): int { $this->requireCloudObjectsCLI(); $this->parse($input->getArgument('coid-target')); $this->dependencyPrecheck(); // Check Class $coidClass = COIDParser::fromString($input->getArgument('coid-class')); if (COIDParser::getType($coidClass) != COIDParser::COID_VERSIONED && COIDParser::getType($coidClass) != COIDParser::COID_UNVERSIONED) throw new \Exception("Invalid COID: ".$coid); // Checking type $output->writeln("Fetching configuration for ".(string)$coidClass." ..."); $this->getObjectAndAssertType((string)$coidClass, 'coid://phpmae.dev/Class'); // Add dependency $this->addDependency( $input->getOption('key') ? $input->getOption('key') : uniqid('class-'), 'coid://phpmae.dev/ClassDependency', [ 'coid://phpmae.dev/hasClass' => [ [ 'type' => 'uri', 'value' => (string)$coidClass ] ] ], $input, $output ); // Print documentation $className = COIDParser::getName($coidClass); $key = $input->getOption('key') ? $input->getOption('key') : strtolower($className[0]).substr($className, 1); $output->writeln(""); $output->writeln("Use your class dependency:"); $output->writeln(""); $output->writeln("1) Add the class ".$className." to your constructor parameters."); $output->writeln("2) Assign the class to \$this->".$key."."); $output->writeln(""); $output->writeln(" private \$".$key.";"); $output->writeln(""); $output->writeln(" public function __construct(".$className." \$".$key.") {"); $output->writeln(" \$this->".$key." = \$".$key.";"); $output->writeln(" }"); $output->writeln(""); $output->writeln("3) Use \$this->".$key." wherever required."); $output->writeln(""); $output->writeln("You can find the documentation for the available class methods on this page:"); $output->writeln("➡️ https://cloudobjects.io/".$coidClass->getHost().$coidClass->getPath()); $output->writeln(""); return Command::SUCCESS; } }