Skip to content

Commit 5b0b482

Browse files
authored
Merge pull request #22 from KorvinSzanto/feature/twig-parser
Add optional support for twig
2 parents 5777243 + 5cbfd55 commit 5b0b482

31 files changed

+371
-192
lines changed

.github/workflows/tests.yml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
strategy:
1717
matrix:
1818
php-version:
19-
- "5.3"
19+
- "7.2"
2020
- "8.5"
2121
steps:
2222
-
@@ -65,12 +65,6 @@ jobs:
6565
os:
6666
- ubuntu-latest
6767
php-version:
68-
- "5.3"
69-
- "5.4"
70-
- "5.5"
71-
- "5.6"
72-
- "7.0"
73-
- "7.1"
7468
- "7.2"
7569
- "7.3"
7670
- "7.4"
@@ -83,7 +77,7 @@ jobs:
8377
include:
8478
-
8579
os: windows-latest
86-
php-version: "5.6"
80+
php-version: "7.2"
8781
-
8882
os: windows-latest
8983
php-version: "7.4"

.php-cs-fixer.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,10 @@
1010
->setRules([
1111
'@PER-CS' => true,
1212
'@PER-CS:risky' => true,
13-
// PHP arrays should be declared using the configured syntax.
14-
'array_syntax' => ['syntax' => 'long'],
15-
// Classes, constants, properties, and methods MUST have visibility declared, and keyword modifiers MUST be in the following order: inheritance modifier (`abstract` or `final`), visibility modifier (`public`, `protected`, or `private`), set-visibility modifier (`public(set)`, `protected(set)`, or `private(set)`), scope modifier (`static`), mutation modifier (`readonly`), type declaration, name.
16-
'modifier_keywords' => ['elements' => ['method', 'property']],
1713
// Empty body of class, interface, trait, enum or function must be abbreviated as `{}` and placed on the same line as the previous symbol, separated by a single space.
1814
'single_line_empty_body' => false,
1915
// Arguments lists, array destructuring lists, arrays that are multi-line, `match`-lines and parameters lists must have a trailing comma.
2016
'trailing_comma_in_multiline' => ['after_heredoc' => false, 'elements' => ['arrays']],
21-
// Classes, constants, properties, and methods MUST have visibility declared, and keyword modifiers MUST be in the following order: inheritance modifier (`abstract` or `final`), visibility modifier (`public`, `protected`, or `private`), set-visibility modifier (`public(set)`, `protected(set)`, or `private(set)`), scope modifier (`static`), mutation modifier (`readonly`), type declaration, name.
22-
'visibility_required' => ['elements' => ['method', 'property']],
2317
])
2418
->setFinder(PhpCsFixer\Finder::create()
2519
->in(__DIR__)

composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"source": "https://github.com/concrete5-community/translation-library"
2929
},
3030
"require": {
31-
"php": ">=5.3.0",
31+
"php": ">=7.2",
3232
"gettext/gettext": "~3.5.9"
3333
},
3434
"autoload": {
@@ -37,7 +37,11 @@
3737
}
3838
},
3939
"require-dev": {
40-
"phpunit/phpunit": "^4.8.36 || ^6.5.14 || ^8.5.39"
40+
"phpunit/phpunit": "^8.5.39",
41+
"twig/twig": "v3.11.3"
42+
},
43+
"suggest": {
44+
"twig/twig": "To parse twig files for translations"
4145
},
4246
"autoload-dev": {
4347
"psr-4": {

src/Gettext.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ class Gettext
1616
*/
1717
public static function commandIsAvailable($command)
1818
{
19-
static $cache = array();
19+
static $cache = [];
2020
if (!isset($cache[$command])) {
2121
$cache[$command] = false;
2222
$safeMode = @ini_get('safe_mode');
2323
if (empty($safeMode)) {
2424
if (function_exists('exec')) {
2525
if (!in_array('exec', array_map('trim', explode(',', strtolower(@ini_get('disable_functions')))), true)) {
2626
$rc = 1;
27-
$output = array();
27+
$output = [];
2828
@exec($command . ' --version 2>&1', $output, $rc);
2929
if ($rc === 0) {
3030
$cache[$command] = true;

src/Options.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static function getTemporaryDirectory()
5050
}
5151
if ($result === '') {
5252
if (isset($_ENV) && is_array($_ENV)) {
53-
foreach (array('TMP', 'TMPDIR', 'TEMP') as $k) {
53+
foreach (['TMP', 'TMPDIR', 'TEMP'] as $k) {
5454
if (isset($_ENV[$k])) {
5555
$result = $check($_ENV[$k]);
5656
if ($result !== '') {

src/Parser.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ abstract class Parser
1212
*
1313
* @var array
1414
*/
15-
private static $cache = array();
15+
private static $cache = [];
1616

1717
/**
1818
* The parser factory.
@@ -166,15 +166,15 @@ protected function parseRunningConcrete5Do(\Gettext\Translations $translations,
166166
*/
167167
public function getSubParsers()
168168
{
169-
return array();
169+
return [];
170170
}
171171

172172
/**
173173
* Clears the memory cache.
174174
*/
175175
final public static function clearCache()
176176
{
177-
self::$cache = array();
177+
self::$cache = [];
178178
}
179179

180180
/**
@@ -189,7 +189,7 @@ final protected static function getDirectoryStructure($rootDirectory, $exclude3r
189189
{
190190
$rootDirectory = rtrim(str_replace(DIRECTORY_SEPARATOR, '/', $rootDirectory), '/');
191191
if (!isset(self::$cache[__FUNCTION__])) {
192-
self::$cache[__FUNCTION__] = array();
192+
self::$cache[__FUNCTION__] = [];
193193
}
194194
$cacheKey = $rootDirectory . '*' . ($exclude3rdParty ? '1' : '0');
195195
if (!isset(self::$cache[__FUNCTION__][$cacheKey])) {
@@ -216,7 +216,7 @@ private static function getDirectoryStructureDo($relativePath, $rootDirectory, $
216216
if ($relativePath !== '') {
217217
$thisRoot .= '/' . $relativePath;
218218
}
219-
$subDirs = array();
219+
$subDirs = [];
220220
$hDir = @opendir($thisRoot);
221221
if ($hDir === false) {
222222
throw new \Exception("Unable to open directory $rootDirectory");
@@ -237,7 +237,7 @@ private static function getDirectoryStructureDo($relativePath, $rootDirectory, $
237237
$subDirs[] = $entry;
238238
}
239239
@closedir($hDir);
240-
$result = array();
240+
$result = [];
241241
foreach ($subDirs as $subDir) {
242242
$rel = ($relativePath === '') ? $subDir : "$relativePath/$subDir";
243243
$result = array_merge($result, static::getDirectoryStructureDo($rel, $rootDirectory, $exclude3rdParty));
@@ -294,7 +294,7 @@ final public static function getByHandle($parserHandle)
294294
*/
295295
final protected static function unhandleString($string)
296296
{
297-
return ucwords(str_replace(array('_', '-', '/'), ' ', $string));
297+
return ucwords(str_replace(['_', '-', '/'], ' ', $string));
298298
}
299299

300300
/**
@@ -364,13 +364,13 @@ private function getAbsoluteDirectoryPath($path)
364364
if (!$isAbsolute) {
365365
$path = rtrim($cwd, '/') . '/' . $path;
366366
}
367-
$parts = array();
367+
$parts = [];
368368
foreach (explode('/', $path) as $part) {
369369
if ($part === '' || $part === '.') {
370370
continue;
371371
}
372372
if ($part === '..') {
373-
if ($parts === array()) {
373+
if ($parts === []) {
374374
return '';
375375
}
376376
array_pop($parts);

src/Parser/BlockTemplates.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ public function canParseDirectory()
3434
*/
3535
protected function parseDirectoryDo(\Gettext\Translations $translations, $rootDirectory, $relativePath, $subParsersFilter, $exclude3rdParty)
3636
{
37-
$templateHandles = array();
37+
$templateHandles = [];
3838
$prefix = ($relativePath === '') ? '' : "$relativePath/";
3939
$matches = null;
40-
foreach (array_merge(array(''), $this->getDirectoryStructure($rootDirectory, $exclude3rdParty)) as $child) {
40+
foreach (array_merge([''], $this->getDirectoryStructure($rootDirectory, $exclude3rdParty)) as $child) {
4141
$shownChild = ($child === '') ? rtrim($prefix, '/') : ($prefix . $child);
4242
$fullpath = ($child === '') ? $rootDirectory : "$rootDirectory/$child";
4343
if (preg_match('%(?:^|/)blocks/\w+/(?:templates|composer)/(\w+)$%', $fullpath, $matches)) {
4444
if (!isset($templateHandles[$matches[1]])) {
45-
$templateHandles[$matches[1]] = array();
45+
$templateHandles[$matches[1]] = [];
4646
}
4747
$templateHandles[$matches[1]][] = $shownChild;
4848
} elseif (preg_match('%(^|/)blocks/\w+/(?:templates|composer)$%', $fullpath)) {
@@ -54,7 +54,7 @@ protected function parseDirectoryDo(\Gettext\Translations $translations, $rootDi
5454
if ($file[0] !== '.') {
5555
if (preg_match('/^(.*)\.php$/', $file, $matches) && is_file("$fullpath/$file")) {
5656
if (!isset($templateHandles[$matches[1]])) {
57-
$templateHandles[$matches[1]] = array();
57+
$templateHandles[$matches[1]] = [];
5858
}
5959
$templateHandles[$matches[1]][] = $shownChild . "/$file";
6060
}
@@ -63,7 +63,7 @@ protected function parseDirectoryDo(\Gettext\Translations $translations, $rootDi
6363
}
6464
}
6565
foreach ($templateHandles as $templateHandle => $references) {
66-
$translation = $translations->insert('TemplateFileName', ucwords(str_replace(array('_', '-', '/'), ' ', $templateHandle)));
66+
$translation = $translations->insert('TemplateFileName', ucwords(str_replace(['_', '-', '/'], ' ', $templateHandle)));
6767
foreach ($references as $reference) {
6868
$translation->addReference($reference);
6969
}

src/Parser/Cif.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function canParseDirectory()
3535
protected function parseDirectoryDo(\Gettext\Translations $translations, $rootDirectory, $relativePath, $subParsersFilter, $exclude3rdParty)
3636
{
3737
$prefix = ($relativePath === '') ? '' : "$relativePath/";
38-
foreach (array_merge(array(''), $this->getDirectoryStructure($rootDirectory, $exclude3rdParty)) as $child) {
38+
foreach (array_merge([''], $this->getDirectoryStructure($rootDirectory, $exclude3rdParty)) as $child) {
3939
$shownDirectory = $prefix . (($child === '') ? '' : "$child/");
4040
$fullDirectoryPath = ($child === '') ? $rootDirectory : "$rootDirectory/$child";
4141
$contents = @scandir($fullDirectoryPath);
@@ -222,11 +222,11 @@ private static function parseXmlNode(\Gettext\Translations $translations, $filen
222222
break;
223223
case '/concrete5-cif/pages/page/area/block/data/record':
224224
// Skip this node and *almost* all its children
225-
$childnodesLimit = array('title');
225+
$childnodesLimit = ['title'];
226226
break;
227227
case '/concrete5-cif/pagefeeds/feed':
228228
// Skip this node and *almost* all its children
229-
$childnodesLimit = array('title', 'description');
229+
$childnodesLimit = ['title', 'description'];
230230
break;
231231
case '/concrete5-cif/area/blocks':
232232
case '/concrete5-cif/attributecategories/category/attributetypes':

src/Parser/ConfigFiles.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ protected function parseDirectoryDo(\Gettext\Translations $translations, $rootDi
5858
}
5959
}
6060
}
61-
$directoryAlternatives = array('application/config/generated_overrides', 'application/config', "{$corePrefix}/config");
61+
$directoryAlternatives = ['application/config/generated_overrides', 'application/config', "{$corePrefix}/config"];
6262
break;
6363
case 'application':
64-
$directoryAlternatives = array('config/generated_overrides', 'config');
64+
$directoryAlternatives = ['config/generated_overrides', 'config'];
6565
break;
6666
case 'concrete':
67-
$directoryAlternatives = array('config');
67+
$directoryAlternatives = ['config'];
6868
break;
6969
default:
7070
return;
@@ -148,11 +148,11 @@ private function parseTranslatableStrings(\Gettext\Translations $translations, $
148148
*/
149149
private function getTranslatableKeys()
150150
{
151-
return array(
151+
return [
152152
'concrete.user.deactivation.message' => '',
153153
'concrete.user.username.allowed_characters.requirement_string' => '',
154154
'concrete.user.username.allowed_characters.error_string' => '',
155-
);
155+
];
156156
}
157157

158158
/**

src/Parser/Dynamic.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
class Dynamic extends \C5TL\Parser
1111
{
12-
private $subParsers = array();
12+
private $subParsers = [];
1313

1414
public function __construct()
1515
{
@@ -87,7 +87,7 @@ public function getSubParserByHandle($handle)
8787
*/
8888
private function getDefaultSubParsers()
8989
{
90-
$result = array();
90+
$result = [];
9191
$dir = __DIR__ . '/DynamicItem';
9292
if (is_dir($dir) && is_readable($dir)) {
9393
$matches = null;

0 commit comments

Comments
 (0)