]> Raphaël G. Git Repositories - packbundle/commitdiff
Add context supporting getBaseUrl feature
authorRaphaël Gertz <git@rapsys.eu>
Sun, 8 Aug 2021 12:11:20 +0000 (14:11 +0200)
committerRaphaël Gertz <git@rapsys.eu>
Sun, 8 Aug 2021 12:11:20 +0000 (14:11 +0200)
Context/NullContext.php [new file with mode: 0644]
Context/RequestStackContext.php [new file with mode: 0644]

diff --git a/Context/NullContext.php b/Context/NullContext.php
new file mode 100644 (file)
index 0000000..9f728cb
--- /dev/null
@@ -0,0 +1,28 @@
+<?php declare(strict_types=1);
+
+/*
+ * This file is part of the Rapsys PackBundle package.
+ *
+ * (c) Raphaël Gertz <symfony@rapsys.eu>
+ *
+ * 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\NullContext as BaseNullContext;
+
+/**
+ * {@inheritdoc}
+ */
+class NullContext extends BaseNullContext {
+       /**
+        * Returns the base url
+        *
+        * @return string The base url
+        */
+       public function getBaseUrl(): string {
+               return '';
+       }
+}
diff --git a/Context/RequestStackContext.php b/Context/RequestStackContext.php
new file mode 100644 (file)
index 0000000..0421de7
--- /dev/null
@@ -0,0 +1,56 @@
+<?php declare(strict_types=1);
+
+/*
+ * This file is part of the Rapsys PackBundle package.
+ *
+ * (c) Raphaël Gertz <symfony@rapsys.eu>
+ *
+ * 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();
+       }
+}