Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony best practice for data export file location

Tags:

symfony

I am writing a console command which generates data files to be used by external services (for example, a Google feed, inventory feed, etc). Should the location of the generated data files be within the Symfony app? I know they can actually be anywhere, I'm just wondering if there is a standard way to do it.

like image 860
Steven Musumeche Avatar asked Oct 20 '22 09:10

Steven Musumeche


1 Answers

It's up to you, but it is better to have this path in a parameter. For example you can you have a parameter group related to your command. This allows you to have different configurations depending on the current environment:

parameters:
    # /app/config.yml 
    # @see MyExportCommand.php
    my_export_command:
        base_path:       '/data/ftp/export'
        other_command_related_param: true

In your command, get and store those parameters in the initialize function:

// MyExportCommand.php
protected function initialize(InputInterface $input, OutputInterface $output)
{
    $this->parameters = $this->getContainer()->getParameter('my_export_command');
}

Finally in your execute function, you can use something like this: ($this->fs is an instance of the Symfony2 Filesystem component)

// execute()
// Write the file
$filePath = $this->parameters['base_path']. '/'. $this->fileName;
$this->fs->dumpFile($filePath, $myContent);
like image 159
COil Avatar answered Jan 02 '23 20:01

COil