-
Notifications
You must be signed in to change notification settings - Fork 20
/
ResponseXmlServiceProvider.php
103 lines (94 loc) · 3.95 KB
/
ResponseXmlServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
namespace Mtownsend\ResponseXml\Providers;
use Illuminate\Container\Container;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Spatie\ArrayToXml\ArrayToXml;
class ResponseXmlServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->isLumen() ? $this->loadLumenResponseMacros() : $this->loadLaravelResponseMacros();
}
/**
* If the application is Laravel, load Laravel's response factory and macro the xml methods
*
*/
protected function loadLaravelResponseMacros()
{
\Illuminate\Routing\ResponseFactory::macro('xml', function ($xml, $status = 200, array $headers = [], $xmlRoot = 'response', $encoding = null) {
if (is_array($xml)) {
$xml = ArrayToXml::convert($xml, $xmlRoot, true, $encoding);
} elseif (is_object($xml) && method_exists($xml, 'toArray')) {
$xml = ArrayToXml::convert($xml->toArray(), $xmlRoot, true, $encoding);
} elseif (is_string($xml)) {
$xml = $xml;
} else {
$xml = '';
}
if (!isset($headers['Content-Type'])) {
$headers = array_merge($headers, ['Content-Type' => 'application/xml']);
}
return \Illuminate\Routing\ResponseFactory::make($xml, $status, $headers);
});
\Illuminate\Routing\ResponseFactory::macro('preferredFormat', function ($data, $status = 200, array $headers = [], $xmlRoot = 'response', $encoding = null) {
$request = Container::getInstance()->make('request');
if (Str::contains($request->headers->get('Accept'), 'xml')) {
return $this->xml($data, $status, array_merge($headers, ['Content-Type' => $request->headers->get('Accept')]), $xmlRoot, $encoding);
} else {
return $this->json($data, $status, array_merge($headers, ['Content-Type' => $request->headers->get('Accept')]));
}
});
}
/**
* If the application is Lumen, load Lumens's response factory and macro the xml methods
*
*/
protected function loadLumenResponseMacros()
{
\Laravel\Lumen\Http\ResponseFactory::macro('xml', function ($xml, $status = 200, array $headers = [], $xmlRoot = 'response', $encoding = null) {
if (is_array($xml)) {
$xml = ArrayToXml::convert($xml, $xmlRoot, true, $encoding);
} elseif (is_object($xml) && method_exists($xml, 'toArray')) {
$xml = ArrayToXml::convert($xml->toArray(), $xmlRoot, true, $encoding);
} elseif (is_string($xml)) {
$xml = $xml;
} else {
$xml = '';
}
if (!isset($headers['Content-Type'])) {
$headers = array_merge($headers, ['Content-Type' => 'application/xml']);
}
return \Laravel\Lumen\Http\ResponseFactory::make($xml, $status, $headers);
});
\Laravel\Lumen\Http\ResponseFactory::macro('preferredFormat', function ($data, $status = 200, array $headers = [], $xmlRoot = 'response', $encoding = null) {
$request = Container::getInstance()->make('request');
if (Str::contains($request->headers->get('Accept'), 'xml')) {
return $this->xml($data, $status, array_merge($headers, ['Content-Type' => $request->headers->get('Accept')]), $xmlRoot, $encoding);
} else {
return $this->json($data, $status, array_merge($headers, ['Content-Type' => $request->headers->get('Accept')]));
}
});
}
protected function isLumen()
{
if (!isset($this->app)) {
return false;
}
return Str::contains($this->app->version(), 'Lumen');
}
}