-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
225 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symfony\Component\DependencyInjection\Loader\Configurator; | ||
|
||
use Sylius\AdminUi\Twig\Extension\RedirectPathExtension; | ||
|
||
return function (ContainerConfigurator $configurator): void { | ||
$services = $configurator->services(); | ||
|
||
$services->set('sylius_twig_extra.twig.extension.redirect_path', RedirectPathExtension::class) | ||
->args([ | ||
service('router'), | ||
service('sylius.grid.filter_storage')->nullOnInvalid(), | ||
]) | ||
->tag(name: 'twig.extension') | ||
; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\AdminUi\Twig\Extension; | ||
|
||
use Sylius\Bundle\GridBundle\Storage\FilterStorageInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Routing\RouterInterface; | ||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFunction; | ||
|
||
final class RedirectPathExtension extends AbstractExtension | ||
{ | ||
private const NUMBER_OF_ROUTE_PROPERTIES = 3; | ||
|
||
public function __construct( | ||
private readonly RouterInterface $router, | ||
private readonly ?FilterStorageInterface $filterStorage = null, | ||
) { | ||
} | ||
|
||
public function getFunctions(): array | ||
{ | ||
return [ | ||
new TwigFunction('sylius_generate_redirect_path', [$this, 'generateRedirectPath']), | ||
]; | ||
} | ||
|
||
public function generateRedirectPath(?string $path): ?string | ||
{ | ||
if (null === $path) { | ||
return null; | ||
} | ||
|
||
$request = Request::create($path); | ||
|
||
try { | ||
$routeInfo = $this->router->match($request->getPathInfo()); | ||
} catch (\Throwable) { | ||
return $path; | ||
} | ||
|
||
if ([] !== $request->query->all() || $this->hasAdditionalParameters($routeInfo)) { | ||
return $path; | ||
} | ||
|
||
$route = $routeInfo['_route']; | ||
|
||
return $this->router->generate($route, $this->filterStorage?->all() ?? []); | ||
} | ||
|
||
/** | ||
* @param mixed[] $routeInfo | ||
*/ | ||
private function hasAdditionalParameters(array $routeInfo): bool | ||
{ | ||
return count($routeInfo) > self::NUMBER_OF_ROUTE_PROPERTIES; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/AdminUi/tests/Functional/.application/config/packages/doctrine.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
doctrine: | ||
dbal: | ||
url: '%env(resolve:DATABASE_URL)%' | ||
|
||
# IMPORTANT: You MUST configure your server version, | ||
# either here or in the DATABASE_URL env var (see .env file) | ||
#server_version: '16' | ||
|
||
profiling_collect_backtrace: '%kernel.debug%' | ||
use_savepoints: true | ||
orm: | ||
auto_generate_proxy_classes: true | ||
enable_lazy_ghost_objects: true | ||
report_fields_where_declared: true | ||
validate_xml_mapping: true | ||
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware | ||
auto_mapping: true | ||
mappings: | ||
App: | ||
type: attribute | ||
is_bundle: false | ||
dir: '%kernel.project_dir%/src/Entity' | ||
prefix: 'App\Entity' | ||
alias: App | ||
|
||
when@test: | ||
doctrine: | ||
dbal: | ||
# "TEST_TOKEN" is typically set by ParaTest | ||
dbname_suffix: '_test%env(default::TEST_TOKEN)%' | ||
|
||
when@prod: | ||
doctrine: | ||
orm: | ||
auto_generate_proxy_classes: false | ||
proxy_dir: '%kernel.build_dir%/doctrine/orm/Proxies' | ||
query_cache_driver: | ||
type: pool | ||
pool: doctrine.system_cache_pool | ||
result_cache_driver: | ||
type: pool | ||
pool: doctrine.result_cache_pool | ||
|
||
framework: | ||
cache: | ||
pools: | ||
doctrine.result_cache_pool: | ||
adapter: cache.app | ||
doctrine.system_cache_pool: | ||
adapter: cache.system |
66 changes: 66 additions & 0 deletions
66
src/AdminUi/tests/Functional/Twig/Extension/RedirectPathExtensionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Sylius\AdminUi\Functional\Twig\Extension; | ||
|
||
use Sylius\AdminUi\Twig\Extension\RedirectPathExtension; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
final class RedirectPathExtensionTest extends KernelTestCase | ||
{ | ||
private RedirectPathExtension $redirectPathExtension; | ||
|
||
protected function setUp(): void | ||
{ | ||
$container = self::getContainer(); | ||
|
||
$session = $container->get('session.factory')->createSession(); | ||
$request = new Request(); | ||
$request->setSession($session); | ||
$container->get('request_stack')->push($request); | ||
|
||
$this->redirectPathExtension = $container->get('sylius_twig_extra.twig.extension.redirect_path'); | ||
$container->get('sylius.grid.filter_storage')->set(['criteria' => ['enabled' => true]]); | ||
} | ||
|
||
public function testItReturnsRedirectPathWithFiltersFromStorageApplied(): void | ||
{ | ||
// Note, it requires an existing route path. | ||
$redirectPath = $this->redirectPathExtension->generateRedirectPath('/books'); | ||
|
||
$this->assertSame('/books?criteria%5Benabled%5D=1', $redirectPath); | ||
} | ||
|
||
public function testItReturnsGivenPathIfRouteHasSomeMoreConfiguration(): void | ||
{ | ||
// TODO ask internal core team member to reproduce this route with "some more configuration" | ||
$redirectPath = $this->redirectPathExtension->generateRedirectPath('/admin/ajax/products/search'); | ||
|
||
$this->assertSame('/admin/ajax/products/search', $redirectPath); | ||
} | ||
|
||
public function testItReturnsGivenPathIfRouteIsNotMatched(): void | ||
{ | ||
$redirectPath = $this->redirectPathExtension->generateRedirectPath('/invalid-path'); | ||
|
||
$this->assertSame('/invalid-path', $redirectPath); | ||
} | ||
|
||
public function testItReturnsNullIfThePathIsNullAsWell(): void | ||
{ | ||
$redirectPath = $this->redirectPathExtension->generateRedirectPath(null); | ||
|
||
$this->assertNull($redirectPath); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters