|
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
|
15 |
|
public function boot(): void |
|
19
|
|
|
{ |
|
20
|
15 |
|
if ($this->app->runningInConsole()) { |
|
21
|
15 |
|
$this->publishes([ |
|
22
|
15 |
|
__DIR__.'/../config/xml.php' => config_path('xml.php'), |
|
23
|
15 |
|
], 'config'); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/* |
|
27
|
|
|
* Determine if the request is sending XML. |
|
28
|
|
|
* @deprecated v3.0.0 |
|
29
|
|
|
* |
|
30
|
|
|
* @return bool |
|
31
|
|
|
*/ |
|
32
|
15 |
|
Request::macro('isXml', function () { |
|
33
|
3 |
|
return 'xml' === $this->getContentTypeFormat(); |
|
|
|
|
|
|
34
|
15 |
|
}); |
|
35
|
|
|
|
|
36
|
|
|
/* |
|
37
|
|
|
* Determine if the request is sending XML. |
|
38
|
|
|
* |
|
39
|
|
|
* @return bool |
|
40
|
|
|
*/ |
|
41
|
15 |
|
Request::macro('sentXml', function () { |
|
42
|
|
|
return 'xml' === $this->getContentTypeFormat(); |
|
43
|
15 |
|
}); |
|
44
|
|
|
|
|
45
|
|
|
/* |
|
46
|
|
|
* Get the XML payload for the request. |
|
47
|
|
|
* |
|
48
|
|
|
* @return \Bmatovu\LaravelXml\Support\XmlElement |
|
49
|
|
|
*/ |
|
50
|
15 |
|
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
|
15 |
|
}); |
|
57
|
|
|
|
|
58
|
|
|
/* |
|
59
|
|
|
* Determine if the current request is asking for XML in return. |
|
60
|
|
|
* |
|
61
|
|
|
* @return bool |
|
62
|
|
|
*/ |
|
63
|
15 |
|
Request::macro('wantsXml', function () { |
|
64
|
1 |
|
$acceptable = $this->getAcceptableContentTypes(); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
1 |
|
return isset($acceptable[0]) && Str::contains($acceptable[0], ['/xml', '+xml']); |
|
67
|
15 |
|
}); |
|
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
|
15 |
|
Response::macro('xml', function ($data, $status = 200, array $headers = [], $options = []) { |
|
80
|
2 |
|
return new XmlResponse($data, $status, $headers, $options); |
|
81
|
15 |
|
}); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Register the application services. |
|
86
|
|
|
*/ |
|
87
|
15 |
|
public function register(): void |
|
88
|
|
|
{ |
|
89
|
15 |
|
$this->mergeConfigFrom(__DIR__.'/../config/xml.php', 'xml'); |
|
90
|
|
|
|
|
91
|
15 |
|
$this->app->bind('laravel-xml', function () { |
|
92
|
8 |
|
return new LaravelXml(); |
|
93
|
15 |
|
}); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: