Symfony: Accessing parameters without container or static configured service - php

We have a project we're migrating to Symfony 3.3 and the new DI configuration. We have a service that used to be passed values from parameters file based on an argument supplied to the command. (bit more complex than this but you get the idea):
$this->service->setOptions($this->getContainer()->getParameter('account_'.$accountId));
With the migration we want to stop making the commands ContainerAwareCommand as we can now use new DI for the services. However, this leaves us stuck working out an alternative method of fetching the parameters when we cannot statically configure them in services.yml
Would it be best for me to just use the Yaml component and read the parameters.yml file in manually for this or is there some other, more appropriate solution?

Related

How to use my own service definition for a security firewall guard in Symfony 4.2?

I have a service defined in services.yaml that has a calls entry since I don't want to specify my dependencies in it's constructor so I use setters.
Now I am trying to point the security.firewalls.main.guard.authenticators.0 configuration entry to it, but if I try to reference it like this: '#App\Service\Authentication\Guard' I get The service "security.authentication.provider.guard.main" has a dependency on a non-existent service "#App\Service\Authentication\Guard".
If I specify just the name of the class as per Symfony's docs: App\Service\Authentication\Guard then there's no exception and the guard works, however the calls section of my definition is completely ignored and thus my setters don't work.
My best guess would be that the security module uses it's own service locator that's somehow disconnected and ignores definitions in services.yaml, which is probably why you won't find examples of the above config entry using simple service names rather than canonical class names.

How to handle config files in PHP library

I wrote a small library (https://github.com/dborsatto/php-giantbomb) that acts as a wrapper for an API. In order to work, the library needs 2 basic configuration options:
An API key
A file that describes the API options
What is the suggested way to handle these two files?
The API key is of course personal so it can't be added to the repository. But by doing so, I can't write a functional test for the library as a whole, thus limiting myself to only unit tests for the single parts. Do I give up the functional tests altogether or is there a way to make this work?
About the other config, basically it's a YAML file (https://github.com/dborsatto/php-giantbomb/blob/master/api_config.yml) that includes the base API endpoint, and configuration for each data repository. At the moment this is handled by a Config class that is decoupled in a way that the user must write glue code and inject data into the Config. This way it's easier to test, but as a whole I feel like it's creating a bigger disadvantage than just letting the Config class load the file, parse it and behave accordingly. What is the best way to do this? At the moment there are no tests in the repository, but I'm working on it (along with some code refactoring).
I would suggest to leave the configuration outside your library; I've done something similar for the Mandrill mailing service, where I left to the developer the management of the configuration (I was working in a Symfony 2 project). For me there is no Config class, just the service constructor that accepts the API key and an (optional) array of options:
public function __construct($api, $options = array())
{
// My code
}
When I need to use my service inside the Symfony 2 application, I take the needed parameters and configuration from a place (Symfony's config files) external to my service; this way I can decouple the library from the configuration. Of course the service contructor throws an exception if the mandatory parameters are missing.

Injecting the validator service into a bundle

I'm learning Symfony2 at a new workplace and was tasked with adding validation to the code which is currently using poor inline based code instead of the Symfony validator and annotations.
I've added validation annotations to my model and added a parameter to my constructor. I've also added an argument to the service.yml so that it gets injected into my bundle's class, but it seems i cannot find the
#validator
as per described in the Symfony documentation (http://symfony.com/doc/current/book/validation.html). If i read the documentation right, i should be able to just add #validator to my services.yml and get it fed directly to my class but when running my tests, it says that the service #validator cannot be found:
The service "ugroup_media_personalization.flattening_service"
has a dependency on a non-existent service "validator"
So what am i doing wrong here?
The problem was that the configuration file for the bundle did not specify the
framework:
validation: {enable_annotations: true}
And thus the framework was not loading the validation service. You can simply use:
framework:
validation:
if you want to trigger the loading the validation module, but in my case, i added enable_annotations to make sure the validation using annotations work!

Dependency injection in a symfony console command? Not full Symfony application

I've seen this How can i inject dependencies to Symfony Console commands? but that answer doesn't really give enough information and is already explained here http://symfony.com/doc/current/cookbook/console/console_command.html
The problem is a containerAwareCommand doesn't work with the setup here http://symfony.com/doc/current/components/console/introduction.html
In order to use containerAwareCommand from what I can tell, I need my application to use
Symfony\Bundle\FrameworkBundle\Console\Application
instead of
Symfony\Component\Console\Application
But using the frameworkBundle Application class requires an instance of KernelInterface and won't allow me to pass in a name and version to my application.
Here is what I have that won't work with containerAwareCommands
#!/usr/bin/env php
<?php
require __DIR__.'/../src/vendor/autoload.php';
$app = new Symfony\Component\Console\Application('spud', '0.0.1');
$app->add(new Isimmons\Spudster\Console\Commands\SayHelloCommand);
$app->run();
The command it's self runs but I get an error when trying to use getContainer
Call to undefined method Symfony\Component\Console\Application::getKernel()
On a related topic which will probably come up next, The documentation for registering a class in the container shows using a app/config/config.php file. But I don't have an app directory since this is not a full symfony application. My base directory in which all of the app except for the file above is located, is src/lib. If I can figure out the first part above, will symfony be able to find the config file at src/lib/config/config.php?
You can use Consolefull application.
Consolefull is a simple library to work with Symfony Console Component and Symfony Dependency Injection Component

Obtain the full configuration of a service including its parameters

php app/console container:debug does not give enough information on the services.
1 - What parameters are injected during the creation of a service?
If I want to know what parameters are injected into the service form.factory for example, is there a command for this?
php app/console container:debug form.factory does not give the list of parameters.
2 - Is there a way to find the location of a service (meaning the file where it actually is declared)?
It could be either:
a services.yml file
a services.xml file
through the DependancyInjection\Configuration.php file
But which one?
What parameters are injected during the creation of a service?
This does not exists yet, however I have planned to create a Pull Request for this for Symfony2.2
Is there a way to find the location of a service (meaning the file where it actually is declared)?
No and you won't get a feature like this. The code is not able to get the location of the file.
What you can do is locate in which bundle where the service is declared. All services from the core framework are declared in the SymfonyFrameworkBundle. You can find the service configuration in #bundleroot/Resources/config. The form.factory service is loctated in #SymfonyFrameworkBundle/Resources/config/form.xml.
There is a tool: http://jmsyst.com/bundles/JMSDebuggingBundle
It partly answers to your needs, by dumping dependencies of a given service.
However, it does not says where a service has been defined.

Categories