These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | require_once 'vendor/autoload.php'; |
||
3 | |||
4 | |||
5 | use Adlogix\Confluence\Client\ClientBuilder; |
||
6 | use Adlogix\Confluence\Client\Entity\Connect\Descriptor; |
||
7 | use Adlogix\Confluence\Client\Entity\Connect\DescriptorLifecycle; |
||
8 | use Adlogix\Confluence\Client\Entity\Connect\SecurityContext; |
||
9 | use Adlogix\Confluence\Client\Exception\ApiException; |
||
10 | use Adlogix\Confluence\Client\Security\Authentication\JwtHeaderAuthentication; |
||
11 | use GuzzleHttp\Psr7\Uri; |
||
12 | |||
13 | /** |
||
14 | * The Lifecycle is mandatory since Confluence needs to contact the application. |
||
15 | * So you need to implement those endpoints. Minimal is to send a 200 OK header. |
||
16 | * When Confluence calls those webhooks, it will send you a payload with some information. |
||
17 | * @see https://developer.atlassian.com/static/connect/docs/latest/modules/lifecycle.html |
||
18 | * |
||
19 | * Installed => called when the plugin is installed on an instance |
||
20 | * Enabled => called when the plugin is enabled on an instance. If you don't supply that the plugin is installable but can't be enabled |
||
21 | */ |
||
22 | $lifecycle = new DescriptorLifecycle(); |
||
23 | $lifecycle |
||
24 | ->setInstalled('/installed') |
||
25 | ->setEnabled('/enabled'); |
||
26 | |||
27 | |||
28 | /** |
||
29 | * The descriptor is the description of your plugin. |
||
30 | * This one is the minimum to have read access on Confluence. |
||
31 | * The authentication method will be set depending on the authentication you pass at the client builder later |
||
32 | * |
||
33 | * @see https://developer.atlassian.com/static/connect/docs/latest/modules/ |
||
34 | * |
||
35 | * You can validate it's output there: |
||
36 | * @see https://atlassian-connect-validator.herokuapp.com/validate |
||
37 | */ |
||
38 | $descriptor = new Descriptor( |
||
39 | "http://atlassianconnect.dev/", |
||
40 | 'dev.mypluginkey' |
||
41 | ); |
||
42 | |||
43 | $descriptor->setLifecycle($lifecycle) |
||
44 | ->setScopes([ |
||
45 | 'read' |
||
46 | ]); |
||
47 | |||
48 | /** |
||
49 | * The Security Context is that payload you received upon installation. |
||
50 | * It is needed to be able to sign the Authentication Token |
||
51 | */ |
||
52 | $securityContext = new SecurityContext(); |
||
53 | |||
54 | /** |
||
55 | * The only needed information is the shared secret. |
||
56 | */ |
||
57 | if (file_exists('payload.json')) { |
||
58 | $payload = json_decode(file_get_contents('payload.json')); |
||
59 | $securityContext->setSharedSecret($payload->sharedSecret); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Actually building our client. |
||
64 | */ |
||
65 | $client = ClientBuilder::create( |
||
66 | 'http://confluence.dev/confluence', |
||
67 | new JwtHeaderAuthentication( |
||
68 | $securityContext, |
||
69 | $descriptor |
||
70 | ) |
||
71 | ) |
||
72 | ->setDebug(true) |
||
73 | ->build(); |
||
74 | |||
75 | |||
76 | $url = new Uri(@$_SERVER['REQUEST_URI']); |
||
77 | switch ($url->getPath()) { |
||
78 | case '/descriptor.json': |
||
79 | // Show our descriptor |
||
80 | echo $client->descriptor()->get(); |
||
0 ignored issues
–
show
|
|||
81 | break; |
||
82 | |||
83 | case '/installed': |
||
84 | //Installation webhook |
||
85 | file_put_contents('payload.json', file_get_contents('php://input')); |
||
86 | http_response_code(200); |
||
87 | echo 'OK'; |
||
88 | break; |
||
89 | |||
90 | case '/enabled': |
||
91 | //Enabled webhook. |
||
92 | http_response_code(200); |
||
93 | echo 'OK'; |
||
94 | break; |
||
95 | |||
96 | default: |
||
97 | // When no action is given, just run test code. |
||
98 | try { |
||
99 | var_dump($client->space()->all()); |
||
0 ignored issues
–
show
The method
space does not exist on object<Adlogix\Confluence\Client\Client> ? Since you implemented __call , maybe consider adding a @method annotation.
If you implement This is often the case, when class ParentClass {
private $data = array();
public function __call($method, array $args) {
if (0 === strpos($method, 'get')) {
return $this->data[strtolower(substr($method, 3))];
}
throw new \LogicException(sprintf('Unsupported method: %s', $method));
}
}
/**
* If this class knows which fields exist, you can specify the methods here:
*
* @method string getName()
*/
class SomeClass extends ParentClass { }
![]() |
|||
100 | } catch (ApiException $e) { |
||
101 | echo 'ApiException' . $e->getMessage(); |
||
102 | } |
||
103 | |||
104 | break; |
||
105 | } |
||
106 | |||
107 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: