PageRenderTime 27ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/codeception/codeception/src/Codeception/Lib/Connector/Phalcon.php

https://gitlab.com/merial/WETE_Ryhma3
PHP | 460 lines | 247 code | 61 blank | 152 comment | 24 complexity | d6412c3de2023170a6340b17ccd917e5 MD5 | raw file
  1. <?php
  2. namespace Codeception\Lib\Connector;
  3. use Codeception\Lib\Connector\Shared\PhpSuperGlobalsConverter;
  4. use Symfony\Component\BrowserKit\Response;
  5. use Symfony\Component\BrowserKit\Cookie;
  6. use Symfony\Component\BrowserKit\Client;
  7. use Codeception\Util\Stub;
  8. use Phalcon\Di;
  9. use Phalcon\Mvc\Application;
  10. use Phalcon\Mvc\Micro As MicroApplication;
  11. use Phalcon\Http\Request;
  12. use Phalcon\Http\RequestInterface;
  13. use Phalcon\Session\AdapterInterface as SessionInterface;
  14. use ReflectionProperty;
  15. use RuntimeException;
  16. use Closure;
  17. class Phalcon extends Client
  18. {
  19. use PhpSuperGlobalsConverter;
  20. /**
  21. * Phalcon Application
  22. * @var mixed
  23. */
  24. private $application;
  25. /**
  26. * Set Phalcon Application by \Phalcon\DI\Injectable, Closure or bootstrap file path
  27. *
  28. * @param mixed $application
  29. */
  30. public function setApplication($application)
  31. {
  32. $this->application = $application;
  33. }
  34. /**
  35. * Get Phalcon Application
  36. *
  37. * @return Application|MicroApplication
  38. */
  39. public function getApplication()
  40. {
  41. $application = $this->application;
  42. if ($application instanceof Closure) {
  43. return $application();
  44. } elseif (is_string($application)) {
  45. return require $application;
  46. } else {
  47. return $application;
  48. }
  49. }
  50. /**
  51. * Makes a request.
  52. *
  53. * @param \Symfony\Component\BrowserKit\Request $request
  54. *
  55. * @return \Symfony\Component\BrowserKit\Response
  56. * @throws \RuntimeException
  57. */
  58. public function doRequest($request)
  59. {
  60. $application = $this->getApplication();
  61. if (!$application instanceof Application && !$application instanceof MicroApplication) {
  62. throw new RuntimeException('Unsupported application class.');
  63. }
  64. $di = $application->getDI();
  65. /** @var \Phalcon\Http\Request $phRequest */
  66. if ($di->has('request')) {
  67. $phRequest = $di->get('request');
  68. }
  69. if (!$phRequest instanceof RequestInterface) {
  70. $phRequest = new Request;
  71. }
  72. $uri = $request->getUri() ?: $phRequest->getURI();
  73. $pathString = parse_url($uri, PHP_URL_PATH);
  74. $queryString = parse_url($uri, PHP_URL_QUERY);
  75. $_SERVER = $request->getServer();
  76. $_SERVER['REQUEST_METHOD'] = strtoupper($request->getMethod());
  77. $_SERVER['REQUEST_URI'] = null === $queryString ? $pathString : $pathString . '?' . $queryString;
  78. $_COOKIE = $request->getCookies();
  79. $_FILES = $this->remapFiles($request->getFiles());
  80. $_REQUEST = $this->remapRequestParameters($request->getParameters());
  81. $_POST = [];
  82. $_GET = [];
  83. if ($_SERVER['REQUEST_METHOD'] == 'GET') {
  84. $_GET = $_REQUEST;
  85. } else {
  86. $_POST = $_REQUEST;
  87. }
  88. parse_str($queryString, $output);
  89. foreach ($output as $k => $v) {
  90. $_GET[$k] = $v;
  91. }
  92. $_GET['_url'] = $pathString;
  93. $_SERVER['QUERY_STRING'] = http_build_query($_GET);
  94. Di::reset();
  95. Di::setDefault($di);
  96. $di['request'] = Stub::construct($phRequest, [], ['getRawBody' => $request->getContent()]);
  97. $response = $application->handle();
  98. $headers = $response->getHeaders();
  99. $status = (int) $headers->get('Status');
  100. $headersProperty = new ReflectionProperty($headers, '_headers');
  101. $headersProperty->setAccessible(true);
  102. $headers = $headersProperty->getValue($headers);
  103. if (!is_array($headers)) {
  104. $headers = [];
  105. }
  106. $cookiesProperty = new ReflectionProperty($di['cookies'], '_cookies');
  107. $cookiesProperty->setAccessible(true);
  108. $cookies = $cookiesProperty->getValue($di['cookies']);
  109. if (is_array($cookies)) {
  110. $restoredProperty = new ReflectionProperty('\Phalcon\Http\Cookie', '_restored');
  111. $restoredProperty->setAccessible(true);
  112. $valueProperty = new ReflectionProperty('\Phalcon\Http\Cookie', '_value');
  113. $valueProperty->setAccessible(true);
  114. foreach ($cookies as $name => $cookie) {
  115. if (!$restoredProperty->getValue($cookie)) {
  116. $clientCookie = new Cookie(
  117. $name,
  118. $valueProperty->getValue($cookie),
  119. $cookie->getExpiration(),
  120. $cookie->getPath(),
  121. $cookie->getDomain(),
  122. $cookie->getSecure(),
  123. $cookie->getHttpOnly()
  124. );
  125. $headers['Set-Cookie'][] = (string)$clientCookie;
  126. }
  127. }
  128. }
  129. return new Response(
  130. $response->getContent(),
  131. $status ? $status : 200,
  132. $headers
  133. );
  134. }
  135. }
  136. class PhalconMemorySession implements SessionInterface
  137. {
  138. /**
  139. * @var string
  140. */
  141. protected $sessionId;
  142. /**
  143. * @var string
  144. */
  145. protected $name;
  146. /**
  147. * @var bool
  148. */
  149. protected $started = false;
  150. /**
  151. * @var array
  152. */
  153. protected $memory = [];
  154. /**
  155. * @var array
  156. */
  157. protected $options = [];
  158. public function __construct(array $options = null)
  159. {
  160. $this->sessionId = $this->generateId();
  161. if (is_array($options)) {
  162. $this->setOptions($options);
  163. }
  164. }
  165. /**
  166. * @inheritdoc
  167. */
  168. public function start()
  169. {
  170. if ($this->status() !== PHP_SESSION_ACTIVE) {
  171. $this->memory = [];
  172. $this->started = true;
  173. return true;
  174. }
  175. return false;
  176. }
  177. /**
  178. * @inheritdoc
  179. *
  180. * @param array $options
  181. */
  182. public function setOptions(array $options)
  183. {
  184. if (isset($options['uniqueId'])) {
  185. $this->sessionId = $options['uniqueId'];
  186. }
  187. $this->options = $options;
  188. }
  189. /**
  190. * @inheritdoc
  191. *
  192. * @return array
  193. */
  194. public function getOptions()
  195. {
  196. return $this->options;
  197. }
  198. /**
  199. * @inheritdoc
  200. *
  201. * @param string $index
  202. * @param mixed $defaultValue
  203. * @param bool $remove
  204. * @return mixed
  205. */
  206. public function get($index, $defaultValue = null, $remove = false)
  207. {
  208. $key = $this->prepareIndex($index);
  209. if (!isset($this->memory[$key])) {
  210. return $defaultValue;
  211. }
  212. $return = $this->memory[$key];
  213. if ($remove) {
  214. unset($this->memory[$key]);
  215. }
  216. return $return;
  217. }
  218. /**
  219. * @inheritdoc
  220. *
  221. * @param string $index
  222. * @param mixed $value
  223. */
  224. public function set($index, $value)
  225. {
  226. $this->memory[$this->prepareIndex($index)] = $value;
  227. }
  228. /**
  229. * @inheritdoc
  230. *
  231. * @param string $index
  232. * @return bool
  233. */
  234. public function has($index)
  235. {
  236. return isset($this->memory[$this->prepareIndex($index)]);
  237. }
  238. /**
  239. * @inheritdoc
  240. *
  241. * @param string $index
  242. */
  243. public function remove($index)
  244. {
  245. unset($this->memory[$this->prepareIndex($index)]);
  246. }
  247. /**
  248. * @inheritdoc
  249. *
  250. * @return string
  251. */
  252. public function getId()
  253. {
  254. return $this->sessionId;
  255. }
  256. /**
  257. * @inheritdoc
  258. *
  259. * @return bool
  260. */
  261. public function isStarted()
  262. {
  263. return $this->started;
  264. }
  265. /**
  266. * Returns the status of the current session
  267. *
  268. * ``` php
  269. * <?php
  270. * if ($session->status() !== PHP_SESSION_ACTIVE) {
  271. * $session->start();
  272. * }
  273. * ?>
  274. * ```
  275. *
  276. * @return int
  277. */
  278. public function status()
  279. {
  280. if ($this->isStarted()) {
  281. return PHP_SESSION_ACTIVE;
  282. }
  283. return PHP_SESSION_NONE;
  284. }
  285. /**
  286. * @inheritdoc
  287. *
  288. * @param bool $removeData
  289. * @return bool
  290. */
  291. public function destroy($removeData = false)
  292. {
  293. if ($removeData) {
  294. if (!empty($this->sessionId)) {
  295. foreach ($this->memory as $key => $value) {
  296. if (0 === strpos($key, $this->sessionId . '#')) {
  297. unset($this->memory[$key]);
  298. }
  299. }
  300. } else {
  301. $this->memory = [];
  302. }
  303. }
  304. $this->started = false;
  305. }
  306. /**
  307. * @inheritdoc
  308. *
  309. * @param bool $deleteOldSession
  310. * @return \Phalcon\Session\AdapterInterface
  311. */
  312. public function regenerateId($deleteOldSession = true)
  313. {
  314. $this->sessionId = $this->generateId();
  315. return $this;
  316. }
  317. /**
  318. * @inheritdoc
  319. *
  320. * @param string $name
  321. */
  322. public function setName($name)
  323. {
  324. $this->name = $name;
  325. }
  326. /**
  327. * @inheritdoc
  328. *
  329. * @return string
  330. */
  331. public function getName()
  332. {
  333. return $this->name;
  334. }
  335. /**
  336. * Dump all session
  337. *
  338. * @return array
  339. */
  340. public function toArray()
  341. {
  342. return (array) $this->memory;
  343. }
  344. /**
  345. * Alias: Gets a session variable from an application context
  346. *
  347. * @param string $index
  348. * @return mixed
  349. */
  350. public function __get($index)
  351. {
  352. return $this->get($index);
  353. }
  354. /**
  355. * Alias: Sets a session variable in an application context
  356. *
  357. * @param string $index
  358. * @param mixed $value
  359. */
  360. public function __set($index, $value)
  361. {
  362. $this->set($index, $value);
  363. }
  364. /**
  365. * Alias: Check whether a session variable is set in an application context
  366. *
  367. * @param string $index
  368. * @return bool
  369. */
  370. public function __isset($index)
  371. {
  372. return $this->has($index);
  373. }
  374. /**
  375. * Alias: Removes a session variable from an application context
  376. *
  377. * @param string $index
  378. */
  379. public function __unset($index)
  380. {
  381. $this->remove($index);
  382. }
  383. private function prepareIndex($index)
  384. {
  385. if ($this->sessionId) {
  386. $key = $this->sessionId . '#' . $index;
  387. } else {
  388. $key = $index;
  389. }
  390. return $key;
  391. }
  392. /**
  393. * @return string
  394. */
  395. private function generateId()
  396. {
  397. return md5(time());
  398. }
  399. }