Symfony Dependency Injection: Set a repository as a service using php - symfony

I have the set the following respository as a service on my Symfony 3.2 project:
person_in_need_repository:
class: AppBundle\Repository\PersonInNeedRepository
factory: ["#doctrine","getRepository"]
arguments: ["AppBundle:PersonInNeed"]
How can I change its annotation in php format?

It can be done by like this:
use AppBundle\Repository\PersonInNeedRepository;
$container->register('app.person_in_need_repository',PersonInNeedRepository::class)
->setFactory([new Reference("doctrine"),"getRepository"])
->addArgument('AppBundle:PersonInNeed');
Also ensure that you have replaced the services.yml with services.php on config.yml when using it as a global way to set the services. Also rename properly the services.yml.

Related

bundle's service (autowire) not available in controller's method

I'd like to use https://github.com/tedious/TedivmStashBundle bundle in my symfony 4 project. Added by composer, configured in /config/stash.yaml file and according to the profiler bar, it's working basically.
Now, I want to use to caching values in my controller. I've try to add the service stash by its name to the method's parameter, using the same name as the example says:
$pool = $this->container->get('stash');
but the framework did not find the service. It can't be added like the example explained neither.
How can I use this bundle as a service (autowired) in my symfony 4 app?
UPDATE
services.yaml:
stash.default_cache:
class: Tedivm\StashBundle\Service\CacheService
public: true
arguments:
$name: 'stash'
Tedivm\StashBundle\Service\CacheService: '#stash.default_cache'
Controller:
public function something(Request $request, CacheService $service, ...
It's looks like working now :)
Thanx for the suggestion (and the correct solution later) to #Cerad
Here is an example of how to approach this sort of problem when dealing with bundles that are not quite ready for autowire.
Start by installing a test project:
symfony new --full stash --version=lts
composer require tedivm/stash-bundle
Note that the bundle does not directly support Symfony 5 hence the lts. Note also that the bundles does not support Flex so you have to add your own config/packages/stash.yaml file per the bundle's readme file.
# config/packages/stash.yaml
stash:
drivers: [ FileSystem ]
FileSystem: ~
At this point we can determine which service we need to inject:
bin/console debug:container stash
Information for Service "stash.default_cache"
=============================================
Class CacheService
---------------- -----------------------------------------
Option Value
---------------- -----------------------------------------
Service ID stash.default_cache
Class Tedivm\StashBundle\Service\CacheService
Most of the time you would like to use an interface for injection but a peek at the source code reveals that the bundle does not use interfaces. As a side note, calling a third party service 'stash' is not a good idea. It really should have been 'tedivm.stash' but I digress.
We can now create an alias and then typehint against it:
# config/services.yaml
Tedivm\StashBundle\Service\CacheService : '#stash' # alias for typehinting
# Controller class
public function index(CacheService $cacheService)
{
return new Response('Cache ' . get_class($cacheService));
}
And that should do it.

How to enable the Intl extension from twig/intl-extra

I've been running through the docs and couldn't find where to init twig/intl-extra
The twig doc here says to either use use Twig\Extra\Intl\IntlExtension; or to add the extension explicitly on the Twig environment
$twig = new \Twig\Environment(...);
$twig->addExtension(new IntlExtension());
I would like tpo use the second case and add explicitly to the twig environment.
In which file should I do it?
Above answer will not work in symfony 4. The path to Extension is different.
For example IntlExtension need to be configured like that:
services:
Twig\Extra\Intl\IntlExtension: null
When using twig with Symfony, you should register the extension as a service in your services.yaml file.
If installed using the twig/extensions flex recipe, it will provide the config/packages/twig_extensions.yaml file, you just have to uncomment the desired extensions (see the original comment on github).
The default configuration for reference:
services:
_defaults:
public: false
autowire: true
autoconfigure: true
# Uncomment any lines below to activate that Twig extension
#Twig\Extensions\ArrayExtension: null
#Twig\Extensions\DateExtension: null
#Twig\Extensions\IntlExtension: null
#Twig\Extensions\TextExtension: null

JMS Serializer: overriding default naming strategy in symfony 4 has no effect

I found this thread proposing a neat way of overriding globally the default naming strategy in config.yml
but this is for symfony 3 and I am on symfony 4 so I added the line to my config > packages > jms_serializer.yaml but this has no effect at all.
jms_serializer:
visitors:
xml_serialization:
format_output: '%kernel.debug%'
property_naming:
id: 'jms_serializer.identical_property_naming_strategy'
Does anyone understand why ?
I do dependency injection of SerializerInterface $serializer to use the serializer as it is not possible to call the service from AbstractController in SF4. Controller is deprecated in SF4.
Try removing var/cache manually. When I cleared cache with cache:clear command it didn't work, but after I cleaned it manually it did!
I hope this solution will help you too.
Add jms_serializer.camel_case_naming_strategy.class: JMS\Serializer\Naming\IdenticalPropertyNamingStrategy to your parameters configuration.

Symfony Bundle references "#Bundle" - FileLocatorFileNotFoundException

I am currently building my own Symfony bundle (I am using version 3.3). It works fine so far, but now I wanted to move the bundle-specific service definition out of my app/config/services.yml to a service definition within the bundle.
I created a src/MyBundle/Resources/config/services.yml and followed the guide How to Load Service Configuration inside a Bundle to load it. That works just fine, but I thought that the paths inside the newly created services.yml look a bit ugly:
MyBundle\:
resource: '../../*'
exclude: '../../{Tests}'
So I thought it would look a little cleaner, if I use the #Bundle-notation instead:
MyBundle\:
resource: '#MyBundle/*'
exclude: '#MyBundle/{Tests}'
However, than I was facing the error:
FileLocatorFileNotFoundException
The file "#MyBundle" does not exist (in: <...>\MyBundle\DependencyInjection/../Resources/config), where <...> corresponds to an absolute path.
I also tested whether it was a naming issue of the bundle name, but when I have a wrongly named bundle reference in the app/config/services.yml, e.g. #SomeBundleThatDoesNotExist then I get a different error:
FileLoaderLoadException
Bundle "SomeBundleThatDoesNotExist" does not exist or it is not enabled.
So my question is: Why does the #Bundle-notation work fine in the app/config/services.yml but not in the src/MyBundle/Resources/config/services.yml?
If you use the service remember make public for external use:
autowire: true
public: true
But, if your symfony dont know where is the bundle please chec this answer:
Symfony generated Bundle doesn't work

After tagging a service in services.yml symfony starts throwing ReflectionException

I have the following configuration in my SF2 Bundle
parameters:
catalogue.title.class: My\SomeBundle\Services\TitleService
services:
catalogue.title:
class: %catalogue.title.class%
Which works like a charm.
Now I want to enhance the configured service and tag it so it gets the knp_paginator injected (I've also tried with other tags). My config becomes:
parameters:
catalogue.title.class: My\SomeBundle\Services\TitleService
services:
catalogue.title:
class: %catalogue.title.class%
tags:
- { name: knp_paginator.injectable, paginator: knp_paginator }
For the mentioned tag to work the service implements the Knp\Bundle\PaginatorBundle\Definition\PaginatorAware. As mentioned on the KnpPaginatorBundle docs.
Running my app will show the following:
ReflectionException: Class %catalogue.title.class% does not exist.
Since it works without adding the tags part, I know the class does exist.
Replacing %catalogue.title.class% with the value of the parameter (the fully qualified class name) solves the issue, but we are using this way of configuring our service classes in all our projects and really don't want to deviate from that just to resolve this issue.
[EDIT] Using a different parameter like catalogue_title_class or my_abc has the same result.
I'm running Symfony version 2.3.5.
What am I doing wrong?
Probably this exceptions throws because of same names of class of parameter and provided service name.
Try to rename
parameters:
catalogue.title.class: My\SomeBundle\Services\TitleService
to some other name
parameters:
catalogue_title.class: My\SomeBundle\Services\TitleService
and use this
services:
catalogue.title:
class: %catalogue_title.class%
tags:
- { name: knp_paginator.injectable, paginator: knp_paginator }

Resources