forked from craftcms/element-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElementApiController.php
More file actions
261 lines (228 loc) · 6.44 KB
/
ElementApiController.php
File metadata and controls
261 lines (228 loc) · 6.44 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
namespace Craft;
use League\Fractal\Manager;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Serializer\DataArraySerializer;
use League\Fractal\Serializer\JsonApiSerializer;
use League\Fractal\Serializer\SerializerAbstract;
use League\Fractal\TransformerAbstract;
/**
* Element API controller
*/
class ElementApiController extends BaseController
{
/**
* @var bool Allows anonymous access to this controller's actions.
*/
protected $allowAnonymous = true;
/**
* Returns the requested elements as JSON
*
* @param callable|null $configFactory A function for generating the config
* @param array|null $config The API endpoint configuration
*
* @throws Exception
* @throws HttpException
*/
public function actionGetElements($configFactory = null, array $config = null)
{
if ($configFactory !== null)
{
$params = craft()->urlManager->getRouteParams();
$variables = (isset($params['variables']) ? $params['variables'] : null);
$config = $this->_callWithParams($configFactory, $variables);
}
// Merge in default config options
$config = array_merge(
[
'paginate' => true,
'pageParam' => 'page',
'elementsPerPage' => 100,
'first' => false,
'transformer' => [
'class' => 'Craft\ElementApi_ElementTransformer',
],
'cache' => false,
],
craft()->config->get('defaults', 'elementapi'),
$config
);
// Before anything else, check the cache
if ($config['cache']) {
$cacheKey = 'elementapi:'.craft()->request->getPath().':'.craft()->request->getQueryStringWithoutPath();
$cache = craft()->cache->get($cacheKey);
if ($cache !== false)
{
JsonHelper::sendJsonHeaders();
echo $cache;
craft()->end();
}
}
if ($config['pageParam'] === 'p')
{
throw new Exception('The pageParam setting cannot be set to "p" because that’s the parameter Craft uses to check the requested path.');
}
if (!isset($config['elementType']))
{
throw new Exception('Element API configs must specify the elementType.');
}
/** @var ElementCriteriaModel $criteria */
$criteria = craft()->elements->getCriteria($config['elementType'], [
'limit' => null
]);
if (!empty($config['criteria']))
{
$criteria->setAttributes($config['criteria']);
}
// Load Fractal
$pluginPath = craft()->path->getPluginsPath().'elementapi/';
require $pluginPath.'vendor/autoload.php';
$fractal = new Manager();
// Set the serializer
$serializer = isset($config['serializer']) ? $config['serializer'] : null;
if (!$serializer instanceof SerializerAbstract)
{
switch ($serializer)
{
case 'dataArray':
$serializer = new DataArraySerializer();
break;
case 'jsonApi':
$serializer = new JsonApiSerializer();
break;
case 'jsonFeed':
Craft::import('plugins.elementapi.ElementApi_JsonFeedV1Serializer');
$serializer = new ElementApi_JsonFeedV1Serializer();
break;
default:
$serializer = new ArraySerializer();
}
}
$fractal->setSerializer($serializer);
// Set the includes
$includes = isset($config['includes']) ? $config['includes'] : [];
$fractal->parseIncludes($includes);
// Set the excludes
$excludes = isset($config['excludes']) ? $config['excludes'] : [];
$fractal->parseExcludes($excludes);
// Define the transformer
if (is_callable($config['transformer']) || $config['transformer'] instanceof TransformerAbstract)
{
$transformer = $config['transformer'];
}
else
{
Craft::import('plugins.elementapi.ElementApi_ElementTransformer');
$transformer = Craft::createComponent($config['transformer']);
}
$resourceKey = isset($config['resourceKey']) ? $config['resourceKey'] : null;
if ($config['first'])
{
$element = $criteria->first();
if (!$element)
{
throw new HttpException(404);
}
$resource = new Item($element, $transformer, $resourceKey);
}
else if ($config['paginate'])
{
// Create the paginator
require $pluginPath.'ElementApi_PaginatorAdapter.php';
$paginator = new ElementApi_PaginatorAdapter($config['elementsPerPage'], $criteria->total(), $config['pageParam']);
// Fetch this page's elements
$criteria->offset = $config['elementsPerPage'] * ($paginator->getCurrentPage() - 1);
$criteria->limit = $config['elementsPerPage'];
$elements = $criteria->find();
$paginator->setCount(count($elements));
$resource = new Collection($elements, $transformer, $resourceKey);
$resource->setPaginator($paginator);
}
else
{
$resource = new Collection($criteria, $transformer, $resourceKey);
}
// Set any custom meta values
if (isset($config['meta']))
{
$resource->setMeta($config['meta']);
}
$data = $fractal->createData($resource);
// Fire an 'onBeforeSendData' event
craft()->elementApi->onBeforeSendData(new Event($this, [
'data' => $data,
]));
// Serialize and JSON-encode the data
$data = $data->toArray();
JsonHelper::sendJsonHeaders();
$jsonOptions = isset($config['jsonOptions']) ? $config['jsonOptions'] : 0;
$output = json_encode($data, $jsonOptions);
// Cache it?
if ($config['cache'])
{
if (is_int($config['cache']))
{
$expire = $config['cache'];
}
else if (is_string($config['cache']))
{
$expire = DateTimeHelper::timeFormatToSeconds($config['cache']);
}
else
{
$expire = null;
}
craft()->cache->set($cacheKey, $output, $expire);
}
// Output and the request
echo $output;
craft()->end();
}
/**
* Calls a given function. If any params are given, they will be mapped to the function's arguments.
*
* @param callable $func The function to call
* @param array $params Any params that should be mapped to function arguments
*
* @return mixed The result of the function
*/
private function _callWithParams($func, $params)
{
if (!$params)
{
return call_user_func($func);
}
$ref = new \ReflectionFunction($func);
$args = [];
foreach ($ref->getParameters() as $param)
{
$name = $param->getName();
if (isset($params[$name]))
{
if ($param->isArray())
{
$args[] = is_array($params[$name]) ? $params[$name] : [$params[$name]];
}
else if (!is_array($params[$name]))
{
$args[] = $params[$name];
}
else
{
return false;
}
}
else if ($param->isDefaultValueAvailable())
{
$args[] = $param->getDefaultValue();
}
else
{
return false;
}
}
return $ref->invokeArgs($args);
}
}