PHP code example of simonmarx / symfony-service-annotations

1. Go to this page and download the library: Download simonmarx/symfony-service-annotations library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

simonmarx / symfony-service-annotations example snippets


// config/bundles.php

return [
    // ...
    \SimonMarx\Symfony\Bundles\ServiceAnnotations\SimaServiceAnnotationsBundle::class => ['all' => true],
];



namespace App\Serializer;

use SimonMarx\Symfony\Bundles\ServiceAnnotations\Annotation\ServiceTag;

/**
 * @ServiceTag(CircularReferenceHandlerInterface::SERVICE_TAG, priority=-222)
 */
interface CircularReferenceHandlerInterface
{
    public const SERVICE_TAG = 'sm.serializer.circular_reference_handler';

    public function supports(object $object): bool;

    public function handle(object $object);
}



namespace App\Serializer;

use SimonMarx\Symfony\Bundles\ServiceAnnotations\Annotation\ServiceTagArgument;

/**
 * @ServiceTagArgument(argument="priority", value=-9999, ignoreWhenDefined=false)
 * @ServiceTagArgument(argument="someOther", value=false)
 */
class DefaultCircularReferenceHandler implements CircularReferenceHandlerInterface
{
    public function supports(object $object): bool
    {
        return true;
    }

    public function handle(object $object)
    {
        dd($object);
    }
}




namespace App\Serializer;

use SimonMarx\Symfony\Bundles\ServiceAnnotations\Annotation\ServiceAlias;

/**
 * @ServiceAlias(CircularReferenceHandlerInterface::class)
 */
class CircularReferenceHandlerChain implements CircularReferenceHandlerInterface
{
    private array $handlers = [];

    public function __construct(iterable $handlers = [])
    {
        $this->handlers = $handlers;
    }

    public function handle(object $object)
    {

    }

    public function supports(object $object): bool
    {
    }
}



namespace App\Struct;

use SimonMarx\Symfony\Bundles\ServiceAnnotations\Annotation\NoService;

/**
 * @NoService()
 */
class SomeStruct
{
    public ?string $name = null;
}



namespace App\Serializer;

use SimonMarx\Symfony\Bundles\ServiceAnnotations\Annotation\IgnoreParentServiceAnnotations;
use SimonMarx\Symfony\Bundles\ServiceAnnotations\Annotation\ServiceTag;
use SimonMarx\Symfony\Bundles\ServiceAnnotations\Annotation\ServiceTagArgument;

/** 
 * @ServiceTag("some_service_tag")
 * @ServiceTagArgument(argument="priority", value=2)
 */
abstract class MyParent {}

## Ignores all tags (related to service configuration) in your parent
/**
 * @IgnoreParentServiceAnnotations()
 */
class MyChild extends MyParent {}

## ignores only the configures annotations
/**
 * @IgnoreParentServiceAnnotations({ServiceTagArgument::class})
 */
class AnotherChild extends MyParent {}


## ignores all annotations except the excluded
/**
 * @IgnoreParentServiceAnnotations(exclude={ServiceTag::class})
 */
class SomeChild extends MyParent {}




namespace App;

use SimonMarx\Symfony\Bundles\ServiceAnnotations\Annotation\DependencyInjection;

class SomeClass {
    /**
     * The annotation also works for public properties injection
     * 
     * @DependencyInjection(serviceId="my_service")
     */
    public SomeInterface $service;

    /**
     * if you have more than one argument in constructor use "target" to identify which argument should get the injection
     *
     * @DependencyInjection(target="handlers", tagged="your_tag_id")
     * @DependencyInjection(target="anotherService", serviceId="some_service")
     */
    public function __construct(SomeAutowiredService $service, iterable $handlers, SomeService $anotherService) {
        
    }   

    /**
     * if you have only one argument in your constructor there is no need for the "target" option
     *
     * @DependencyInjection(tagged="your_tag_id")
     */
    public function __construct(iterable $handlers) {
        
    }   
}



namespace App;

use SimonMarx\Symfony\Bundles\ServiceAnnotations\Annotation\ParentService;

/**
 * @ParentService("App\Service\ParentService")
 */
class ChildService {
}