Conditions | 65 |
Paths | > 20000 |
Total Lines | 361 |
Code Lines | 188 |
Lines | 6 |
Ratio | 1.66 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
356 | public function execute(FilterChain $filterChain, ExecutionContainer $container) |
||
357 | { |
||
358 | // $lm = $this->context->getLoggerManager(); |
||
359 | |||
360 | // get the context, Dispatcher and validator manager |
||
361 | $dispatcher = $this->context->getDispatcher(); |
||
362 | |||
363 | // get the current controller information |
||
364 | $controllerName = $container->getControllerName(); |
||
365 | $moduleName = $container->getModuleName(); |
||
366 | |||
367 | // the controller instance |
||
368 | $controllerInstance = $container->getControllerInstance(); |
||
369 | |||
370 | $request = $this->context->getRequest(); |
||
371 | |||
372 | $isCacheable = false; |
||
373 | $cachingDotXml = Toolkit::evaluateModuleDirective( |
||
374 | $moduleName, |
||
375 | 'agavi.cache.path', |
||
376 | array( |
||
377 | 'moduleName' => $moduleName, |
||
378 | 'controllerName' => $controllerName, |
||
379 | ) |
||
380 | ); |
||
381 | |||
382 | $config = array(); |
||
383 | |||
384 | if ($this->getParameter('enable_caching', true) && is_readable($cachingDotXml)) { |
||
385 | // $lm->log('Caching enabled, configuration file found, loading...'); |
||
386 | // no _once please! |
||
387 | include(ConfigCache::checkConfig($cachingDotXml, $this->context->getName())); |
||
388 | } |
||
389 | |||
390 | $isControllerCached = false; |
||
391 | |||
392 | if ($isCacheable) { |
||
393 | try { |
||
394 | $groups = $this->determineGroups($config['groups'], $container); |
||
395 | $controllerGroups = array_merge($groups, array(self::CONTROLLER_CACHE_ID)); |
||
396 | } catch (UncacheableException $e) { |
||
397 | // a group callback threw an exception. that means we're not allowed t cache |
||
398 | $isCacheable = false; |
||
399 | } |
||
400 | if ($isCacheable) { |
||
401 | // this is not wrapped in the try/catch block above as it might throw an exception itself |
||
402 | $isControllerCached = $this->checkCache(array_merge($groups, array(self::CONTROLLER_CACHE_ID)), $config['lifetime']); |
||
403 | |||
404 | if (!$isControllerCached) { |
||
405 | // cacheable, but controller is not cached. notify our callback so it can prevent the stampede that follows |
||
406 | $this->startedCacheCreationCallback(self::CACHE_CALLBACK_CONTROLLER_NOT_CACHED, $controllerGroups, $config, $container); |
||
407 | } |
||
408 | } |
||
409 | } else { |
||
410 | // $lm->log('Controller is not cacheable!'); |
||
411 | } |
||
412 | |||
413 | if ($isControllerCached) { |
||
414 | // $lm->log('Controller is cached, loading...'); |
||
415 | // cache/dir/4-8-15-16-23-42 contains the controller cache |
||
416 | try { |
||
417 | $controllerCache = $this->readCache($controllerGroups); |
||
418 | // and restore controller attributes |
||
419 | $controllerInstance->setAttributes($controllerCache['controller_attributes']); |
||
420 | } catch (\Exception $e) { |
||
421 | // cacheable, but controller is not cached. notify our callback so it can prevent the stampede that follows |
||
422 | $this->startedCacheCreationCallback(self::CACHE_CALLBACK_CONTROLLER_CACHE_GONE, $controllerGroups, $config, $container); |
||
423 | $isControllerCached = false; |
||
424 | } |
||
425 | } |
||
426 | |||
427 | $isViewCached = false; |
||
428 | $rememberTheView = null; |
||
429 | |||
430 | while (true) { |
||
431 | if (!$isControllerCached) { |
||
432 | $controllerCache = array(); |
||
433 | |||
434 | // $lm->log('Controller not cached, executing...'); |
||
435 | // execute the Controller and get the View to execute |
||
436 | list($controllerCache['view_module'], $controllerCache['view_name']) = $container->runController(); |
||
437 | |||
438 | // check if we've just run the controller again after a previous cache read revealed that the view is not cached for this output type and we need to go back to square one due to the lack of controller attribute caching configuration... |
||
439 | // if yes: is the view module/name that we got just now different from what was in the cache? |
||
440 | if (isset($rememberTheView) && $controllerCache != $rememberTheView) { |
||
441 | // yup. clear it! |
||
442 | $ourClass = get_class($this); |
||
443 | $ourClass::clearCache($groups); |
||
444 | } |
||
445 | |||
446 | // check if the returned view is cacheable |
||
447 | if ($isCacheable && is_array($config['views']) && !in_array(array('module' => $controllerCache['view_module'], 'name' => $controllerCache['view_name']), $config['views'], true)) { |
||
448 | $isCacheable = false; |
||
449 | $this->abortedCacheCreationCallback(self::CACHE_CALLBACK_VIEW_NOT_CACHEABLE, $controllerGroups, $config, $container); |
||
450 | |||
451 | // so that view is not cacheable? okay then: |
||
452 | // check if we've just run the controller again after a previous cache read revealed that the view is not cached for this output type and we need to go back to square one due to the lack of controller attribute caching configuration... |
||
453 | // 'cause then we need to flush all those existing caches - obviously, that data is stale now, as we learned, since we are not allowed to cache anymore for the view that was returned now |
||
454 | if (isset($rememberTheView)) { |
||
455 | // yup. clear it! |
||
456 | $ourClass = get_class($this); |
||
457 | $ourClass::clearCache($groups); |
||
458 | } |
||
459 | // $lm->log('Returned View is not cleared for caching, setting cacheable status to false.'); |
||
460 | } else { |
||
461 | // $lm->log('Returned View is cleared for caching, proceeding...'); |
||
462 | } |
||
463 | |||
464 | $controllerAttributes = $controllerInstance->getAttributes(); |
||
465 | } |
||
466 | |||
467 | // clear the response |
||
468 | $response = $container->getResponse(); |
||
469 | $response->clear(); |
||
470 | |||
471 | // clear any forward set, it's ze view's job |
||
472 | $container->clearNext(); |
||
473 | |||
474 | if ($controllerCache['view_name'] !== View::NONE) { |
||
475 | $container->setViewModuleName($controllerCache['view_module']); |
||
476 | $container->setViewName($controllerCache['view_name']); |
||
477 | |||
478 | $key = $request->toggleLock(); |
||
479 | try { |
||
480 | $viewInstance = $container->getViewInstance(); |
||
481 | } catch (\Exception $e) { |
||
482 | // we caught an exception... unlock the request and rethrow! |
||
483 | $request->toggleLock($key); |
||
484 | throw $e; |
||
485 | } |
||
486 | $request->toggleLock($key); |
||
487 | |||
488 | $outputType = $container->getOutputType()->getName(); |
||
489 | |||
490 | if ($isCacheable) { |
||
491 | if (isset($config['output_types'][$otConfig = $outputType]) || isset($config['output_types'][$otConfig = '*'])) { |
||
492 | $otConfig = $config['output_types'][$otConfig]; |
||
493 | |||
494 | $viewGroups = array_merge($groups, array($outputType)); |
||
495 | |||
496 | if ($isControllerCached) { |
||
497 | $isViewCached = $this->checkCache($viewGroups, $config['lifetime']); |
||
498 | if (!$isViewCached) { |
||
499 | // cacheable, but view is not cached. notify our callback so it can prevent the stampede that follows |
||
500 | $this->startedCacheCreationCallback(self::CACHE_CALLBACK_VIEW_NOT_CACHED, $viewGroups, $config, $container); |
||
501 | } |
||
502 | } |
||
503 | } else { |
||
504 | $this->abortedCacheCreationCallback(self::CACHE_CALLBACK_OUTPUT_TYPE_NOT_CACHEABLE, $controllerGroups, $config, $container); |
||
505 | $isCacheable = false; |
||
506 | } |
||
507 | } |
||
508 | |||
509 | if ($isViewCached) { |
||
510 | // $lm->log('View is cached, loading...'); |
||
511 | try { |
||
512 | $viewCache = $this->readCache($viewGroups); |
||
513 | } catch (AgaviException $e) { |
||
514 | $this->startedCacheCreationCallback(self::CACHE_CALLBACK_VIEW_CACHE_GONE, $viewGroups, $config, $container); |
||
515 | $isViewCached = false; |
||
516 | } |
||
517 | } |
||
518 | if (!$isViewCached) { |
||
519 | // view not cached |
||
520 | // has the cache config a list of controller attributes? |
||
521 | if ($isControllerCached && !$config['controller_attributes']) { |
||
522 | // no. that means we must run the controller again! |
||
523 | $isControllerCached = false; |
||
524 | |||
525 | if ($isCacheable) { |
||
526 | // notify our callback so it can remove the lock that's on the view |
||
527 | // but only if we're still marked as cacheable (if not, then that means the OT is not cacheable, so there wouldn't be a $viewGroups) |
||
528 | $this->abortedCacheCreationCallback(self::CACHE_CALLBACK_CONTROLLER_CACHE_USELESS, $viewGroups, $config, $container); |
||
529 | } |
||
530 | // notify our callback so it can prevent the stampede that follows |
||
531 | $this->startedCacheCreationCallback(self::CACHE_CALLBACK_CONTROLLER_CACHE_USELESS, $controllerGroups, $config, $container); |
||
532 | |||
533 | // but remember the view info, just in case it differs if we run the controller again now |
||
534 | $rememberTheView = array( |
||
535 | 'view_module' => $controllerCache['view_module'], |
||
536 | 'view_name' => $controllerCache['view_name'], |
||
537 | ); |
||
538 | continue; |
||
539 | } |
||
540 | |||
541 | $viewCache = array(); |
||
542 | $viewCache['next'] = $this->executeView($container); |
||
543 | } |
||
544 | |||
545 | if ($viewCache['next'] instanceof ExecutionContainer) { |
||
546 | // $lm->log('Forwarding request, skipping rendering...'); |
||
547 | $container->setNext($viewCache['next']); |
||
548 | } else { |
||
549 | $output = array(); |
||
550 | $nextOutput = null; |
||
551 | |||
552 | if ($isViewCached) { |
||
553 | $layers = $viewCache['layers']; |
||
554 | /** @var Response $response */ |
||
555 | $response = $viewCache['response']; |
||
556 | $container->setResponse($response); |
||
557 | |||
558 | foreach ($viewCache['template_variables'] as $name => $value) { |
||
559 | $viewInstance->setAttribute($name, $value); |
||
560 | } |
||
561 | |||
562 | View Code Duplication | foreach ($viewCache['request_attributes'] as $requestAttribute) { |
|
563 | $request->setAttribute($requestAttribute['name'], $requestAttribute['value'], $requestAttribute['namespace']); |
||
564 | } |
||
565 | |||
566 | foreach ($viewCache['request_attribute_namespaces'] as $ranName => $ranValues) { |
||
567 | $request->setAttributes($ranValues, $ranName); |
||
568 | } |
||
569 | |||
570 | $nextOutput = $response->getContent(); |
||
571 | } else { |
||
572 | if ($viewCache['next'] !== null) { |
||
573 | // response content was returned from view execute() |
||
574 | $response->setContent($nextOutput = $viewCache['next']); |
||
575 | $viewCache['next'] = null; |
||
576 | } |
||
577 | |||
578 | $layers = $viewInstance->getLayers(); |
||
579 | |||
580 | if ($isCacheable) { |
||
581 | $viewCache['template_variables'] = array(); |
||
582 | foreach ($otConfig['template_variables'] as $varName) { |
||
583 | $viewCache['template_variables'][$varName] = $viewInstance->getAttribute($varName); |
||
584 | } |
||
585 | |||
586 | $viewCache['response'] = clone $response; |
||
587 | |||
588 | $viewCache['layers'] = array(); |
||
589 | |||
590 | $viewCache['slots'] = array(); |
||
591 | |||
592 | $lastCacheableLayer = -1; |
||
593 | if (is_array($otConfig['layers'])) { |
||
594 | if (count($otConfig['layers'])) { |
||
595 | for ($i = count($layers)-1; $i >= 0; $i--) { |
||
596 | $layer = $layers[$i]; |
||
597 | $layerName = $layer->getName(); |
||
598 | if (isset($otConfig['layers'][$layerName])) { |
||
599 | if (is_array($otConfig['layers'][$layerName])) { |
||
600 | $lastCacheableLayer = $i - 1; |
||
601 | } else { |
||
602 | $lastCacheableLayer = $i; |
||
603 | } |
||
604 | } |
||
605 | } |
||
606 | } |
||
607 | } else { |
||
608 | $lastCacheableLayer = count($layers) - 1; |
||
609 | } |
||
610 | |||
611 | for ($i = $lastCacheableLayer + 1; $i < count($layers); $i++) { |
||
612 | // $lm->log('Adding non-cacheable layer "' . $layers[$i]->getName() . '" to list'); |
||
613 | $viewCache['layers'][] = clone $layers[$i]; |
||
614 | } |
||
615 | } |
||
616 | } |
||
617 | |||
618 | $attributes =& $viewInstance->getAttributes(); |
||
619 | |||
620 | // whether or not we should assign the previous' layer's output to the $slots array |
||
621 | $assignInnerToSlots = $this->getParameter('assign_inner_to_slots', false); |
||
622 | |||
623 | // $lm->log('Starting rendering...'); |
||
624 | for ($i = 0; $i < count($layers); $i++) { |
||
625 | $layer = $layers[$i]; |
||
626 | $layerName = $layer->getName(); |
||
627 | // $lm->log('Running layer "' . $layerName . '"...'); |
||
628 | foreach ($layer->getSlots() as $slotName => $slotContainer) { |
||
629 | if ($isViewCached && isset($viewCache['slots'][$layerName][$slotName])) { |
||
630 | // $lm->log('Loading cached slot "' . $slotName . '"...'); |
||
631 | $slotResponse = $viewCache['slots'][$layerName][$slotName]; |
||
632 | } else { |
||
633 | // $lm->log('Running slot "' . $slotName . '"...'); |
||
634 | $slotResponse = $slotContainer->execute(); |
||
635 | if ($isCacheable && !$isViewCached && isset($otConfig['layers'][$layerName]) && is_array($otConfig['layers'][$layerName]) && in_array($slotName, $otConfig['layers'][$layerName])) { |
||
636 | // $lm->log('Adding response of slot "' . $slotName . '" to cache...'); |
||
637 | $viewCache['slots'][$layerName][$slotName] = $slotResponse; |
||
638 | } |
||
639 | } |
||
640 | // set the presentation data as a template attribute |
||
641 | ArrayPathDefinition::setValue($slotName, $output, $slotResponse->getContent()); |
||
642 | // and merge the other slot's response (this used to be conditional and done only when the content was not null) |
||
643 | // $lm->log('Merging in response from slot "' . $slotName . '"...'); |
||
644 | $response->merge($slotResponse); |
||
645 | } |
||
646 | $moreAssigns = array( |
||
647 | 'container' => $container, |
||
648 | 'inner' => $nextOutput, |
||
649 | 'request_data' => $container->getRequestData(), |
||
650 | 'response' => $response, |
||
651 | 'validation_manager' => $container->getValidationManager(), |
||
652 | 'view' => $viewInstance, |
||
653 | ); |
||
654 | // lock the request. can't be done outside the loop for the whole run, see #628 |
||
655 | $key = $request->toggleLock(); |
||
656 | try { |
||
657 | $nextOutput = $layer->getRenderer()->render($layer, $attributes, $output, $moreAssigns); |
||
658 | } catch (\Exception $e) { |
||
659 | // we caught an exception... unlock the request and rethrow! |
||
660 | $request->toggleLock($key); |
||
661 | throw $e; |
||
662 | } |
||
663 | // and unlock the request again |
||
664 | $request->toggleLock($key); |
||
665 | |||
666 | $response->setContent($nextOutput); |
||
667 | |||
668 | if ($isCacheable && !$isViewCached && $i === $lastCacheableLayer) { |
||
669 | $viewCache['response'] = clone $response; |
||
670 | } |
||
671 | |||
672 | $output = array(); |
||
673 | if ($assignInnerToSlots) { |
||
674 | $output[$layer->getName()] = $nextOutput; |
||
675 | } |
||
676 | } |
||
677 | } |
||
678 | |||
679 | if ($isCacheable && !$isViewCached) { |
||
680 | // we're writing the view cache first. this is just in case we get into a situation with really bad timing on the leap of a second |
||
681 | $viewCache['request_attributes'] = array(); |
||
682 | View Code Duplication | foreach ($otConfig['request_attributes'] as $requestAttribute) { |
|
683 | $viewCache['request_attributes'][] = $requestAttribute + array('value' => $request->getAttribute($requestAttribute['name'], $requestAttribute['namespace'])); |
||
684 | } |
||
685 | $viewCache['request_attribute_namespaces'] = array(); |
||
686 | foreach ($otConfig['request_attribute_namespaces'] as $requestAttributeNamespace) { |
||
687 | $viewCache['request_attribute_namespaces'][$requestAttributeNamespace] = $request->getAttributes($requestAttributeNamespace); |
||
688 | } |
||
689 | |||
690 | $this->writeCache($viewGroups, $viewCache, $config['lifetime']); |
||
691 | |||
692 | // notify callback that the execution has finished and caches have been written |
||
693 | $this->finishedCacheCreationCallback(self::CACHE_CALLBACK_VIEW_CACHE_WRITTEN, $viewGroups, $config, $container); |
||
694 | // $lm->log('Writing View cache...'); |
||
695 | } |
||
696 | } |
||
697 | |||
698 | // controller cache writing must occur here, so controllers that return View::NONE also get their cache written |
||
699 | if ($isCacheable && !$isControllerCached) { |
||
700 | $controllerCache['controller_attributes'] = array(); |
||
701 | foreach ($config['controller_attributes'] as $attributeName) { |
||
702 | $controllerCache['controller_attributes'][$attributeName] = $controllerAttributes[$attributeName]; |
||
703 | } |
||
704 | |||
705 | // $lm->log('Writing Controller cache...'); |
||
706 | |||
707 | $this->writeCache($controllerGroups, $controllerCache, $config['lifetime']); |
||
708 | |||
709 | // notify callback that the execution has finished and caches have been written |
||
710 | $this->finishedCacheCreationCallback(self::CACHE_CALLBACK_CONTROLLER_CACHE_WRITTEN, $controllerGroups, $config, $container); |
||
711 | } |
||
712 | |||
713 | // we're done here. bai. |
||
714 | break; |
||
715 | } |
||
716 | } |
||
717 | |||
769 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.