Skip to content

Commit

Permalink
Move extensions methods to ExtensionsTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Dec 5, 2017
1 parent cbb7a41 commit 0a1a8bb
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 70 deletions.
72 changes: 72 additions & 0 deletions src/Phug/Partial/ExtensionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Phug\Partial;

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

Expand Down Expand Up @@ -108,4 +109,75 @@ public static function getExtensionsOptions(array $extensions, array $options =

return $options;
}

/**
* Check if an extension is available globally.
*
* @param string $extensionClassName
*
* @return bool
*/
public static function hasExtension($extensionClassName)
{
return in_array(
self::normalizeExtensionClassName($extensionClassName),
array_map(
[self::class, 'normalizeExtensionClassName'],
self::$extensions
)
);
}

/**
* Add an extension to the Phug facade (will be available in the current renderer instance and next static calls).
* Throws an exception if the extension is not a valid class name.
*
* @param string $extensionClassName
*
* @throws PhugException
*/
public static function addExtension($extensionClassName)
{
if (!class_exists($extensionClassName)) {
throw new PhugException(
'Invalid '.$extensionClassName.' extension given: '.
'it must be a class name.'
);
}

if (!static::hasExtension($extensionClassName)) {
self::$extensions[] = $extensionClassName;

/* @var Renderer $renderer */
if ($renderer = self::$renderer) {
$renderer->setOptionsRecursive(static::getOptions());
}
}
}

/**
* Remove an extension from the Phug facade (remove from current renderer instance).
*
* @param string $extensionClassName
*/
public static function removeExtension($extensionClassName)
{
if (static::hasExtension($extensionClassName)) {
if (self::$renderer) {
self::removeExtensionFromCurrentRenderer($extensionClassName);
}

self::$extensions = array_diff(self::$extensions, [$extensionClassName]);
}
}

/**
* Get extensions list added through the Phug facade.
*
* @return array
*/
public static function getExtensions()
{
return self::$extensions;
}
}
70 changes: 0 additions & 70 deletions src/Phug/Phug.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,76 +432,6 @@ public static function getKeywords()
return self::$keywords;
}

/**
* Check if an extension is available globally.
*
* @param string $extensionClassName
*
* @return bool
*/
public static function hasExtension($extensionClassName)
{
return in_array(
self::normalizeExtensionClassName($extensionClassName),
array_map(
[self::class, 'normalizeExtensionClassName'],
self::$extensions
)
);
}

/**
* Add an extension to the Phug facade (will be available in the current renderer instance and next static calls).
* Throws an exception if the extension is not a valid class name.
*
* @param string $extensionClassName
*
* @throws PhugException
*/
public static function addExtension($extensionClassName)
{
if (!class_exists($extensionClassName)) {
throw new PhugException(
'Invalid '.$extensionClassName.' extension given: '.
'it must be a class name.'
);
}

if (!static::hasExtension($extensionClassName)) {
self::$extensions[] = $extensionClassName;

if (self::$renderer) {
self::$renderer->setOptionsRecursive(static::getOptions());
}
}
}

/**
* Remove an extension from the Phug facade (remove from current renderer instance).
*
* @param string $extensionClassName
*/
public static function removeExtension($extensionClassName)
{
if (static::hasExtension($extensionClassName)) {
if (self::$renderer) {
self::removeExtensionFromCurrentRenderer($extensionClassName);
}

self::$extensions = array_diff(self::$extensions, [$extensionClassName]);
}
}

/**
* Get extensions list added through the Phug facade.
*
* @return array
*/
public static function getExtensions()
{
return self::$extensions;
}

/**
* Cache a whole directory and return an array with [$successCount, $errorCount, $errorDetails].
*
Expand Down

0 comments on commit 0a1a8bb

Please sign in to comment.