Componette

Componette

wavevision

wavevision / di-service-annotation 4.0.1

๐Ÿ”Œ Register DI services using annotation for @nette

download-cloud-line composer require wavevision/di-service-annotation

DIService Annotation

CI Coverage Status PHPStan

Helper for registering Nette DI services via Doctrine Annotations, factory generator and inject generator.

Install

composer require --dev wavevision/di-service-annotation

Note: Install phpstan-nette if you need support for strict return types.

Usage

Annotate your service

use Wavevision\DIServiceAnnotation\DIService;

/**
 * @DIService(params={"%wwwDir%"}, generateInject=true, generateFactory=true)
 */
class ExampleService
{

}

Create runner script

For example bin/extract-services.php

use Wavevision\DIServiceAnnotation\Configuration;
use Wavevision\DIServiceAnnotation\Runner;

Runner::run(new Configuration('sourceDirectory', 'services.neon'));

Running this script with php bin/extract-services.php

will generate from class following:

Annotation options

  • enableInject: bool โ€“ will add inject: on to generated service config (default true)
  • generateComponent: bool โ€“ will generate <className>Component trait, with factory and createComponent<ClassName> implemented
  • generateFactory: bool โ€“ will generate <ClassName>Factory interface with create function
  • generateInject: bool โ€“ will generate Inject<ClassName> trait with property $<className> and inject<ClassName> function implemented
  • params: string[] โ€“ list of DI parameters to be passed to service constructor
  • tags: string[] โ€“ list of tags to be used with the service in generated config

For configuration options see Configuration properties.

Configuration option

Required

  • sourceDirectory: string โ€“ location of services
  • outputFile: string โ€“ output file for registered services

Optional

  • setMask: string โ€“ mask for file locator - default *.php
  • setFileMapping: array โ€“ map for splitting configs by namespace
$configuration->setFileMapping([
    'RootNamespace\Namespace1' => 'config1.neon',
    'RootNamespace\Namespace2' => 'config2.neon',
]);
  • setInjectGenerator: Inject โ€“ set custom generator for injects
  • setFactoryGenerator: Factory โ€“ set custom generator for factories
  • setComponentFactory: Component โ€“ set custom generator for components
  • setRegenerate: bool โ€“ regenerate all generated code each run - default false
  • enableFileValidation โ€“ check each file for fatal errors before reading annotation, skip file on error
    • autoloadFile: string โ€“ file for class autoloading, e.g. vendor/autoload.php
    • tempDir: string โ€“ enable cache, directory for cache file, only changed files are validated
  • 4.0.1

  • 2.0.0 Change annotation settings

    BC: inject renamed to enableInject to disambiguate inject and generateInject properly

  • 1.0.1 Update coding standard

Componette Componette felix@nette.org