Convert xml to yaml, section "calls" - symfony

How I can convert this configuration from xml to yaml?
<service id="sonata.media.admin.media" class="%sonata.media.admin.media.class%">
...
<call method="setTemplates">
<argument type="collection">
<argument key="list">ApplicationSonataMediaBundle:MediaAdmin:list.html.twig</argument>
</argument>
</call>
...
</service>
Thanks!

Try this :
services:
sonata.media.admin.media:
class: %sonata.media.admin.media.class%
calls:
- [setTemplates, [list: "ApplicationSonataMediaBundle:MediaAdmin:list.html.twig"]]
Docs here

The online converter from Ross Tuck turns out to be a good tool!

Related

Symfony 2: Is there a difference between repository defined as a service vs repository injected with expression?

I can define repository as a service:
<service id="books_repository" class="MyApp\BooksBundle\Repository\BooksRepository">
<factory service="doctrine.orm.default_entity_manager" method="getRepository"/>
<argument>MyApp\BooksBundle\Entity\BookEntity</argument>
</service>
and then inject:
<service id="books_service" class="MyApp\BooksBundle\Service\BooksService">
<argument type="service" id="books_repository"/>
</service>
But I can also use expression argument:
<service id="books_service" class="MyApp\BooksBundle\Service\BooksService">
<argument type="expression">service('doctrine').getManager('default').getRepository('MyApp\BooksBundle\Repository\BooksRepository')</argument>
</service>
Are there any reasons I should prefer one solution over the other one? Are they functionally equivalent?

Override service definitions parameters by configuration

I have noticed in several bundles that some services definition files had parameters embedded in it, like in the following example:
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<!-- CUSTOMER -->
<parameter key="sonata.customer.admin.customer.class">Sonata\CustomerBundle\Admin\CustomerAdmin</parameter>
<parameter key="sonata.customer.admin.customer.controller">SonataAdminBundle:CRUD</parameter>
</parameters>
<services>
<service id="sonata.customer.admin.customer" class="%sonata.customer.admin.customer.class%">
<tag name="sonata.admin" manager_type="orm" group="sonata_ecommerce" label="B2C" label_translator_strategy="sonata.admin.label.strategy.underscore"/>
<argument />
<argument>%sonata.customer.admin.customer.entity%</argument>
<argument>%sonata.customer.admin.customer.controller%</argument>
<call method="addChild">
<argument type="service" id="sonata.customer.admin.address" />
</call>
<call method="addChild">
<argument type="service" id="sonata.order.admin.order" />
</call>
</service>
</services>
</container>
Those parameters are not exposed as bundle configuration.
I recently discovered it is possible to add a CompilerPass class to override some service definitions. Although, this looks a bit tedious to do.
Is it possible to override those <parameter> by configuration ?
In your app/config/config.yml (or whatever imported config file) add :
parameters:
parameter.name: new_value

SonataUserBundle overriding EditableRolesBuilder

Im trying to override EditableRolesBuilder in Security folder.
I try to make same hierarchy in
Application\Sonata\UserBundle\Security\EditableRolesBuilder which
extends the base one Sonata\UserBundle\Security\EditableRolesBuilder
from vendor but with no success.
I try to override service in
Sonata\UserBundle\Resources\config\admin.xml, to pass my custom class
but i can't find anywhere how to override whole service.This service
is without parameters but hardcoded class and i cant't pass it in
config.yml
In Sonata admin if you wish to override security roles handler you have to override 2 services
sonata.user.editable_role_builder
sonata.user.form.type.security_roles
And definitions will look like as below
<services>
<service id="sonata.user.editable_role_builder" class="Acme\DemoBundle\Security\EditableRolesBuilder">
<argument type="service" id="security.context" />
<argument type="service" id="sonata.admin.pool" />
<argument>%security.role_hierarchy.roles%</argument>
</service>
<service id="sonata.user.form.type.security_roles" class="Acme\DemoBundle\Form\Type\SecurityRolesType">
<tag name="form.type" alias="sonata_security_roles" />
<argument type="service" id="sonata.user.editable_role_builder" />
</service>
</services>
For reference see Sonata Admin Override Security Roles Handler

Convert fos user bundle service into yml

I have a FosUserBundle service I need to convert to yml format. How can I do this is there a dumper convertor or anything?
How this would look in yml?
<service id="fos_user.mailer.twig_swift" class="FOS\UserBundle\Mailer\TwigSwiftMailer" public="false">
<argument type="service" id="mailer" />
<argument type="service" id="router" />
<argument type="service" id="twig" />
<argument type="collection">
<argument key="template" type="collection">
<argument key="confirmation">%fos_user.registration.confirmation.template%</argument>
<argument key="resetting">%fos_user.resetting.email.template%</argument>
</argument>
<argument key="from_email" type="collection">
<argument key="confirmation">%fos_user.registration.confirmation.from_email%</argument>
<argument key="resetting">%fos_user.resetting.email.from_email%</argument>
</argument>
</argument>
</service>
I was trying to use Yml dumper but this is just giving me serialised object:
$cs = new ContainerBuilder();
$loader1 = new Loader\XmlFileLoader($cs, new FileLocator(__DIR__ . '/../../../../vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Resources/config'));
$loader1->load('mailer.xml');
$dumper = new \Symfony\Component\Yaml\Dumper();
file_put_contents(__DIR__ . '/test2.yml', $dumper->dump($cs));
Any tips would be much appreciated. Thanks in advance.
You can try do it in such way:
services:
fos_user.mailer.twig_swift:
class: FOS\UserBundle\Mailer\TwigSwiftMailer
arguments:
- #mailer
- #router
- #templating
- { template: { confirmation: %fos_user.registration.confirmation.template%, resetting: %fos_user.resetting.email.template% }, from_email: { confirmation: %fos_user.registration.confirmation.from_email%, resetting: %fos_user.resetting.email.from_email% } }
You use wrong dumper as well you should use Symfony\Component\DependencyInjection\Dumper\YamlDumper

What is Form Context in Symfony2

I am getting started with Symfony2 and I am trying to understand the Form Component. I am looking at this page http://docs.symfony-reloaded.org/guides/forms/overview.html
And I can understand how we create form classes but what is confusing is how we actually use those forms in our controller.
$form = ContactForm::create($this->get('form.context'));
Does anyone have a more in depth explanation of the form.context portion of this code, and the actual process behind using forms within controllers?
Thanks.
form.context service is a Symfony\Component\Form\FormContext object by default. Here's a full definition of this service:
<service id="form.context" class="%form.context.class%">
<argument type="collection">
<argument key="validator" type="service" id="validator" />
<argument key="validation_groups">%form.validation_groups%</argument>
<argument key="field_factory" type="service" id="form.field_factory" />
<argument key="csrf_protection">%form.csrf_protection.enabled%</argument>
<argument key="csrf_field_name">%form.csrf_protection.field_name%</argument>
<argument key="csrf_provider" type="service" id="form.csrf_provider" />
</argument>
</service>
Actually it's a very simple object that just prepare some basic options used by almost every form, ie. a validator, CSRF protection and field factory.
In fact the code you've posted is equivalent of:
$form = new \Symfony\Components\Form\Form(null, array(
'validator' => $this->get('validator'),
'validation_groups' => ...
...
));

Resources