/
LaravelXmlServiceProvider.php
95 lines (83 loc) · 2.63 KB
/
LaravelXmlServiceProvider.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
<?php
namespace Bmatovu\LaravelXml;
use Bmatovu\LaravelXml\Http\XmlResponse;
use Bmatovu\LaravelXml\Support\Facades\LaravelXml;
use Bmatovu\LaravelXml\Support\XmlElement;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
class LaravelXmlServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*/
public function boot(): void
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/xml.php' => config_path('xml.php'),
], 'config');
}
/*
* Determine if the request is sending XML.
* @deprecated v3.0.0
*
* @return bool
*/
Request::macro('isXml', function () {
return 'xml' === $this->getContentTypeFormat();
});
/*
* Determine if the request is sending XML.
*
* @return bool
*/
Request::macro('sentXml', function () {
return 'xml' === $this->getContentTypeFormat();
});
/*
* Get the XML payload for the request.
*
* @return \Bmatovu\LaravelXml\Support\XmlElement
*/
Request::macro('xml', function () {
if (! $this->isXml() || ! $content = $this->getContent()) {
return new XmlElement('<document></document>');
}
return simplexml_load_string($content, XmlElement::class);
});
/*
* Determine if the current request is asking for XML in return.
*
* @return bool
*/
Request::macro('wantsXml', function () {
$acceptable = $this->getAcceptableContentTypes();
return isset($acceptable[0]) && Str::contains($acceptable[0], ['/xml', '+xml']);
});
/*
* Return a new XML response from the application.
*
* @param string|array $data
* @param int $status
* @param array $headers
* @param array $options
*
* @return \Bmatovu\LaravelXml\Http\XmlResponse
*/
Response::macro('xml', function ($data, $status = 200, array $headers = [], $options = []) {
return new XmlResponse($data, $status, $headers, $options);
});
}
/**
* Register the application services.
*/
public function register(): void
{
$this->mergeConfigFrom(__DIR__.'/../config/xml.php', 'xml');
$this->app->bind('laravel-xml', function () {
return new LaravelXml();
});
}
}