4 * Run a conduit method in-process, without requiring HTTP requests. Usage:
6 * $call = new ConduitCall('method.name', array('param' => 'value'));
7 * $call->setUser($user);
8 * $result = $call->execute();
11 final class ConduitCall
extends Phobject
{
18 public function __construct($method, array $params, $strictly_typed = true) {
19 $this->method
= $method;
20 $this->handler
= $this->buildMethodHandler($method);
22 $param_types = $this->handler
->getParamTypes();
24 foreach ($param_types as $key => $spec) {
25 if (ConduitAPIMethod
::getParameterMetadataKey($key) !== null) {
26 throw new ConduitException(
28 'API Method "%s" defines a disallowed parameter, "%s". This '.
29 'parameter name is reserved.',
35 $invalid_params = array_diff_key($params, $param_types);
36 if ($invalid_params) {
37 throw new ConduitException(
39 'API Method "%s" does not define these parameters: %s.',
41 "'".implode("', '", array_keys($invalid_params))."'"));
44 $this->request
= new ConduitAPIRequest($params, $strictly_typed);
47 public function getAPIRequest() {
48 return $this->request
;
51 public function setUser(PhabricatorUser
$user) {
56 public function getUser() {
60 public function shouldRequireAuthentication() {
61 return $this->handler
->shouldRequireAuthentication();
64 public function shouldAllowUnguardedWrites() {
65 return $this->handler
->shouldAllowUnguardedWrites();
68 public function getErrorDescription($code) {
69 return $this->handler
->getErrorDescription($code);
72 public function execute() {
73 $profiler = PhutilServiceProfiler
::getInstance();
74 $call_id = $profiler->beginServiceCall(
77 'method' => $this->method
,
81 $result = $this->executeMethod();
82 } catch (Exception
$ex) {
83 $profiler->endServiceCall($call_id, array());
87 $profiler->endServiceCall($call_id, array());
91 private function executeMethod() {
92 $user = $this->getUser();
94 $user = new PhabricatorUser();
97 $this->request
->setUser($user);
99 if (!$this->shouldRequireAuthentication()) {
100 // No auth requirement here.
103 $allow_public = $this->handler
->shouldAllowPublic() &&
104 PhabricatorEnv
::getEnvConfig('policy.allow-public');
105 if (!$allow_public) {
106 if (!$user->isLoggedIn() && !$user->isOmnipotent()) {
107 // TODO: As per below, this should get centralized and cleaned up.
108 throw new ConduitException('ERR-INVALID-AUTH');
112 // TODO: This would be slightly cleaner by just using a Query, but the
113 // Conduit auth workflow requires the Call and User be built separately.
114 // Just do it this way for the moment.
115 $application = $this->handler
->getApplication();
117 $can_view = PhabricatorPolicyFilter
::hasCapability(
120 PhabricatorPolicyCapability
::CAN_VIEW
);
123 throw new ConduitException(
125 'You do not have access to the application which provides this '.
131 return $this->handler
->executeMethod($this->request
);
134 protected function buildMethodHandler($method_name) {
135 $method = ConduitAPIMethod
::getConduitMethod($method_name);
138 throw new ConduitMethodDoesNotExistException($method_name);
141 $application = $method->getApplication();
142 if ($application && !$application->isInstalled()) {
143 $app_name = $application->getName();
144 throw new ConduitApplicationNotInstalledException($method, $app_name);
150 public function getMethodImplementation() {
151 return $this->handler
;