-
-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[LinkGeneration] introduce possibility to disable slugs and use fallb…
…ack routes
- Loading branch information
1 parent
e58ecd1
commit e227dc9
Showing
21 changed files
with
327 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,3 @@ imports: | |
- { resource: migrations.yaml } | ||
- { resource: system.yml } | ||
- { resource: 'local/' } | ||
|
76 changes: 76 additions & 0 deletions
76
src/CoreShop/Bundle/CoreBundle/Pimcore/LinkGenerator/ResourceConfigurationLinkGenerator.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,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']); | ||
} | ||
} |
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
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
37 changes: 37 additions & 0 deletions
37
src/CoreShop/Bundle/PimcoreBundle/DependencyInjection/Compiler/LinkGeneratorPass.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,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', | ||
); | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
src/CoreShop/Bundle/PimcoreBundle/Resources/config/services/link_generator.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,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 |
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
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
45 changes: 45 additions & 0 deletions
45
src/CoreShop/Bundle/ResourceBundle/Slug/ResourceConfigurationSlugGenerator.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,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); | ||
} | ||
} |
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
Oops, something went wrong.