Services.yml and file path in the same bundle

How can I access the data file in service.yml, which is in the same or any package?

Im sending a csv file that I would like to enter as an argument.
It works when I use the direct path:

mybundle.data.csv: %kernel.root_dir%/../vendor/mybundle/my-bundle/mybundle/MyBundle/Resources/data/data.csv 

This is pretty verbose and inflexible, and so I'm looking for a resolver like:

 data.csv: "@MyBundle/Resources/data/data.csv" 

But this does not work:

... has a dependency on the nonexistent service "Mybundle / resources / data / data.csv".

any ideas?

+4
source share
2 answers

First of all: in the YAML service and parameter definitions, @ also refers to another service. This is why your code is not working.

In principle, you have two options. The first and easiest is to use the relative path in your services.yml packages and add it to your CSV class.

For instance:

 # src/Acme/DemoBundle/Resources/config/services.yml parameters: data.csv: "Resources/data/data.csv" 

In the class, you want to read the CSV file:

 // src/Acme/DemoBundle/Import/ReadCsv.php ... class ReadCsv { ... public function __construct($csvFile) { $this->csvFile = __DIR__.'/../'.$csvFile; } ... } 

Another possibility is to make the CSV file custom with config.yml (see this article in the Symfony2 cookbook ) and insert a special placeholder in the configuration value that you replace in the AcmeDemoBundleExtension class:

 // src/Acme/DemoBundle/DependencyInjection/AcmeDemoBundleExtension.php ... public function load(array $configs, ContainerBuilder $container) { ... $container->setParameter('acme_demo.csv_file', str_replace('__BUNDLE_DIR__', __DIR__'./.., $config['csv_file']); } ... 

Your config.yml will look like this:

 # app/config/config.yml acme_demo: csv_file: __BUNDLE_DIR__/Resources/data/data.csv 
+3
source

@ - yaml escaping is now supported

https://github.com/symfony/symfony/issues/4889

You can use it like this:

 data.csv: "@@MyBundle/Resources/data/data.csv" 
+4
source

Source: https://habr.com/ru/post/1434599/


All Articles