symplify/package-builder

Dependency Injection, Console and Kernel toolkit for Symplify packages.

Installs: 3 731 835

Dependents: 77

Suggesters: 0

Security: 0

Stars: 143

Watchers: 4

Forks: 1

9.0.0-rc1 2020-12-05 18:57 UTC

This package is auto-updated.

Last update: 2020-12-05 19:23:40 UTC


README

Downloads total

This tools helps you with Collectors in DependecyInjection, Console shortcuts, ParameterProvider as service and many more.

Install

composer require symplify/package-builder

Use

Get All Parameters via Service

# app/config/services.yaml
parameters:
    source: "src"

services:
    _defaults:
        autowire: true

    Symplify\PackageBuilder\Parameter\ParameterProvider: ~

Then require in __construct() where needed:

namespace App\Configuration;

use Symplify\PackageBuilder\Parameter\ParameterProvider;

final class ProjectConfiguration
{
    /**
     * @var ParameterProvider
     */
    private $parameterProvider;

    public function __construct(ParameterProvider $parameterProvider)
    {
        $this->parameterProvider = $parameterProvider;
    }

    public function getSource(): string
    {
        // returns "src"
        return $this->parameterProvider->provideParameter('source');
    }
}

Get Vendor Directory from Anywhere

$vendorDirProvider = new Symplify\PackageBuilder\Composer\VendorDirProvider();
// returns path to vendor directory
$vendorDirProvider->provide();

Smart Compiler Passes for Lazy Programmers ↓

How to add compiler pass?


Always Autowire this Type

Do you want to allow users to register services without worrying about autowiring? After all, they might forget it and that would break their code. Set types to always autowire:

namespace App;

use PhpCsFixer\Fixer\FixerInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel;
use Symplify\PackageBuilder\DependencyInjection\CompilerPass\AutowireInterfacesCompilerPass;

final class AppKernel extends Kernel
{
    protected function build(ContainerBuilder $containerBuilder): void
    {
        $containerBuilder->addCompilerPass(new AutowireInterfacesCompilerPass([FixerInterface::class]));
    }
}

This will make sure, that PhpCsFixer\Fixer\FixerInterface instances are always autowired.


That's all :)


Report Issues

In case you are experiencing a bug or want to request a new feature head over to the Symplify monorepo issue tracker

Contribute

The sources of this package are contained in the Symplify monorepo. We welcome contributions for this package on symplify/symplify.