1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bmatovu\LaravelXml; |
4
|
|
|
|
5
|
|
|
use Bmatovu\LaravelXml\Http\XmlResponse; |
6
|
|
|
use Bmatovu\LaravelXml\Support\Facades\LaravelXml; |
|
|
|
|
7
|
|
|
use Bmatovu\LaravelXml\Support\XmlElement; |
8
|
|
|
use Illuminate\Support\Facades\Request; |
9
|
|
|
use Illuminate\Support\Facades\Response; |
10
|
|
|
use Illuminate\Support\ServiceProvider; |
11
|
|
|
use Illuminate\Support\Str; |
12
|
|
|
|
13
|
|
|
class LaravelXmlServiceProvider extends ServiceProvider |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Bootstrap the application services. |
17
|
|
|
*/ |
18
|
14 |
|
public function boot(): void |
19
|
|
|
{ |
20
|
14 |
|
if ($this->app->runningInConsole()) { |
21
|
14 |
|
$this->publishes([ |
22
|
14 |
|
__DIR__.'/../config/xml.php' => config_path('xml.php'), |
23
|
|
|
], 'config'); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/* |
27
|
|
|
* Determine if the request is sending XML. |
28
|
|
|
* @deprecated v3.0.0 |
29
|
|
|
* |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
14 |
|
Request::macro('isXml', function () { |
33
|
3 |
|
return 'xml' === $this->getContentType(); |
|
|
|
|
34
|
|
|
}); |
35
|
|
|
|
36
|
|
|
/* |
37
|
|
|
* Determine if the request is sending XML. |
38
|
|
|
* |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
14 |
|
Request::macro('sentXml', function () { |
42
|
|
|
return 'xml' === $this->getContentType(); |
43
|
|
|
}); |
44
|
|
|
|
45
|
|
|
/* |
46
|
|
|
* Get the XML payload for the request. |
47
|
|
|
* |
48
|
|
|
* @return \Bmatovu\LaravelXml\Support\XmlElement |
49
|
|
|
*/ |
50
|
14 |
|
Request::macro('xml', function () { |
51
|
2 |
|
if (! $this->isXml() || ! $content = $this->getContent()) { |
|
|
|
|
52
|
1 |
|
return new XmlElement('<document></document>'); |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
return simplexml_load_string($content, XmlElement::class); |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
/* |
59
|
|
|
* Determine if the current request is asking for XML in return. |
60
|
|
|
* |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
14 |
|
Request::macro('wantsXml', function () { |
64
|
1 |
|
$acceptable = $this->getAcceptableContentTypes(); |
|
|
|
|
65
|
|
|
|
66
|
1 |
|
return isset($acceptable[0]) && Str::contains($acceptable[0], ['/xml', '+xml']); |
67
|
|
|
}); |
68
|
|
|
|
69
|
|
|
/* |
70
|
|
|
* Return a new XML response from the application. |
71
|
|
|
* |
72
|
|
|
* @param string|array $data |
73
|
|
|
* @param int $status |
74
|
|
|
* @param array $headers |
75
|
|
|
* @param array $options |
76
|
|
|
* |
77
|
|
|
* @return \Bmatovu\LaravelXml\Http\XmlResponse |
78
|
|
|
*/ |
79
|
14 |
|
Response::macro('xml', function ($data, $status = 200, array $headers = [], $options = []) { |
80
|
2 |
|
return new XmlResponse($data, $status, $headers, $options); |
81
|
|
|
}); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Register the application services. |
86
|
|
|
*/ |
87
|
14 |
|
public function register(): void |
88
|
|
|
{ |
89
|
14 |
|
$this->mergeConfigFrom(__DIR__.'/../config/xml.php', 'xml'); |
90
|
|
|
|
91
|
14 |
|
$this->app->bind('laravel-xml', function () { |
92
|
7 |
|
return new LaravelXml(); |
93
|
|
|
}); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: