Skip to content

Commit

Permalink
Support Symfony 7.2.*
Browse files Browse the repository at this point in the history
  • Loading branch information
meritoo committed Jan 11, 2025
1 parent a4bf521 commit a4076c4
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 84 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.0
0.5.0
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@
"ext-intl": "*",
"doctrine/annotations": "^2.0",
"meritoo/common-library": "^1.3",
"symfony/form": "^5.4",
"symfony/framework-bundle": "^5.4",
"symfony/twig-bundle": "^5.4",
"symfony/validator": "^5.4"
"symfony/form": "^7.2",
"symfony/framework-bundle": "^7.2",
"symfony/twig-bundle": "^7.2",
"symfony/validator": "^7.2"
},
"require-dev": {
"friends-of-phpspec/phpspec-code-coverage": "^6.1",
"friendsofphp/php-cs-fixer": "^3.8",
"infection/infection": "^0.26.6",
"infection/infection": "^0.27.11",
"php-coveralls/php-coveralls": "^2.5",
"phpspec/phpspec": "^7.2",
"phpstan/phpstan": "^1.6",
"phpunit/phpunit": "^9.5",
"sebastian/phpcpd": "^6.0",
"squizlabs/php_codesniffer": "^3.6",
"symfony/phpunit-bridge": "^6.0",
"symfony/translation": "^6.0",
"vimeo/psalm": "^4.22"
"symfony/phpunit-bridge": "^7.2",
"symfony/translation": "^7.2",
"vimeo/psalm": "^5.26"
},
"autoload": {
"psr-4": {
Expand Down
86 changes: 46 additions & 40 deletions src/Service/RequestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Meritoo\CommonBundle\Service\Base\BaseService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

/**
* Serves request
Expand All @@ -28,13 +27,6 @@ class RequestService extends BaseService implements RequestServiceInterface
private const ROUTE_PARAMETER = '_route';
private const PARAMETERS_PARAMETER = '_route_params';

/**
* The session
*
* @var SessionInterface
*/
private SessionInterface $session;

/**
* Request stack
*
Expand All @@ -52,12 +44,10 @@ class RequestService extends BaseService implements RequestServiceInterface
/**
* Class constructor
*
* @param SessionInterface $session The session
* @param RequestStack $requestStack
* @param RequestStack $requestStack
*/
public function __construct(SessionInterface $session, RequestStack $requestStack)
public function __construct(RequestStack $requestStack)
{
$this->session = $session;
$this->requestStack = $requestStack;
}

Expand All @@ -68,17 +58,21 @@ public function __construct(SessionInterface $session, RequestStack $requestStac
*/
public function fetchRefererUrl(): string
{
$url = $this->session->get($this->refererUrlKey, '');
$this->session->remove($this->refererUrlKey);
$url = $this
->requestStack
->getSession()
->get($this->refererUrlKey, '')
;

$this
->requestStack
->getSession()
->remove($this->refererUrlKey)
;

return $url;
}

public function getCurrentRoute(): string
{
return $this->getParameter(self::ROUTE_PARAMETER) ?? '';
}

public function getCurrentRouteParameters(): array
{
return $this->getParameter(self::PARAMETERS_PARAMETER) ?? [];
Expand All @@ -89,39 +83,21 @@ public function getParameter(string $parameter)
return $this->getCurrentRequest()->get($parameter);
}

/**
* Returns url of referer
*
* @param Request $request The request (that probably contains referer)
* @return string
*/
public function getRefererUrl(Request $request): string
{
return $request->headers->get('referer', '');
}

public function isCurrentRoute(string $route): bool
{
return $this->getCurrentRoute() === $route;
}

/**
* Stores url of referer in session
*
* @param string $url Url of referer to store
* @return RequestServiceInterface
*/
public function storeRefererUrl(string $url): RequestServiceInterface
public function getCurrentRoute(): string
{
$this->session->set($this->refererUrlKey, $url);

return $this;
return $this->getParameter(self::ROUTE_PARAMETER) ?? '';
}

/**
* Stores the referer url in session grabbed from given request
*
* @param Request $request The request (that probably contains referer)
*
* @return RequestServiceInterface
*/
public function storeRefererUrlFromRequest(Request $request): RequestServiceInterface
Expand All @@ -139,6 +115,36 @@ public function storeRefererUrlFromRequest(Request $request): RequestServiceInte
return $this->storeRefererUrl($url);
}

/**
* Returns url of referer
*
* @param Request $request The request (that probably contains referer)
*
* @return string
*/
public function getRefererUrl(Request $request): string
{
return $request->headers->get('referer', '');
}

/**
* Stores url of referer in session
*
* @param string $url Url of referer to store
*
* @return RequestServiceInterface
*/
public function storeRefererUrl(string $url): RequestServiceInterface
{
$this
->requestStack
->getSession()
->set($this->refererUrlKey, $url)
;

return $this;
}

private function getCurrentRequest(): Request
{
$request = $this->requestStack->getCurrentRequest();
Expand Down
22 changes: 22 additions & 0 deletions tests/Controller/Base/BaseControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
use Meritoo\CommonBundle\Exception\Controller\BaseController\CannotRedirectToEmptyRefererUrlException;
use Meritoo\Test\CommonBundle\Controller\Base\BaseController\RealController;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;

/**
* Test case for the base controller with common and useful methods
Expand All @@ -28,6 +31,7 @@ class BaseControllerTest extends KernelTestCase
{
private RequestServiceInterface $requestService;

/** @runInSeparateProcess */
public function testRedirectToReferer(): void
{
$refererUrl = '/';
Expand All @@ -40,6 +44,7 @@ public function testRedirectToReferer(): void
static::assertSame($refererUrl, $response->getTargetUrl());
}

/** @runInSeparateProcess */
public function testRedirectToRefererOrRoute(): void
{
$controller = $this->getRealController();
Expand All @@ -49,6 +54,7 @@ public function testRedirectToRefererOrRoute(): void
static::assertSame('/test/index', $response->getTargetUrl());
}

/** @runInSeparateProcess */
public function testRedirectToRefererUsingEmptyRefererUrl(): void
{
$this->expectException(CannotRedirectToEmptyRefererUrlException::class);
Expand All @@ -74,12 +80,28 @@ protected function setUp(): void
parent::setUp();
static::bootKernel();

/** @var RequestStack $requestStack */
$requestStack = static::getContainer()->get(RequestStack::class);

/** @var RequestServiceInterface $requestService */
$requestService = static::getContainer()->get(RequestServiceInterface::class);

$request = $this->createRequestWithSession();
$requestStack->push($request);

$this->requestService = $requestService;
}

private function createRequestWithSession(): Request
{
$session = new Session();

$request = new Request();
$request->setSession($session);

return $request;
}

/**
* Returns instance of RealController used for testing
*
Expand Down
2 changes: 1 addition & 1 deletion tests/Resources/config/packages/test/framework.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
framework:
test: true
session:
storage_id: session.storage.mock_file
storage_factory_id: session.storage.factory.mock_file
translator:
default_path: '%kernel.project_dir%/../src/Resources/translations'
fallbacks:
Expand Down
Loading

0 comments on commit a4076c4

Please sign in to comment.