Skip to content

Commit

Permalink
improvements, php81
Browse files Browse the repository at this point in the history
  • Loading branch information
Gappa committed Aug 28, 2023
1 parent e79f5f8 commit 6d3a21c
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 32 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "nelson/svgfilter",
"description": "Simple inline SVG filter for Nette/Latte.",
"require": {
"php": "^8.0",
"php": "^8.1",
"nette/di": "^3.0",
"nette/utils": "^3.0 || ^4.0",
"latte/latte": "^3.0",
Expand All @@ -15,7 +15,9 @@
"phpstan/phpstan-nette": "^1.0.0",
"roave/security-advisories": "dev-master",
"phpunit/phpunit": "^9.0.0",
"tracy/tracy": "^2.9"
"tracy/tracy": "^2.9",
"phpstan/phpstan-strict-rules": "^1.4",
"phpstan/phpstan-deprecation-rules": "^1.1"
},
"autoload": {
"psr-4": {
Expand Down
Binary file added composer.phar
Binary file not shown.
6 changes: 4 additions & 2 deletions phpstan/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ parameters:

paths:
- %currentWorkingDirectory%/src/
# - %currentWorkingDirectory%/tests/
- %currentWorkingDirectory%/tests/

excludePaths:
- %currentWorkingDirectory%/tests/temp/*

level: 8
level: max

reportMaybesInPropertyPhpDocTypes: false
106 changes: 106 additions & 0 deletions run
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/usr/bin/env bash

# Define vars
MAINTENANCE_FILE=".maintenance";
PHP="php81"
COMPOSER_INSTALL_COMMAND_DEV="$PHP composer.phar install";

PHP="$PHP";
COMPOSER_INSTALL_COMMAND="$COMPOSER_INSTALL_COMMAND_DEV";

# Command shortcuts
PHPSTAN="$PHP vendor/bin/phpstan analyse --configuration ./phpstan/phpstan.neon --memory-limit 512M"
PHPUNIT="$PHP vendor/bin/phpunit --configuration tests/phpunit.xml tests"

# Graphics
#OK="\e[32m✔\e[0m";
#KO="\e[31m✖\e[0m";
HR="\e[95m---------------------------------------------------------------------------\e[0m";
VR="\e[95m|\e[0m";
#INFO="\e[96mINFO\e[0m"
#PROMPT="\e[93mQUESTION\e[0m"

# Define standard colors
if [[ $TERM == *xterm* ]]; then
BLACK=$(tput -Txterm setaf 0)
RED=$(tput -Txterm setaf 1)
GREEN=$(tput -Txterm setaf 2)
GREEN2="";
YELLOW=$(tput -Txterm setaf 3)
LIGHTPURPLE=$(tput -Txterm setaf 4)
PURPLE=$(tput -Txterm setaf 5)
BLUE=$(tput -Txterm setaf 6)
WHITE=$(tput -Txterm setaf 7)
RESET=$(tput -Txterm sgr0)
RESET2="";
else
BLACK=""
RED=""
GREEN=""
YELLOW=""
LIGHTPURPLE=""
PURPLE=""
BLUE=""
WHITE=""
RESET=""
fi

function help() { ## Print out this help
echo ""
echo "${YELLOW}Available commands:${RESET}"
grep -E '^function [a-zA-Z0-9_-]+\(\) { ## ' "$0" \
| sed -E 's/^function ([a-zA-Z0-9_-]+)\(\) \{ ## (.+)/'" ${GREEN2}"'\1'"${RESET2}"';\2/' \
| sort \
| column -t -s ";"
}

function composer() { ## Run composer with parameters
"$PHP" "composer.phar" "$@"
}

function phpstan() { ## Run phpstan
eval "$PHPSTAN"
}

function tests() { ## Run tests
eval "$PHPUNIT"
}

function test() { ## Run a specific set of tests, the argument is passed to the --filter param
eval "$PHPUNIT" --filter "$@"
}

function php() { ## Run the project's version of PHP
"$PHP" "$@"
}

function set-permissions() { ## Set correct permissions
chmodCommand="chmod ug+w . -R --quiet";
$chmodCommand
}

function log() {
echo -e "$HR";
echo -e "$VR \e[96m$1\e[0m";
echo -e "$HR";
}

function pre-push() { ## Run checks before pushing
tests;
}

# ------------------------------------------------ Must be at the end of the file

# Check if there are any arguments passed
if [[ $# -eq 0 ]]; then
help
exit 0
fi

# Check if the specified command is a function and call it
if declare -f "$1" > /dev/null; then
"$@"
else
echo -E "${RED}Error:${RESET} Unknown command '$1'. Type 'help' for a list of available commands."
exit 1
fi
2 changes: 1 addition & 1 deletion src/DI/SvgFilterExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
final class SvgFilterExtension extends CompilerExtension
{

/** @var SvgFilterConfig[] */
/** @var SvgFilterConfig */
public $config;


Expand Down
32 changes: 16 additions & 16 deletions src/SvgFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ final class SvgFilter


public function __construct(
private Storage $cacheStorage,
private SvgFilterConfig $config,
private readonly Storage $cacheStorage,
private readonly SvgFilterConfig $config,
) {
$this->cache = new Cache($this->cacheStorage, $config->cacheNS);
}
Expand All @@ -34,23 +34,23 @@ public function inline(
string $fill = null,
string $class = null,
): ?Html {
if (!empty($file)) {
$filepath = $this->config->assetsPath . $file;
if (strlen($file) === 0) {
return null;
}

// Has to be a string because of cache
$rawString = $this->cache->call([$this, 'getSvg'], $filepath);
$document = $this->getDOMDocument($rawString);
$element = $this->getSVGElement($document);
$filepath = $this->config->assetsPath . $file;

$this->applyDimensions($element, $width, $height);
$this->applyFill($element, $fill);
$this->applyClass($element, $class);
/** @var string $rawString Has to be a string because of cache */
$rawString = $this->cache->call([$this, 'getSvg'], $filepath);
$document = $this->getDOMDocument($rawString);
$element = $this->getSVGElement($document);

$html = $this->saveHtml($document) ?? '';
return Html::el()->setHtml($html);
}
$this->applyDimensions($element, $width, $height);
$this->applyFill($element, $fill);
$this->applyClass($element, $class);

return null;
$html = $this->saveHtml($document) ?? '';
return Html::el()->setHtml($html);
}


Expand All @@ -64,7 +64,7 @@ public function getSvg(string $filepath): ?string
$result = new DOMDocument;
$result->appendChild($result->importNode($element, true));

return $this->saveHTML($result);
return $this->saveHtml($result);
}


Expand Down
22 changes: 11 additions & 11 deletions tests/SvgFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,51 +23,51 @@ public static function setUpBeforeClass(): void
$config = new SvgFilterConfig;
$config->assetsPath = self::$assetsPath;

static::$svgFilter = new SvgFilter($storage, $config);
self::$svgFilter = new SvgFilter($storage, $config);
}


public function testSvgBasic(): void
{
$expected = file_get_contents(self::$assetsPath . 'basic.svg.expected');
$actual = self::$svgFilter->inline(self::$svgFile)->toHtml();
$actual = self::$svgFilter->inline(self::$svgFile)?->toHtml();

$this->assertSame($expected, $actual);
self::assertSame($expected, $actual);
}


public function testSvgDimensions(): void
{
$expected = file_get_contents(self::$assetsPath . 'dimensions.svg.expected');
$actual = self::$svgFilter->inline(self::$svgFile, 50, 50)->toHtml();
$actual = self::$svgFilter->inline(self::$svgFile, 50, 50)?->toHtml();

$this->assertSame($expected, $actual);
self::assertSame($expected, $actual);
}


public function testSvgFill(): void
{
$expected = file_get_contents(self::$assetsPath . 'fill.svg.expected');
$actual = self::$svgFilter->inline(self::$svgFile, fill: 'currentColor')->toHtml();
$actual = self::$svgFilter->inline(self::$svgFile, fill: 'currentColor')?->toHtml();

$this->assertSame($expected, $actual);
self::assertSame($expected, $actual);
}


public function testSvgClass(): void
{
$expected = file_get_contents(self::$assetsPath . 'class.svg.expected');
$actual = self::$svgFilter->inline(self::$svgFile, class: 'big')->toHtml();
$actual = self::$svgFilter->inline(self::$svgFile, class: 'big')?->toHtml();

$this->assertSame($expected, $actual);
self::assertSame($expected, $actual);
}


public function testSvgAll(): void
{
$expected = file_get_contents(self::$assetsPath . 'all.svg.expected');
$actual = self::$svgFilter->inline(self::$svgFile, 50, 50, 'currentColor', 'big')->toHtml();
$actual = self::$svgFilter->inline(self::$svgFile, 50, 50, 'currentColor', 'big')?->toHtml();

$this->assertSame($expected, $actual);
self::assertSame($expected, $actual);
}
}

0 comments on commit 6d3a21c

Please sign in to comment.