HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
Yii2RequestPanel.php
Go to the documentation of this file.
1 <?php
2 
9 {
10  public function getName()
11  {
12  return 'Request';
13  }
14 
15  public function getSummary()
16  {
17  $data = $this->getData();
18  return $this->render(dirname(__FILE__) . '/../views/panels/request_bar.php', array(
19  'statusCode' => $data['statusCode'],
20  'route' => $data['route'],
21  'action' => $data['action'],
22  ));
23  }
24 
25  public function getDetail()
26  {
27  return $this->render(dirname(__FILE__) . '/../views/panels/request.php', array(
28  'data' => $this->getData(),
29  ));
30  }
31 
32  public function save()
33  {
34  if (function_exists('apache_request_headers')) {
35  $requestHeaders = apache_request_headers();
36  } elseif (function_exists('http_get_request_headers')) {
37  $requestHeaders = http_get_request_headers();
38  } else {
39  $requestHeaders = array();
40  }
41  $responseHeaders = array();
42  foreach (headers_list() as $header) {
43  if (($pos = strpos($header, ':')) !== false) {
44  $name = substr($header, 0, $pos);
45  $value = trim(substr($header, $pos + 1));
46  if (isset($responseHeaders[$name])) {
47  if (!is_array($responseHeaders[$name])) {
48  $responseHeaders[$name] = array($responseHeaders[$name], $value);
49  } else {
50  $responseHeaders[$name][] = $value;
51  }
52  } else {
53  $responseHeaders[$name] = $value;
54  }
55  } else {
56  $responseHeaders[] = $header;
57  }
58  }
59 
60  $route = Yii::app()->getUrlManager()->parseUrl(Yii::app()->getRequest());
61  $action = null;
62  $actionParams = array();
63  if (($ca = @Yii::app()->createController($route)) !== null) {
64  /* @var CController $controller */
65  /* @var string $actionID */
66  list($controller, $actionID) = $ca;
67  if (!$actionID) $actionID = $controller->defaultAction;
68  if (($a = $controller->createAction($actionID)) !== null) {
69  if ($a instanceof CInlineAction) {
70  $action = get_class($controller) . '::action' . ucfirst($actionID) . '()';
71  } else {
72  $action = get_class($a) . '::run()';
73  }
74  }
75  $actionParams = $controller->actionParams;
76  }
77 
78  $flashes = array();
79  $user = Yii::app()->getComponent('user', false);
80  if ($user instanceof CWebUser) {
81  $flashes = $user->getFlashes(false);
82  }
83 
84  return array(
85  'flashes' => $flashes,
86  'statusCode' => $this->getStatusCode(),
87  'requestHeaders' => $requestHeaders,
88  'responseHeaders' => $responseHeaders,
89  'route' => $route,
90  'action' => $action,
91  'actionParams' => $actionParams,
92  'SERVER' => empty($_SERVER) ? array() : $_SERVER,
93  'GET' => empty($_GET) ? array() : $_GET,
94  'POST' => empty($_POST) ? array() : $_POST,
95  'COOKIE' => empty($_COOKIE) ? array() : $_COOKIE,
96  'FILES' => empty($_FILES) ? array() : $_FILES,
97  'SESSION' => empty($_SESSION) ? array() : $_SESSION,
98  );
99  }
100 
101  private $_statusCode;
102 
106  protected function getStatusCode()
107  {
108  if (function_exists('http_response_code')) {
109  return http_response_code();
110  } else {
111  return $this->_statusCode;
112  }
113  }
114 
115  public function __construct($owner, $id)
116  {
117  parent::__construct($owner, $id);
118  if (!function_exists('http_response_code')) {
119  Yii::app()->attachEventHandler('onException', array($this, 'onException'));
120  }
121  }
122 
126  protected function onException($event)
127  {
128  if ($event->exception instanceof CHttpException) {
129  $this->_statusCode = $event->exception->statusCode;
130  } else {
131  $this->_statusCode = 500;
132  }
133  }
134 
139  public static function getStatusCodeHtml($statusCode)
140  {
141  $type = 'important';
142  if ($statusCode >= 100 && $statusCode < 200) {
143  $type = 'info';
144  } elseif ($statusCode >= 200 && $statusCode < 300) {
145  $type = 'success';
146  } elseif ($statusCode >= 300 && $statusCode < 400) {
147  $type = 'warning';
148  }
149  return CHtml::tag('span', array('class' => 'label label-' . $type), $statusCode);
150  }
151 }