-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apply and enforce kdyby/coding-standard
- Loading branch information
1 parent
fccf077
commit 03890a1
Showing
53 changed files
with
570 additions
and
1,115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0"?> | ||
<ruleset name="Kdyby/Translation"> | ||
<rule ref="vendor/kdyby/coding-standard/KdybyCodingStandard/ruleset-5.6.xml"/> | ||
|
||
<rule ref="Consistence.NamingConventions.ValidVariableName.NotCamelCaps"> | ||
<exclude-pattern>tests/KdybyTests/Translation/CatalogueCompilerTest.phpt</exclude-pattern> | ||
</rule> | ||
|
||
<rule ref="KdybyCodingStandard.Files.TypeNameMatchesFileName.NoMatchBetweenTypeNameAndFileName"> | ||
<exclude-pattern>tests/KdybyTests/Translation/data/extractor-files/*</exclude-pattern> | ||
</rule> | ||
</ruleset> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,29 +10,23 @@ | |
|
||
namespace Kdyby\Translation\Caching; | ||
|
||
use Kdyby; | ||
use Nette; | ||
|
||
|
||
use Nette\Caching\Cache; | ||
|
||
/** | ||
* @author David Grudl | ||
* @author Filip Procházka <[email protected]> | ||
* @internal | ||
*/ | ||
class PhpFileStorage extends Nette\Caching\Storages\FileStorage implements Nette\Caching\IStorage | ||
class PhpFileStorage extends \Nette\Caching\Storages\FileStorage implements \Nette\Caching\IStorage | ||
{ | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $hint; | ||
|
||
|
||
|
||
/** | ||
* Reads cache data from disk. | ||
* @param array | ||
* | ||
* @param array $meta | ||
* @return mixed | ||
*/ | ||
protected function readData($meta) | ||
|
@@ -43,21 +37,22 @@ protected function readData($meta) | |
]; | ||
} | ||
|
||
|
||
|
||
/** | ||
* Returns file name. | ||
* @param string | ||
* | ||
* @param string $key | ||
* @return string | ||
*/ | ||
protected function getCacheFile($key) | ||
{ | ||
return parent::getCacheFile(substr_replace( | ||
$cacheKey = substr_replace( | ||
$key, | ||
trim(strtr($this->hint, '\\/@', '.._'), '.') . '-', | ||
strpos($key, Nette\Caching\Cache::NAMESPACE_SEPARATOR) + 1, | ||
strpos($key, Cache::NAMESPACE_SEPARATOR) + 1, | ||
0 | ||
)) . '.php'; | ||
); | ||
|
||
return parent::getCacheFile($cacheKey) . '.php'; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,41 +10,35 @@ | |
|
||
namespace Kdyby\Translation; | ||
|
||
use Kdyby; | ||
use Nette; | ||
use Kdyby\Translation\Caching\PhpFileStorage; | ||
use Nette\Caching\Cache; | ||
use Nette\PhpGenerator as Code; | ||
use Symfony\Component\Translation\MessageCatalogueInterface; | ||
use Nette\Caching\IStorage; | ||
use Nette\Caching\Storages\MemoryStorage; | ||
use Nette\PhpGenerator\Helpers as GeneratorHelpers; | ||
use Nette\PhpGenerator\PhpLiteral; | ||
|
||
|
||
|
||
/** | ||
* @author Filip Procházka <[email protected]> | ||
*/ | ||
class CatalogueCompiler | ||
{ | ||
|
||
use Kdyby\StrictObjects\Scream; | ||
use \Kdyby\StrictObjects\Scream; | ||
|
||
/** | ||
* @var \Nette\Caching\Cache | ||
*/ | ||
private $cache; | ||
|
||
/** | ||
* @var FallbackResolver | ||
* @var \Kdyby\Translation\FallbackResolver | ||
*/ | ||
private $fallbackResolver; | ||
|
||
/** | ||
* @var CatalogueFactory | ||
* @var \Kdyby\Translation\CatalogueFactory | ||
*/ | ||
private $catalogueFactory; | ||
|
||
|
||
|
||
public function __construct( | ||
Nette\Caching\IStorage $cacheStorage, | ||
IStorage $cacheStorage, | ||
FallbackResolver $fallbackResolver, | ||
CatalogueFactory $catalogueFactory | ||
) | ||
|
@@ -54,24 +48,19 @@ public function __construct( | |
$this->catalogueFactory = $catalogueFactory; | ||
} | ||
|
||
|
||
|
||
/** | ||
* Replaces cache storage with simple memory storage (per-request). | ||
*/ | ||
public function enableDebugMode() | ||
{ | ||
$this->cache = new Cache(new Nette\Caching\Storages\MemoryStorage()); | ||
$this->cache = new Cache(new MemoryStorage()); | ||
} | ||
|
||
|
||
|
||
public function invalidateCache() | ||
{ | ||
$this->cache->clean([Cache::ALL => TRUE]); | ||
} | ||
|
||
|
||
/** | ||
* @param string $format | ||
* @param string $resource | ||
|
@@ -83,19 +72,17 @@ public function addResource($format, $resource, $locale, $domain = NULL) | |
$this->catalogueFactory->addResource($format, $resource, $locale, $domain); | ||
} | ||
|
||
|
||
|
||
/** | ||
* @param Translator $translator | ||
* @param MessageCatalogueInterface[] $availableCatalogues | ||
* @param \Kdyby\Translation\Translator $translator | ||
* @param \Symfony\Component\Translation\MessageCatalogueInterface[] $availableCatalogues | ||
* @param string $locale | ||
* @throws InvalidArgumentException | ||
* @return MessageCatalogueInterface[] | ||
* @throws \Kdyby\Translation\InvalidArgumentException | ||
* @return \Symfony\Component\Translation\MessageCatalogueInterface[] | ||
*/ | ||
public function compile(Translator $translator, array &$availableCatalogues, $locale) | ||
{ | ||
if (empty($locale)) { | ||
throw new InvalidArgumentException('Invalid locale'); | ||
throw new \Kdyby\Translation\InvalidArgumentException('Invalid locale'); | ||
} | ||
|
||
if (isset($availableCatalogues[$locale])) { | ||
|
@@ -104,8 +91,9 @@ public function compile(Translator $translator, array &$availableCatalogues, $lo | |
$cacheKey = [$locale, $translator->getFallbackLocales()]; | ||
|
||
$storage = $this->cache->getStorage(); | ||
if (!$storage instanceof Kdyby\Translation\Caching\PhpFileStorage) { | ||
if (($messages = $this->cache->load($cacheKey)) !== NULL) { | ||
if (!$storage instanceof PhpFileStorage) { | ||
$messages = $this->cache->load($cacheKey); | ||
if ($messages !== NULL) { | ||
$availableCatalogues[$locale] = new MessageCatalogue($locale, $messages); | ||
return $availableCatalogues; | ||
} | ||
|
@@ -129,32 +117,29 @@ public function compile(Translator $translator, array &$availableCatalogues, $lo | |
return $availableCatalogues; | ||
} | ||
|
||
|
||
|
||
/** | ||
* @param Translator $translator | ||
* @param MessageCatalogueInterface[] $availableCatalogues | ||
* @param \Kdyby\Translation\Translator $translator | ||
* @param \Symfony\Component\Translation\MessageCatalogueInterface[] $availableCatalogues | ||
* @param string $locale | ||
* @return string | ||
*/ | ||
protected function compilePhpCache(Translator $translator, array &$availableCatalogues, $locale) | ||
{ | ||
$fallbackContent = ''; | ||
$current = new Code\PhpLiteral(''); | ||
$current = new PhpLiteral(''); | ||
foreach ($this->fallbackResolver->compute($translator, $locale) as $fallback) { | ||
$fallbackSuffix = new Code\PhpLiteral(ucfirst(preg_replace('~[^a-z0-9_]~i', '_', $fallback))); | ||
$fallbackSuffix = new PhpLiteral(ucfirst(preg_replace('~[^a-z0-9_]~i', '_', $fallback))); | ||
|
||
$fallbackContent .= Code\Helpers::format(<<<EOF | ||
$fallbackContent .= GeneratorHelpers::format(<<<EOF | ||
\$catalogue? = new MessageCatalogue(?, ?); | ||
\$catalogue?->addFallbackCatalogue(\$catalogue?); | ||
EOF | ||
, $fallbackSuffix, $fallback, $availableCatalogues[$fallback]->all(), $current, $fallbackSuffix | ||
); | ||
, $fallbackSuffix, $fallback, $availableCatalogues[$fallback]->all(), $current, $fallbackSuffix); | ||
$current = $fallbackSuffix; | ||
} | ||
|
||
$content = Code\Helpers::format(<<<EOF | ||
$content = GeneratorHelpers::format(<<<EOF | ||
use Kdyby\\Translation\\MessageCatalogue; | ||
\$catalogue = new MessageCatalogue(?, ?); | ||
|
@@ -163,18 +148,15 @@ protected function compilePhpCache(Translator $translator, array &$availableCata | |
return \$catalogue; | ||
EOF | ||
, $locale, $availableCatalogues[$locale]->all(), new Code\PhpLiteral($fallbackContent) | ||
); | ||
, $locale, $availableCatalogues[$locale]->all(), new PhpLiteral($fallbackContent)); | ||
|
||
return '<?php' . "\n\n" . $content; | ||
} | ||
|
||
|
||
|
||
/** | ||
* @return MessageCatalogueInterface | ||
* @return \Symfony\Component\Translation\MessageCatalogueInterface | ||
*/ | ||
protected static function load(/*$file*/) | ||
protected static function load() | ||
{ | ||
return include func_get_arg(0); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,26 +11,19 @@ | |
namespace Kdyby\Translation; | ||
|
||
use Kdyby; | ||
use Symfony\Component\Translation\Exception\NotFoundResourceException; | ||
use Symfony\Component\Translation\MessageCatalogueInterface; | ||
|
||
|
||
|
||
/** | ||
* @author Filip Procházka <[email protected]> | ||
*/ | ||
class CatalogueFactory | ||
{ | ||
|
||
use Kdyby\StrictObjects\Scream; | ||
use \Kdyby\StrictObjects\Scream; | ||
|
||
/** | ||
* @var FallbackResolver | ||
* @var \Kdyby\Translation\FallbackResolver | ||
*/ | ||
private $fallbackResolver; | ||
|
||
/** | ||
* @var IResourceLoader | ||
* @var \Kdyby\Translation\IResourceLoader | ||
*/ | ||
private $loader; | ||
|
||
|
@@ -39,16 +32,12 @@ class CatalogueFactory | |
*/ | ||
private $resources = []; | ||
|
||
|
||
|
||
public function __construct(FallbackResolver $fallbackResolver, IResourceLoader $loader) | ||
{ | ||
$this->fallbackResolver = $fallbackResolver; | ||
$this->loader = $loader; | ||
} | ||
|
||
|
||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
|
@@ -57,8 +46,6 @@ public function addResource($format, $resource, $locale, $domain = 'messages') | |
$this->resources[$locale][] = [$format, $resource, $domain]; | ||
} | ||
|
||
|
||
|
||
/** | ||
* @return array | ||
*/ | ||
|
@@ -74,21 +61,19 @@ public function getResources() | |
return $list; | ||
} | ||
|
||
|
||
|
||
/** | ||
* @param Translator $translator | ||
* @param MessageCatalogueInterface[] $availableCatalogues | ||
* @param \Kdyby\Translation\Translator $translator | ||
* @param \Symfony\Component\Translation\MessageCatalogueInterface[] $availableCatalogues | ||
* @param string $locale | ||
* @throws NotFoundResourceException | ||
* @return MessageCatalogueInterface | ||
* @throws \Symfony\Component\Translation\Exception\NotFoundResourceException | ||
* @return \Symfony\Component\Translation\MessageCatalogueInterface | ||
*/ | ||
public function createCatalogue(Translator $translator, array &$availableCatalogues, $locale) | ||
{ | ||
try { | ||
$this->doLoadCatalogue($availableCatalogues, $locale); | ||
|
||
} catch (NotFoundResourceException $e) { | ||
} catch (\Symfony\Component\Translation\Exception\NotFoundResourceException $e) { | ||
if (!$this->fallbackResolver->compute($translator, $locale)) { | ||
throw $e; | ||
} | ||
|
@@ -103,7 +88,8 @@ public function createCatalogue(Translator $translator, array &$availableCatalog | |
} | ||
|
||
$newFallback = $availableCatalogues[$fallback]; | ||
if (($newFallbackFallback = $newFallback->getFallbackCatalogue()) && isset($chain[$newFallbackFallback->getLocale()])) { | ||
$newFallbackFallback = $newFallback->getFallbackCatalogue(); | ||
if ($newFallbackFallback !== NULL && isset($chain[$newFallbackFallback->getLocale()])) { | ||
break; | ||
} | ||
|
||
|
@@ -115,8 +101,6 @@ public function createCatalogue(Translator $translator, array &$availableCatalog | |
return $availableCatalogues[$locale]; | ||
} | ||
|
||
|
||
|
||
private function doLoadCatalogue(array &$availableCatalogues, $locale) | ||
{ | ||
$availableCatalogues[$locale] = $catalogue = new MessageCatalogue($locale); | ||
|
Oops, something went wrong.