PHP Classes

Mezon Service: Implement Web services routing requests to classes

Recommend this page to a friend!
  Info   View files View files (49)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 37 This week: 1All time: 10,889 This week: 560Up
Version License PHP version Categories
mezon-service 1.0.0MIT/X Consortium ...5HTTP, PHP 5, Design Patterns
Description 

Author

This package can implement Web services routing requests to action classes.

It provides a base class to implement Web service actions that will be executed when a HTTP request is received by the server running PHP with this package.

The package also provides a base logic class that can be extended by application to determine how request can be mapped to service handler classes.

Separate configuration can define the URL patterns that need to be matched to route requests to logic classes.

Picture of Alexey Dodonov
  Performance   Level  
Name: Alexey Dodonov <contact>
Classes: 58 packages by
Country: Russian Federation Russian Federation
Age: ???
All time rank: 185254 in Russian Federation Russian Federation
Week rank: 109 Up7 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 13x

Details

Set of classes for creating microservices

Build Status codecov Scrutinizer Code Quality

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<Endpoint>'
  • \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


  Files folder image Files  
File Role Description
Files folder imageServiceConsoleTransport (2 files)
Files folder imageServiceHttpTransport (2 files)
Files folder imageServiceRestTransport (1 file)
Files folder imageTests (20 files, 1 directory)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Plain text file CustomFieldsModel.php Class Class source
Plain text file DbServiceModel.php Class Class source
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Read me
Plain text file Service.php Class Class source
Plain text file ServiceAuthenticat...ecurityProvider.php Class Class source
Plain text file ServiceAuthenticat...oviderInterface.php Class Class source
Plain text file ServiceAuthorizati...oviderInterface.php Class Class source
Plain text file ServiceBase.php Class Class source
Plain text file ServiceBaseLogic.php Class Class source
Plain text file ServiceBaseLogicInterface.php Class Class source
Plain text file ServiceLogic.php Class Class source
Plain text file ServiceMockSecurityProvider.php Class Class source
Plain text file ServiceModel.php Class Class source
Plain text file ServiceRequestParams.php Class Class source
Plain text file ServiceRequestParamsInterface.php Class Class source
Plain text file ServiceSecurityProviderInterface.php Class Class source
Plain text file ServiceTransport.php Class Class source
Plain text file ServiceTransportInterface.php Class Class source

  Files folder image Files  /  ServiceConsoleTransport  
File Role Description
  Plain text file ConsoleRequestParams.php Class Class source
  Plain text file ServiceConsoleTransport.php Class Class source

  Files folder image Files  /  ServiceHttpTransport  
File Role Description
  Plain text file HttpRequestParams.php Class Class source
  Plain text file ServiceHttpTransport.php Class Class source

  Files folder image Files  /  ServiceRestTransport  
File Role Description
  Plain text file ServiceRestTransport.php Class Class source

  Files folder image Files  /  Tests  
File Role Description
Files folder imageconf (2 files)
  Plain text file ConsoleRequestParamsUnitTest.php Class Class source
  Plain text file CustomFieldsModelUnitTest.php Class Class source
  Plain text file DbServiceModelUnitTest.php Class Class source
  Plain text file HttpRequestParamsUnitTest.php Class Class source
  Plain text file MockParamsFetcher.php Class Class source
  Plain text file ServiceAuthenticat...roviderUnitTest.php Class Class source
  Plain text file ServiceBaseLogicUnitTest.php Class Class source
  Plain text file ServiceBaseLogicUnitTests.php Class Class source
  Plain text file ServiceBaseUnitTest.php Class Class source
  Plain text file ServiceConsoleTransportUnitTest.php Class Class source
  Plain text file ServiceHttpTransportUnitTest.php Class Class source
  Plain text file ServiceLogicUnitTest.php Class Class source
  Plain text file ServiceLogicUnitTests.php Class Class source
  Plain text file ServiceMockSecurityProviderUnitTest.php Class Class source
  Plain text file ServiceRestTransportUnitTest.php Class Class source
  Plain text file ServiceTests.php Class Class source
  Plain text file ServiceTransportUnitTest.php Class Class source
  Plain text file ServiceUnitTest.php Class Class source
  Plain text file ServiceUnitTests.php Class Class source
  Plain text file TestService.php Class Class source

  Files folder image Files  /  Tests  /  conf  
File Role Description
  Accessible without login Plain text file routes.json Data Auxiliary data
  Accessible without login Plain text file routes.php Aux. Auxiliary script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:37
This week:1
All time:10,889
This week:560Up