Skip to content

Commit c8b066d

Browse files
committed
Port
0 parents  commit c8b066d

File tree

7 files changed

+205
-0
lines changed

7 files changed

+205
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
vendor
3+
composer.lock
4+
composer.phar

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Custom tag and modifier loader for Fenom
2+
=======
3+
4+
# Setup
5+
6+
# Use
7+
8+
# Format

authors.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Project Founder and Developer:
2+
3+
- Ivan Shalganov <[email protected]>

composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "fenom/entity-loader",
3+
"type": "library",
4+
"description": "Fenom extension: set you rule for tag and modifier loader",
5+
"keywords": ["fenom", "loader", "tag"],
6+
"license": "BSD-3",
7+
"authors": [
8+
{
9+
"name": "Ivan Shalganov",
10+
"email": "[email protected]"
11+
}
12+
],
13+
"require": {
14+
"php": ">=5.4.8"
15+
},
16+
"require-dev": {
17+
"fenom/fenom": "2.*",
18+
"phpunit/phpunit": "*",
19+
"satooshi/php-coveralls": "dev-master"
20+
},
21+
"autoload": {
22+
"psr-0": { "Fenom\\": "src/" }
23+
}
24+
}

phpunit.xml.dist

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
>
13+
<php>
14+
<ini name="memory_limit" value="-1"/>
15+
<ini name="display_errors" value="1"/>
16+
<ini name="error_reporting" value="-1"/>
17+
</php>
18+
19+
<testsuites>
20+
<testsuite >
21+
<directory>./tests/</directory>
22+
</testsuite>
23+
</testsuites>
24+
25+
<groups>
26+
<exclude>
27+
<group>benchmark</group>
28+
</exclude>
29+
</groups>
30+
31+
<filter>
32+
<whitelist>
33+
<directory>./src/</directory>
34+
</whitelist>
35+
<blacklist>
36+
<directory>./tests/</directory>
37+
</blacklist>
38+
</filter>
39+
<!--<logging>-->
40+
<!--<log type="coverage-clover" target="build/logs/clover.xml"/>-->
41+
<!--</logging>-->
42+
</phpunit>

src/Fenom/EntityLoaderTrait.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Fenom;
4+
5+
trait EntityLoaderTrait
6+
{
7+
8+
/**
9+
* @var callable[]
10+
*/
11+
private $_tag_loaders = [];
12+
/**
13+
* @var callable[]
14+
*/
15+
private $_modifier_loaders = [];
16+
17+
/**
18+
* @param $modifier
19+
* @param Template $template
20+
* @return bool|mixed
21+
*/
22+
protected function _loadModifier($modifier, $template)
23+
{
24+
foreach ($this->_modifier_loaders as $loader) {
25+
$mod = call_user_func($loader, $modifier, $template);
26+
if ($mod) {
27+
return $mod;
28+
}
29+
}
30+
31+
return false;
32+
}
33+
34+
/**
35+
* @param $tag
36+
* @param Template $template
37+
* @return bool|array
38+
*/
39+
protected function _loadTag($tag, $template)
40+
{
41+
foreach ($this->_tag_loaders as $loader) {
42+
$info = call_user_func($loader, $tag, $template);
43+
if ($info) {
44+
return $info;
45+
}
46+
}
47+
48+
return false;
49+
}
50+
51+
/**
52+
* Add modifiers loader
53+
*
54+
* @param callable $loader
55+
* @param bool $prepend
56+
* @return $this
57+
*/
58+
public function addModifierLoader(callable $loader, $prepend = false)
59+
{
60+
if ($prepend) {
61+
array_unshift($this->_modifier_loaders, $loader);
62+
} else {
63+
$this->_modifier_loaders[] = $loader;
64+
}
65+
return $this;
66+
}
67+
68+
/**
69+
* Add tags loader
70+
*
71+
* @param callable $loader
72+
* @param bool $prepend
73+
* @return $this
74+
*/
75+
public function addTagLoader(callable $loader, $prepend = false)
76+
{
77+
if ($prepend) {
78+
array_unshift($this->_tag_loaders, $loader);
79+
} else {
80+
$this->_tag_loaders[] = $loader;
81+
}
82+
return $this;
83+
}
84+
}

tests/EntityLoaderTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Fenom;
4+
5+
6+
class LoaderTest extends \PHPUnit_Framework_TestCase {
7+
8+
public static function inlineFunction() {
9+
return 'inline function';
10+
}
11+
12+
public function testLoaders() {
13+
$fenom = new TemplaterLoader(new Provider(__DIR__));
14+
15+
$fenom->addModifierLoader(function ($modifier, $template) {
16+
$this->assertSame('some_mod', $modifier);
17+
$this->assertInstanceOf('Fenom\Template', $template);
18+
return 'strtoupper';
19+
});
20+
21+
$fenom->addTagLoader(function ($tag, $template) {
22+
$this->assertSame('some_tag', $tag);
23+
$this->assertInstanceOf('Fenom\Template', $template);
24+
return array(
25+
"type" => \Fenom::INLINE_FUNCTION,
26+
"function" => __CLASS__.'::inlineFunction',
27+
"parser" => \Fenom::DEFAULT_FUNC_PARSER
28+
);
29+
});
30+
31+
$this->assertSame('inline function', $fenom->compileCode('{some_tag}')->fetch([]));
32+
$this->assertSame('MODIFIER', $fenom->compileCode('{$a|some_mod}')->fetch(["a" => "modifier"]));
33+
34+
}
35+
36+
}
37+
38+
class TemplaterLoader extends \Fenom {
39+
use EntityLoaderTrait;
40+
}

0 commit comments

Comments
 (0)