Skip to content

Commit

Permalink
Extract extensions methods in a dedicated ExtensionsTrait trait
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Dec 5, 2017
1 parent 2d4c2b0 commit cbb7a41
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 110 deletions.
43 changes: 30 additions & 13 deletions src/Phug/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,40 @@ protected function execute($facade, $method, $arguments, $outputFile)
return true;
}

protected function getNamedArgumentBySpaceDelimiter(array &$arguments, $index, $name)
{
if ($arguments[$index] === $name) {
array_splice($arguments, $index, 1);
if (isset($arguments[$index])) {
$value = $arguments[$index];
array_splice($arguments, $index, 1);

return $value;
}
}

return false;
}

protected function getNamedArgumentByEqualOperator(array &$arguments, $index, $name)
{
if (preg_match('/^'.preg_quote($name).'=(.*)$/', $arguments[$index], $match)) {
array_splice($arguments, $index, 1);

return $match[1];
}

return false;
}

protected function getNamedArgument(array &$arguments, array $names)
{
foreach ($names as $name) {
foreach ($arguments as $index => $argument) {
if ($argument === $name) {
array_splice($arguments, $index, 1);
if (isset($arguments[$index])) {
$value = $arguments[$index];
array_splice($arguments, $index, 1);

return $value;
}
}
if (preg_match('/^'.preg_quote($name).'=(.*)$/', $argument, $match)) {
array_splice($arguments, $index, 1);

return $match[1];
$value = $this->getNamedArgumentBySpaceDelimiter($arguments, $index, $name) ?:
$this->getNamedArgumentByEqualOperator($arguments, $index, $name);
if ($value) {
return $value;
}
}
}
Expand Down
111 changes: 111 additions & 0 deletions src/Phug/Partial/ExtensionsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace Phug\Partial;

use Phug\Renderer;
use Phug\Util\ModuleInterface;

trait ExtensionsTrait
{
/**
* List of global extensions. Class names that add custom behaviors to the engine.
*
* @var array
*/
private static $extensions = [];

private static function normalizeExtensionClassName($name)
{
return ltrim('\\', strtolower($name));
}

private static function getExtensionsGetters()
{
return [
'includes' => 'getIncludes',
'scanners' => 'getScanners',
'token_handlers' => 'getTokenHandlers',
'node_compilers' => 'getCompilers',
'formats' => 'getFormats',
'patterns' => 'getPatterns',
'filters' => 'getFilters',
'keywords' => 'getKeywords',
'element_handlers' => 'getElementHandlers',
'php_token_handlers' => 'getPhpTokenHandlers',
'assignment_handlers' => 'getAssignmentHandlers',
];
}

private static function removeExtensionFromCurrentRenderer($extensionClassName)
{
/* @var Renderer $renderer */
$renderer = self::$renderer;

if (is_a($extensionClassName, ModuleInterface::class, true)) {
$renderer->setOption(
'modules',
array_filter($renderer->getOption('modules'), function ($module) use ($extensionClassName) {
return $module !== $extensionClassName;
})
);

return;
}

$extension = new $extensionClassName();
foreach (['getOptions', 'getEvents'] as $method) {
static::removeOptions([], $extension->$method());
}
foreach (static::getExtensionsGetters() as $option => $method) {
static::removeOptions([$option], $extension->$method());
}
$rendererClassName = self::getRendererClassName();
$renderer->setOptionsDefaults((new $rendererClassName())->getOptions());
}

private static function extractExtensionOptions(&$options, $extensionClassName, $methods)
{
$extension = is_string($extensionClassName)
? new $extensionClassName()
: $extensionClassName;
foreach (['getOptions', 'getEvents'] as $method) {
$value = $extension->$method();
if (!empty($value)) {
$options = array_merge_recursive($options, $value);
}
}
foreach ($methods as $option => $method) {
$value = $extension->$method();
if (!empty($value)) {
$options = array_merge_recursive($options, [$option => $value]);
}
}
}

/**
* Get options from extensions list and default options.
*
* @param array $extensions list of extensions instances of class names
* @param array $options optional default options to merge with
*
* @return array
*/
public static function getExtensionsOptions(array $extensions, array $options = [])
{
$methods = static::getExtensionsGetters();
foreach ($extensions as $extensionClassName) {
if (is_a($extensionClassName, ModuleInterface::class, true)) {
if (!isset($options['modules'])) {
$options['modules'] = [];
}
$options['modules'][] = $extensionClassName;

continue;
}

static::extractExtensionOptions($options, $extensionClassName, $methods);
}

return $options;
}
}
98 changes: 3 additions & 95 deletions src/Phug/Phug.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Phug;

use Phug\Util\ModuleInterface;
use Phug\Partial\ExtensionsTrait;

class Phug
{
use ExtensionsTrait;

/**
* List of global filters stored as array where keys are filter names, and values the action callback.
*
Expand All @@ -20,13 +22,6 @@ class Phug
*/
private static $keywords = [];

/**
* List of global extensions. Class names that add custom behaviors to the engine.
*
* @var array
*/
private static $extensions = [];

/**
* The current rendering instance used for ::compile(File,Directory), ::render(File,Directory), ::display(File).
*
Expand All @@ -51,28 +46,6 @@ private static function normalizeKeywordName($name)
return str_replace(' ', '-', strtolower($name));
}

private static function normalizeExtensionClassName($name)
{
return ltrim('\\', strtolower($name));
}

private static function getExtensionsGetters()
{
return [
'includes' => 'getIncludes',
'scanners' => 'getScanners',
'token_handlers' => 'getTokenHandlers',
'node_compilers' => 'getCompilers',
'formats' => 'getFormats',
'patterns' => 'getPatterns',
'filters' => 'getFilters',
'keywords' => 'getKeywords',
'element_handlers' => 'getElementHandlers',
'php_token_handlers' => 'getPhpTokenHandlers',
'assignment_handlers' => 'getAssignmentHandlers',
];
}

private static function getOptions(array $options = [])
{
$extras = [];
Expand All @@ -84,71 +57,6 @@ private static function getOptions(array $options = [])
return array_merge_recursive(self::getExtensionsOptions(self::$extensions, $extras), $options);
}

private static function removeExtensionFromCurrentRenderer($extensionClassName)
{
if (is_a($extensionClassName, ModuleInterface::class, true)) {
self::$renderer->setOption(
'modules',
array_filter(self::$renderer->getOption('modules'), function ($module) use ($extensionClassName) {
return $module !== $extensionClassName;
})
);

return;
}

$extension = new $extensionClassName();
foreach (['getOptions', 'getEvents'] as $method) {
static::removeOptions([], $extension->$method());
}
foreach (static::getExtensionsGetters() as $option => $method) {
static::removeOptions([$option], $extension->$method());
}
$rendererClassName = self::getRendererClassName();
self::$renderer->setOptionsDefaults((new $rendererClassName())->getOptions());
}

/**
* Get options from extensions list and default options.
*
* @param array $extensions list of extensions instances of class names
* @param array $options optional default options to merge with
*
* @return array
*/
public static function getExtensionsOptions(array $extensions, array $options = [])
{
$methods = static::getExtensionsGetters();
foreach ($extensions as $extensionClassName) {
if (is_a($extensionClassName, ModuleInterface::class, true)) {
if (!isset($options['modules'])) {
$options['modules'] = [];
}
$options['modules'][] = $extensionClassName;

continue;
}

$extension = is_string($extensionClassName)
? new $extensionClassName()
: $extensionClassName;
foreach (['getOptions', 'getEvents'] as $method) {
$value = $extension->$method();
if (!empty($value)) {
$options = array_merge_recursive($options, $value);
}
}
foreach ($methods as $option => $method) {
$value = $extension->$method();
if (!empty($value)) {
$options = array_merge_recursive($options, [$option => $value]);
}
}
}

return $options;
}

/**
* Set the engine class used to render templates.
*
Expand Down
15 changes: 13 additions & 2 deletions tests/Phug/CliTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ protected function emptyDirectory($dir)
if (!is_dir($dir)) {
return;
}

foreach (scandir($dir) as $file) {
if ($file !== '.' && $file !== '..') {
$path = $dir.'/'.$file;
if (is_dir($path)) {
$this->emptyDirectory($path);
} else {
unlink($path);

continue;
}

unlink($path);
}
}
}
Expand Down Expand Up @@ -62,6 +65,8 @@ public function tearDown()
* @group cli
* @covers ::convertToKebabCase
* @covers ::convertToCamelCase
* @covers ::getNamedArgumentBySpaceDelimiter
* @covers ::getNamedArgumentByEqualOperator
* @covers ::getNamedArgument
* @covers ::execute
* @covers ::<public>
Expand Down Expand Up @@ -161,6 +166,8 @@ public function testCallableActions()
* @group cli
* @covers ::convertToKebabCase
* @covers ::convertToCamelCase
* @covers ::getNamedArgumentBySpaceDelimiter
* @covers ::getNamedArgumentByEqualOperator
* @covers ::getNamedArgument
* @covers ::execute
* @covers ::<public>
Expand All @@ -180,6 +187,8 @@ public function testOptions()
* @group cli
* @covers ::convertToKebabCase
* @covers ::convertToCamelCase
* @covers ::getNamedArgumentBySpaceDelimiter
* @covers ::getNamedArgumentByEqualOperator
* @covers ::getNamedArgument
* @covers ::execute
* @covers ::<public>
Expand Down Expand Up @@ -225,6 +234,8 @@ public function testCacheDirectory()
* @group cli
* @covers ::convertToKebabCase
* @covers ::convertToCamelCase
* @covers ::getNamedArgumentBySpaceDelimiter
* @covers ::getNamedArgumentByEqualOperator
* @covers ::getNamedArgument
* @covers ::execute
* @covers ::<public>
Expand Down
2 changes: 2 additions & 0 deletions tests/Phug/ExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function testGetters()
* @covers \Phug\Phug::addExtension
* @covers \Phug\Phug::removeExtension
* @covers \Phug\Phug::getOptions
* @covers \Phug\Phug::extractExtensionOptions
* @covers \Phug\Phug::getExtensionsOptions
* @covers \Phug\Phug::removeExtensionFromCurrentRenderer
*/
Expand Down Expand Up @@ -68,6 +69,7 @@ public function testImplement()
}

/**
* @covers \Phug\Phug::extractExtensionOptions
* @covers \Phug\Phug::getExtensionsOptions
* @covers \Phug\Phug::removeExtensionFromCurrentRenderer
*/
Expand Down

0 comments on commit cbb7a41

Please sign in to comment.