Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add redirect path Twig extension #43

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"laminas/laminas-stdlib": "^3.18",
"pagerfanta/doctrine-orm-adapter": "^4.6",
"pagerfanta/twig": "^4.6",
"sylius/grid-bundle": "^1.13@alpha",
"sylius/resource-bundle": "^1.11 || ^1.12@alpha",
"sylius/grid-bundle": "^1.13@beta",
"sylius/resource-bundle": "^1.12@beta",
"symfony/asset": "^6.4 || ^7.0",
"symfony/config": "^6.4 || ^7.0",
"symfony/dependency-injection": "^6.4 || ^7.0",
Expand Down
2 changes: 1 addition & 1 deletion src/AdminUi/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"symfony/security-bundle": "^6.4 || ^7.0",
"symfony/security-http": "^6.4 || ^7.0",
"symfony/twig-bundle": "^6.4 || ^7.0",
"sylius/resource-bundle": "^1.11 || ^1.12@alpha",
"sylius/resource-bundle": "^1.12@beta",
"twig/twig": "^2.15 || ^3.0"
},
"require-dev": {
Expand Down
4 changes: 2 additions & 2 deletions src/BootstrapAdminUi/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"php": "^8.1",
"pagerfanta/twig": "^4.6",
"sylius/admin-ui": "dev-main",
"sylius/grid-bundle": "^1.13@alpha",
"sylius/resource-bundle": "^1.11 || ^1.12@alpha",
"sylius/grid-bundle": "^1.13@beta",
"sylius/resource-bundle": "^1.12@beta",
"sylius/twig-extra": "dev-main",
"sylius/twig-hooks": "^0.3 || dev-main",
"symfony/asset": "^6.4 || ^7.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@

{% set configuration = hookable_metadata.context.configuration %}

{#{% set index_url = sylius_generate_redirect_path(app.request.headers.get('referer')|default(path(#}
{# configuration.vars.index.route.name|default(configuration.getRouteName('index')),#}
{# configuration.vars.index.route.parameters|default(configuration.vars.route.parameters|default({}))#}
{#))) %}#}

{% set index_url = path(
{% set index_url = sylius_generate_redirect_path(app.request.headers.get('referer')|default(path(
configuration.vars.index.route.name|default(configuration.getRouteName('index')),
configuration.vars.index.route.parameters|default(configuration.vars.route.parameters|default({}))
) %}
))) %}

{{ button.cancel(sylius_test_form_attribute('cancel-changes-button')|merge({ text: 'sylius.ui.cancel'|trans, url: index_url, class: 'btn' })) }}
9 changes: 8 additions & 1 deletion src/TwigExtra/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@
],
"require": {
"php": "^8.1",
"symfony/form": "^6.4 || ^7.0",
"symfony/http-kernel": "^6.4 || ^7.0",
"symfony/twig-bundle": "^6.4 || ^7.0",
"symfony/ux-twig-component": "^2.17"
},
"require-dev": {
"doctrine/doctrine-bundle": "^2.12",
"doctrine/orm": "^2.0",
"matthiasnoback/symfony-dependency-injection-test": "^5.1",
"phpunit/phpunit": "^9.6",
"sylius/grid-bundle": "^1.13@beta",
"sylius/resource-bundle": "^1.12@beta",
"symfony/browser-kit": "^6.4 || ^7.0",
"symfony/console": "^6.4 || ^7.0",
"symfony/css-selector": "^6.4 || ^7.0",
Expand Down Expand Up @@ -56,5 +61,7 @@
"branch-alias": {
"dev-main": "0.4-dev"
}
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
9 changes: 9 additions & 0 deletions src/TwigExtra/config/services/twig/extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Sylius\TwigExtra\Twig\Extension\MergeRecursiveExtension;
use Sylius\TwigExtra\Twig\Extension\RedirectPathExtension;
use Sylius\TwigExtra\Twig\Extension\RouteExistsExtension;
use Sylius\TwigExtra\Twig\Extension\SortByExtension;
use Sylius\TwigExtra\Twig\Extension\TestFormAttributeExtension;
Expand Down Expand Up @@ -52,4 +53,12 @@
])
->tag(name: 'twig.extension')
;

$services->set('sylius_twig_extra.twig.extension.redirect_path', RedirectPathExtension::class)
->args([
service('router'),
service('sylius.grid.filter_storage')->nullOnInvalid(),
])
->tag(name: 'twig.extension')
;
};
69 changes: 69 additions & 0 deletions src/TwigExtra/src/Twig/Extension/RedirectPathExtension.php
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\TwigExtra\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;
}
}
4 changes: 4 additions & 0 deletions src/TwigExtra/tests/Functional/.application/.env
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@
APP_ENV=dev
APP_SECRET=c9570eaa5cbfadf33ad1a93f95c2e42a
###< symfony/framework-bundle ###

###> doctrine/doctrine-bundle ###
DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
###< doctrine/doctrine-bundle ###
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
Symfony\UX\TwigComponent\TwigComponentBundle::class => ['all' => true],
Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
Sylius\TwigExtra\Symfony\SyliusTwigExtraBundle::class => ['all' => true],
Sylius\Bundle\GridBundle\SyliusGridBundle::class => ['all' => true],
Sylius\Bundle\ResourceBundle\SyliusResourceBundle::class => ['all' => true],
];
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# @see https://github.com/Sylius/SyliusResourceBundle/blob/master/docs/index.md
sylius_resource:
drivers: []

# Override default settings
#settings:

# Configure the mapping for your resources
mapping:
paths:
- '%kernel.project_dir%/src/Resource'

# Configure your resources
resources:
#app.book:
#classes:
#model: App\Entity\Book
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
sylius_crud_routes:
resource: 'sylius.routing.loader.crud_routes_attributes'
type: service

sylius_routes:
resource: 'sylius.routing.loader.routes_attributes'
type: service
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters:
locale: en

services:
# default configuration for services in *this* file
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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 TestApplication\Sylius\TwigExtra\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use TestApplication\Sylius\TwigExtra\Resource\BookResource;

final class BookResourceType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('name')
;
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => BookResource::class,
]);
}
}
59 changes: 59 additions & 0 deletions src/TwigExtra/tests/Functional/.application/src/Grid/BookGrid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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 TestApplication\Sylius\TwigExtra\Grid;

use Sylius\Bundle\GridBundle\Builder\Action\CreateAction;
use Sylius\Bundle\GridBundle\Builder\Action\UpdateAction;
use Sylius\Bundle\GridBundle\Builder\ActionGroup\ItemActionGroup;
use Sylius\Bundle\GridBundle\Builder\ActionGroup\MainActionGroup;
use Sylius\Bundle\GridBundle\Builder\Field\StringField;
use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface;
use Sylius\Bundle\GridBundle\Grid\AbstractGrid;
use Sylius\Bundle\GridBundle\Grid\ResourceAwareGridInterface;
use TestApplication\Sylius\TwigExtra\Grid\Provider\BookGridProvider;
use TestApplication\Sylius\TwigExtra\Resource\BookResource;

final class BookGrid extends AbstractGrid implements ResourceAwareGridInterface
{
public static function getName(): string
{
return 'app_book';
}

public function buildGrid(GridBuilderInterface $gridBuilder): void
{
$gridBuilder
->setProvider(BookGridProvider::class)
->addField(
StringField::create('name')
->setLabel('Name'),
)
->addActionGroup(
MainActionGroup::create(
CreateAction::create(),
),
)
->addActionGroup(
ItemActionGroup::create(
UpdateAction::create(),
),
)
;
}

public function getResourceClass(): string
{
return BookResource::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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 TestApplication\Sylius\TwigExtra\Grid\Provider;

use Pagerfanta\Adapter\ArrayAdapter;
use Pagerfanta\Pagerfanta;
use Sylius\Component\Grid\Data\DataProviderInterface;
use Sylius\Component\Grid\Definition\Grid;
use Sylius\Component\Grid\Parameters;
use TestApplication\Sylius\TwigExtra\Resource\BookResource;

class BookGridProvider implements DataProviderInterface
{
public function getData(Grid $grid, Parameters $parameters): Pagerfanta
{
return new Pagerfanta(new ArrayAdapter([
new BookResource('shinning', 'Shinning'),
new BookResource('carrie', 'Carrie'),
]));
}
}
Loading
Loading