Auto register some common services
PHP
Latest commit 0ef1e1f Jun 14, 2016 @AntoineLelaisant AntoineLelaisant committed on GitHub Merge pull request #34 from KnpLabs/v2.2.0
v2.2.0

README.md

Rapid Application Development : Auto-Registration

Auto register some common services

Build Status Scrutinizer Code Quality Latest Stable Version Total Downloads Latest Unstable Version License

Installation

composer require knplabs/rad-auto-registration:~2.0
class AppKernel
{
    function registerBundles()
    {
        $bundles = array(
            //...
            new Knp\Rad\AutoRegistration\Bundle\AutoRegistrationBundle($this), // !! Do not forget to inject the kernel !!
            //...
        );

        //...

        return $bundles;
    }
}

Usages

Doctrine repositories auto-registration

Just activate doctrine (or doctrine_mongodb ou doctrine_couchdb depending on your needs) into your configuration.

knp_rad_auto_registration:
    enable:
        doctrine: ~
        doctrine_mongodb: ~
        doctrine_couchdb: ~

Now all repositories are auto-registred.

Entity Repository
MyProjectBundle\Entity\User my_project_bundle.entity.user_repository
TheOtherBundle\Entity\Model\Address the_other_bundle.model.user.address_repository
MyProjectBunde\Document\User my_project_bundle.document.user_repository

Requirements

Your doctrine entity (or documents) should be stored under Entity, Document or Model sub-namespace.

Constraint validators

Just activate constraint_validator into your configuration.

knp_rad_auto_registration:
    enable:
        constraint_validator: ~

Now all custom constaint validators are auto-registered.

Requirements

Your constraint validators should be stored under Validation or Validator sub-namespace.

Form extensions

Just activate form_type_extension into your configuration.

knp_rad_auto_registration:
    enable:
        form_type_extension: ~

Now all form type extensions are auto-registred.

Requirements

Your form type extensions should be stored under Form sub-namespace.

Security voters

Just activate security_voter into your configuration.

knp_rad_auto_registration:
    enable:
        security_voter: ~

Now all the voters are auto-registered.

Requirements

Your security voters should be stored under Security sub-namespace.

Twig extensions

Just activate twig_extension into your configuration.

knp_rad_auto_registration:
    enable:
        twig_extension: ~

Now all Twig extensions are auto-registered.

Requirements

Your Twig extensions should be stored under Twig or Templating sub-namespace.

Public/Private services

By default, all autoregistered services are marked as private (public: false). You can overide this behavior by setting the public parameter to true.

Example:

knp_rad_auto_registration:
    enable:
        doctrine:
            public: true
        doctrine_mongodb:
            public: false
        doctrine_couchdb:
            public: false

Restrictions

Autoloading

  • Only services without constructor or without required parameters into the constructor will be auto-registred.
  • If a service already exists, it will not be erased.

Bundle

You can apply auto-registration on only certains bundle. You just have to add a bundles configuration:

knp_rad_auto_registration:
    enable:
        # ...
    bundles: [ App, ProductBundle ]

WARNING

All auto-registered services are set as private, so it's impossible to get them by using $container->get('service_name'). If needed, create the service definition by yourself.