]> Raphaël G. Git Repositories - packbundle/blob - Context/RequestStackContext.php
Cleanup
[packbundle] / Context / RequestStackContext.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys PackBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\PackBundle\Context;
13
14 use Symfony\Component\Asset\Context\RequestStackContext as BaseRequestStackContext;
15 use Symfony\Component\HttpFoundation\RequestStack;
16
17 /**
18 * {@inheritdoc}
19 */
20 class RequestStackContext extends BaseRequestStackContext {
21 //RequestStack instance
22 private $requestStack;
23
24 //Base path
25 private $basePath;
26
27 /**
28 * {@inheritdoc}
29 */
30 public function __construct(RequestStack $requestStack, string $basePath = '', bool $secure = false) {
31 //Call parent constructor
32 parent::__construct($requestStack, $basePath, $secure);
33
34 //Set request stack
35 $this->requestStack = $requestStack;
36
37 //Set base path
38 $this->basePath = $basePath;
39 }
40
41 /**
42 * Returns the base url
43 *
44 * @return string The base url
45 */
46 public function getBaseUrl(): string {
47 //Without request
48 if (!$request = $this->requestStack->getMainRequest()) {
49 //Return base path
50 return $this->basePath;
51 }
52
53 //Return base uri
54 return $request->getSchemeAndHttpHost().$request->getBaseUrl();
55 }
56 }