Skip to content

Commit 9bd80ea

Browse files
author
slicewuff
committed
Add webSPELL class 'Language'
1 parent 7b6e757 commit 9bd80ea

File tree

4 files changed

+166
-0
lines changed

4 files changed

+166
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/vendor/
22

33
/tests/tmp/
4+
.phpunit.result.cache

src/Template.php

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
/*
3+
##########################################################################
4+
# #
5+
# Version 4 / / / #
6+
# -----------__---/__---__------__----__---/---/- #
7+
# | /| / /___) / ) (_ ` / ) /___) / / #
8+
# _|/_|/__(___ _(___/_(__)___/___/_(___ _/___/___ #
9+
# Free Content / Management System #
10+
# / #
11+
# #
12+
# #
13+
# Copyright 2005-2015 by webspell.org #
14+
# #
15+
# visit webSPELL.org, webspell.info to get webSPELL for free #
16+
# - Script runs under the GNU GENERAL PUBLIC LICENSE #
17+
# - It's NOT allowed to remove this copyright-tag #
18+
# -- http://www.fsf.org/licensing/licenses/gpl.html #
19+
# #
20+
# Code based on WebSPELL Clanpackage (Michael Gruber - webspell.at), #
21+
# Far Development by Development Team - webspell.org #
22+
# #
23+
# visit webspell.org #
24+
# #
25+
##########################################################################
26+
*/
27+
28+
namespace webspell_ng;
29+
30+
use webspell_ng\Language;
31+
32+
class Template
33+
{
34+
35+
/**
36+
* @var string $moduleName
37+
*/
38+
private $moduleName;
39+
40+
/**
41+
* @var bool $moduleName
42+
*/
43+
private $isAdminLanguage;
44+
45+
/**
46+
* @var string $defaultFolder
47+
*/
48+
private $defaultFolder = __DIR__ . '/../templates';
49+
50+
/**
51+
* @var string $rootFolder
52+
*/
53+
private $rootFolder;
54+
55+
/**
56+
* @var Language $language
57+
*/
58+
private $language;
59+
60+
/**
61+
* @param string $rootFolder base folder where the template files are located
62+
*/
63+
public function __construct(string $module_name, bool $is_admin_language=false, string $language="en", $rootFolder = "templates")
64+
{
65+
66+
$this->moduleName = $module_name;
67+
$this->isAdminLanguage = $is_admin_language;
68+
$this->rootFolder = $rootFolder;
69+
70+
$this->language = new Language();
71+
$this->language->setLanguage($language);
72+
$this->language->readModule($this->moduleName, false, $this->isAdminLanguage);
73+
74+
}
75+
76+
/**
77+
* returns the content of a template file
78+
*
79+
* @param string $template name of the template
80+
*
81+
* @return string content of the template
82+
*/
83+
private function loadFile($template): string
84+
{
85+
86+
$file = $this->rootFolder . "/" . $template . ".html";
87+
if (!file_exists($file)) {
88+
$file = $this->defaultFolder . "/error.html";
89+
}
90+
91+
return file_get_contents($file);
92+
93+
}
94+
95+
/**
96+
* Replace all keys of data with its values in the string
97+
* Longer keys are replaced first (users before user)
98+
*
99+
* @param string $template
100+
* @param array $data
101+
*
102+
* @return string
103+
*/
104+
private function replace($template, $data = array()): string
105+
{
106+
return strtr($template, $data);
107+
}
108+
109+
/**
110+
* Replace a single template with one set of data and translate all language keys
111+
*
112+
* @param string $template name of a template
113+
* @param array $data data which gets replaced
114+
*
115+
* @return string
116+
*/
117+
public function replaceTemplate($template, $data = array()): string
118+
{
119+
$templateString = $this->loadFile($template);
120+
$templateTranslated = $this->replaceLanguage($templateString);
121+
return $this->replace($templateTranslated, $data);
122+
}
123+
124+
/**
125+
* Replaces all language variables which are available
126+
*
127+
* @param string $template content of a template
128+
*
129+
* @return string
130+
*/
131+
private function replaceLanguage(string $template): string
132+
{
133+
return $this->replace(
134+
$template,
135+
$this->language->getTranslationTable()
136+
);
137+
}
138+
139+
}

templates/error.html

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class="alert alert-danger">
2+
404 - Template not found.
3+
</div>

tests/TemplateTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types=1);
2+
3+
use PHPUnit\Framework\TestCase;
4+
5+
use webspell_ng\Template;
6+
7+
final class TemplateTest extends TestCase
8+
{
9+
10+
public function testIfDefaultErrorTemplateCanBeLoaded(): void
11+
{
12+
13+
$template = new Template("test_module", false, "templates");
14+
$template_content = $template->replaceTemplate(
15+
"not_existing_template",
16+
array()
17+
);
18+
19+
$this->assertTrue(!empty($template_content), "Default template is not empty!");
20+
21+
}
22+
23+
}

0 commit comments

Comments
 (0)