Guest User

Untitled

a guest
Feb 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Project\Model;
  4.  
  5. use SilverStripe\Core\ClassInfo;
  6. use SilverStripe\CMS\Model\SiteTree;
  7. use App\Project\Control\PageController;
  8.  
  9. class Page extends SiteTree
  10. {
  11. /**
  12. * Returns the controller class for this page. If a subclass of the page
  13. * controller exists, use that. Otherwise default to the base namespaced
  14. * controller.
  15. *
  16. * This is required as the SiteTree getControllerName doesn't handle cross
  17. * namespace (i.e \Model\ vs \Control).
  18. */
  19. public function getControllerName()
  20. {
  21. $current = static::class;
  22. $ancestry = ClassInfo::ancestry($current);
  23. $controller = null;
  24.  
  25. while ($class = array_pop($ancestry)) {
  26. if ($class == self::class) {
  27. break;
  28. }
  29.  
  30. if (class_exists($candidate = sprintf('%sController', $class))) {
  31. $controller = $candidate;
  32. break;
  33. }
  34.  
  35. $candidate = sprintf('%sController', str_replace('\\Model\\', '\\Control\\', $class));
  36.  
  37. if (class_exists($candidate)) {
  38. $controller = $candidate;
  39. break;
  40. }
  41. }
  42.  
  43. if ($controller) {
  44. return $controller;
  45. }
  46.  
  47. return PageController::class;
  48. }
  49. }
Add Comment
Please, Sign In to add comment