This repository has been archived by the owner on Sep 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
116 lines (105 loc) · 3.47 KB
/
index.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
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
* Rest server for Grommet_Shipwire testing.
*
* @author tmannherz
* @link https://github.com/marcj/php-rest-service
*/
spl_autoload_register(function ($class) {
if ('\\' == $class[0]) {
$class = substr($class, 1);
}
$file = str_replace('\\', '/', $class) . '.php';
if (file_exists($file)) {
require_once $file;
}
});
use RestService\Server;
use Shipwire\Resource;
class Controller
{
/**
* @var RestService\Server
*/
public $server;
public function __construct ()
{
$this->server = Server::create('/')->setHttpStatusCodes(true);
}
/**
* Add GET/POST/PUT/DELETE routes for a resource.
*
* @param string $path
* @param \Shipwire\Resource $controller
* @return \Controller
*/
public function addEntityRoutes ($path, Resource $controller)
{
$this->server
->addGetRoute($path, array($controller, 'get'))
->addGetRoute($path . '/E([0-9]+)', array($controller, 'get'))
->addPostRoute($path, array($controller, 'post'))
->addPutRoute($path . '/E([0-9]+)', array($controller, 'put'))
->addDeleteRoute($path . '/E([0-9]+)', array($controller, 'delete'));
return $this;
}
/**
* @param string $path
* @param \Shipwire\Resource $controller
* @return \Controller
*/
public function addPostRoute ($path, Resource $controller)
{
$this->server->addPostRoute($path, array($controller, 'post'));
return $this;
}
/**
* @param string $path
* @param \Shipwire\Resource $controller
* @return \Controller
*/
public function addGetRoute ($path, Resource $controller)
{
$this->server->addGetRoute($path, array($controller, 'get'));
return $this;
}
/**
* @param string $path
* @param \Shipwire\Resource $controller
* @param string $controllerMethod
* @param string $restMethod
* @return \Controller
*/
public function addCustomRoute ($path, Resource $controller, $controllerMethod, $restMethod = 'get')
{
$serverMethod = 'add' . ucfirst(strtolower($restMethod)) . 'Route';
$this->server->$serverMethod($path, array($controller, $controllerMethod));
return $this;
}
/**
* @return \Controller
*/
public function run ()
{
$this->server->run();
return $this;
}
}
$controller = new Controller();
$controller
->addEntityRoutes('vendors', new \Shipwire\Vendor())
->addCustomRoute('vendors/E([0-9]+)/packing-lists', new \Shipwire\Vendor(), 'packingLists')
->addCustomRoute('vendors/E([0-9]+)/shipping-labels', new \Shipwire\Vendor(), 'shippingLabels')
->addEntityRoutes('warehouses', new \Shipwire\Warehouse())
->addEntityRoutes('products', new \Shipwire\Product())
->addCustomRoute('products/E([0-9]+)/stock', new \Shipwire\Product(), 'stock')
->addCustomRoute('products/E([0-9]+)/stock-adjustments', new \Shipwire\Product(), 'stockAdjust', 'post')
->addPostRoute('rate', new \Shipwire\Rate())
->addEntityRoutes('shipments', new \Shipwire\Shipment())
->addCustomRoute('shipments/E([0-9]+)/packing-list', new \Shipwire\Shipment(), 'packingList')
->addCustomRoute('shipments/E([0-9]+)/shipping-label', new \Shipwire\Shipment(), 'shippingLabel')
->addCustomRoute('shipments/E([0-9]+)/tracking-numbers', new \Shipwire\Shipment(), 'trackingGet', 'get')
->addCustomRoute('shipments/E([0-9]+)/tracking-numbers', new \Shipwire\Shipment(), 'trackingPost', 'post')
->addEntityRoutes('orders', new \Shipwire\Order())
->addCustomRoute('orders/E([0-9]+)/packing-lists', new \Shipwire\Order(), 'packingLists')
->run();