Download the PHP package kostislav/php-class-symfony-service-config without Composer

On this page you can find all versions of the php package kostislav/php-class-symfony-service-config. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package php-class-symfony-service-config

PHP-based Symfony service configuration

Build status

The Symfony framework offers three methods of configuring its Dependency Injection Container: XML configuration, YAML configuration and PHP configuration. The XML and YAML approaches are a little developer-unfriendly because of things like missing IDE completion. The PHP approach tries to solve these problems, but instead of using PHP directly, it invents a DSL on top of PHP and in turn suffers from the same problems, albeit to a lesser degree.

This library offers a different method of configuring services using plain PHP, heavily inspired by Java's Spring framework Java-based configuration.

NOTE: The library makes extensive use of PHP attributes, which means it requires at least PHP 8.0.

Example

Consider the following simple setup: One public service depending on two other services. In Symfony PHP config, it might look like this:

Or, if you prefer YAML:

Using this library, you can write the same definition like this:

For this simple example, the code might not be shorter, but there is an obvious advantage: the creation of your services is written in plain PHP. IDE features like code completion, type hinting and usage analysis work out of the box. And if you need to do something more complicated, like calling a setter or using a static factory method, you can do it like in any other place in your codebase - no need to learn yet another expression language to call a method on another service.

Setting it up

  1. Add a composer dependency on kostislav/php-class-symfony-service-config
  2. Unfortunately, Symfony does not offer any way for the library to hook into the right internals, so a manual change of your Kernel class is necessary. Add the following line to the configureContainer method:

  3. Register your configuration classes with the $loader. A convenient place to do it is in the Kernel class, just below the line added in step 2.

What it does

By default, each public non-static method in the config class that is annotated with the ServiceDefinition attribute will be used to create a private service with the same name as that method. This method must have a return type hint. Any parameters of this method will be added as dependencies of this service and will be resolved by name.

The config class itself must have a no-argument constructor.

Explicitly naming a service

If you need a service to have a name other than the defining method, pass it to the ServiceDefinition attribute.

Public services

In Symfony, services are by default private and cannot be requested from the container. Things like controllers need to be marked as public. This is controlled by the isPublic parameter of the ServiceDefinition attribute.

Using a service with a name that cannot be used as a PHP identifier

Symfony is full of services with names like annotations.cached_reader. As PHP won't allow us to use parameter names like that, you can specify the name of the injected service explicitly using the Service parameter attribute.

Using container parameters

Method parameters annotated with the Parameter attribute will be populated with the corresponding container parameter rather than a service. Again, by default, the method parameter name is used as the name of the parameter to look up, but another name can be specified on the attribute.

Splitting the configuration class

When you have a lot of services, the configuration class can get long. You can split it into multiple classes and either load each of them in your Kernel, or just load one of them and use the Import attribute to include the other ones. Services from the imported classes will be available in the importing config class.

Imports work transitively - if the imported class itself has another Import attribute with another class, that class will be included as well.

Service tags

If you need to tag your service, just add a Tag attribute. There can be multiple Tag attributes on the same method.

Performance

What is the performance impact of all this reflection and attribute reading? Don't worry, the configuration classes are only analyzed once when the container is built, at the same time the services.yaml file would get parsed. After that, the performance difference compared to the other approaches is one additional method call (of the service definition method), which isn't noticeable at all.


All versions of php-class-symfony-service-config with dependencies

PHP Build Version
Package Version
Requires php Version >=8.0
symfony/config Version ^5.2
symfony/dependency-injection Version ^5.2
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package kostislav/php-class-symfony-service-config contains the following files

Loading the files please wait ....