<?php
namespace App\Controller;
use App\Entity\Upload;
use App\Form\UploadType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class ImportController extends AbstractController
{
/**
* @Route("/user/upload", name="app_upload")
*/
public function Upload(Request $request)
{
$upload = new Upload();
$form = $this->createForm(UploadType::class, $upload);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
$file = $form['filename']->getData();
$file->move($this->getParameter('upload_directory'), 'datas.csv');
return $this->redirectToRoute('app_import');
}
return $this->render('upload/form.html.twig', array(
'form'=>$form->createView()
));
}
/**
* @Route("/user/import", name="app_import")
*/
public function Import(KernelInterface $kernel): Response
{
$application = new Application($kernel);
$application->setAutoExit(false);
$input = new ArrayInput([
'command' => 'csv:import',
// (optional) define the value of command arguments
//'fooArgument' => 'barValue',
// (optional) pass options to the command
//'--bar' => 'fooValue',
]);
// You can use NullOutput() if you don't need the output
$output = new BufferedOutput();
$application->run($input, $output);
// return the output, don't use if you used NullOutput()
$content = $output->fetch();
// return new Response(""), if you used NullOutput()
return new Response($content);
}
}