-
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.
Merge pull request #4 from lmc-eu/feature/add-config-for-verbosity
Feature/add config for verbosity
- Loading branch information
Showing
12 changed files
with
289 additions
and
30 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
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
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
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
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
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
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
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
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,97 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Lmc\Cqrs\Bundle\Service; | ||
|
||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFilter; | ||
|
||
class ClassExtension extends AbstractExtension | ||
{ | ||
public function getFilters() | ||
{ | ||
return [ | ||
new TwigFilter( | ||
'genericClass', | ||
fn (string $class) => $this->formatGenericClass($class), | ||
['is_safe' => ['html']] | ||
), | ||
]; | ||
} | ||
|
||
private function formatGenericClass(string $class): string | ||
{ | ||
if (empty($class)) { | ||
return $class; | ||
} | ||
|
||
try { | ||
if ($this->tryParseClassWithoutGenerics($class, $shortName)) { | ||
return sprintf( | ||
'<small class="className">%s</small><strong>%s</strong>', | ||
$this->replaceOnceFromEnd($shortName, '', $class), | ||
$shortName | ||
); | ||
} elseif ($this->tryParseClassWithGenerics($class, $shortName, $genericArguments)) { | ||
if ($this->tryParseClassWithGenerics($genericArguments)) { | ||
$generics = $this->formatGenericClass($genericArguments); | ||
} else { | ||
$generics = array_map( | ||
fn (string $genericArgument) => $this->formatGenericClass(trim($genericArgument)), | ||
explode(',', $genericArguments) | ||
); | ||
|
||
$generics = implode(', ', $generics); | ||
} | ||
|
||
[$classWithoutGenerics] = explode('<', $class, 2); | ||
|
||
return sprintf( | ||
'<small class="className">%s</small><strong>%s</strong><%s>', | ||
$this->replaceOnceFromEnd($shortName, '', $classWithoutGenerics), | ||
$shortName, | ||
$generics, | ||
); | ||
} | ||
|
||
return $class; | ||
} catch (\Throwable $e) { | ||
return $class; | ||
} | ||
} | ||
|
||
private function replaceOnceFromEnd(string $search, string $replace, string $value): string | ||
{ | ||
$position = mb_strrpos($value, $search); | ||
if ($position === false) { | ||
return $value; | ||
} | ||
|
||
return substr_replace($value, $replace, $position, mb_strlen($search)); | ||
} | ||
|
||
private function tryParseClassWithoutGenerics(string $class, ?string &$shortClassName = null): bool | ||
{ | ||
if (preg_match('/^([A-Z][A-Za-z0-9]*\\\\)*([A-Z][A-Za-z]*?)$/', $class, $matches) === 1) { | ||
$shortClassName = array_pop($matches); | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private function tryParseClassWithGenerics( | ||
string $class, | ||
?string &$shortClassName = null, | ||
?string &$genericArguments = null | ||
): bool { | ||
if (preg_match('/^([A-Z][A-Za-z0-9]*\\\\)*([A-Z][A-Za-z]*?)<(.*)>$/', $class, $matches) === 1) { | ||
$genericArguments = array_pop($matches); | ||
$shortClassName = array_pop($matches); | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
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
Oops, something went wrong.