Path: blob/master/src/applications/conduit/management/PhabricatorConduitCallManagementWorkflow.php
12256 views
<?php12final class PhabricatorConduitCallManagementWorkflow3extends PhabricatorConduitManagementWorkflow {45protected function didConstruct() {6$this7->setName('call')8->setSynopsis(pht('Call a Conduit method..'))9->setArguments(10array(11array(12'name' => 'method',13'param' => 'method',14'help' => pht('Method to call.'),15),16array(17'name' => 'input',18'param' => 'input',19'help' => pht(20'File to read parameters from, or "-" to read from '.21'stdin.'),22),23array(24'name' => 'local',25'help' => pht(26'Force the request to execute in this process, rather than '.27'proxying to another host in the cluster.'),28),29array(30'name' => 'as',31'param' => 'username',32'help' => pht(33'Execute the call as the given user. (If omitted, the call will '.34'be executed as an omnipotent user.)'),35),36));37}3839public function execute(PhutilArgumentParser $args) {40$viewer = $this->getViewer();4142$method = $args->getArg('method');43if (!strlen($method)) {44throw new PhutilArgumentUsageException(45pht('Specify a method to call with "--method".'));46}4748$input = $args->getArg('input');49if (!strlen($input)) {50throw new PhutilArgumentUsageException(51pht('Specify a file to read parameters from with "--input".'));52}5354$as = $args->getArg('as');55if (strlen($as)) {56$actor = id(new PhabricatorPeopleQuery())57->setViewer($viewer)58->withUsernames(array($as))59->executeOne();60if (!$actor) {61throw new PhutilArgumentUsageException(62pht(63'No such user "%s" exists.',64$as));65}6667// Allow inline generation of user caches for the user we're acting68// as, since some calls may read user preferences.69$actor->setAllowInlineCacheGeneration(true);70} else {71$actor = $viewer;72}7374if ($input === '-') {75fprintf(STDERR, tsprintf("%s\n", pht('Reading input from stdin...')));76$input_json = file_get_contents('php://stdin');77} else {78$input_json = Filesystem::readFile($input);79}8081$params = phutil_json_decode($input_json);8283$call = id(new ConduitCall($method, $params))84->setUser($actor);8586$api_request = $call->getAPIRequest();8788$is_local = $args->getArg('local');89if ($is_local) {90$api_request->setIsClusterRequest(true);91}9293$result = $call->execute();9495$output = array(96'result' => $result,97);9899echo tsprintf(100"%B\n",101id(new PhutilJSON())->encodeFormatted($output));102103return 0;104}105106}107108109