-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin.php
More file actions
160 lines (132 loc) · 4.57 KB
/
Plugin.php
File metadata and controls
160 lines (132 loc) · 4.57 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/**
* Created by PhpStorm.
* User: kkeiper1103
* Date: 10/13/2015
* Time: 11:20 AM
*/
namespace WpJsonApi;
use League\Container\Container;
use League\Container\ReflectionContainer;
use League\Fractal\Manager;
use League\Fractal\Resource\ResourceAbstract;
use Phroute\Phroute\Dispatcher;
use Phroute\Phroute\Exception\HttpMethodNotAllowedException;
use Phroute\Phroute\Exception\HttpRouteNotFoundException;
use Symfony\Component\HttpFoundation\Response;
use WpJsonApi\Exceptions\AuthorizationNotValidException;
use WpJsonApi\Providers\FractalProvider;
use WpJsonApi\Providers\PlatesProvider;
use WpJsonApi\Providers\RoutingProvider;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use WpJsonApi\Providers\SettingsProvider;
use WpJsonApi\Providers\WhoopsProvider;
class Plugin extends Container
{
/**
*
*/
public function __construct() {
parent::__construct();
// sets up auto-dependency resolution within container
$this->delegate( new ReflectionContainer );
// set option name
$this->share("option_name", "wp-json-api");
}
/**
*
*/
public function register()
{
// register all bindings
$this->registerServiceProviders();
// register wp action hooks for menus / pages, etc
$this->registerActionHooks();
}
/**
* @return $this
*/
protected function registerServiceProviders()
{
// if the host header contains "local" within it, assume dev environment
// I know this can be spoofed, but I'm planning to remove this when it goes live;
// this is just a precaution in case I forget.
if( strpos($_SERVER['HTTP_HOST'], "local") !== false )
$this->addServiceProvider(WhoopsProvider::class);
$this->addServiceProvider(RoutingProvider::class);
$this->addServiceProvider(FractalProvider::class);
$this->addServiceProvider(PlatesProvider::class);
$this->addServiceProvider(SettingsProvider::class);
return $this;
}
/**
* register the different action hooks as objects
*/
protected function registerActionHooks()
{
add_action("admin_menu", $this->get(Actions\MenuAction::class));
add_action("init", [$this, 'dispatch']);
}
/**
*
*/
public function run()
{
/**
* @var $request Request
*/
$request = $this->get(Request::class);
/**
* @var $dispatcher Dispatcher
*/
$dispatcher = $this->get(Dispatcher::class);
// what is the content for the response?
$content = $dispatcher->dispatch($request->getMethod(), $request->getPathInfo());
// if the returned content is a string, wrap it in an array and jsonify that
if( is_string($content) ) {
$content = ["content" => $content];
}
// if the $content is a prepared Collection, have fractal process it
else if( $content instanceof ResourceAbstract ) {
$content = $this->get( Manager::class )
->createData($content)->toArray();
}
// if the response is a Response Object, send that instead
else if( $content instanceof Response ) {
return $content->send();
}
// send the response
$response = new JsonResponse($content);
$response->headers->set("Content-Type", "application/vnd.api+json");
return $response->send();
}
/**
*
*/
public function dispatch() {
try {
$this->run(); exit;
}
catch (HttpMethodNotAllowedException $mna)
{
$content = [
"error" => "Requested HTTP Method not Allowed!"
];
$response = new JsonResponse($content, Response::HTTP_METHOD_NOT_ALLOWED);
$response->headers->set("Allow", ltrim($mna->getMessage(), "Allow: "));
$response->send(); exit;
}
// if someone throws an invalid auth exception, just output an unauthorized jsonresponse
catch (AuthorizationNotValidException $anv)
{
$content = [
"error" => "Authorization Header Not Valid! Please Include a Correct Value in X-WPJSONAPI-AUTH Header."
];
$response = new JsonResponse($content, Response::HTTP_UNAUTHORIZED);
$response->send(); exit;
}
// don't do anything if the routecollector doesn't have the uri registered. WordPress will handle it
catch (HttpRouteNotFoundException $nfe) {}
}
}