Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make service config a bit more flexible #272

Merged
merged 1 commit into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,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 @@ -177,7 +177,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 @@ -199,7 +199,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