-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c8b066d
Showing
7 changed files
with
205 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.idea | ||
vendor | ||
composer.lock | ||
composer.phar |
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,8 @@ | ||
Custom tag and modifier loader for Fenom | ||
======= | ||
|
||
# Setup | ||
|
||
# Use | ||
|
||
# Format |
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,3 @@ | ||
Project Founder and Developer: | ||
|
||
- Ivan Shalganov <[email protected]> |
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,24 @@ | ||
{ | ||
"name": "fenom/entity-loader", | ||
"type": "library", | ||
"description": "Fenom extension: set you rule for tag and modifier loader", | ||
"keywords": ["fenom", "loader", "tag"], | ||
"license": "BSD-3", | ||
"authors": [ | ||
{ | ||
"name": "Ivan Shalganov", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.4.8" | ||
}, | ||
"require-dev": { | ||
"fenom/fenom": "2.*", | ||
"phpunit/phpunit": "*", | ||
"satooshi/php-coveralls": "dev-master" | ||
}, | ||
"autoload": { | ||
"psr-0": { "Fenom\\": "src/" } | ||
} | ||
} |
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,42 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
syntaxCheck="false" | ||
> | ||
<php> | ||
<ini name="memory_limit" value="-1"/> | ||
<ini name="display_errors" value="1"/> | ||
<ini name="error_reporting" value="-1"/> | ||
</php> | ||
|
||
<testsuites> | ||
<testsuite > | ||
<directory>./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<groups> | ||
<exclude> | ||
<group>benchmark</group> | ||
</exclude> | ||
</groups> | ||
|
||
<filter> | ||
<whitelist> | ||
<directory>./src/</directory> | ||
</whitelist> | ||
<blacklist> | ||
<directory>./tests/</directory> | ||
</blacklist> | ||
</filter> | ||
<!--<logging>--> | ||
<!--<log type="coverage-clover" target="build/logs/clover.xml"/>--> | ||
<!--</logging>--> | ||
</phpunit> |
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 | ||
|
||
namespace Fenom; | ||
|
||
trait EntityLoaderTrait | ||
{ | ||
|
||
/** | ||
* @var callable[] | ||
*/ | ||
private $_tag_loaders = []; | ||
/** | ||
* @var callable[] | ||
*/ | ||
private $_modifier_loaders = []; | ||
|
||
/** | ||
* @param $modifier | ||
* @param Template $template | ||
* @return bool|mixed | ||
*/ | ||
protected function _loadModifier($modifier, $template) | ||
{ | ||
foreach ($this->_modifier_loaders as $loader) { | ||
$mod = call_user_func($loader, $modifier, $template); | ||
if ($mod) { | ||
return $mod; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @param $tag | ||
* @param Template $template | ||
* @return bool|array | ||
*/ | ||
protected function _loadTag($tag, $template) | ||
{ | ||
foreach ($this->_tag_loaders as $loader) { | ||
$info = call_user_func($loader, $tag, $template); | ||
if ($info) { | ||
return $info; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Add modifiers loader | ||
* | ||
* @param callable $loader | ||
* @param bool $prepend | ||
* @return $this | ||
*/ | ||
public function addModifierLoader(callable $loader, $prepend = false) | ||
{ | ||
if ($prepend) { | ||
array_unshift($this->_modifier_loaders, $loader); | ||
} else { | ||
$this->_modifier_loaders[] = $loader; | ||
} | ||
return $this; | ||
} | ||
|
||
/** | ||
* Add tags loader | ||
* | ||
* @param callable $loader | ||
* @param bool $prepend | ||
* @return $this | ||
*/ | ||
public function addTagLoader(callable $loader, $prepend = false) | ||
{ | ||
if ($prepend) { | ||
array_unshift($this->_tag_loaders, $loader); | ||
} else { | ||
$this->_tag_loaders[] = $loader; | ||
} | ||
return $this; | ||
} | ||
} |
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,40 @@ | ||
<?php | ||
|
||
namespace Fenom; | ||
|
||
|
||
class LoaderTest extends \PHPUnit_Framework_TestCase { | ||
|
||
public static function inlineFunction() { | ||
return 'inline function'; | ||
} | ||
|
||
public function testLoaders() { | ||
$fenom = new TemplaterLoader(new Provider(__DIR__)); | ||
|
||
$fenom->addModifierLoader(function ($modifier, $template) { | ||
$this->assertSame('some_mod', $modifier); | ||
$this->assertInstanceOf('Fenom\Template', $template); | ||
return 'strtoupper'; | ||
}); | ||
|
||
$fenom->addTagLoader(function ($tag, $template) { | ||
$this->assertSame('some_tag', $tag); | ||
$this->assertInstanceOf('Fenom\Template', $template); | ||
return array( | ||
"type" => \Fenom::INLINE_FUNCTION, | ||
"function" => __CLASS__.'::inlineFunction', | ||
"parser" => \Fenom::DEFAULT_FUNC_PARSER | ||
); | ||
}); | ||
|
||
$this->assertSame('inline function', $fenom->compileCode('{some_tag}')->fetch([])); | ||
$this->assertSame('MODIFIER', $fenom->compileCode('{$a|some_mod}')->fetch(["a" => "modifier"])); | ||
|
||
} | ||
|
||
} | ||
|
||
class TemplaterLoader extends \Fenom { | ||
use EntityLoaderTrait; | ||
} |