mike227/n-service-annotations

This package is abandoned and no longer maintained. No replacement package was suggested.

An alternative way how to define Nette's services using annotations.

dev-master 2015-03-12 00:13 UTC

This package is not auto-updated.

Last update: 2017-09-05 07:02:39 UTC


README

Adds support for annotated services and factories to Nette Framework. With annotation can be defined service or generated factory with custom settings.

Requirements

Mike227/NServiceAnnotations requires PHP 5.3.3 or higher.

Installation

The best way to install Mike227/NServiceAnnotations is using Composer:

composer require mike227/n-service-annotations:@dev

Edit your app/config/config.neon and register extension:

extensions:
	serviceAnnotations: Mike227\NServiceAnnotations\DI\Extension

Configuration

serviceAnnotations:
	dirs: # directories with annotated classes and interfaces
		- %appDir%/model

Usage

Service definition

Add annotation @Mike227\NServiceAnnotations\Service to your service class.

This extension automatically registers the class as service.

use Nette;
use Mike227\NServiceAnnotations\Service;

/**
 * @Service
 */
class Users extends Nette\Object {
    ....
}

Generated factory definition

Add annotation @Mike227\NServiceAnnotations\Factory to your factory interface.

This extension automatically registers the interface as generated factory.

use Nette;
use Mike227\NServiceAnnotations\Factory;

/**
 * @Factory
 */
interface IRegistrationControlFactory {
    /**
     * @return RegistrationControl
     */
    public function create();
}

Your own annotations

Implementing IServiceAnnotation interface you can create your own annotations, which also serve as a factory for the service definition.

See IServiceAnnotation::createServiceDefinition method.

Example annotation:

use Mike227\NServiceAnnotations\IServiceAnnotation;

/**
 * @Annotation
 */
class EventSubscriber implements IServiceAnnotation
{
	/**
	 * @param string $class
	 * @return ServiceDefinition
	 */
	public function createServiceDefinition($class)
	{
		$definition = new ServiceDefinition();
		$definition->setClass($class);
		$definition->addTag('kdyby.subscriber');
		return $definition;
	}

	/**
	 * @param ServiceDefinition $serviceDefinition
     * @param ContainerBuilder $builder
     * @return void
     */
	public function beforeContainerCompile(ServiceDefinition $factoryDefinition, ContainerBuilder $builder) {
		// ... this method can be used to connect your service with other services or adjusts DI container
	}
}

Service definition:

/**
 * @EventSubscriber
 */
class TestEventSubscriber extends Nette\Object implements Kdyby\Events\Subscriber
{
    ...
}

You can also make own implementation of IFactoryAnnotation. See Factory for inspiration.

For more examples see examples directory.

License

NServiceAnnotations is licensed under the MIT License - see the LICENSE.md file for details