setName('dependencies:add-webapi') ->setDescription('Adds a 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('coid-webapi', InputArgument::REQUIRED, 'The COID of the Web API.'); parent::configure(); } protected function execute(InputInterface $input, OutputInterface $output) { $this->requireCloudObjectsCLI(); $this->parse($input->getArgument('coid-target')); $this->dependencyPrecheck(); // Check Web API $coidWebAPI = COIDParser::fromString($input->getArgument('coid-webapi')); if (COIDParser::getType($coidWebAPI) != COIDParser::COID_VERSIONED && COIDParser::getType($coidWebAPI) != COIDParser::COID_UNVERSIONED) throw new \Exception("Invalid COID: ".$coid); // Checking type $output->writeln("Fetching configuration for ".(string)$coidWebAPI." ..."); $this->getObjectAndAssertType((string)$coidWebAPI, 'coid://webapis.co-n.net/HTTPEndpoint'); // Add dependency $this->addDependency( $input->getArgument('key'), 'coid://phpmae.dev/WebAPIDependency', [ 'coid://phpmae.dev/hasAPI' => [ [ 'type' => 'uri', 'value' => (string)$coidWebAPI ] ] ], $input, $output ); // Print documentation $key = $input->getArgument('key'); $output->writeln(""); $output->writeln("Use your WebAPI 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 an API client from the container using the key \"".$key."\"."); $output->writeln(""); $output->writeln(" private \$".$key."Api;"); $output->writeln(""); $output->writeln(" public function __construct(\Psr\Container\ContainerInterface \$container) {"); $output->writeln(" \$this->".$key."Api = \$container->get('".$key."');"); $output->writeln(" }"); $output->writeln(""); $output->writeln("3) Make API requests in your class by calling methods on \$this->".$key."Api."); $output->writeln(""); } }