| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the Blast Project package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Copyright (C) 2015-2017 Libre Informatique | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * This file is licenced under the GNU LGPL v3. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * For the full copyright and license information, please view the LICENSE.md | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | namespace Blast\Bundle\CoreBundle\Exporter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Exporter\Source\SourceIteratorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Exporter\Writer\TypedWriterInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\HttpFoundation\StreamedResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class Exporter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var TypedWriterInterface[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $writers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @param TypedWriterInterface[] $writers an array of allowed typed writers, indexed by format name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public function __construct(array $writers = array()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $this->writers = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         foreach ($writers as $writer) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             $this->addWriter($writer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @throws \RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param string                  $format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param string                  $filename | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param SourceIteratorInterface $source | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @return StreamedResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     public function getResponse($format, $filename, SourceIteratorInterface $source) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         if (!array_key_exists($format, $this->writers)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             throw new \RuntimeException(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 'Invalid "%s" format, supported formats are : "%s"', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 $format, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 implode(', ', array_keys($this->writers)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $writer = $this->writers[$format]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $callback = function () use ($source, $writer) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $handler = \Blast\Bundle\CoreBundle\Exporter\Handler::create($source, $writer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $handler->export(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $headers = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             'Content-Disposition' => sprintf('attachment; filename="%s"', $filename), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $headers['Content-Type'] = $writer->getDefaultMimeType(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         return new StreamedResponse($callback, 200, $headers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * Returns a simple array of export formats. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @return string[] writer formats as returned by the TypedWriterInterface::getFormat() method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     public function getAvailableFormats() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return array_keys($this->writers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * The main benefit of this method is the type hinting. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @param TypedWriterInterface $writer a possible writer for exporting data | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 86 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |     public function addWriter(TypedWriterInterface $writer) | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |         $this->writers[$writer->getFormat()] = $writer; | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 91 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |  |