commit
71c97e6d91
@ -0,0 +1,18 @@
|
||||
test: test-unit
|
||||
lint: lint-md lint-cs
|
||||
|
||||
# Run every available unit test
|
||||
test-unit:
|
||||
vendor/bin/phpunit --bootstrap vendor/autoload.php test
|
||||
|
||||
# Run PHP Mess Detector against all the code, settings are in phpmd.xml
|
||||
lint-md:
|
||||
vendor/bin/phpmd TplBlock.php text ./phpmd.xml
|
||||
vendor/bin/phpmd test/TplBlockTest.php text ./phpmd.xml
|
||||
vendor/bin/phpmd sample/sample.php text ./phpmd.xml
|
||||
|
||||
# Run PHP Code Sniffer against all the code, settings are in phpcs.xml
|
||||
lint-cs:
|
||||
vendor/bin/phpcs TplBlock.php -s
|
||||
vendor/bin/phpcs test/TplBlockTest.php -s
|
||||
vendor/bin/phpcs sample/sample.php -s
|
@ -0,0 +1,2 @@
|
||||
<?php
|
||||
require "TplBlock.php";
|
@ -1,139 +0,0 @@
|
||||
<?php
|
||||
class InvalidTemplateException extends UnexpectedValueException{
|
||||
|
||||
}
|
||||
class TplBlock {
|
||||
/*
|
||||
* Class TplBlog
|
||||
* By gnieark https://blog-du-grouik.tinad.fr 2018
|
||||
* Licenced under the GNU General Public License V3
|
||||
* https://www.gnu.org/licenses/gpl-3.0.fr.html
|
||||
*/
|
||||
const blockStartStart = '<!-- BEGIN ';
|
||||
const blockStartEnd = ' -->';
|
||||
const blockEndStart = '<!-- END ';
|
||||
const blockEndEnd = ' -->';
|
||||
|
||||
const startEnclosure = '{{';
|
||||
const endEnclosure = '}}';
|
||||
|
||||
public $name = '';
|
||||
private $vars = array();
|
||||
private $subBlocs = array();
|
||||
private $unusedRegex = "";
|
||||
|
||||
/*
|
||||
* Initialise TplBlock
|
||||
* Input object name
|
||||
* Can be empty only for the top one block
|
||||
*/
|
||||
|
||||
public function __construct($name = NULL){
|
||||
$this->name = $name;
|
||||
|
||||
if(!is_null($name) && !ctype_alnum($name)){
|
||||
throw new InvalidTemplateException("Only alpha-numerics chars are allowed on the block name");
|
||||
return false;
|
||||
}
|
||||
$this->unusedRegex = '/'
|
||||
. self::blockStartStart
|
||||
. ' *([a-z][a-z0-9.]*) *'
|
||||
. self::blockStartEnd
|
||||
. '(.*?)'
|
||||
. self::blockEndStart
|
||||
. ' *\1 *'
|
||||
. self::blockEndEnd
|
||||
. '/is'
|
||||
;
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* Add simple vars
|
||||
* Input array structured like:
|
||||
* {"key":"value","key2":"value2"}
|
||||
*/
|
||||
public function add_vars(ARRAY $vars){
|
||||
$this->vars = array_merge($this->vars,$vars);
|
||||
}
|
||||
|
||||
/*
|
||||
* add_sub_block
|
||||
* Input: a TplBlock object.
|
||||
*/
|
||||
public function add_sub_block(TplBlock $bloc){
|
||||
if(is_null($bloc->name) || empty($bloc->name)){
|
||||
throw new InvalidTemplateException("A sub tpl bloc can't have an empty name");
|
||||
return false;
|
||||
}
|
||||
$this->subBlocs[$bloc->name][] = $bloc;
|
||||
}
|
||||
|
||||
private function subBlockRegex($prefix, $blocName,$trim = true) {
|
||||
return '/'
|
||||
. self::blockStartStart
|
||||
. preg_quote($prefix . $blocName)
|
||||
. self::blockStartEnd
|
||||
. (($trim === false)? '' : '(?:\R|)?' )
|
||||
. '(.*?)'
|
||||
. (($trim === false)? '' : '(?:\R|)?' )
|
||||
. self::blockEndStart
|
||||
. preg_quote($prefix . $blocName)
|
||||
. self::blockEndEnd
|
||||
. '/is';
|
||||
}
|
||||
|
||||
/*
|
||||
* Shake the template string and input vars
|
||||
* Then returns the parsed text
|
||||
* Input:
|
||||
* $str String containing the template to parse
|
||||
* $subBlocsPath String optional, for this class internal use. The path like "bloc.subbloc"
|
||||
* $trim Boolean
|
||||
* if true(default), the potentials Carriages returns beginning
|
||||
* and ending the bloc are deleted
|
||||
*/
|
||||
public function apply_tpl_str($str,$subBlocsPath = "", $trim = true){
|
||||
|
||||
//replace all simple vars
|
||||
$prefix = (empty($subBlocsPath)? "" : $subBlocsPath.".");
|
||||
foreach($this->vars as $key=>$value){
|
||||
|
||||
$str = str_replace(self::startEnclosure . $prefix . $key . self::endEnclosure,
|
||||
$value,
|
||||
$str);
|
||||
}
|
||||
|
||||
//parse blocs
|
||||
foreach($this->subBlocs as $blocName => $blocsArr){
|
||||
$str = preg_replace_callback(
|
||||
$this->subBlockRegex($prefix, $blocName, $trim),
|
||||
function($m) use($blocName,$blocsArr,$prefix, $trim) {
|
||||
$out = "";
|
||||
foreach($blocsArr as $bloc){
|
||||
//recursion
|
||||
$out.=$bloc->apply_tpl_str( $m[1] , $prefix . $blocName , $trim );
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
,$str
|
||||
);
|
||||
}
|
||||
|
||||
// Delete unused blocs
|
||||
$str = preg_replace($this->unusedRegex, "", $str);
|
||||
return $str;
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* load a file, and pass his content to apply_tpl_str function.
|
||||
*/
|
||||
public function apply_tpl_file($file, $trim = true){
|
||||
if(!$tplStr = file_get_contents($file)){
|
||||
throw new InvalidTemplateException("Cannot read given file ".$file);
|
||||
return false;
|
||||
}
|
||||
return $this->apply_tpl_str($tplStr, null, $trim);
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
{
|
||||
"autoload": {
|
||||
"classmap": [
|
||||
"./"
|
||||
]
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "5.7",
|
||||
"phpdocumentor/phpdocumentor": "2.*",
|
||||
"phpmd/phpmd": "^2.6",
|
||||
"gamegos/php-code-sniffer": "*"
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset name="Basic Project Coding Standards">
|
||||
<rule ref="PSR2" />
|
||||
|
||||
<rule ref="Generic">
|
||||
<!-- Rules of an ancient time... -->
|
||||
<exclude name="Generic.PHP.ClosingPHPTag.NotFound" />
|
||||
<exclude name="Generic.WhiteSpace.DisallowSpaceIndent.SpacesUsed" />
|
||||
<exclude name="Generic.Arrays.DisallowShortArraySyntax.Found" />
|
||||
<exclude name="Generic.Files.EndFileNoNewline.Found" />
|
||||
<exclude name="Generic.Files.LowercasedFilename.NotFound" />
|
||||
<exclude name="Generic.Functions.OpeningFunctionBraceBsdAllman.BraceOnSameLine" />
|
||||
|
||||
<!-- These rules are in conflict with PSR2 -->
|
||||
<exclude name="Generic.Classes.OpeningBraceSameLine.BraceOnNewLine" />
|
||||
<exclude name="Generic.PHP.UpperCaseConstant.Found" />
|
||||
<exclude name="Generic.Functions.OpeningFunctionBraceKernighanRitchie.BraceOnNewLine" />
|
||||
<exclude name="Generic.Formatting.NoSpaceAfterCast.SpaceFound" />
|
||||
</rule>
|
||||
|
||||
<rule ref="PEAR">
|
||||
<!-- This rule is in conflict with Generic -->
|
||||
<exclude name="PEAR.WhiteSpace.ScopeIndent.IncorrectExact" />
|
||||
|
||||
<!-- This rule is in conflict with PSR2 -->
|
||||
<exclude name="PEAR.NamingConventions.ValidFunctionName.PrivateNoUnderscore"/>
|
||||
<exclude name="PEAR.NamingConventions.ValidVariableName.PrivateNoUnderscore" />
|
||||
</rule>
|
||||
|
||||
<!-- Some interesting rules from Squiz -->
|
||||
<rule ref="Squiz.PHP.NonExecutableCode"/>
|
||||
<rule ref="Squiz.PHP.CommentedOutCode"/>
|
||||
<rule ref="Squiz.PHP.DiscouragedFunctions"/>
|
||||
<rule ref="Squiz.PHP.ForbiddenFunctions"/>
|
||||
|
||||
<rule ref="Squiz.Classes.ClassDeclaration"/>
|
||||
<rule ref="Squiz.ControlStructures.ControlSignature"/>
|
||||
<rule ref="Squiz.ControlStructures.ForEachLoopDeclaration"/>
|
||||
<rule ref="Squiz.ControlStructures.ForLoopDeclaration"/>
|
||||
<rule ref="Squiz.Functions.FunctionDeclaration"/>
|
||||
<rule ref="Squiz.Functions.FunctionDeclarationArgumentSpacing"/>
|
||||
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace"/>
|
||||
|
||||
<!-- Our files are located in src and tests directories -->
|
||||
<file>.</file>
|
||||
<file>./test</file>
|
||||
</ruleset>
|
@ -0,0 +1,18 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset name="PHPMD rule set for TemplateEngine"
|
||||
xmlns="http://pmd.sf.net/ruleset/1.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0
|
||||
http://pmd.sf.net/ruleset_xml_schema.xsd"
|
||||
xsi:noNamespaceSchemaLocation="
|
||||
http://pmd.sf.net/ruleset_xml_schema.xsd">
|
||||
|
||||
<description>Custom rule set for TemplateEngine</description>
|
||||
|
||||
<rule ref="rulesets/cleancode.xml" />
|
||||
<rule ref="rulesets/codesize.xml" />
|
||||
<rule ref="rulesets/controversial.xml" />
|
||||
<rule ref="rulesets/design.xml" />
|
||||
<rule ref="rulesets/naming.xml" />
|
||||
<rule ref="rulesets/unusedcode.xml" />
|
||||
</ruleset>
|
Loading…
Reference in New Issue