Skip to content

Commit

Permalink
registration of FriendlyUrlDataProviders is now done via service cont…
Browse files Browse the repository at this point in the history
…ainer (#1140)
  • Loading branch information
TomasLudvik committed Jun 28, 2019
2 parents 0cd2af0 + 524dffd commit 439a4e2
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 65 deletions.
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
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
6 changes: 5 additions & 1 deletion docs/introduction/faq-and-common-issues.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ For more detailed information about the Shopsys Framework, please see [Shopsys F
- [How to change the behavior of the product search on the front-end?](#how-to-change-the-behavior-of-the-product-search-on-the-front-end)
- [Why are e-mails not sent immediately but at the end of the script](#why-are-e-mails-sent-before-end-of-the-script-and-not-immediately)
- [Where does the business logic belong?](#where-does-the-business-logic-belong)
- [How can I create a friendly URL for my entity?](#how-can-i-create-a-friendly-url-for-my-entity)

## What are the phing targets?
Every phing target is a task that can be executed simply by `php phing <target-name>` command.
Expand Down Expand Up @@ -117,4 +118,7 @@ It is also possible to turn the spool off by removing it from [swiftmailer.yml](

## Where does the business logic belong?
The business logic should be implemented directly in an entity every time when there is no need for external services.
Otherwise, the logic is in facades (resp. the facades are used as delegates to other services, e.g. another *Facade*, *Repository*, *Calculation*, etc.). You can read more about the model architecture in [Introduction to model architecture](/docs/model/introduction-to-model-architecture.md).
Otherwise, the logic is in facades (resp. the facades are used as delegates to other services, e.g. another *Facade*, *Repository*, *Calculation*, etc.). You can read more about the model architecture in [Introduction to model architecture](/docs/model/introduction-to-model-architecture.md).

## How can I create a friendly URL for my entity?
See [Friendly URL](/docs/introduction/friendly-url.md#friendly-url) article.
36 changes: 36 additions & 0 deletions docs/introduction/friendly-url.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 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. `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](/packages/framework/src/Model/Product/ProductDetailFriendlyUrlDataProvider.php) 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`)*
- update your `<entity_name>Facade`:
- in `create` and `edit` methods, after calling `$this->em->flush()`, add similar code like this:
```
$this->friendlyUrlFacade->createFriendlyUrls('front_<entity_name>_detail', $entity->getId(), $entity->getNames());
```
- third parameter is an array of names indexed by locale that will be used for URL generation (e.g. 'cs' => 'Televize', 'en' => 'Television')
- this way you will have always generated friendly URLs based on your entity name
- if the name of the entity will be changed, the old URLs will now redirect to your new URLs
- run `php phing friendly-urls-generate` to generate new URLs
- visit some of provided URLs and check if everything works fine

*Note: You can use [`UrlListType` in your forms](https://github.com/shopsys/shopsys/blob/master/docs/introduction/using-form-types.md#urllisttype) to edit friendly URLs of existing entities.*
*If you're interested, you can take a look at the processing of `Article` entity (see `ArticleFacade`, `ArticleData::$urls`, `ArticleDataFactory` and `ArticleFormType`), which allows for this functionality.*
4 changes: 4 additions & 0 deletions docs/upgrade/UPGRADE-v8.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ 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))
- JS functionality connected to `#js-close-without-saving` has been removed, implement your own if you relied on this ([#1168](https://github.com/shopsys/shopsys/pull/1168))
- update your way of registration of `FriendlyUrlDataProviders` ([#1140](https://github.com/shopsys/shopsys/pull/1140))
- the namespace of `FriendlyUrlDataProviderInterface` and `FriendlyUrlDataProviderRegistry` has changed from `Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\CompilerPass` to `Shopsys\FrameworkBundle\Component\Router\FriendlyUrl` so change all your usages accordingly
- you no longer need to tag your `FriendlyUrlDataProviders` with `shopsys.friendly_url_provider` because it is now done automatically
- remove the usages of `RegisterFriendlyUrlDataProviderCompilerPass` class and `FriendlyUrlDataProviderRegistry::registerFriendlyUrlDataProvider` method because they have been removed

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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
<?php

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

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

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;
Assert::allIsInstanceOf($friendlyUrlDataProviders, FriendlyUrlDataProviderInterface::class);

$this->friendlyUrlDataProviders = $friendlyUrlDataProviders;
}

/**
Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Shopsys\FrameworkBundle\Component\Environment\EnvironmentType;
use Shopsys\FrameworkBundle\Component\Grid\InlineEdit\GridInlineEditInterface;
use Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlDataProviderInterface;
use Shopsys\FrameworkBundle\Twig\NoVarDumperExtension;
use Shopsys\FrameworkBundle\Twig\VarDumperExtension;
use Symfony\Component\Config\FileLocator;
Expand Down Expand Up @@ -35,6 +36,9 @@ public function load(array $configs, ContainerBuilder $container)

$container->registerForAutoconfiguration(GridInlineEditInterface::class)
->addTag('shopsys.grid_inline_edit');

$container->registerForAutoconfiguration(FriendlyUrlDataProviderInterface::class)
->addTag('shopsys.friendly_url_provider');
}

/**
Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
19 changes: 3 additions & 16 deletions packages/framework/src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,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 Expand Up @@ -669,10 +672,6 @@ services:
Shopsys\FrameworkBundle\Model\Article\ArticleFactoryInterface:
alias: Shopsys\FrameworkBundle\Model\Article\ArticleFactory

Shopsys\FrameworkBundle\Model\Article\ArticleDetailFriendlyUrlDataProvider:
tags:
- {name: shopsys.friendly_url_provider}

Shopsys\FrameworkBundle\Model\Product\Availability\AvailabilityFactoryInterface:
alias: Shopsys\FrameworkBundle\Model\Product\Availability\AvailabilityFactory

Expand All @@ -682,10 +681,6 @@ services:
Shopsys\FrameworkBundle\Model\Product\Brand\BrandFactoryInterface:
alias: Shopsys\FrameworkBundle\Model\Product\Brand\BrandFactory

Shopsys\FrameworkBundle\Model\Product\Brand\BrandDetailFriendlyUrlDataProvider:
tags:
- {name: shopsys.friendly_url_provider}

Shopsys\FrameworkBundle\Model\Cart\Item\CartItemFactoryInterface:
alias: Shopsys\FrameworkBundle\Model\Cart\Item\CartItemFactory

Expand Down Expand Up @@ -760,14 +755,6 @@ services:
Shopsys\FrameworkBundle\Model\Product\ProductFactoryInterface:
alias: Shopsys\FrameworkBundle\Model\Product\ProductFactory

Shopsys\FrameworkBundle\Model\Product\ProductDetailFriendlyUrlDataProvider:
tags:
- {name: shopsys.friendly_url_provider}

Shopsys\FrameworkBundle\Model\Product\ProductListFriendlyUrlDataProvider:
tags:
- {name: shopsys.friendly_url_provider}

Shopsys\FrameworkBundle\Model\Product\Accessory\ProductAccessoryFactoryInterface:
alias: Shopsys\FrameworkBundle\Model\Product\Accessory\ProductAccessoryFactory

Expand Down
2 changes: 0 additions & 2 deletions packages/framework/src/ShopsysFrameworkBundle.php
Original file line number Diff line number Diff line change
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

0 comments on commit 439a4e2

Please sign in to comment.