| 133 | | * Builds an array of cache groups |
| 134 | | * |
| 135 | | * @param array The configuration |
| 136 | | * @param string The Action's Module name |
| 137 | | * @param string The Action's name |
| 138 | | * |
| 139 | | * @return array An array of groups |
| | 133 | * Builds an array of cache groups using the configuration and a container. |
| | 134 | * |
| | 135 | * @param array The group array from the configuration. |
| | 136 | * @param AgaviExecutionContainer The execution container. |
| | 137 | * |
| | 138 | * @return array An array of groups. |
| 144 | | public function determineGroups(array $cfg, $moduleName, $actionName) |
| 145 | | { |
| 146 | | $groups = array(); |
| 147 | | |
| 148 | | if(isset($cfg['groups'])) { |
| 149 | | foreach($cfg['groups'] as $group) { |
| 150 | | $group += array('name' => null, 'source' => null, 'namespace' => null); |
| 151 | | $val = $this->getVariable($group['name'], $group['source'], $group['namespace']); |
| 152 | | if($val === null) { |
| 153 | | $val = "0"; |
| 154 | | } |
| 155 | | $groups[] = $val; |
| 156 | | } |
| 157 | | } |
| 158 | | |
| 159 | | $groups[] = $moduleName . '_' . $actionName; |
| 160 | | |
| 161 | | return $groups; |
| | 143 | public function determineGroups(array $groups, $container) |
| | 144 | { |
| | 145 | $retval = array(); |
| | 146 | |
| | 147 | foreach($groups as $group) { |
| | 148 | $group += array('name' => null, 'source' => null, 'namespace' => null); |
| | 149 | $val = $this->getVariable($group['name'], $group['source'], $group['namespace']); |
| | 150 | if($val === null) { |
| | 151 | $val = "0"; |
| | 152 | } |
| | 153 | $retval[] = $val; |
| | 154 | } |
| | 155 | |
| | 156 | $retval[] = $container->getModuleName() . '_' . $container->getActionName(); |
| | 157 | |
| | 158 | return $retval; |
| 224 | | if($this->getParameter('enable_caching', false) && is_readable($cachingDotXml = AgaviConfig::get('core.module_dir') . '/' . $moduleName . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'caching.xml')) { |
| 225 | | // $lm->log(new AgaviLoggerMessage('Caching enabled, configuration file found, loading...')); |
| 226 | | $defaultConfig = array( |
| 227 | | 'enabled' => true, |
| 228 | | // lifetime. null = forever |
| 229 | | 'lifetime' => null, |
| 230 | | // request methods to cache. null = any |
| 231 | | 'methods' => null, |
| 232 | | // group definitions |
| 233 | | 'groups' => array( |
| 234 | | ), |
| 235 | | // views to cache. null = any |
| 236 | | 'views' => null, |
| 237 | | // a list of names of action attributes to cache and restore so they are available in view initialization |
| 238 | | 'actionAttributes' => array( |
| 239 | | ), |
| 240 | | // a list of request attributes to cache and restore so they are available for subsequent stuff |
| 241 | | 'requestAttributes' => array( |
| 242 | | ), |
| 243 | | 'decorator' => array( |
| 244 | | 'include' => true, |
| 245 | | 'slots' => array( |
| 246 | | ), |
| 247 | | 'variables' => array( |
| 248 | | ), |
| 249 | | ) |
| 250 | | ); |
| 251 | | // // $lm->log(new AgaviLoggerMessage(print_r(include(AgaviConfigCache::checkConfig($cachingDotXml)), true))); |
| 252 | | // $config = array_merge($config, include(AgaviConfigCache::checkConfig($cachingDotXml))); |
| 253 | | $config['SearchEngineSpam'] = array_merge($defaultConfig, array( |
| 254 | | 'enabled' => false, |
| 255 | | 'lifetime' => '10 seconds', |
| 256 | | // group definitions |
| 257 | | 'groups' => array( |
| 258 | | array( |
| 259 | | 'name' => 'index', |
| 260 | | ), |
| 261 | | array( |
| 262 | | 'source' => 'requestParameter', |
| 263 | | 'name' => 'name', |
| 264 | | ), |
| 265 | | array( |
| 266 | | 'source' => 'locale', |
| 267 | | ), |
| 268 | | ), |
| 269 | | 'actionAttributes' => array( |
| 270 | | 'testing' |
| 271 | | ), |
| 272 | | // other variables to cache and restore so they are available for subsequent stuff |
| 273 | | 'requestAttributes' => array( |
| 274 | | // array( |
| 275 | | // 'namespace' => 'foo.bar', |
| 276 | | // 'name' => 'testing', |
| 277 | | // ), |
| 278 | | ), |
| 279 | | 'decorator' => array( |
| 280 | | 'include' => true, |
| 281 | | 'slots' => array( |
| 282 | | ), |
| 283 | | 'variables' => array( |
| 284 | | ), |
| 285 | | ), |
| 286 | | )); |
| 287 | | if(isset($config[$actionName]) && $config[$actionName]['enabled'] && (!is_array($config[$actionName]['methods']) || in_array($method = $request->getMethod(), $config[$actionName]['methods']))) { |
| 288 | | // $lm->log(new AgaviLoggerMessage('Current action and request method are configured for caching, proceeding...')); |
| 289 | | $config = $config[$actionName]; |
| 290 | | $groups = $this->determineGroups($config, $moduleName, $actionName); |
| 291 | | // $lm->log(new AgaviLoggerMessage('Fetched groups "' . implode('", "', $groups) . '"')); |
| 292 | | $isCacheable = true; |
| 293 | | } |
| 294 | | } |
| 295 | | |
| | 219 | if($this->getParameter('enable_caching', true) && is_readable($cachingDotXml = AgaviConfig::get('core.module_dir') . '/' . $moduleName . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . $actionName . '.xml')) { |
| | 220 | // $lm->log('Caching enabled, configuration file found, loading...'); |
| | 221 | // no _once please! |
| | 222 | include(AgaviConfigCache::checkConfig($cachingDotXml)); |
| | 223 | } |
| | 224 | |
| 316 | | list($viewModule, $viewName) = $this->runAction($container); |
| 317 | | |
| 318 | | // check if the returned view is even cacheable |
| 319 | | if($isCacheable && is_array($config['views']) && !(in_array($viewName, $config['views'], true) || in_array(array($viewModule, $viewName), $config['views']))) { |
| | 242 | list($actionCache['view_module'], $actionCache['view_name']) = $this->runAction($container); |
| | 243 | |
| | 244 | // check if the returned view is cacheable |
| | 245 | if($isCacheable && is_array($config['views']) && !in_array(array('module' => $actionCache['view_module'], 'name' => $actionCache['view_name']), $config['views'], true)) { |
| 360 | | // $lm->log(new AgaviLoggerMessage('View is not cached, executing...')); |
| | 286 | $viewCache = array(); |
| | 287 | |
| | 288 | // create a new response instance for this action |
| | 289 | $rfi = $this->context->getFactoryInfo('response'); |
| | 290 | $response = new $rfi['class']; |
| | 291 | $response->initialize($this->context, $rfi['parameters']); |
| | 292 | $container->setResponse($response); |
| | 293 | |
| | 294 | // $lm->log('View is not cached, executing...'); |
| 369 | | |
| 370 | | if($next instanceof AgaviExecutionContainer) { |
| 371 | | $container->setNext($next); |
| | 303 | } |
| | 304 | |
| | 305 | if($viewCache['next'] instanceof AgaviExecutionContainer) { |
| | 306 | // $lm->log('Forwarding request, skipping rendering...'); |
| | 307 | $container->setNext($viewCache['next']); |
| | 308 | } else { |
| | 309 | if($isViewCached) { |
| | 310 | $layers = $viewCache['layers']; |
| | 311 | $response = $viewCache['response']; |
| | 312 | $container->setResponse($response); |
| | 313 | |
| | 314 | foreach($viewCache['template_variables'] as $name => $value) { |
| | 315 | $viewInstance->setAttribute($name, $value); |
| | 316 | } |
| | 317 | |
| | 318 | foreach($viewCache['request_attributes'] as $requestAttribute) { |
| | 319 | $request->setAttribute($requestAttribute['name'], $requestAttribute['value'], $requestAttribute['namespace']); |
| | 320 | } |
| | 321 | |
| | 322 | $output = array(); |
| | 323 | $nextOutput = $response->getContent(); |
| 373 | | $attributes =& $viewInstance->getAttributes(); |
| 374 | | |
| | 325 | $layers = $viewInstance->getLayers(); |
| | 326 | |
| | 327 | if($isCacheable) { |
| | 328 | $viewCache['template_variables'] = array(); |
| | 329 | foreach($otConfig['template_variables'] as $varName) { |
| | 330 | $viewCache['template_variables'][$varName] = $viewInstance->getAttribute($varName); |
| | 331 | } |
| | 332 | |
| | 333 | $viewCache['response'] = clone $response; |
| | 334 | |
| | 335 | $viewCache['layers'] = array(); |
| | 336 | |
| | 337 | $viewCache['slots'] = array(); |
| | 338 | |
| | 339 | $lastCacheableLayer = -1; |
| | 340 | if(is_array($otConfig['layers'])) { |
| | 341 | if(count($otConfig['layers'])) { |
| | 342 | for($i = count($layers)-1; $i >= 0; $i--) { |
| | 343 | $layer = $layers[$i]; |
| | 344 | $layerName = $layer->getName(); |
| | 345 | $cacheSlots[$layerName] = array(); |
| | 346 | if(isset($otConfig['layers'][$layerName])) { |
| | 347 | if(is_array($otConfig['layers'][$layerName])) { |
| | 348 | $lastCacheableLayer = $i - 1; |
| | 349 | } else { |
| | 350 | $lastCacheableLayer = $i; |
| | 351 | } |
| | 352 | } |
| | 353 | } |
| | 354 | } |
| | 355 | } else { |
| | 356 | $lastLayer = end($layers); |
| | 357 | if($lastLayer !== false) { |
| | 358 | $lastCacheableLayer = $lastLayer->getName(); |
| | 359 | } |
| | 360 | } |
| | 361 | |
| | 362 | for($i = $lastCacheableLayer + 1; $i < count($layers); $i++) { |
| | 363 | // $lm->log('Adding non-cacheable layer "' . $layers[$i]->getName() . '" to list'); |
| | 364 | $viewCache['layers'][] = clone $layers[$i]; |
| | 365 | } |
| | 366 | } |
| | 367 | |
| 377 | | foreach($viewInstance->getLayers() as $layer) { |
| 378 | | foreach($layer->getSlots() as $slotName => $slotContainer) { |
| | 370 | } |
| | 371 | |
| | 372 | $attributes =& $viewInstance->getAttributes(); |
| | 373 | |
| | 374 | // $lm->log('Starting rendering...'); |
| | 375 | for($i = 0; $i < count($layers); $i++) { |
| | 376 | $layer = $layers[$i]; |
| | 377 | $layerName = $layer->getName(); |
| | 378 | // $lm->log('Running layer "' . $layerName . '"...'); |
| | 379 | foreach($layer->getSlots() as $slotName => $slotContainer) { |
| | 380 | if($isViewCached && isset($viewCache['slots'][$layerName][$slotName])) { |
| | 381 | // $lm->log('Loading cached slot "' . $slotName . '"...'); |
| | 382 | $slotResponse = $viewCache['slots'][$layerName][$slotName]; |
| | 383 | } else { |
| | 384 | // $lm->log('Running slot "' . $slotName . '"...'); |
| 380 | | // set the presentation data as a template attribute |
| 381 | | if(($output[$slotName] = $slotResponse->getContent()) !== null) { |
| 382 | | // the slot really output something |
| 383 | | // let our response grab the stuff it needs from the slot's response |
| 384 | | $response->merge($slotResponse); |
| | 386 | if($isCacheable && !$isViewCached && in_array($slotName, $otConfig['layers'][$layerName])) { |
| | 387 | // $lm->log('Adding response of slot "' . $slotName . '" to cache...'); |
| | 388 | $viewCache['slots'][$layerName][$slotName] = $slotResponse; |
| 387 | | $moreAssigns = array( |
| 388 | | 'container' => $container, |
| 389 | | 'inner' => $nextOutput, |
| 390 | | 'view' => $viewInstance, |
| 391 | | 'request_data' => $container->getRequestData() |
| 392 | | ); |
| 393 | | $nextOutput = $layer->getRenderer()->render($layer, $attributes, $output, $moreAssigns); |
| 394 | | $output = array(); |
| 395 | | $output[$layer->getName()] = $nextOutput; |
| 396 | | } |
| | 391 | // set the presentation data as a template attribute |
| | 392 | if(($output[$slotName] = $slotResponse->getContent()) !== null) { |
| | 393 | // $lm->log('Merging in response from slot "' . $slotName . '"...'); |
| | 394 | // the slot really output something |
| | 395 | // let our response grab the stuff it needs from the slot's response |
| | 396 | $response->merge($slotResponse); |
| | 397 | } |
| | 398 | } |
| | 399 | $moreAssigns = array( |
| | 400 | 'container' => $container, |
| | 401 | 'inner' => $nextOutput, |
| | 402 | 'request_data' => $container->getRequestData(), |
| | 403 | 'view' => $viewInstance, |
| | 404 | ); |
| | 405 | $nextOutput = $layer->getRenderer()->render($layer, $attributes, $output, $moreAssigns); |
| | 406 | |
| 403 | | $actionCache = array(); |
| 404 | | |
| 405 | | $actionCache['viewModule'] = $viewModule; |
| 406 | | $actionCache['viewName'] = $viewName; |
| 407 | | |
| 408 | | $actionCache['actionAttributes'] = array(); |
| 409 | | foreach($config['actionAttributes'] as $attributeName) { |
| 410 | | $actionCache['actionAttributes'][$attributeName] = $actionAttributes[$attributeName]; |
| 411 | | } |
| 412 | | |
| 413 | | // $lm->log(new AgaviLoggerMessage('Writing Action cache...')); |
| | 420 | $actionCache['action_attributes'] = array(); |
| | 421 | foreach($config['action_attributes'] as $attributeName) { |
| | 422 | $actionCache['action_attributes'][$attributeName] = $actionAttributes[$attributeName]; |
| | 423 | } |
| | 424 | |
| | 425 | // $lm->log('Writing Action cache...'); |
| 418 | | $viewCache = array(); |
| 419 | | |
| 420 | | $viewCache['response'] = $response->export(); |
| 421 | | |
| 422 | | $viewCache['requestAttributes'] = array(); |
| 423 | | foreach($config['requestAttributes'] as $requestAttribute) { |
| 424 | | $viewCache['requestAttributes'][] = $requestAttribute + array('value' => $request->getAttribute($requestAttribute['name'], $requestAttribute['namespace'])); |
| | 430 | $viewCache['request_attributes'] = array(); |
| | 431 | foreach($otConfig['request_attributes'] as $requestAttribute) { |
| | 432 | $viewCache['request_attributes'][] = $requestAttribute + array('value' => $request->getAttribute($requestAttribute['name'], $requestAttribute['namespace'])); |