Custom service¶
The plugin already provides a set of supported services which can be extended to make use of custom implemented services. Follow this guide to learn how you can implement your own services and use them together with the update reporter plugin.
Two variants
To register new services, two variants are available:
- In the recommended variant, you create a separate Composer plugin which can be used in your specific project in addition to the update reporter plugin.
- In the alternative variant, you create the service directly in your concrete project. This variant is easier to implement on the one hand, but does not fit optimally into the Composer lifecycle.
Both variants have upsides and downsides. Feel free to test both of them and find a suitable way matching your requirements.
1. Create service class¶
Create a new service class – depending on your selected variant either in your custom Composer plugin or directly in your project:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
|
In case you're sending the report to some remote service provider, you might want
to use the provided RemoteServiceTrait
:
@@ -5,11 +5,14 @@ namespace App\Service;
use EliasHaeussler\ComposerUpdateCheck\Package\UpdateCheckResult;
use EliasHaeussler\ComposerUpdateReporter\Service\AbstractService;
use EliasHaeussler\ComposerUpdateReporter\Service\ServiceInterface;
+use EliasHaeussler\ComposerUpdateReporter\Traits\RemoteServiceTrait;
use Nyholm\Psr7\Uri;
use Psr\Http\Message\UriInterface;
class MyCustomService extends AbstractService
{
+ use RemoteServiceTrait;
+
/**
* @var UriInterface
*/
@@ -48,6 +51,8 @@ class MyCustomService extends AbstractService
{
// Do something...
- return $successful;
+ $response = $this->sendRequest($payload);
+
+ return $response->getStatusCode() < 400;
}
}
2. Register custom service¶
Variant 1: Custom Composer plugin (recommended)¶
First, ensure your plugins' composer.json
looks similar to the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
Now provide the appropriate Plugin
class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Variant 2: Extending a concrete project¶
First, ensure class autoloading is enabled in your projects' composer.json
:
1 2 3 4 5 6 7 |
|
Now create a new class which serves as event listener in the Composer lifecycle:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Finally, let Composer call the previously created class to trigger the
service registration by adding the following lines to your projects'
composer.json
:
1 2 3 4 5 |
|
3. Add service configuration¶
Once the service is implemented and properly registered, you should provide
a valid service configuration in your project. This can be done by either using
the projects' composer.json
or providing the appropriate environment variables.
composer.json
¶
1 2 3 4 5 6 7 8 9 10 11 |
|
Environment variables¶
1 2 3 |
|