EclipseGc

DrupalResponse.php

Feb 3rd, 2012
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.20 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Drupal\Core\Response;
  4.  
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Routing\RequestContext;
  8. use Symfony\Component\Routing\RouteCollection;
  9. use Symfony\Component\Routing\Matcher\UrlMatcher;
  10.  
  11. class DrupalResponse {
  12.   protected $context;
  13.   protected $routes;
  14.  
  15.   public function __construct(RequestContext $context, RouteCollection $routes) {
  16.     $this->context = $context;
  17.     $this->routes = $routes;
  18.   }
  19.  
  20.   public function getResponse(Request $request) {
  21.     $matcher = new UrlMatcher($this->routes, $this->context);
  22.     try {
  23.       $attributes = $matcher->match($request->getPathInfo());
  24.       $request->attributes->add($attributes);
  25.  
  26.       $menu_item = $attributes['menu item'];
  27.       $this->arguments_populate($menu_item, $attributes, 'access arguments');
  28.       if ($menu_item['access callback'] === TRUE || call_user_func_array($menu_item['access callback'], $menu_item['access arguments'])) {
  29.         if (isset($menu_item['file'])) {
  30.           require_once DRUPAL_ROOT . '/' . $menu_item['file path'] . '/'. $menu_item['file'];
  31.         }
  32.         $this->arguments_populate($menu_item, $attributes, 'page arguments');
  33.         $output = call_user_func_array($menu_item['page callback'], $menu_item['page arguments']);
  34.         if ($output) {
  35.           $response = new Response(drupal_render_page($output));
  36.         }
  37.         else {
  38.           $response = new Response(drupal_render_page('An error occurred'), 500);
  39.         }
  40.       }
  41.       else {
  42.         $response = new Response(drupal_render_page('Access Denied'), 403);
  43.       }
  44.     }
  45.     catch (Exception $e) {
  46.       $response = new Response(drupal_render_page('Not Found'), 404);
  47.     }
  48.     $response->prepare($request);
  49.     return $response;
  50.   }
  51.  
  52.   public function arguments_populate(&$menu_item, $attributes, $argument_type) {
  53.     foreach ($menu_item[$argument_type] as &$argument) {
  54.       if (is_numeric($argument)) {
  55.         $load_callback = $attributes['arguments'][$argument] .'_load';
  56.         if (function_exists($load_callback)) {
  57.           $argument = $load_callback($attributes[$attributes['arguments'][$argument]]);
  58.         }
  59.       }
  60.     }
  61.   }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment