Skip to content

Commit

Permalink
Merge pull request #272 from cdarken/fix-custom-resources-import
Browse files Browse the repository at this point in the history
Make service config a bit more flexible
  • Loading branch information
oallain authored Aug 30, 2022
2 parents 5ddec95 + 9b4bf2a commit af93f74
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ admin overview panel using the event hook system, ie. `admin/tax-categories/`.

#### Notes

- Replace `app.foo` with the name of the resource (under `sylius_resource` config) you want to implement in the following examples.
- Replace `app` and `foo` with the name of the resource (under `sylius_resource` config) you want to implement in the following examples.
- Replace `bar` with the name of the format you want to implement in the following examples (csv, json, ...).
- Note it is of course also possible to implement a dedicated importer for `app.foo` resource and format `bar`,
in case a generic type implementation is not possible.
Expand All @@ -170,7 +170,7 @@ sylius.importer.foo.bar:
- "@sylius.processor.foo"
- "@sylius.importer.result"
tags:
- { name: sylius.importer, type: app.foo, format: csv }
- { name: sylius.importer, type: foo, domain: app, format: csv }
```

##### Alternatively implement a custom ResourceImporter _FooImporter_
Expand All @@ -192,7 +192,7 @@ sylius.importer.foo.bar:
- "@sylius.processor.foo"
- "@sylius.importer.result"
tags:
- { name: sylius.importer, type: app.foo, format: bar }
- { name: sylius.importer, type: foo, domain: app, format: bar }
```

#### Adding a ResourceProcessor
Expand Down
11 changes: 6 additions & 5 deletions src/DependencyInjection/Compiler/RegisterImporterPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,26 @@ public function process(ContainerBuilder $container): void

$type = $attributes[0]['type'];
$format = $attributes[0]['format'];
$domain = $attributes[0]['domain'] ?? null;
$name = ImporterRegistry::buildServiceName($type, $format);

$importersRegistry->addMethodCall('register', [$name, new Reference($id)]);

if ($container->getParameter('sylius.importer.web_ui')) {
$this->registerImportFormBlockEvent($container, $type);
$this->registerImportFormBlockEvent($container, $type, $domain);
}
}
}

private function registerImportFormBlockEvent(ContainerBuilder $container, string $type): void
private function registerImportFormBlockEvent(ContainerBuilder $container, string $type, ?string $domain): void
{
$eventHookName = $this->buildEventHookName($type);

if ($container->has($eventHookName)) {
return;
}

$eventName = $this->buildEventName($type);
$eventName = $this->buildEventName($type, $domain);
$templateName = $this->buildTemplateName($type);
$container
->register(
Expand Down Expand Up @@ -92,13 +93,13 @@ private function buildEventHookName(string $type): string
return sprintf('app.block_event_listener.admin.crud.after_content_%s.import', $type);
}

private function buildEventName(string $type): string
private function buildEventName(string $type, ?string $domain): string
{
if (isset($this->eventNames[$type])) {
return $this->eventNames[$type];
}

$domain = 'sylius';
$domain = $domain ?? 'sylius';
// backward compatibility with the old configuration
if (count(\explode('.', $type)) === 2) {
[$domain, $type] = \explode('.', $type);
Expand Down

0 comments on commit af93f74

Please sign in to comment.