Download the PHP package mnapoli/fluent-symfony without Composer
On this page you can find all versions of the php package mnapoli/fluent-symfony. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package fluent-symfony
Fluent configuration for Symfony
This package offers an alternative configuration syntax for Symfony's container, inspired by PHP-DI's configuration.
- Why?
- Comparison with existing formats
- Installation
- Syntax
- Parameters
- Services
- Using the class name as the entry ID
- Autowiring
- Constructor arguments
- Dependencies
- Setter injection
- Property injection
- Optional service references
- Decorated services
- Non shared services
- Factories
- Aliases
- Tags
- Imports
- Extensions
Why?
The main goal is to benefit from stricter analysis from the PHP engine and IDEs. If you are interested you can also read why YAML was replaced by a similar syntax in PHP-DI 5.
-
auto-completion on classes or constants:
-
auto-completion when writing configuration:
-
real time validation in IDEs:
-
constant support:
- better refactoring support
Comparison with existing formats
Currently, in Symfony, you can configure the container using:
-
YAML
-
XML
- PHP code
With this package, you can now use a 4th alternative:
Installation
To enable the new format in a Symfony fullstack application, simply import the EnableFluentConfig
trait in app/AppKernel.php
, for example:
You can now either:
-
write all your config in "fluent" syntax, to do that change your
AppKernel
to load.php
files instead of.yml
: - or import PHP config files from YAML config files:
Be advised that PHP config files in the "traditional" form (see the documentation) are still supported and will continue to work.
Syntax
A configuration file must return
a PHP array. In that array, parameters, services and imports are defined altogether:
Parameters
Parameters are declared as simple values:
This is the same as:
Parameters and services can be mixed in the same array.
Services
Services can be declared simply using the create()
function helper:
When calling $container->get('mailer')
an instance of the Mailer
class will be created and returned.
This is the same as:
Using the class name as the entry ID
If the container entry ID is a class name, you can skip it when calling create()
.
Autowiring
Services can also be automatically wired using the autowire()
function helper in place of create()
:
This is the same as:
Constructor arguments
This is the same as:
Dependencies
Parameters can be injected using the '%foo%'
syntax:
This is the same as:
Services can be injected using the get()
function helper:
This is the same as:
Setter injection
This is the same as:
Property injection
This is the same as:
Optional service references
Services can have optional dependencies, so that the dependency is not required for it to work.
Setting missing dependencies to null
This is the same as :
Ignore missing dependencies
When used with setter injection, it's possible to remove the method call using ignoreIfMissing()
:
This is the same as :
Private Services
This is the same as:
Decorated services
Services can be decorated with the decorate()
method
This is the same as:
If you want to apply more than one decorator to a service, you can change the inner service name (IE the decorated service) and configure the priority of decoration :
This is the same as:
Non shared services
All services are shared by default. You can force the container to always create a new instance using the unshared()
function helper:
This is the same as:
yaml
services: app.phpmailer: class: AppBundle\Mail\PhpMailer synthetic: true yaml services: newsletter_manager: factory: ['AppBundle\Email\NewsletterManager', 'create'] class: 'AppBundle\Email\NewsletterManager' arguments: ['foo', 'bar'] yaml services: app.phpmailer: class: AppBundle\Mail\PhpMailer app.mailer: alias: app.phpmailer yaml
services: app.phpmailer: class: AppBundle\Mail\PhpMailer app.mailer: alias: app.phpmailer public: false yaml services: mailer: class: Mailer tags:
- {name: foo, fizz: buzz, bar: baz}
- {name: bar}
yaml
imports:
- { resource: services/mailer.yml }
yaml
framework:
http_method_override: true
trusted_proxies: [192.0.0.1, 10.0.0.0/8]
- { resource: services/mailer.yml }
yaml
framework:
http_method_override: true
trusted_proxies: [192.0.0.1, 10.0.0.0/8]