X-Git-Url: https://git.rapsys.eu/packbundle/blobdiff_plain/57148c75381fac1f83a4afe886471c6aaa1cc2ea..59ae967e218457b2ab3d77cb621c0640345f5e9b:/Context/RequestStackContext.php diff --git a/Context/RequestStackContext.php b/Context/RequestStackContext.php new file mode 100644 index 0000000..0421de7 --- /dev/null +++ b/Context/RequestStackContext.php @@ -0,0 +1,56 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Rapsys\PackBundle\Context; + +use Symfony\Component\Asset\Context\RequestStackContext as BaseRequestStackContext; +use Symfony\Component\HttpFoundation\RequestStack; + +/** + * {@inheritdoc} + */ +class RequestStackContext extends BaseRequestStackContext { + //RequestStack instance + private $requestStack; + + //Base path + private $basePath; + + /** + * {@inheritdoc} + */ + public function __construct(RequestStack $requestStack, string $basePath = '', bool $secure = false) { + //Call parent constructor + parent::__construct($requestStack, $basePath, $secure); + + //Set request stack + $this->requestStack = $requestStack; + + //Set base path + $this->basePath = $basePath; + } + + /** + * Returns the base url + * + * @return string The base url + */ + public function getBaseUrl(): string { + //Without request + if (!$request = $this->requestStack->getMasterRequest()) { + //Return base path + return $this->basePath; + } + + //Return base uri + return $request->getSchemeAndHttpHost().$request->getBaseUrl(); + } +}