PHP code example of vcn / symfony-autofactory

1. Go to this page and download the library: Download vcn/symfony-autofactory 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/ */

    

vcn / symfony-autofactory example snippets




use Vcn\Symfony\AutoFactory\AutoFactory;

class FooFactory implements AutoFactory
{
    public static function createFoo(): Foo
    {
        return new Foo();
    }
}



use Vcn\Symfony\AutoFactory\AutoFactory;
use Vcn\Symfony\AutoFactory\Annotation\Alias;

class FooFactory implements AutoFactory
{
    /**
     * @Alias(id="foo.service", public=true)
     */
    public static function createFoo(): Foo
    {
        return new Foo();
    }
}



use Vcn\Symfony\AutoFactory\AutoFactory;
use Vcn\Symfony\AutoFactory\Annotation\Autoconfigure;

/**
 * Override the default, now all factory methods in this class are not autoconfigured by default
* @Autoconfigure(false)
 */
class FooFactory implements AutoFactory
{
    /**
     * But we want autoconfiguration for this specific dependency
     * @Autoconfigure(true)
     */
    public static function createFoo(): Foo
    {
        return new Foo();
    }
}



use Vcn\Symfony\AutoFactory\AutoFactory;
use Vcn\Symfony\AutoFactory\Annotation\Autowire;

/**
 * Override the default, now all factory methods in this class are not autowired by default
 * @Autowire(false)
 */
class FooFactory implements AutoFactory
{
    public static function createFoo(): Foo
    {
        return new Foo();
    }
    
    /**
     * But we want autowiring for this specific method
     * @Autowire(true)
     */
    public static function createBar(): Bar
    {
        return new Bar();
    }
}



use Vcn\Symfony\AutoFactory\AutoFactory;
use Vcn\Symfony\AutoFactory\Annotation\Bind;

class FooFactory implements AutoFactory
{
    /**
     * @Bind(arg="$barService", id="bar.service")
     */
    public static function createFoo(Bar $barService): Foo
    {
        return new Foo($barService->getSomethingINeed());
    }
}



use Vcn\Symfony\AutoFactory\AutoFactory;
use Vcn\Symfony\AutoFactory\Annotation\Bind;

class FooFactory implements AutoFactory
{
    /**
     * @Id("foo.consumer")
     */
    public static function createConsumerFoo(): Foo
    {
        return new Foo();
    }
    
    /**
     * @Id("foo.business")
     */
    public static function createBusinessFoo(): Foo
    {
        return new Foo();
    }
    
    /**
     * @Bind(arg="$foo", id="foo.consumer")
     */
    public static function createConsumerBar(Foo $foo): Bar
    {
        return new Bar($foo);
    }
    
    /**
     * @Bind(arg="$foo", id="foo.business")
     */
    public static function createBusinessBar(Foo $foo): Bar
    {
        return new Bar($foo);
    }
}



use Vcn\Symfony\AutoFactory\AutoFactory;
use Vcn\Symfony\AutoFactory\Annotation\IsPublic;

/**
 * Override the default, now all factory methods in this class are public by default
* @IsPublic(true)
 */
class FooFactory implements AutoFactory
{
    public static function createFoo(): Foo
    {
        return new Foo();
    }
    
    /**
     * We can override again. This dependency will not be public after all. 
     * @IsPublic(false)
     */
    public static function createBar(): Bar
    {
        return new Bar();
    }
}



use Vcn\Symfony\AutoFactory\AutoFactory;
use Vcn\Symfony\AutoFactory\Annotation\Tag;

class FooFactory implements AutoFactory
{
    /**
     * @Tag("some.tag")
     * @Tag("foo.tag", important=true, bar="baz")
     */
    public static function createFoo(): Foo
    {
        return new Foo();
    }
}