Skip to content

Commit

Permalink
Drop logger and debugger translator (#36)
Browse files Browse the repository at this point in the history
* Update Translator.phpt

* Delete LoggerTranslator.phpt

* Delete DebuggerTranslator.phpt

* Update TranslationExtension.php

* Create compatibility.php

* Delete DebuggerTranslator.php

* Delete LoggerTranslator.php

* Update Panel.php

* Update Translator.php

* Update phpstan.neon

* Update ruleset.xml

* Update Panel.php

* Update compatibility.php

* Update compatibility.php
  • Loading branch information
aleswita committed Nov 13, 2020
1 parent 486f0d0 commit 87257af
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 182 deletions.
9 changes: 2 additions & 7 deletions phpstan.neon
Expand Up @@ -8,44 +8,39 @@ parameters:
level: max
paths:
- src
excludes_analyse:
- src/compatibility.php
ignoreErrors:
# src\Loaders\Doctrine.php
-
count: 2
message: '#^Variable property access on T\.$#'
path: src/Loaders/Doctrine.php

# src\Tracy\Panel.php
-
count: 1
message: '#^Only numeric types are allowed in post-increment, int\|string given\.$#'
path: src/Tracy/Panel.php

# src\Translator.php
-
count: 1
message: '#^Method Contributte\\Translation\\Translator\:\:addResource\(\) has no return typehint specified\.$#'
path: src/Translator.php

# src\Translator.php
-
count: 1
message: '#^Method Contributte\\Translation\\Translator\:\:setLocale\(\) has no return typehint specified\.$#'
path: src/Translator.php

# src\Translator.php
-
count: 1
message: '#^Method Contributte\\Translation\\Translator\:\:setFallbackLocales\(\) has no return typehint specified\.$#'
path: src/Translator.php

# src\Translator.php
-
count: 1
message: '#^Method Contributte\\Translation\\Translator\:\:computeFallbackLocales\(\) has no return typehint specified\.$#'
path: src/Translator.php

# src\Translator.php
-
count: 1
message: '#^Method Contributte\\Translation\\Translator\:\:computeFallbackLocales\(\) has parameter \$locale with no typehint specified\.$#'
Expand Down
1 change: 1 addition & 0 deletions ruleset.xml
Expand Up @@ -13,4 +13,5 @@
</property>
</properties>
</rule>
<exclude-pattern>./src/compatibility.php</exclude-pattern>
</ruleset>
10 changes: 1 addition & 9 deletions src/DI/TranslationExtension.php
Expand Up @@ -112,15 +112,7 @@ public function loadConfiguration(): void
->setFactory($this->config->cache->factory, [$this->config->debug]);

// Translator
if ($this->config->debug && $this->config->debugger) {
$factory = Contributte\Translation\DebuggerTranslator::class;

} elseif ($this->config->logger) {
$factory = Contributte\Translation\LoggerTranslator::class;

} else {
$factory = Contributte\Translation\Translator::class;
}
$factory = Contributte\Translation\Translator::class;

$translator = $builder->addDefinition($this->prefix('translator'))
->setFactory($factory, ['defaultLocale' => $this->config->locales->default, 'cacheDir' => $this->config->cache->dir, 'debug' => $this->config->debug, 'cacheVary' => $this->config->cache->vary])
Expand Down
51 changes: 0 additions & 51 deletions src/DebuggerTranslator.php

This file was deleted.

55 changes: 0 additions & 55 deletions src/LoggerTranslator.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Tracy/Panel.php
Expand Up @@ -13,7 +13,7 @@ class Panel implements Tracy\IBarPanel

use Nette\SmartObject;

/** @var Contributte\Translation\DebuggerTranslator */
/** @var Contributte\Translation\Translator */
private $translator;

/** @var (string|int)[][] */
Expand All @@ -37,7 +37,7 @@ class Panel implements Tracy\IBarPanel
/** @var int */
private $ignoredResourcesCount = 0;

public function __construct(Contributte\Translation\DebuggerTranslator $translator)
public function __construct(Contributte\Translation\Translator $translator)
{
$this->translator = $translator;
$translator->setTracyPanel($this);
Expand Down
67 changes: 67 additions & 0 deletions src/Translator.php
Expand Up @@ -4,6 +4,7 @@

use Contributte;
use Nette;
use Psr;
use Symfony;

/**
Expand All @@ -18,6 +19,8 @@
* @property-read string $formattedPrefix
* @property-read string[] $availableLocales
* @property string|null $locale
* @property Psr\Log\LoggerInterface|null $psrLogger
* @property Contributte\Translation\Tracy\Panel|null $tracyPanel
*/
class Translator extends Symfony\Component\Translation\Translator implements Nette\Localization\ITranslator
{
Expand Down Expand Up @@ -51,6 +54,12 @@ class Translator extends Symfony\Component\Translation\Translator implements Net
/** @var bool[] @internal */
private $resourcesLocales = [];

/** @var Psr\Log\LoggerInterface|null */
private $psrLogger;

/** @var Contributte\Translation\Tracy\Panel|null */
private $tracyPanel;

/**
* @param string[] $cacheVary
*/
Expand Down Expand Up @@ -231,6 +240,28 @@ public function setFallbackLocales(array $locales)
$this->fallbackResolver->setFallbackLocales($locales);
}

public function getPsrLogger(): ?Psr\Log\LoggerInterface
{
return $this->psrLogger;
}

public function setPsrLogger(?Psr\Log\LoggerInterface $psrLogger): self
{
$this->psrLogger = $psrLogger;
return $this;
}

public function getTracyPanel(): ?Tracy\Panel
{
return $this->tracyPanel;
}

public function setTracyPanel(?Tracy\Panel $tracyPanel): self
{
$this->tracyPanel = $tracyPanel;
return $this;
}

/**
* @param mixed $message
* @param mixed ...$parameters
Expand Down Expand Up @@ -291,6 +322,42 @@ public function translate($message, ...$parameters): string
return $this->trans($message, $params, $domain, $locale);
}

/**
* @param string|null $id
* @param mixed[] $parameters
* @param string|null $domain
* @param string|null $locale
* @return string
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint
*/
public function trans($id, array $parameters = [], $domain = null, $locale = null)
{
if ($domain === null) {
$domain = 'messages';
}

if ($id !== null) {
if ($this->psrLogger !== null) {
if (!$this->getCatalogue()->has($id, $domain)) {
$this->psrLogger->notice('Missing translation', [
'id' => $id,
'domain' => $domain,
'locale' => $locale ?? $this->getLocale(),
]);
}
}

if ($this->tracyPanel !== null) {
if (!$this->getCatalogue()->has($id, $domain)) {
$this->tracyPanel->addMissingTranslation($id, $domain);
}
}
}

return parent::trans($id, $parameters, $domain, $locale);
}

/**
* {@inheritdoc}
*/
Expand Down
27 changes: 27 additions & 0 deletions src/compatibility.php
@@ -0,0 +1,27 @@
<?php declare(strict_types = 1);

namespace Contributte\Translation;

if (false) {

/** @deprecated use Contributte\Translation\LoggerTranslator */
class LoggerTranslator
{

}

} elseif (!class_exists(LoggerTranslator::class)) {
class_alias(Translator::class, LoggerTranslator::class);
}

if (false) {

/** @deprecated use Contributte\Translation\DebuggerTranslator */
class DebuggerTranslator
{

}

} elseif (!class_exists(DebuggerTranslator::class)) {
class_alias(Translator::class, DebuggerTranslator::class);
}
29 changes: 0 additions & 29 deletions tests/Tests/DebuggerTranslator.phpt

This file was deleted.

29 changes: 0 additions & 29 deletions tests/Tests/LoggerTranslator.phpt

This file was deleted.

0 comments on commit 87257af

Please sign in to comment.