Skip to content

Commit

Permalink
set controller as service (#173)
Browse files Browse the repository at this point in the history
* set controller as service

* ParameterBag service no exist in Symfony 3.4

* use ParameterBag instead of container service

* add ParameterBag comment

* explode services for export

* remove entityManager not used

* fix unused param in service

* update README

* fix wording
  • Loading branch information
oallain authored and lsmith77 committed Apr 17, 2019
1 parent 2b5bb2c commit 5463140
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 54 deletions.
35 changes: 32 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,26 @@ app_export_data_foo:
methods: [GET]
defaults:
resource: sylius.foo
_controller: sylius.controller.export_data:exportAction
_controller: sylius.controller.export_data_foo:exportAction
_sylius:
filterable: true
grid: sylius_admin_foo # Name of defined grid here
```
And add the associated controller service definition to your services

```yaml
sylius.controller.export_data_foo:
public: true
class: FriendsOfSylius\SyliusImportExportPlugin\Controller\ExportDataController
arguments:
- "@sylius.exporters_registry"
- "@sylius.resource_controller.request_configuration_factory"
- "@sylius.resource_controller.resources_collection_provider"
- "@sylius.repository.foo"
- "%sylius.resources%"
tags: ['controller.service_arguments']
```


In case you don't add it, the UI exporters will still function. They will simply load all data of that resource for the export (similar as CLI).

Expand Down Expand Up @@ -368,12 +383,26 @@ app_export_data_country:
methods: [GET]
defaults:
resource: sylius.country
_controller: sylius.controller.export_data:exportAction
_controller: sylius.controller.export_data_contry:exportAction
_sylius:
filterable: true
grid: sylius_admin_country
```


And add the associated controller service definition to your services

```yaml
sylius.controller.export_data_country:
public: true
class: FriendsOfSylius\SyliusImportExportPlugin\Controller\ExportDataController
arguments:
- "@sylius.exporters_registry"
- "@sylius.resource_controller.request_configuration_factory"
- "@sylius.resource_controller.resources_collection_provider"
- "@sylius.repository.country"
- "%sylius.resources%"
tags: ['controller.service_arguments']
```

### PluginPool
The idea behind the plugin pool is, to be able to have different kind of plugins, which could possibly
Expand Down
18 changes: 10 additions & 8 deletions spec/Controller/ExportDataControllerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,27 @@
use Sylius\Bundle\ResourceBundle\Controller\RequestConfigurationFactoryInterface;
use Sylius\Bundle\ResourceBundle\Controller\ResourcesCollectionProviderInterface;
use Sylius\Component\Registry\ServiceRegistryInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sylius\Component\Resource\Repository\RepositoryInterface;

class ExportDataControllerSpec extends ObjectBehavior
{
function let(
ServiceRegistryInterface $registry,
RequestConfigurationFactoryInterface $requestConfigurationFactory,
ResourcesCollectionProviderInterface $resourcesCollectionProvider
ResourcesCollectionProviderInterface $resourcesCollectionProvider,
RepositoryInterface $repository
) {
$this->beConstructedWith($registry, $requestConfigurationFactory, $resourcesCollectionProvider);
$this->beConstructedWith(
$registry,
$requestConfigurationFactory,
$resourcesCollectionProvider,
$repository,
[]
);
}

function it_is_initializable()
{
$this->shouldHaveType(ExportDataController::class);
}

function it_implements_the_plugin_pool_interface()
{
$this->shouldImplement(Controller::class);
}
}
33 changes: 33 additions & 0 deletions spec/Controller/ImportDataControllerSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace spec\FriendsOfSylius\SyliusImportExportPlugin\Controller;

use FriendsOfSylius\SyliusImportExportPlugin\Controller\ImportDataController;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Registry\ServiceRegistryInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;

class ImportDataControllerSpec extends ObjectBehavior
{
function let(
ServiceRegistryInterface $registry,
FlashBagInterface $flashBag,
FormFactoryInterface $formFactory,
\Twig_Environment $twig
) {
$this->beConstructedWith(
$registry,
$flashBag,
$formFactory,
$twig
);
}

function it_is_initializable()
{
$this->shouldHaveType(ImportDataController::class);
}
}
33 changes: 14 additions & 19 deletions src/Controller/ExportDataController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@
use Sylius\Component\Resource\Metadata\Metadata;
use Sylius\Component\Resource\Model\ResourceInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;

final class ExportDataController extends Controller
final class ExportDataController
{
/** @var array */
private $resources;

/** @var RepositoryInterface */
private $repository;

/** @var ServiceRegistryInterface */
private $registry;

Expand All @@ -35,11 +40,15 @@ final class ExportDataController extends Controller
public function __construct(
ServiceRegistryInterface $registry,
RequestConfigurationFactoryInterface $requestConfigurationFactory,
ResourcesCollectionProviderInterface $resourcesCollectionProvider
ResourcesCollectionProviderInterface $resourcesCollectionProvider,
RepositoryInterface $repository,
array $resources
) {
$this->registry = $registry;
$this->requestConfigurationFactory = $requestConfigurationFactory;
$this->resourcesCollectionProvider = $resourcesCollectionProvider;
$this->repository = $repository;
$this->resources = $resources;
}

public function exportAction(Request $request, string $resource, string $format): Response
Expand All @@ -51,9 +60,8 @@ public function exportAction(Request $request, string $resource, string $format)

private function exportData(Request $request, string $exporter, string $format, string $outputFilename): Response
{
[$applicationName, $resource] = explode('.', $exporter);
$metadata = Metadata::fromAliasAndConfiguration($exporter,
$this->container->getParameter('sylius.resources')[$exporter]);
$this->resources[$exporter]);
$configuration = $this->requestConfigurationFactory->create($metadata, $request);

$name = ExporterRegistry::buildServiceName($exporter, $format);
Expand All @@ -63,7 +71,7 @@ private function exportData(Request $request, string $exporter, string $format,
/** @var ResourceExporterInterface $service */
$service = $this->registry->get($name);

$resources = $this->findResources($configuration, $this->findRepository($resource));
$resources = $this->findResources($configuration, $this->repository);
$service->export($this->getResourceIds($resources));

$response = new Response($service->getExportedData());
Expand All @@ -76,19 +84,6 @@ private function exportData(Request $request, string $exporter, string $format,
return $response;
}

private function findRepository(string $resource): RepositoryInterface
{
$repositoryName = sprintf('sylius.repository.%s', $resource);
if (!$this->has($repositoryName)) {
throw new \Exception(sprintf("No repository found with id '%s'", $repositoryName));
}

/** @var \Sylius\Component\Resource\Repository\RepositoryInterface $repository */
$repository = $this->get($repositoryName);

return $repository;
}

/**
* @param ResourceGridView|array $resources
*
Expand Down
22 changes: 11 additions & 11 deletions src/Controller/ImportDataController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@
use FriendsOfSylius\SyliusImportExportPlugin\Form\ImportType;
use FriendsOfSylius\SyliusImportExportPlugin\Importer\ImporterInterface;
use FriendsOfSylius\SyliusImportExportPlugin\Importer\ImporterRegistry;
use Sylius\Component\Registry\ServiceRegistry;
use Sylius\Component\Registry\ServiceRegistryInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;

final class ImportDataController
{
/** @var ServiceRegistry */
private $registry;
/** @var FlashBagInterface */
private $flashBag;

/** @var Session */
private $session;
/** @var ServiceRegistryInterface */
private $registry;

/** @var FormFactoryInterface */
private $formFactory;
Expand All @@ -32,15 +32,15 @@ final class ImportDataController
private $twig;

public function __construct(
ServiceRegistry $registry,
Session $session,
ServiceRegistryInterface $registry,
FlashBagInterface $flashBag,
FormFactoryInterface $formFactory,
\Twig_Environment $twig
) {
$this->registry = $registry;
$this->session = $session;
$this->formFactory = $formFactory;
$this->twig = $twig;
$this->flashBag = $flashBag;
}

public function importFormAction(Request $request): Response
Expand Down Expand Up @@ -81,7 +81,7 @@ private function importData(string $importer, FormInterface $form): void
$name = ImporterRegistry::buildServiceName($importer, $format);
if (!$this->registry->has($name)) {
$message = sprintf("No importer found of type '%s' for format '%s'", $importer, $format);
$this->session->getFlashBag()->add('error', $message);
$this->flashBag->add('error', $message);
}

/** @var UploadedFile $file */
Expand All @@ -103,6 +103,6 @@ private function importData(string $importer, FormInterface $form): void
count($result->getFailedRows())
);

$this->session->getFlashBag()->add('success', $message);
$this->flashBag->add('success', $message);
}
}
49 changes: 49 additions & 0 deletions src/DependencyInjection/ParameterBag/ParameterBag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This is file is required as long as SyliusImportExportPlugin supports Symfony versions lower than 4.1. Before this version, ParameterBag wasn't a service available.
*
* See: https://symfony.com/blog/new-in-symfony-4-1-getting-container-parameters-as-a-service
*/

declare(strict_types=1);

namespace FriendsOfSylius\SyliusImportExportPlugin\DependencyInjection\ParameterBag;

use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

final class ParameterBag extends FrozenParameterBag implements ParameterBagInterface
{
private $container;

public function __construct(Container $container)
{
$this->container = $container;
}

/**
* {@inheritdoc}
*/
public function all()
{
return $this->container->getParameterBag()->all();
}

/**
* {@inheritdoc}
*/
public function get($name)
{
return $this->container->getParameter($name);
}

/**
* {@inheritdoc}
*/
public function has($name)
{
return $this->container->hasParameter($name);
}
}
17 changes: 6 additions & 11 deletions src/Resources/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@ app_import_data:

## Export routes for specific settings
app_export_data_country:
path: /export/sylius.country/{format}
path: /export/{resource_country}/{format}
methods: [GET]
defaults:
resource: sylius.country
_controller: sylius.controller.export_data:exportAction
_controller: sylius.controller.export_data_country:exportAction
_sylius:
filterable: true
grid: sylius_admin_country

app_export_data_order:
path: /export/sylius.order/{format}
path: /export/{resource_order}/{format}
methods: [GET]
defaults:
resource: sylius.order
_controller: sylius.controller.export_data:exportAction
_controller: sylius.controller.export_data_order:exportAction
_sylius:
filterable: true
grid: sylius_admin_order

app_export_data_customer:
path: /export/sylius.customer/{format}
path: /export/{resource_customer}/{format}
methods: [GET]
defaults:
resource: sylius.customer
_controller: sylius.controller.export_data:exportAction
_controller: sylius.controller.export_data_customer:exportAction
_sylius:
filterable: true
grid: sylius_admin_customer
Expand All @@ -40,8 +40,3 @@ app_export_data_customer:
app_export_data:
path: /export/{resource}/{format}
methods: [GET]
defaults:
_controller: sylius.controller.export_data:exportAction
_sylius:
paginate: false
filterable: false
Loading

0 comments on commit 5463140

Please sign in to comment.