mezon / service
Small service script
Installs: 5 899
Dependents: 2
Suggesters: 0
Security: 0
Stars: 6
Watchers: 4
Forks: 0
Open Issues: 0
Requires
- php: >=7.2.0
- mezon/custom-client: 1.0.*
- mezon/dns-client: 1.0.*
- mezon/fields-set: 1.0.*
- mezon/functional: 1.1.*
- mezon/http-request-params: 1.0.*
- mezon/infrastructure-layer: >=1.2.6 <1.3.0
- mezon/request: >=1.0.6 <1.1.0
- mezon/rest-exception: 1.0.*
- mezon/router: 1.5.*
- mezon/service-logic: 1.3.*
- mezon/service-transport: 1.6.*
Requires (Dev)
- infection/infection: ^0.21.5
- phpunit/php-token-stream: 3.1.2
- phpunit/phpunit: ^8.5
- vimeo/psalm: ^4.2
- dev-master
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.1
- 1.5.0
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.0
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0.x-dev
- 1.2.0
- 1.1.33
- 1.1.32.x-dev
- 1.1.32
- 1.1.31.x-dev
- 1.1.31
- 1.1.30
- 1.1.29
- 1.1.28
- 1.1.27
- 1.1.25
- 1.1.24
- 1.1.23
- 1.1.22
- 1.1.21
- 1.1.20
- 1.1.19
- 1.1.18
- 1.1.17
- 1.1.16
- 1.1.15
- 1.1.14
- 1.1.13
- 1.1.12
- 1.1.11
- 1.1.10
- 1.1.9
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
This package is auto-updated.
Last update: 2024-10-27 19:35:06 UTC
README
Installation
Just type
composer require mezon/service
First step
This is our first service.
class TodoService extends \Mezon\Service\ServiceBase implements \Mezon\Service\ServiceBaseLogicInterface { /** * First endpoint */ public function actionPing() { return ('I am alive!'); } } \Mezon\Service\Service::start(TodoService::class);
Here:
- \Mezon\Service\ServiceBase - base class for simple services
- \Mezon\Service\ServiceBaseLogicInterface - class must implement this interface to provide actions in format 'action'
- \Mezon\Service\Service::start(TodoService::class) - launching our service
Then you can access your first endpoint in a way like this:
http://localhost/?r=ping
Here is 'ping' is part afer 'action' in the 'actionPing'.
You can create longer endpoints:
public function actionLongEndpoint() { return ('long endpoint'); }
And it will be available via this URL:
http://localhost/?r=long-enpoint
Introducing logic
In the concept of Mezon framework service class is only a container for logic, model, transport, security providerrs and so on.
So we may want to fetch all logic to the separate class. Then we shall do it in this way:
/** * Logic implementation * * @author Dodonov A.A. */ class TodoLogic extends \Mezon\Service\ServiceBaseLogic { /** * First endpoint */ public function actionPing() { return ('I am alive!'); } }
And then we shall modify service class like this:
/** * Service class * * @author Dodonov A.A. */ class TodoService extends \Mezon\Service\ServiceBase { } \Mezon\Service\Service::start(TodoService::class, TodoLogic::class);
But as you see - we have empty service class with only base functionality. So we can completely remove it and change our code:
\Mezon\Service\Service::start(\Mezon\Service\ServiceBase::class, TodoLogic::class);
Multyple logic classes
But you can split your functionality into several classes like in the next example:
\Mezon\Service\Service::start(\Mezon\Service\ServiceBase::class, [ TodoSystemLogic::class, TodoReadLogic::class, TodoWriteLogic::class ]);
Here you just pass several classes when creating service.
Complex routing
You can create more complex routes. To do this you have to setup them in the routes.json config of your service. The content of this file must looks like this:
[ { "route": "/user/[s:userName]/profile/articles/[i:articleId]/comments/[i:headCommentId]", "callback": "userHeadComment", "method": "GET", "call_type": "public_call" } ]
And we need logic class for this route:
class CommentLogic extends \Mezon\Service\ServiceBaseLogic { /** * Our endpoint */ public function userHeadComment(string $route, array $params) { return [ // some data here ]; } }
In this example the method userHeadComment handles routing processing. And this method receives array with routes parameters. Something like that:
[ 'userName' => 'name of the user', 'articleId' => 'id of the article', 'headCommentId' => 'comment's id' ]
But you can also store all route configs in PHP files like this:
<?php return [ [ "route" => "/user/[s:userName]/profile/articles/[i:articleId]/comments/[i:headCommentId]", "callback" => "userHeadComment", "method" => "GET", "call_type" => "public_call" ] ];
Authentication
It is quite obvious that not all your endpoints will be public. Some of them should check credentials.
First of all you should understand that framework knows nothing about your registry of users and their permissions and roles. So you have to provide information about that.
Do it by implementing security provider like inthe listing below:
// this is first step // but we have some more ) class TodoSecurityProvider extends \Mezon\Service\ServiceAuthenticationSecurityProvider{ }
And our code will look like this:
/** * Logic implementation */ class TodoLogic extends \Mezon\Service\ServiceBaseLogic { /** * First endpoint */ public function ping(): string { return 'I am alive!'; } /** * Second route */ public function whoAmI(): string { return 'I\'am Batman!'; } } /** * Service class */ class TodoService extends \Mezon\Service\ServiceBase { } /** * Security provider */ class TodoSecurityProvider extends \Mezon\Service\ServiceAuthenticationSecurityProvider{} \Mezon\Service\Service::start(TodoService::class, TodoLogic::class, null, TodoSecurityProvider::class);
And routes must be described like this:
[ { "route": "/ping/", "callback": "ping", "method": "GET", "call_type": "public_call" }, { "route": "/who-am-i/", "callback": "whoAmI", "method": "GET" } ]
Custom fields
You can extend your models with fields wich configuration will be defined in the client's code. For example you have entity 'user' with some custom fields. But you don't know what custom fields it will have. You don't know will it have field 'skype', or field 'notes', or field 'gender' etc. List of these fields may vary from project to project.
To work with such fields you can use \Mezon\Service\CustomFieldsModel
Table structure
tba
Methods reference
tba