-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
V0.0.1
- Loading branch information
Showing
23 changed files
with
1,583 additions
and
2 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
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 |
---|---|---|
@@ -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.#' |
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,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; | ||
} | ||
|
||
} |
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,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() | ||
); | ||
} | ||
|
||
} |
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,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]; | ||
} | ||
|
||
} |
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,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') | ||
); | ||
} | ||
|
||
} |
Oops, something went wrong.