diff --git a/README.md b/README.md index da60b803..0680a497 100644 --- a/README.md +++ b/README.md @@ -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). @@ -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 diff --git a/spec/Controller/ExportDataControllerSpec.php b/spec/Controller/ExportDataControllerSpec.php index 80820cd7..8a4a12bc 100644 --- a/spec/Controller/ExportDataControllerSpec.php +++ b/spec/Controller/ExportDataControllerSpec.php @@ -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); - } } diff --git a/spec/Controller/ImportDataControllerSpec.php b/spec/Controller/ImportDataControllerSpec.php new file mode 100644 index 00000000..dcd02391 --- /dev/null +++ b/spec/Controller/ImportDataControllerSpec.php @@ -0,0 +1,33 @@ +beConstructedWith( + $registry, + $flashBag, + $formFactory, + $twig + ); + } + + function it_is_initializable() + { + $this->shouldHaveType(ImportDataController::class); + } +} diff --git a/src/Controller/ExportDataController.php b/src/Controller/ExportDataController.php index 2656a453..c3a57dac 100644 --- a/src/Controller/ExportDataController.php +++ b/src/Controller/ExportDataController.php @@ -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; @@ -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 @@ -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); @@ -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()); @@ -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 * diff --git a/src/Controller/ImportDataController.php b/src/Controller/ImportDataController.php index b55006a8..ca430090 100644 --- a/src/Controller/ImportDataController.php +++ b/src/Controller/ImportDataController.php @@ -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; @@ -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 @@ -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 */ @@ -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); } } diff --git a/src/DependencyInjection/ParameterBag/ParameterBag.php b/src/DependencyInjection/ParameterBag/ParameterBag.php new file mode 100644 index 00000000..1928191b --- /dev/null +++ b/src/DependencyInjection/ParameterBag/ParameterBag.php @@ -0,0 +1,49 @@ +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); + } +} diff --git a/src/Resources/config/routing.yml b/src/Resources/config/routing.yml index 782c7007..56340f5c 100644 --- a/src/Resources/config/routing.yml +++ b/src/Resources/config/routing.yml @@ -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 @@ -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 diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index 377b41de..b9bdcd94 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -1,21 +1,55 @@ services: + # Parameter Bag + sylius.parameter_bag: + class: FriendsOfSylius\SyliusImportExportPlugin\DependencyInjection\ParameterBag\ParameterBag + arguments: + - "@service_container" + # Controllers for Admin-Integration sylius.controller.import_data: public: true class: FriendsOfSylius\SyliusImportExportPlugin\Controller\ImportDataController arguments: - "@sylius.importers_registry" - - "@session" + - "@session.flash_bag" - "@form.factory" - "@twig" + tags: ['controller.service_arguments'] - sylius.controller.export_data: + 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'] + + sylius.controller.export_data_order: + 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.order" + - "@doctrine.orm.entity_manager" + - "%sylius.resources%" + tags: ['controller.service_arguments'] + + sylius.controller.export_data_customer: + 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.customer" + - "@doctrine.orm.entity_manager" + - "%sylius.resources%" + tags: ['controller.service_arguments'] # Form Type sylius.form.type.import: