raketman/service-versioning-bundle

Easy versioning your services

Installs: 1 662

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

Type:symfony-bundle

1.0.1 2020-09-17 09:07 UTC

This package is auto-updated.

Last update: 2024-05-06 18:24:03 UTC


README

##Versioning Symfony Services

composer require raketman/service-versioning-bundle

####Usage :

services:
    
  #Specify the tag 'raketman.version.factory' - now this service will have versions
  #resolver - service that will determine the version
  
  app.validation.test:
    class: App\Validation\VInterface
    tags:
      - {name: 'raketman.version.factory', resolver: 'app.version.resolver'}
  # OR if yuo have autowire
  
  App\Validation\VInterface
    tags:
      - {name: 'raketman.version.factory', resolver: 'app.version.resolver'}



  #The version is indicated through tags that correspond to the name of the service we want to version
  #version - value of version 

  app.validation.test_v1:
    class: App\Validation\V1
    tags:
      - {name: 'app.validation.test', version: 1 }
      - {name: 'app.validation.test', version: 3 }

  app.validation.test_v2:
    class: App\Validation\V2
    tags:
      - {name: 'app.validation.test', version: 2 }
  #OR if yuo have autowire
  
  app.validation.test_v1:
    class: App\Validation\V1
    tags:
      - {name: 'App\Validation\VInterface', version: 1 }
      - {name: 'App\Validation\VInterface', version: 3 }

  app.validation.test_v2:
    class: App\Validation\V2
    tags:
      - {name: 'App\Validation\VInterface', version: 2 }		
	
  # Version resolver
  	
  app.version.resolver:
    class: App\Resolver\Version
    arguments:
        - '@request_stack'

Now you have the service "app.validation.test", upon receipt of which, depending on which version it will return "app.version.resolver", if 1 then "app.validation.test_v1" or if 2 then "app.validation.test_v2 will return

#####For example, the implementation of "app.version.resolver" is as follows:

 namespace App\Resolver;

 use Raketman\Bundle\ServiceVersioningBundle\Resolver\VersionResolverInterface;
 use Symfony\Component\HttpFoundation\RequestStack;

 class Version implements VersionResolverInterface
 {
     /** @var RequestStack  */
     private  $request;
 
     public function __construct(RequestStack $request)
     {
         $this->request = $request;
     }
 
     public function getVersion()
     {
         return $this->request->getMasterRequest()->get('version') ? : 1;
     }
 }

Now we can use "app.validation.test", and get different versions depending on $ _GET ['version']

 $validation = $this->get('app.validation.test');
 
 if $_GET['version'] === 1, then get_class($validation) - App\Validation\V1 // app.validation.test_v1
 if $_GET['version'] === 2, then get_class($validation) - App\Validation\V2 // app.validation.test_v2
 if $_GET['version'] === 3, then get_class($validation) - App\Validation\V1 // app.validation.test_v1
 
 OR
 
 /**
 * @Route("/some-method")
 */
 public function test(App\Validation\VInterface $validation)
 {
     //if $_GET['version'] === 1, then get_class($validation) - App\Validation\V1 // app.validation.test_v1
     //if $_GET['version'] === 2, then get_class($validation) - App\Validation\V2 // app.validation.test_v2
     //if $_GET['version'] === 3, then get_class($validation) - App\Validation\V1 // app.validation.test_v1
 }

The implementation of resolver can be any, for example, based on the current user, which will allow for various users on the go to issue the necessary service implementations

If you use versioning in daemons, then do not forget to set the service mode

  shared: false

otherwise you will always be given the same instance after the first call