Skip to content

Commit

Permalink
Merge pull request #2 from wdes/dev
Browse files Browse the repository at this point in the history
V0.0.1
  • Loading branch information
williamdes authored Dec 30, 2018
2 parents 26fea09 + b1df876 commit d67af60
Show file tree
Hide file tree
Showing 23 changed files with 1,583 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ PHP library/api to provide Internationalisation and Localisation
[![Build Status](https://travis-ci.com/wdes/php-I18n-L10n.svg?branch=master)](https://travis-ci.com/wdes/php-I18n-L10n)
[![codecov](https://codecov.io/gh/wdes/php-I18n-L10n/branch/master/graph/badge.svg)](https://codecov.io/gh/wdes/php-I18n-L10n)
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fwdes%2Fphp-I18n-L10n.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fwdes%2Fphp-I18n-L10n?ref=badge_shield)
[![HitCount](https://hits.dwyl.com/wdes/php-I18n-L10n.svg?style=flat)](https://hits.dwyl.com/wdes/php-I18n-L10n)
![Packagist](https://img.shields.io/packagist/l/wdes/php-I18n-L10n.svg)


## License
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
"phpunit/phpunit": "^7.2",
"squizlabs/php_codesniffer": "^3.3",
"slevomat/coding-standard": "^4.6",
"phpstan/phpstan": "^0.9.2"
"phpstan/phpstan": "^0.9.2",
"twig/twig": "^2.0",
"twig/extensions": "^1.5"
}
}
2 changes: 1 addition & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<file>.</file>
<exclude-pattern>*/vendor/*</exclude-pattern>
<exclude-pattern>*/build/*</exclude-pattern>
<exclude-pattern>*/node_modules/*</exclude-pattern>
<config name="installed_paths" value="../../slevomat/coding-standard"/>
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing"/>
Expand All @@ -26,7 +27,6 @@
<rule ref="SlevomatCodingStandard.Commenting.ForbiddenAnnotations">
<properties>
<property name="forbiddenAnnotations" type="array" value="
@copyright,
@package,
@throws
"/>
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
parameters:
bootstrap: %rootDir%/../../../src/bootstrap.php
ignoreErrors:
- '#Wdes\\PIL\\Twig\\TranslationNode\:\:__construct\(\) does not call parent constructor from Twig_Extensions_Node_Trans.#'
35 changes: 35 additions & 0 deletions src/Launcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
declare(strict_types = 1);
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
namespace Wdes\PIL;

use \Wdes\PIL\plugins\BasePlugin;

/**
* Plugin for reading .mo files
* @see https://www.gnu.org/software/gettext/manual/html_node/MO-Files.html
* @author William Desportes <[email protected]>
* @license MPL-2.0
*/
class Launcher
{
/**
* Plugin to use for translation
*
* @var BasePlugin
*/
public static $plugin;

/**
* Return regitered plugin
*
* @return BasePlugin
*/
public static function getPlugin(): BasePlugin
{
return Launcher::$plugin;
}

}
43 changes: 43 additions & 0 deletions src/Twig/Extension/I18n.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
declare(strict_types = 1);
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
namespace Wdes\PIL\Twig\Extension;

use \Wdes\PIL\Twig\TokenParser;
use \Twig\TwigFilter;
use \Twig\Extensions\I18nExtension;

/**
* I18n extension for Twig
* @license MPL-2.0
*/
class I18n extends I18nExtension
{

/**
* Get the filters
*
* @return TwigFilter[]
*/
public function getFilters(): array
{
return array(
new TwigFilter('trans'),
);
}

/**
* Get the token parsers
*
* @return \Twig\TokenParser\TokenParserInterface[]
*/
public function getTokenParsers(): array
{
return array(
new TokenParser()
);
}

}
95 changes: 95 additions & 0 deletions src/Twig/MemoryCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
declare(strict_types = 1);
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
namespace Wdes\PIL\Twig;

/**
* Token parser for Twig
* @license MPL-2.0
*/
class MemoryCache implements \Twig_CacheInterface
{
/**
* Contains all templates
*
* @var array<string, string>
*/
private $memory = array();

/**
* Generate a cache key
*
* @param string $name The name
* @param string $className The class name
* @return string
*/
public function generateKey($name, $className): string
{
return "memcache_".$name;
}

/**
* Write to cache
*
* @param string $key The cache key
* @param string $content The content to write
* @return void
*/
public function write($key, $content): void
{
$this->memory[$key] = $content;
}

/**
* Load from cache
*
* @param string $key The cache key
* @return void
*/
public function load($key)
{

}

/**
* Get the timestamp
*
* @param string $key The cache key
* @return int
*/
public function getTimestamp($key): int
{
return 0;
}

/**
* Get the cached string for key
*
* @param \Twig_Template $template The template
* @return string
*/
public function getFromCache(\Twig_Template $template): string
{
return $this->memory["memcache_".$template->getTemplateName()];
}

/**
* Extract source code from memory cache
*
* @param \Twig_Template $template The template
* @return string
*/
public function extractDoDisplayFromCache(\Twig_Template $template): string
{
$content = self::getFromCache($template);
// "/function ([a-z_]+)\("
preg_match_all(
'/protected function doDisplay\(([a-z\s,\$=\(\)]+)\)([\s]+){([=%\sa-z\/0-9-\>:\(\)\\\";.,\$\[\]?_-]+)/msi', $content, $output_array
);
//echo $content;
return $output_array[3][0];
}

}
84 changes: 84 additions & 0 deletions src/Twig/TokenParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
declare(strict_types = 1);
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
namespace Wdes\PIL\Twig;

use \Twig\Token;
use \Twig\Extensions\TokenParser\TransTokenParser;
use \Twig\Extensions\Node\TransNode;
use \Wdes\PIL\Twig\TranslationNode;

/**
* Token parser for Twig
* @license MPL-2.0
*/
class TokenParser extends TransTokenParser
{

/**
* Parses a token and returns a node.
*
* @param Token $token Twig token to parse
* @return TransNode
*/
public function parse(Token $token): TransNode
{
$stream = $this->parser->getStream();
$pluralsNbr = null;
$plural = null;
$notes = null;
$context = null;
if (!$stream->test(Token::BLOCK_END_TYPE)) {
$body = $this->parser->getExpressionParser()->parseExpression();
} else {
$stream->expect(Token::BLOCK_END_TYPE);
$body = $this->parser->subparse(
array($this, 'decideForFork')
);
$nextVal = $stream->next()->getValue();
if ($nextVal === 'notes') {
$stream->expect(Token::BLOCK_END_TYPE);
$notes = $this->parser->subparse(
array($this, 'decideForEnd'), true
);
} elseif ($nextVal === 'plural') {
$pluralsNbr = $this->parser->getExpressionParser()->parseExpression();
$stream->expect(Token::BLOCK_END_TYPE);
$plural = $this->parser->subparse(
array($this, 'decideForFork')
);
if ('notes' === $stream->next()->getValue()) {
$stream->expect(Token::BLOCK_END_TYPE);
$notes = $this->parser->subparse(
array($this, 'decideForEnd'), true
);
}
} elseif ($nextVal === 'context') {
$stream->expect(Token::BLOCK_END_TYPE);
$context = $this->parser->subparse(
array($this, 'decideForEnd'), true
);
}
}
$stream->expect(Token::BLOCK_END_TYPE);
$lineNbr = $token->getLine();
$this->checkTransString($body, $lineNbr);
return new TranslationNode($body, $plural, $pluralsNbr, $context, $notes, $lineNbr, $this->getTag());
}

/**
* Decides for fork
*
* @param Token $token Twig Token instance
* @return bool
*/
public function decideForFork(Token $token): bool
{
return $token->test(
array('plural', 'context', 'notes', 'endtrans')
);
}

}
Loading

0 comments on commit d67af60

Please sign in to comment.