Skip to content

Commit

Permalink
Improve error messages, simplify Symfony DIC config
Browse files Browse the repository at this point in the history
  • Loading branch information
mpdude committed Jan 8, 2024
1 parent e93b161 commit cbcd1b6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 31 deletions.
11 changes: 5 additions & 6 deletions src/Doctrine/TranslatableClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static function parseFromClass(string $class, Reader $reader, ClassMetada
$tm->findTranslationsCollection($cm, $reader, $classMetadataFactory);
$tm->findTranslatedProperties($cm, $reader, $classMetadataFactory);

if ($tm->assertNoAnnotationsArePresent()) {
if ($tm->isClassWithoutTranslations()) {
return null;
}
$tm->assertAnnotationsAreComplete($class);
Expand Down Expand Up @@ -149,19 +149,18 @@ public static function wakeup(SerializedTranslatableClassMetadata $data): self
return $self;
}

private function assertNoAnnotationsArePresent(): bool
private function isClassWithoutTranslations(): bool
{
return null === $this->translationClass
&& null === $this->translationLocaleProperty
&& null === $this->translationMappingProperty
&& 0 === \count($this->translatedProperties)
&& null === $this->primaryLocale;
&& 0 === \count($this->translatedProperties);
}

private function assertAnnotationsAreComplete(string $class): void
{
if (null === $this->translationClass) {
throw new RuntimeException('The annotation with the translation class name is missing or incorrect, e.g. @ORM\OneToMany(targetEntity="TestEntityTranslation", ...)');
throw new RuntimeException(sprintf('Unable to find the translations for %s. There should be a one-to-may collection holding the translation entities, and it should be marked with %s.', $class, Annotation\TranslationCollection::class));
}

if (null === $this->translationLocaleProperty) {
Expand All @@ -177,7 +176,7 @@ private function assertAnnotationsAreComplete(string $class): void
}

if (null === $this->primaryLocale) {
throw new RuntimeException('A primary locale has to be set at the class level for '.$class);
throw new RuntimeException(sprintf('Class %s uses translations, so it needs to provide the primary locale with the %s annotation at the class level. This can either be at the class itself, or in one of its parent classes.', $class, Annotation\Locale::class));
}
}

Expand Down
30 changes: 5 additions & 25 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,20 @@
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="webfactory.polyglot.doctrine_listener.class">Webfactory\Bundle\PolyglotBundle\Doctrine\PolyglotListener</parameter>
<parameter key="webfactory.polyglot.symfony_locale_listener.class">Webfactory\Bundle\PolyglotBundle\EventListener\LocaleListener</parameter>
<parameter key="webfactory.polyglot.default_locale_provider.class">Webfactory\Bundle\PolyglotBundle\Locale\DefaultLocaleProvider</parameter>
</parameters>

<services>
<service
id="webfactory.polyglot.doctrine_listener"
class="%webfactory.polyglot.doctrine_listener.class%">
<argument type="service" id="annotation_reader"/>
<argument type="service" id="webfactory.polyglot.default_locale_provider"/>
<argument type="service" id="logger" on-invalid="null"/>

<tag name="doctrine.event_listener" priority="-100" event="postFlush"/>
<tag name="doctrine.event_listener" priority="-100" event="prePersist"/>
<tag name="doctrine.event_listener" priority="-100" event="preFlush"/>
<tag name="doctrine.event_listener" priority="-100" event="postLoad"/>
<defaults autowire="true" autoconfigure="true" />

<service id="Webfactory\Bundle\PolyglotBundle\Doctrine\PolyglotListener">
<tag name="doctrine.event_subscriber" priority="-100" />
<tag name="monolog.logger" channel="webfactory_polyglot_bundle"/>
</service>

<service id="webfactory.polyglot.symfony_locale_listener"
class="%webfactory.polyglot.symfony_locale_listener.class%">
<argument type="service" id="webfactory.polyglot.default_locale_provider" />
<tag name="kernel.event_subscriber" />
</service>
<service id="Webfactory\Bundle\PolyglotBundle\EventListener\LocaleListener" />

<service id="webfactory.polyglot.default_locale_provider"
class="%webfactory.polyglot.default_locale_provider.class%">
<service id="Webfactory\Bundle\PolyglotBundle\Locale\DefaultLocaleProvider">
<call method="setDefaultLocale">
<argument>%webfactory.polyglot.default_locale%</argument>
</call>
</service>

</services>
</container>

0 comments on commit cbcd1b6

Please sign in to comment.