Skip to content

Commit

Permalink
[LinkGeneration] introduce possibility to disable slugs and use fallb…
Browse files Browse the repository at this point in the history
…ack routes
  • Loading branch information
dpfaffenbauer committed Nov 30, 2022
1 parent e58ecd1 commit e227dc9
Show file tree
Hide file tree
Showing 21 changed files with 327 additions and 12 deletions.
1 change: 0 additions & 1 deletion config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ imports:
- { resource: migrations.yaml }
- { resource: system.yml }
- { resource: 'local/' }

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

/*
* CoreShop
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - CoreShop Commercial License (CCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.org)
* @license https://www.coreshop.org/license GPLv3 and CCL
*
*/

namespace CoreShop\Bundle\CoreBundle\Pimcore\LinkGenerator;

use CoreShop\Component\Pimcore\DataObject\AbstractSluggableLinkGenerator;
use CoreShop\Component\Pimcore\DataObject\InheritanceHelper;
use CoreShop\Component\Pimcore\Exception\LinkGenerationNotPossibleException;
use CoreShop\Component\Resource\Metadata\RegistryInterface;
use Pimcore\Model\DataObject\Concrete;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class ResourceConfigurationLinkGenerator extends AbstractSluggableLinkGenerator
{
public function __construct(
private UrlGeneratorInterface $urlGenerator,
private RegistryInterface $registry
) {
}

public function generate(Concrete $object, array $params = []): string
{
if (!$this->registry->hasClass($object::class)) {
throw new LinkGenerationNotPossibleException();
}

$metadata = $this->registry->getByClass($object::class);

if (!$metadata->hasParameter('route')) {
throw new LinkGenerationNotPossibleException();
}

$routeName = $metadata->getParameter('route')['name'];
$idParam = $metadata->getParameter('route')['id_param'];

$locale = $params['_locale'] ?? null;

$name = InheritanceHelper::useInheritedValues(static function () use ($object, $locale) {
if (method_exists($object, 'getName')) {
return $object->getName($locale);
}

return '';
});

$routeParams = [
'name' => $this->slugify($name),
$idParam => $object->getId(),
];

if (isset($locale)) {
$routeParams['_locale'] = $locale;
}

if (!isset($params['referenceType'])) {
$params['referenceType'] = UrlGeneratorInterface::ABSOLUTE_PATH;
}

return $this->urlGenerator->generate($params['route'] ?? $routeName, $routeParams, $params['referenceType']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,21 @@ services:
- 'product'
- 'coreshop_product_detail'
- '@router'
- '@CoreShop\Component\Resource\Metadata\RegistryInterface'
- '@CoreShop\Component\Pimcore\Slug\SluggableLinkGenerator'

coreshop.object.link_generator.category:
class: CoreShop\Bundle\CoreBundle\Pimcore\LinkGenerator\DataObjectLinkGenerator
arguments:
- 'category'
- 'coreshop_category_list'
- '@router'
- '@CoreShop\Component\Resource\Metadata\RegistryInterface'
- '@CoreShop\Component\Pimcore\Slug\SluggableLinkGenerator'

CoreShop\Bundle\CoreBundle\Pimcore\LinkGenerator\ResourceConfigurationLinkGenerator:
arguments:
- '@router'
- '@CoreShop\Component\Resource\Metadata\RegistryInterface'
tags:
- { name: coreshop.link_generator, type: resource-configuration, priority: 10 }
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"allowVariants": false,
"showVariants": false,
"generateTypeDeclarations": true,
"linkGeneratorReference": "@CoreShop\\Component\\Pimcore\\Slug\\SluggableLinkGenerator",
"linkGeneratorReference": "@CoreShop\\Component\\Pimcore\\DataObject\\CompositeLinkGenerator",
"layoutDefinitions": {
"fieldtype": "panel",
"labelWidth": 100,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"allowVariants": true,
"showVariants": true,
"generateTypeDeclarations": true,
"linkGeneratorReference": "@CoreShop\\Component\\Pimcore\\Slug\\SluggableLinkGenerator",
"linkGeneratorReference": "@CoreShop\\Component\\Pimcore\\DataObject\\CompositeLinkGenerator",
"layoutDefinitions": {
"fieldtype": "panel",
"labelWidth": 100,
Expand Down Expand Up @@ -1182,8 +1182,6 @@
"icon": null,
"previewUrl": null,
"group": "CoreShop",
"generateTypeDeclarations": true,

"propertyVisibility": {
"grid": {
"id": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ routes:
variables: _locale,product
priority: 3

coreshop_category_list:
pattern: '/(\w+)\/shop\/(.*?)\~c([0-9]+)$/'
reverse: '/%_locale/shop/%name~c%category'
controller: 'CoreShop\Bundle\FrontendBundle\Controller\CategoryController:indexAction'
variables: _locale,name,category
priority: 2

coreshop_product_detail:
pattern: '/(\w+)\/shop\/(.*?)\~p([0-9]+)$/'
reverse: '/%_locale/shop/%name~p%product'
controller: 'CoreShop\Bundle\FrontendBundle\Controller\ProductController:detailAction'
variables: _locale,name,product
priority: 2

coreshop_partial_cart_add:
pattern: '/(\w+)\/_partial\/cart\/add\/([0-9]+)$/'
reverse: '/%_locale/_partial/cart/add/%product'
Expand Down
2 changes: 2 additions & 0 deletions src/CoreShop/Bundle/PimcoreBundle/CoreShopPimcoreBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use Composer\InstalledVersions;
use CoreShop\Bundle\PimcoreBundle\DependencyInjection\Compiler\ExpressionLanguageServicePass;
use CoreShop\Bundle\PimcoreBundle\DependencyInjection\Compiler\LinkGeneratorPass;
use CoreShop\Bundle\PimcoreBundle\DependencyInjection\Compiler\RegisterGridActionPass;
use CoreShop\Bundle\PimcoreBundle\DependencyInjection\Compiler\RegisterGridFilterPass;
use CoreShop\Bundle\PimcoreBundle\DependencyInjection\Compiler\RegisterPimcoreDocumentTagImplementationLoaderPass;
Expand Down Expand Up @@ -76,5 +77,6 @@ public function build(ContainerBuilder $container): void
$container->addCompilerPass(new RegisterPimcoreDocumentTagPass());
$container->addCompilerPass(new ExpressionLanguageServicePass());
$container->addCompilerPass(new RegisterTypeHintRegistriesPass());
$container->addCompilerPass(new LinkGeneratorPass());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

/*
* CoreShop
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - CoreShop Commercial License (CCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.org)
* @license https://www.coreshop.org/license GPLv3 and CCL
*
*/

namespace CoreShop\Bundle\PimcoreBundle\DependencyInjection\Compiler;

use CoreShop\Component\Pimcore\DataObject\CompositeLinkGenerator;
use CoreShop\Component\Registry\PrioritizedCompositeServicePass;

final class LinkGeneratorPass extends PrioritizedCompositeServicePass
{
public const LINK_GENERATOR_TAG = 'coreshop.link_generator';

public function __construct()
{
parent::__construct(
CompositeLinkGenerator::class,
CompositeLinkGenerator::class,
self::LINK_GENERATOR_TAG,
'addContext',
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ imports:
- { resource: services/grid.yml }
- { resource: services/dynamic_dropdown.yml }
- { resource: services/pimcore.yml }
- { resource: services/link_generator.yaml }

parameters:
coreshop.admin.route.base: /admin/coreshop
Expand Down Expand Up @@ -83,10 +84,12 @@ services:
- { name: kernel.event_subscriber }

CoreShop\Component\Pimcore\Slug\SluggableLinkGenerator:
public: true
arguments:
- '@Pimcore\Http\Request\Resolver\SiteResolver'
- '@Symfony\Component\HttpFoundation\RequestStack'
- '@CoreShop\Component\Resource\Metadata\RegistryInterface'
tags:
- { name: coreshop.link_generator, type: sluggable, priority: 20 }

CoreShop\Component\Pimcore\Twig\Extension\DataObjectRoutingExtension:
tags:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
services:
coreshop.registry.link_generators:
class: CoreShop\Component\Registry\PrioritizedServiceRegistry
arguments:
- Pimcore\Model\DataObject\ClassDefinition\LinkGeneratorInterface
- link-generators
tags:
- { name: coreshop.registry, type_hint: linkGenerators }

CoreShop\Component\Pimcore\DataObject\CompositeLinkGenerator:
public: true
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,14 @@ private function addModelsSection(ArrayNodeDefinition $node): void
->children()
->variableNode('options')->end()
->scalarNode('path')->defaultValue('products')->end()
->booleanNode('slug')->defaultTrue()->end()
->arrayNode('route')
->addDefaultsIfNotSet()
->children()
->scalarNode('name')->defaultValue('coreshop_product_detail')->end()
->scalarNode('id_param')->defaultValue('product')->end()
->end()
->end()
->arrayNode('classes')
->addDefaultsIfNotSet()
->children()
Expand All @@ -275,6 +283,14 @@ private function addModelsSection(ArrayNodeDefinition $node): void
->children()
->variableNode('options')->end()
->scalarNode('path')->defaultValue('categories')->end()
->booleanNode('slug')->defaultTrue()->end()
->arrayNode('route')
->addDefaultsIfNotSet()
->children()
->scalarNode('name')->defaultValue('coreshop_category_list')->end()
->scalarNode('id_param')->defaultValue('category')->end()
->end()
->end()
->arrayNode('classes')
->addDefaultsIfNotSet()
->children()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"allowVariants": false,
"showVariants": false,
"generateTypeDeclarations": true,
"linkGeneratorReference": "@CoreShop\\Component\\Pimcore\\Slug\\SluggableLinkGenerator",
"linkGeneratorReference": "@CoreShop\\Component\\Pimcore\\DataObject\\CompositeLinkGenerator",
"layoutDefinitions": {
"fieldtype": "panel",
"labelWidth": 100,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"allowVariants": false,
"showVariants": false,
"generateTypeDeclarations": true,
"linkGeneratorReference": "@CoreShop\\Component\\Pimcore\\Slug\\SluggableLinkGenerator",
"linkGeneratorReference": "@CoreShop\\Component\\Pimcore\\DataObject\\CompositeLinkGenerator",
"layoutDefinitions": {
"fieldtype": "panel",
"labelWidth": 100,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,3 @@ services:
- '@CoreShop\Component\Pimcore\DataObject\ObjectServiceInterface'

CoreShop\Bundle\ResourceBundle\Pimcore\CacheResourceMarshaller: ~

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

/*
* CoreShop
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - CoreShop Commercial License (CCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.org)
* @license https://www.coreshop.org/license GPLv3 and CCL
*
*/

namespace CoreShop\Bundle\ResourceBundle\Slug;

use CoreShop\Component\Pimcore\Slug\DataObjectSlugGeneratorInterface;
use CoreShop\Component\Pimcore\Slug\SluggableInterface;
use CoreShop\Component\Resource\Metadata\RegistryInterface;

class ResourceConfigurationSlugGenerator implements DataObjectSlugGeneratorInterface
{
public function __construct(
private DataObjectSlugGeneratorInterface $inner,
private RegistryInterface $metadataRegistry,
) {
}

public function generateSlugs(SluggableInterface $sluggable): void
{
if ($this->metadataRegistry->hasClass($sluggable::class)) {
$metadata = $this->metadataRegistry->getByClass($sluggable::class);

if ($metadata->hasParameter('slug') && !$metadata->getParameter('slug')) {
return;
}
}

$this->inner->generateSlugs($sluggable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ services:
- { name: validator.constraint_validator, alias: coreshop_variant_valid_attributes }

CoreShop\Bundle\VariantBundle\Pimcore\VariantLinkGenerator:
decorates: 'CoreShop\Component\Pimcore\Slug\SluggableLinkGenerator'
decorates: 'CoreShop\Component\Pimcore\DataObject\CompositeLinkGenerator'
arguments:
- '@CoreShop\Bundle\VariantBundle\Pimcore\VariantLinkGenerator.inner'
- '%coreshop.variant.redirect_to_main_variant%'
Loading

0 comments on commit e227dc9

Please sign in to comment.