Skip to content

Commit

Permalink
registering of FriendlyUrlDataProviders is now done via service conta…
Browse files Browse the repository at this point in the history
…iner

- documentation about friendly url has been added
  • Loading branch information
TomasLudvik committed Jun 27, 2019
1 parent 7221484 commit 79de76b
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 50 deletions.
1 change: 1 addition & 0 deletions docs/index.md
Expand Up @@ -24,6 +24,7 @@
* [Directories](./introduction/directories.md)
* [Cron](./introduction/cron.md)
* [Using Form Types](./introduction/using-form-types.md)
* [Friendly URL](./introduction/friendly-url.md)

## Model
* [Introduction to Model Architecture](./model/introduction-to-model-architecture.md)
Expand Down
24 changes: 24 additions & 0 deletions docs/introduction/friendly-url.md
@@ -0,0 +1,24 @@
# Friendly URL

Shopsys Framework comes with the implementation to support custom URLs for entities or other use you might need.
By default, there are custom URLs implemented for `Product` detail, `Product` list, `Article` detail and `Brand` detail pages.
Thanks to this functionality you can set your own URL or set of URLs to these entities.
This functionality is provided by `FriendlyUrlDataProviderInterface` implementations, e.g. `\Shopsys\FrameworkBundle\Model\Product\ProductDetailFriendlyUrlDataProvider`.
Such classes are automatically registered in `FriendlyUrlDataProviderRegistry`.
The rest of the work is done automatically and URLs provided by these providers are now accessible via browser.

## How to create new `FriendlyUrlDataProvider`

- create new class with name ending with `FriendlyUrlDataProvider`
- this class has to implement `FriendlyUrlDataProviderInterface`
- this interface requires you to implement two methods *(see `ProductDetailFriendlyUrlDataProvider` class as an example of the implementation)*:
- `getFriendlyUrlData` method that generates `FriendlyUrlData` for all your entities
- `getRouteName` method returns name of route that you have to declare in `routing_friendly_url.yml` file like:
```
front_<entity_name>_detail:
path: friendly-url
defaults: { _controller: ShopsysShopBundle:Front\<entity_name>:detail }
```
this will route all URLs you have provided in `getFriendlyUrlData` method to `<entity_name>Controller::detailAction` method and provide you with ID of entity matching that URL
*(see [Symfony Documentation](https://symfony.com/doc/3.4/controller.html) to learn how to create new `Controller`)*
- visit some of provided URLs and check if everything works fine
14 changes: 14 additions & 0 deletions docs/upgrade/UPGRADE-v8.0.0.md
Expand Up @@ -86,6 +86,20 @@ There you can find links to upgrade notes for other versions too.
```
- check and fix your other tests, they might start failing if they assumed `Product::$availability` is not null when the product is using stock, or that stock quantity is not null when it's not using stock
- follow upgrade instructions for entities simplification in the [separate article](./upgrade-instructions-for-entities-simplification.md) ([#1123](https://github.com/shopsys/shopsys/pull/1123))
- the namespace of `FriendlyUrlDataProviderInterface` has changed from `Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\CompilerPass` to `Shopsys\FrameworkBundle\Component\Router\FriendlyUrl` so change all your usages accordingly ([#1140](https://github.com/shopsys/shopsys/pull/1140))
- the namespace of `FriendlyUrlDataProviderRegistry` has changed from `Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\CompilerPass` to `Shopsys\FrameworkBundle\Component\Router\FriendlyUrl` so change all your usages accordingly
- update your `service.yml` accordingly:
```diff
services:
_defaults:
autowire: true
autoconfigure: true
public: false

+ _instanceof:
+ Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\CompilerPass\FriendlyUrlDataProviderInterface:
+ tags: ['shopsys.friendly_url_provider']
```

### Configuration
- simplify local configuration ([#1004](https://github.com/shopsys/shopsys/pull/1004))
Expand Down

This file was deleted.

@@ -0,0 +1,13 @@
<?php

namespace Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\Exception;

use Exception;

class FriendlyUrlDataProviderMustImplementFriendlyUrlDataProviderInterfaceException extends Exception implements FriendlyUrlException
{
public function __construct()
{
parent::__construct('FriendlyUrlDataProvider must implement FriendlyUrlDataProviderInterface.');
}
}
@@ -1,6 +1,6 @@
<?php

namespace Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\CompilerPass;
namespace Shopsys\FrameworkBundle\Component\Router\FriendlyUrl;

use Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig;

Expand Down
@@ -1,27 +1,23 @@
<?php

namespace Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\CompilerPass;
namespace Shopsys\FrameworkBundle\Component\Router\FriendlyUrl;

use Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig;
use Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\Exception\FriendlyUrlDataProviderMustImplementFriendlyUrlDataProviderInterfaceException;

class FriendlyUrlDataProviderRegistry
{
/**
* @var \Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\CompilerPass\FriendlyUrlDataProviderInterface[]
* @var \Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlDataProviderInterface[]
*/
protected $friendlyUrlDataProviders;

public function __construct()
{
$this->friendlyUrlDataProviders = [];
}

/**
* @param \Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\CompilerPass\FriendlyUrlDataProviderInterface $service
* @param \Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlDataProviderInterface[] $friendlyUrlDataProviders
*/
public function registerFriendlyUrlDataProvider(FriendlyUrlDataProviderInterface $service)
public function __construct(iterable $friendlyUrlDataProviders)
{
$this->friendlyUrlDataProviders[] = $service;
$this->registerFriendlyUrlDataProviders($friendlyUrlDataProviders);
}

/**
Expand All @@ -39,4 +35,18 @@ public function getFriendlyUrlDataByRouteAndDomain($routeName, DomainConfig $dom

throw new \Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\Exception\FriendlyUrlRouteNotSupportedException($routeName);
}

/**
* @param iterable $friendlyUrlDataProviders
*/
protected function registerFriendlyUrlDataProviders(iterable $friendlyUrlDataProviders): void
{
foreach ($this->friendlyUrlDataProviders as $friendlyUrlDataProvider) {
if (!$friendlyUrlDataProvider instanceof FriendlyUrlDataProviderInterface) {
throw new FriendlyUrlDataProviderMustImplementFriendlyUrlDataProviderInterfaceException();
}
}

$this->friendlyUrlDataProviders = $friendlyUrlDataProviders;
}
}
Expand Up @@ -5,7 +5,6 @@
use Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig;
use Shopsys\FrameworkBundle\Component\Domain\Domain;
use Shopsys\FrameworkBundle\Component\Router\DomainRouterFactory;
use Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\CompilerPass\FriendlyUrlDataProviderRegistry;
use Symfony\Component\Console\Output\OutputInterface;

class FriendlyUrlGeneratorFacade
Expand All @@ -26,15 +25,15 @@ class FriendlyUrlGeneratorFacade
protected $friendlyUrlFacade;

/**
* @var \Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\CompilerPass\FriendlyUrlDataProviderRegistry
* @var \Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlDataProviderRegistry
*/
protected $friendlyUrlDataProviderConfig;

/**
* @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain
* @param \Shopsys\FrameworkBundle\Component\Router\DomainRouterFactory $domainRouterFactory
* @param \Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlFacade $friendlyUrlFacade
* @param \Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\CompilerPass\FriendlyUrlDataProviderRegistry $friendlyUrlDataProviderConfig
* @param \Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlDataProviderRegistry $friendlyUrlDataProviderConfig
*/
public function __construct(
Domain $domain,
Expand Down
Expand Up @@ -5,9 +5,9 @@
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query\Expr\Join;
use Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig;
use Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\CompilerPass\FriendlyUrlDataProviderInterface;
use Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrl;
use Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlDataFactoryInterface;
use Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlDataProviderInterface;

class ArticleDetailFriendlyUrlDataProvider implements FriendlyUrlDataProviderInterface
{
Expand Down
Expand Up @@ -5,9 +5,9 @@
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query\Expr\Join;
use Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig;
use Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\CompilerPass\FriendlyUrlDataProviderInterface;
use Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrl;
use Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlDataFactoryInterface;
use Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlDataProviderInterface;

class BrandDetailFriendlyUrlDataProvider implements FriendlyUrlDataProviderInterface
{
Expand Down
Expand Up @@ -5,9 +5,9 @@
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query\Expr\Join;
use Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig;
use Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\CompilerPass\FriendlyUrlDataProviderInterface;
use Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrl;
use Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlDataFactoryInterface;
use Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlDataProviderInterface;

class ProductDetailFriendlyUrlDataProvider implements FriendlyUrlDataProviderInterface
{
Expand Down
Expand Up @@ -5,9 +5,9 @@
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query\Expr\Join;
use Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig;
use Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\CompilerPass\FriendlyUrlDataProviderInterface;
use Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrl;
use Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlDataFactoryInterface;
use Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlDataProviderInterface;
use Shopsys\FrameworkBundle\Model\Category\Category;

class ProductListFriendlyUrlDataProvider implements FriendlyUrlDataProviderInterface
Expand Down
5 changes: 4 additions & 1 deletion packages/framework/src/Resources/config/services.yml
Expand Up @@ -17,7 +17,7 @@ services:
public: false

_instanceof:
Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\CompilerPass\FriendlyUrlDataProviderInterface:
Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlDataProviderInterface:
tags: ['shopsys.friendly_url_provider']

Shopsys\FrameworkBundle\:
Expand Down Expand Up @@ -528,6 +528,9 @@ services:
calls:
- method: setRequestStack

Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlDataProviderRegistry:
arguments: [!tagged shopsys.friendly_url_provider]

Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlRouterFactory:
arguments: ['%shopsys.router.friendly_url_router_filepath%']

Expand Down
2 changes: 0 additions & 2 deletions packages/framework/src/ShopsysFrameworkBundle.php
Expand Up @@ -2,7 +2,6 @@

namespace Shopsys\FrameworkBundle;

use Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\CompilerPass\RegisterFriendlyUrlDataProviderCompilerPass;
use Shopsys\FrameworkBundle\DependencyInjection\Compiler\LazyRedisCompilerPass;
use Shopsys\FrameworkBundle\DependencyInjection\Compiler\RegisterCronModulesCompilerPass;
use Shopsys\FrameworkBundle\DependencyInjection\Compiler\RegisterPluginCrudExtensionsCompilerPass;
Expand All @@ -21,7 +20,6 @@ public function build(ContainerBuilder $container)
parent::build($container);

$container->addCompilerPass(new RegisterCronModulesCompilerPass());
$container->addCompilerPass(new RegisterFriendlyUrlDataProviderCompilerPass());
$container->addCompilerPass(new RegisterPluginCrudExtensionsCompilerPass());
$container->addCompilerPass(new RegisterPluginDataFixturesCompilerPass());
$container->addCompilerPass(new RegisterProductFeedConfigsCompilerPass());
Expand Down
Expand Up @@ -9,7 +9,7 @@ services:
public: false

_instanceof:
Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\CompilerPass\FriendlyUrlDataProviderInterface:
Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlDataProviderInterface:
tags: ['shopsys.friendly_url_provider']

Shopsys\ShopBundle\Controller\:
Expand Down

0 comments on commit 79de76b

Please sign in to comment.