1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* part-db version 0.1 |
5
|
|
|
* Copyright (C) 2005 Christoph Lechner |
6
|
|
|
* http://www.cl-projects.de/ |
7
|
|
|
* |
8
|
|
|
* part-db version 0.2+ |
9
|
|
|
* Copyright (C) 2009 K. Jacobs and others (see authors.php) |
10
|
|
|
* http://code.google.com/p/part-db/ |
11
|
|
|
* |
12
|
|
|
* Part-DB Version 0.4+ |
13
|
|
|
* Copyright (C) 2016 - 2019 Jan Böhmer |
14
|
|
|
* https://github.com/jbtronics |
15
|
|
|
* |
16
|
|
|
* This program is free software; you can redistribute it and/or |
17
|
|
|
* modify it under the terms of the GNU General Public License |
18
|
|
|
* as published by the Free Software Foundation; either version 2 |
19
|
|
|
* of the License, or (at your option) any later version. |
20
|
|
|
* |
21
|
|
|
* This program is distributed in the hope that it will be useful, |
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24
|
|
|
* GNU General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* You should have received a copy of the GNU General Public License |
27
|
|
|
* along with this program; if not, write to the Free Software |
28
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
29
|
|
|
* |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
namespace App\Services; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
use App\Entity\NamedDBElement; |
36
|
|
|
use Symfony\Component\HttpFoundation\Request; |
37
|
|
|
use Symfony\Component\HttpFoundation\Response; |
38
|
|
|
use Symfony\Component\HttpFoundation\ResponseHeaderBag; |
39
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Use this class to export an entity to multiple file formats. |
43
|
|
|
* @package App\Services |
44
|
|
|
*/ |
45
|
|
|
class EntityExporter |
46
|
|
|
{ |
47
|
|
|
protected $serializer; |
48
|
|
|
|
49
|
|
|
public function __construct(SerializerInterface $serializer) |
50
|
|
|
{ |
51
|
|
|
$this->serializer = $serializer; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Exports an Entity or an array of entities to multiple file formats. |
56
|
|
|
* |
57
|
|
|
* @param $entity NamedDBElement|NamedDBElement[] The element/elements[] that should be exported |
58
|
|
|
* @param Request $request The request that should be used for option resolving. |
59
|
|
|
* @return Response The generated response containing the exported data. |
60
|
|
|
* @throws \ReflectionException |
61
|
|
|
*/ |
62
|
|
|
public function exportEntityFromRequest($entity, Request $request) : Response |
63
|
|
|
{ |
64
|
|
|
$format = $request->get('format') ?? "json"; |
65
|
|
|
|
66
|
|
|
//Check if we have one of the supported formats |
67
|
|
|
if (!in_array($format, ['json', 'csv', 'yaml', 'xml'])) { |
68
|
|
|
throw new \InvalidArgumentException("Given format is not supported!"); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
//Check export verbosity level |
72
|
|
|
$level = $request->get('level') ?? 'extended'; |
73
|
|
|
if (!in_array($level, ['simple', 'extended', 'full'])) { |
74
|
|
|
throw new \InvalidArgumentException('Given level is not supported!'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
//Check for include children option |
78
|
|
|
$include_children = $request->get('include_children') ?? false; |
79
|
|
|
|
80
|
|
|
//Check which groups we need to export, based on level and include_children |
81
|
|
|
$groups = array($level); |
82
|
|
|
if ($include_children) { |
83
|
|
|
$groups[] = 'include_children'; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
//Plain text should work for all types |
87
|
|
|
$content_type = "text/plain"; |
88
|
|
|
|
89
|
|
|
//Try to use better content types based on the format |
90
|
|
|
switch ($format) { |
91
|
|
|
case 'xml': |
92
|
|
|
$content_type = "application/xml"; |
93
|
|
|
break; |
94
|
|
|
case 'json': |
95
|
|
|
$content_type = "application/json"; |
96
|
|
|
break; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
//Ensure that we always serialize an array. This makes it easier to import the data again. |
100
|
|
|
if(is_array($entity)) { |
101
|
|
|
$entity_array = $entity; |
102
|
|
|
} else { |
103
|
|
|
$entity_array = [$entity]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$response = new Response($this->serializer->serialize($entity_array, $format, |
107
|
|
|
[ |
108
|
|
|
'groups' => $groups, |
109
|
|
|
'as_collection' => true, |
110
|
|
|
'csv_delimiter' => ';', //Better for Excel |
111
|
|
|
'xml_root_node_name' => 'PartDBExport' |
112
|
|
|
])); |
113
|
|
|
|
114
|
|
|
$response->headers->set('Content-Type', $content_type); |
115
|
|
|
|
116
|
|
|
//If view option is not specified, then download the file. |
117
|
|
|
if (!$request->get('view')) { |
118
|
|
|
if ($entity instanceof NamedDBElement) { |
119
|
|
|
$entity_name = $entity->getName(); |
120
|
|
|
} elseif (is_array($entity)) { |
121
|
|
|
if (empty($entity)) { |
122
|
|
|
throw new \InvalidArgumentException('$entity must not be empty!'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
//Use the class name of the first element for the filename |
126
|
|
|
$reflection = new \ReflectionClass($entity[0]); |
127
|
|
|
$entity_name = $reflection->getShortName(); |
128
|
|
|
} else { |
129
|
|
|
throw new \InvalidArgumentException('$entity type is not supported!'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
$filename = "export_" . $entity_name . "_" . $level . "." . $format; |
134
|
|
|
|
135
|
|
|
// Create the disposition of the file |
136
|
|
|
$disposition = $response->headers->makeDisposition( |
137
|
|
|
ResponseHeaderBag::DISPOSITION_ATTACHMENT, |
138
|
|
|
$filename |
139
|
|
|
); |
140
|
|
|
// Set the content disposition |
141
|
|
|
$response->headers->set('Content-Disposition', $disposition); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $response; |
145
|
|
|
} |
146
|
|
|
} |