Exponential Basic 2.4.0.1 Exponential Basic CMS — PHP Class Reference

rest_controller.php
Go to the documentation of this file.
1 <?php
15 abstract class ezpRestMvcController extends ezcMvcController
16 {
22 const CACHE_ID = 'ezpRestMvcController';
23
29 private $defaultResponsegroups = array();
30
34 protected $restINI;
35
41 public static $isCacheCreated = false;
42
49 public function __construct( $action, ezcMvcRequest $request )
50 {
51 $this->restINI = eZINI::instance( 'rest.ini' );
52 parent::__construct( $action, $request );
53 }
54
62 protected function hasResponseGroup( $name )
63 {
64 return in_array( $name, $this->defaultResponsegroups ) ||
65 (
66 isset( $this->request->variables['ResponseGroups'] ) &&
67 in_array( $name, $this->request->variables['ResponseGroups'] )
68 );
69 }
70
76 protected function getResponseGroups()
77 {
78 $resGroups = $this->request->variables['ResponseGroups'];
79 for ( $i = 0, $iMax = count( $this->defaultResponsegroups ); $i < $iMax; ++$i )
80 {
81 if ( !in_array( $this->defaultResponsegroups[$i], $resGroups ) )
82 $resGroups[] = $this->defaultResponsegroups[$i];
83 }
84
85 return $resGroups;
86 }
87
95 protected function setDefaultResponseGroups( array $defaultResponseGroups )
96 {
97 $this->defaultResponsegroups = $defaultResponseGroups;
98 }
99
107 protected function hasContentVariable( $name )
108 {
109 return isset( $this->request->contentVariables[$name] );
110 }
111
119 protected function getContentVariable( $name )
120 {
121 if ( isset( $this->request->contentVariables[$name] ) )
122 {
123 return $this->request->contentVariables[$name];
124 }
125
126 return null;
127 }
128
134 protected function getAllContentVariables()
135 {
136 return $this->request->contentVariables;
137 }
138
144 public function createResult()
145 {
146 $debug = ezpRestDebug::getInstance();
147 $debug->startTimer( 'GeneratingRestResult', 'RestController' );
148
149 if ( !self::$isCacheCreated )
150 {
151 ezcCacheManager::createCache(
152 self::CACHE_ID,
153 $this->getCacheLocation(),
154 'ezpRestCacheStorageClusterObject',
155 array( 'ttl' => $this->getActionTTL() )
156 );
157 self::$isCacheCreated = true;
158 }
159
160 $cache = ezcCacheManager::getCache( self::CACHE_ID );
161 $controllerCacheId = $this->generateCacheId();
162 $isCacheEnabled = $this->isCacheEnabled();
163
164 // Try to restore application cache.
165 // If expired or not yet available, generate it and store it
166 $cache->isCacheEnabled = $isCacheEnabled;
167 if ( ( $res = $cache->restore( $controllerCacheId ) ) === false )
168 {
169 try
170 {
171 $debug->log( 'Generating cache', ezcLog::DEBUG );
172 $debug->switchTimer( 'GeneratingCache', 'GeneratingRestResult' );
173
174 $res = parent::createResult();
175 $resGroups = $this->getResponseGroups();
176 if ( !empty( $resGroups ) )
177 {
178 $res->variables['requestedResponseGroups'] = $resGroups;
179 }
180
181 if ( $res instanceof ezpRestMvcResult )
182 {
183 $res->responseGroups = $resGroups;
184 }
185
186 if ( $isCacheEnabled )
187 $cache->store( $controllerCacheId, $res );
188
189 $debug->stopTimer( 'GeneratingCache' );
190 }
191 catch ( Exception $e )
192 {
193 $debug->log( 'Exception caught, aborting cache generation', ezcLog::DEBUG );
194 if ( $isCacheEnabled )
195 $cache->abortCacheGeneration();
196 throw $e;
197 }
198 }
199
200 // Add debug infos to output if debug is enabled
201 $debug->stopTimer( 'GeneratingRestResult' );
202 if ( ezpRestDebug::isDebugEnabled() )
203 {
204 $res->variables['debug'] = $debug->getReport();
205 }
206
207 return $res;
208 }
209
215 public function doHttpOptions()
216 {
217 $methods = implode( ', ', $this->supported_http_methods );
218 $result = new ezpRestMvcResult;
219 $result->status = new ezpRestStatusResponse(
220 ezpHttpResponseCodes::OK,
221 'Allowed methods are: ' . $methods,
222 array( 'Allow' => $methods )
223 );
224 return $result;
225 }
226
232 public function getCacheLocation()
233 {
234 $routingInfos = $this->getRouter()->getRoutingInformation();
235 return ezpRestPrefixFilterInterface::getApiProviderName() .
236 '/v' . ezpRestPrefixFilterInterface::getApiVersion() .
237 '/' . str_replace( "\\", ".", $routingInfos->controllerClass ) .
238 '/' . $routingInfos->action;
239 }
240
249 private function getActionTTL()
250 {
251 $routingInfos = $this->getRouter()->getRoutingInformation();
252
253 // Check if we have TTL settings for this controller/action
254 $actionSectionName = $routingInfos->controllerClass . '_' . $routingInfos->action . '_CacheSettings';
255 if ( $this->restINI->hasVariable( $actionSectionName, 'CacheTTL' ) )
256 {
257 return (int)$this->restINI->variable( $actionSectionName, 'CacheTTL' );
258 }
259
260 $controllerSectionName = $routingInfos->controllerClass . '_CacheSettings';
261 if ( $this->restINI->hasVariable( $controllerSectionName, 'CacheTTL' ) ) // Nothing at controller/action level, check at controller level
262 {
263 return (int)$this->restINI->variable( $controllerSectionName, 'CacheTTL' );
264 }
265
266 return (int)$this->restINI->variable( 'CacheSettings', 'DefaultCacheTTL' );
267 }
268
282 private function generateCacheId()
283 {
284 $routingInfos = $this->getRouter()->getRoutingInformation();
285
286 $aCacheId = array(
287 ezpRestPrefixFilterInterface::getApiProviderName(),
288 ezpRestPrefixFilterInterface::getApiVersion(),
289 $routingInfos->controllerClass,
290 $routingInfos->action
291 );
292 // Add internal variables, caught in the URL. See ezpRestHttpRequestParser::fillVariables()
293 // Also add content variables
294 foreach ( $this->request->contentVariables + $this->request->variables as $name => $val )
295 {
296 $aCacheId[] = $name . '=' . ( is_array( $val ) ? implode( ',', $val ) : $val );
297 }
298
299 return md5( implode( '-', $aCacheId ) );
300 }
301
310 private function isCacheEnabled()
311 {
312 // Global switch
313 if ( $this->restINI->variable( 'CacheSettings', 'ApplicationCache' ) !== 'enabled' )
314 {
315 return false;
316 }
317
318 $routingInfos = $this->getRouter()->getRoutingInformation();
319
320 // Check if we have a specific setting for this controller/action
321 $actionSectionName = $routingInfos->controllerClass . '_' . $routingInfos->action . '_CacheSettings';
322 if ( $this->restINI->hasVariable( $actionSectionName, 'ApplicationCache' ) )
323 {
324 return $this->restINI->variable( $actionSectionName, 'ApplicationCache' ) === 'enabled';
325 }
326
327 // Nothing at controller/action level, check at controller level
328 $controllerSectionName = $routingInfos->controllerClass . '_CacheSettings';
329 if ( $this->restINI->hasVariable( $controllerSectionName, 'ApplicationCache' ) )
330 {
331 return $this->restINI->variable( $controllerSectionName, 'ApplicationCache' ) === 'enabled';
332 }
333
334 // Nothing at controller level, take the default value
335 return $this->restINI->variable( 'CacheSettings', 'ApplicationCacheDefault' ) === 'enabled';
336 }
337 }
338 ?>
$i
$i
Definition: admin/adlist.php:91
ezpHttpResponseCodes\OK
const OK
Definition: http_response_codes.php:14
ezpRestDebug\getInstance
static getInstance()
Definition: debug.php:50
ezpRestDebug\isDebugEnabled
static isDebugEnabled()
Definition: debug.php:64
ezpRestMvcController
Definition: rest_controller.php:16
ezpRestMvcController\$restINI
$restINI
Definition: rest_controller.php:34
ezpRestMvcController\hasResponseGroup
hasResponseGroup( $name)
Definition: rest_controller.php:62
ezpRestMvcController\getAllContentVariables
getAllContentVariables()
Definition: rest_controller.php:134
ezpRestMvcController\CACHE_ID
const CACHE_ID
Definition: rest_controller.php:22
ezpRestMvcController\createResult
createResult()
Definition: rest_controller.php:144
ezpRestMvcController\$isCacheCreated
static $isCacheCreated
Definition: rest_controller.php:41
ezpRestMvcController\setDefaultResponseGroups
setDefaultResponseGroups(array $defaultResponseGroups)
Definition: rest_controller.php:95
ezpRestMvcController\getActionTTL
getActionTTL()
Definition: rest_controller.php:249
ezpRestMvcController\__construct
__construct( $action, ezcMvcRequest $request)
Definition: rest_controller.php:49
ezpRestMvcController\getContentVariable
getContentVariable( $name)
Definition: rest_controller.php:119
ezpRestMvcController\isCacheEnabled
isCacheEnabled()
Definition: rest_controller.php:310
ezpRestMvcController\$defaultResponsegroups
$defaultResponsegroups
Definition: rest_controller.php:29
ezpRestMvcController\hasContentVariable
hasContentVariable( $name)
Definition: rest_controller.php:107
ezpRestMvcController\getResponseGroups
getResponseGroups()
Definition: rest_controller.php:76
ezpRestMvcController\generateCacheId
generateCacheId()
Definition: rest_controller.php:282
ezpRestMvcController\doHttpOptions
doHttpOptions()
Definition: rest_controller.php:215
ezpRestMvcController\getCacheLocation
getCacheLocation()
Definition: rest_controller.php:232
ezpRestMvcResult
Definition: private/rest/classes/response/result.php:16
ezpRestPrefixFilterInterface\getApiVersion
static getApiVersion()
Definition: prefix_filter.php:83
ezpRestPrefixFilterInterface\getApiProviderName
static getApiProviderName()
Definition: prefix_filter.php:98
ezpRestStatusResponse
Definition: response.php:13
$result
$result
Definition: companysearch.php:94
$name
$name
Definition: companytypelist.php:194
$res
$res
Definition: ezstats/admin/cron.php:45
$debug
$debug
Definition: ezstats/admin/cron.php:36
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.