From 77c946c6e1062b011b55e004f30a2ee3d6b5a536 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Wed, 5 Feb 2020 07:57:48 +0100 Subject: [PATCH 01/66] composer.json - updated suggestions - oyejorge/less.php is abandoned, use new maintaned fork. - leafo/scssphp is abandoned/moved, use new fork. - joseki/webloader-filters is abandoned, might not even work. - coffeescript/coffeescript is abandoned, also might not work. --- composer.json | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index d12c705..0325157 100755 --- a/composer.json +++ b/composer.json @@ -23,10 +23,8 @@ "ext-json": "*" }, "suggest": { - "oyejorge/less.php": "LESS compiler written in PHP.", - "leafo/scssphp": "SCSS compiler written in PHP.", - "joseki/webloader-filters": "CSSMin & JSMin filters written in PHP.", - "coffeescript/coffeescript": "CoffeeScript compiler written in PHP." + "wikimedia/less.php": "LESS compiler written in PHP.", + "scssphp/scssphp": "SCSS compiler written in PHP." }, "require-dev": { "nette/application": "^3.0", From 1b9a319c9b4796829ade3cff471f35bc6550dfa9 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Wed, 29 Apr 2020 10:26:14 +0200 Subject: [PATCH 02/66] editor config --- .editorconfig | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100755 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100755 index 0000000..a412170 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,14 @@ +# EditorConfig is awesome: http://EditorConfig.org + +root = true + +[*] +end_of_line = lf +insert_final_newline = true +indent_style = tab +indent_size = 3 +charset = utf-8 +trim_trailing_whitespace = true + +[{*.svg, .htaccess}] +insert_final_newline = false From 4ad1e2efa84714383aae375842d1a6fa5b89685b Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Wed, 29 Apr 2020 10:26:24 +0200 Subject: [PATCH 03/66] gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 45bdece..1c00021 100755 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ vendor composer.lock .idea -.editorconfig +*.bak +temp/ From f5ae0ee66d8560abfad1829721ebf17f92bd946d Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Wed, 29 Apr 2020 11:04:49 +0200 Subject: [PATCH 04/66] update composer, fix tests --- WebLoader/Filter/ScssFilter.php | 13 +++++++------ composer.json | 32 ++++++++++++-------------------- tests/Filter/ScssFilterTest.php | 3 ++- 3 files changed, 21 insertions(+), 27 deletions(-) diff --git a/WebLoader/Filter/ScssFilter.php b/WebLoader/Filter/ScssFilter.php index 6a61483..51d1595 100644 --- a/WebLoader/Filter/ScssFilter.php +++ b/WebLoader/Filter/ScssFilter.php @@ -4,7 +4,8 @@ namespace WebLoader\Filter; -use Leafo\ScssPhp\Compiler; +use ScssPhp\ScssPhp\Compiler as ScssCompiler; +use WebLoader\Compiler; /** * Scss CSS filter @@ -15,28 +16,28 @@ class ScssFilter { - /** @var Compiler|null */ + /** @var ScssCompiler|null */ private $sc; - public function __construct(?Compiler $sc = null) + public function __construct(?ScssCompiler $sc = null) { $this->sc = $sc; } - private function getScssC(): Compiler + private function getScssC(): ScssCompiler { // lazy loading if (empty($this->sc)) { - $this->sc = new Compiler(); + $this->sc = new ScssCompiler(); } return $this->sc; } - public function __invoke(string $code, \WebLoader\Compiler $loader, string $file): string + public function __invoke(string $code, Compiler $loader, string $file): string { $file = (string) $file; diff --git a/composer.json b/composer.json index 0325157..c000228 100755 --- a/composer.json +++ b/composer.json @@ -31,35 +31,27 @@ "nette/bootstrap": "^3.0", "nette/caching": "^3.0", "nette/component-model": "^3.0", - "nette/database": "^3.0", - "nette/deprecated": "^2.3", "nette/di": "^3.0", "nette/finder": "^2.5", "nette/forms": "^3.0", "nette/http": "^3.0", - "nette/mail": "^3.0", "nette/neon": "^3.0", - "nette/php-generator": "^3.0", - "nette/reflection": "^2.4", "nette/robot-loader": "^3.0", - "nette/safe-stream": "^2.4", - "nette/security": "^3.0", - "nette/tokenizer": "^3.0", "nette/utils": "^3.0", - "latte/latte": "^2.5", - "tracy/tracy": "^2.6", - "oyejorge/less.php": "^1.7", - "leafo/scssphp": "^0.7", - "kylekatarnls/coffeescript": "1.3.*", + "latte/latte": "^2.8", + "tracy/tracy": "^2.7", + "wikimedia/less.php": "^3.0.0", + "scssphp/scssphp": "^1.1.0", "mockery/mockery": "1.*", "phpunit/phpunit": "7.*", - "jakub-onderka/php-parallel-lint": "~0.7", - "phpstan/phpstan-shim": "^0.11.0", - "phpstan/phpstan-nette": "^0.11.0" + "phpstan/extension-installer": "^1.0", + "phpstan/phpstan": "^0.12.0", + "phpstan/phpstan-nette": "^0.12.0", + "roave/security-advisories": "dev-master", + "kylekatarnls/coffeescript": "1.3.*" }, - "extra": { - "branch-alias": { - "dev-master": "2.4-dev" - } + "scripts": { + "phpstan": "./phpstan/phpstan", + "tests": "./vendor/bin/phpunit --configuration tests/phpunit.xml tests" } } diff --git a/tests/Filter/ScssFilterTest.php b/tests/Filter/ScssFilterTest.php index 1dd4904..2ddaf3a 100755 --- a/tests/Filter/ScssFilterTest.php +++ b/tests/Filter/ScssFilterTest.php @@ -4,6 +4,7 @@ namespace WebLoader\Test\Filter; use PHPUnit\Framework\TestCase; +use ScssPhp\ScssPhp\Compiler as ScssCompiler; use WebLoader\Compiler; use WebLoader\DefaultOutputNamingConvention; use WebLoader\FileCollection; @@ -21,7 +22,7 @@ class ScssFilterTest extends TestCase protected function setUp(): void { - $this->filter = new ScssFilter(new \Leafo\ScssPhp\Compiler()); + $this->filter = new ScssFilter(new ScssCompiler()); $files = new FileCollection(__DIR__ . '/../fixtures'); @mkdir($outputDir = __DIR__ . '/../temp/'); From 1275282a116afb4f2ec65ea8e4212011f1e8f40a Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Wed, 29 Apr 2020 11:37:51 +0200 Subject: [PATCH 05/66] psr-4 --- composer.json | 7 +++++-- {WebLoader => src}/Compiler.php | 0 {WebLoader => src}/DefaultOutputNamingConvention.php | 0 {WebLoader => src}/File.php | 0 {WebLoader => src}/FileCollection.php | 0 {WebLoader => src}/FileNotFoundException.php | 0 {WebLoader => src}/Filter/CoffeeScriptFilter.php | 0 {WebLoader => src}/Filter/CssUrlsFilter.php | 0 {WebLoader => src}/Filter/LessBinFilter.php | 0 {WebLoader => src}/Filter/LessFilter.php | 0 {WebLoader => src}/Filter/PHPCoffeeScriptFilter.php | 0 {WebLoader => src}/Filter/Process.php | 0 {WebLoader => src}/Filter/ScssFilter.php | 0 {WebLoader => src}/Filter/StylusFilter.php | 0 {WebLoader => src}/Filter/TypeScriptFilter.php | 0 {WebLoader => src}/Filter/VariablesFilter.php | 0 {WebLoader => src}/IFileCollection.php | 0 {WebLoader => src}/IOutputNamingConvention.php | 0 {WebLoader => src}/InvalidArgumentException.php | 0 {WebLoader => src}/Nette/CompilationException.php | 0 {WebLoader => src}/Nette/CssLoader.php | 0 {WebLoader => src}/Nette/CssUrlFilter.php | 0 {WebLoader => src}/Nette/Diagnostics/Panel.php | 0 {WebLoader => src}/Nette/Diagnostics/panel.latte | 0 {WebLoader => src}/Nette/Extension.php | 0 {WebLoader => src}/Nette/JavaScriptLoader.php | 0 {WebLoader => src}/Nette/LoaderFactory.php | 0 .../Nette/SymfonyConsole/GenerateCommand.php | 0 {WebLoader => src}/Nette/WebLoader.php | 0 {WebLoader => src}/Path.php | 0 {WebLoader => src}/WebLoaderException.php | 0 31 files changed, 5 insertions(+), 2 deletions(-) rename {WebLoader => src}/Compiler.php (100%) rename {WebLoader => src}/DefaultOutputNamingConvention.php (100%) rename {WebLoader => src}/File.php (100%) rename {WebLoader => src}/FileCollection.php (100%) rename {WebLoader => src}/FileNotFoundException.php (100%) rename {WebLoader => src}/Filter/CoffeeScriptFilter.php (100%) rename {WebLoader => src}/Filter/CssUrlsFilter.php (100%) rename {WebLoader => src}/Filter/LessBinFilter.php (100%) rename {WebLoader => src}/Filter/LessFilter.php (100%) rename {WebLoader => src}/Filter/PHPCoffeeScriptFilter.php (100%) rename {WebLoader => src}/Filter/Process.php (100%) rename {WebLoader => src}/Filter/ScssFilter.php (100%) rename {WebLoader => src}/Filter/StylusFilter.php (100%) rename {WebLoader => src}/Filter/TypeScriptFilter.php (100%) rename {WebLoader => src}/Filter/VariablesFilter.php (100%) rename {WebLoader => src}/IFileCollection.php (100%) rename {WebLoader => src}/IOutputNamingConvention.php (100%) rename {WebLoader => src}/InvalidArgumentException.php (100%) rename {WebLoader => src}/Nette/CompilationException.php (100%) rename {WebLoader => src}/Nette/CssLoader.php (100%) rename {WebLoader => src}/Nette/CssUrlFilter.php (100%) rename {WebLoader => src}/Nette/Diagnostics/Panel.php (100%) rename {WebLoader => src}/Nette/Diagnostics/panel.latte (100%) rename {WebLoader => src}/Nette/Extension.php (100%) rename {WebLoader => src}/Nette/JavaScriptLoader.php (100%) rename {WebLoader => src}/Nette/LoaderFactory.php (100%) rename {WebLoader => src}/Nette/SymfonyConsole/GenerateCommand.php (100%) rename {WebLoader => src}/Nette/WebLoader.php (100%) rename {WebLoader => src}/Path.php (100%) rename {WebLoader => src}/WebLoaderException.php (100%) diff --git a/composer.json b/composer.json index c000228..859b2ef 100755 --- a/composer.json +++ b/composer.json @@ -11,10 +11,13 @@ } ], "autoload": { - "psr-0": { - "WebLoader": "" + "psr-4": { + "WebLoader\\": "src/" } }, + "autoload-dev": { + "psr-4": { "WebLoader\\Test\\": "tests/" } + }, "require": { "php": ">= 7.1", "nette/application": "^3.0", diff --git a/WebLoader/Compiler.php b/src/Compiler.php similarity index 100% rename from WebLoader/Compiler.php rename to src/Compiler.php diff --git a/WebLoader/DefaultOutputNamingConvention.php b/src/DefaultOutputNamingConvention.php similarity index 100% rename from WebLoader/DefaultOutputNamingConvention.php rename to src/DefaultOutputNamingConvention.php diff --git a/WebLoader/File.php b/src/File.php similarity index 100% rename from WebLoader/File.php rename to src/File.php diff --git a/WebLoader/FileCollection.php b/src/FileCollection.php similarity index 100% rename from WebLoader/FileCollection.php rename to src/FileCollection.php diff --git a/WebLoader/FileNotFoundException.php b/src/FileNotFoundException.php similarity index 100% rename from WebLoader/FileNotFoundException.php rename to src/FileNotFoundException.php diff --git a/WebLoader/Filter/CoffeeScriptFilter.php b/src/Filter/CoffeeScriptFilter.php similarity index 100% rename from WebLoader/Filter/CoffeeScriptFilter.php rename to src/Filter/CoffeeScriptFilter.php diff --git a/WebLoader/Filter/CssUrlsFilter.php b/src/Filter/CssUrlsFilter.php similarity index 100% rename from WebLoader/Filter/CssUrlsFilter.php rename to src/Filter/CssUrlsFilter.php diff --git a/WebLoader/Filter/LessBinFilter.php b/src/Filter/LessBinFilter.php similarity index 100% rename from WebLoader/Filter/LessBinFilter.php rename to src/Filter/LessBinFilter.php diff --git a/WebLoader/Filter/LessFilter.php b/src/Filter/LessFilter.php similarity index 100% rename from WebLoader/Filter/LessFilter.php rename to src/Filter/LessFilter.php diff --git a/WebLoader/Filter/PHPCoffeeScriptFilter.php b/src/Filter/PHPCoffeeScriptFilter.php similarity index 100% rename from WebLoader/Filter/PHPCoffeeScriptFilter.php rename to src/Filter/PHPCoffeeScriptFilter.php diff --git a/WebLoader/Filter/Process.php b/src/Filter/Process.php similarity index 100% rename from WebLoader/Filter/Process.php rename to src/Filter/Process.php diff --git a/WebLoader/Filter/ScssFilter.php b/src/Filter/ScssFilter.php similarity index 100% rename from WebLoader/Filter/ScssFilter.php rename to src/Filter/ScssFilter.php diff --git a/WebLoader/Filter/StylusFilter.php b/src/Filter/StylusFilter.php similarity index 100% rename from WebLoader/Filter/StylusFilter.php rename to src/Filter/StylusFilter.php diff --git a/WebLoader/Filter/TypeScriptFilter.php b/src/Filter/TypeScriptFilter.php similarity index 100% rename from WebLoader/Filter/TypeScriptFilter.php rename to src/Filter/TypeScriptFilter.php diff --git a/WebLoader/Filter/VariablesFilter.php b/src/Filter/VariablesFilter.php similarity index 100% rename from WebLoader/Filter/VariablesFilter.php rename to src/Filter/VariablesFilter.php diff --git a/WebLoader/IFileCollection.php b/src/IFileCollection.php similarity index 100% rename from WebLoader/IFileCollection.php rename to src/IFileCollection.php diff --git a/WebLoader/IOutputNamingConvention.php b/src/IOutputNamingConvention.php similarity index 100% rename from WebLoader/IOutputNamingConvention.php rename to src/IOutputNamingConvention.php diff --git a/WebLoader/InvalidArgumentException.php b/src/InvalidArgumentException.php similarity index 100% rename from WebLoader/InvalidArgumentException.php rename to src/InvalidArgumentException.php diff --git a/WebLoader/Nette/CompilationException.php b/src/Nette/CompilationException.php similarity index 100% rename from WebLoader/Nette/CompilationException.php rename to src/Nette/CompilationException.php diff --git a/WebLoader/Nette/CssLoader.php b/src/Nette/CssLoader.php similarity index 100% rename from WebLoader/Nette/CssLoader.php rename to src/Nette/CssLoader.php diff --git a/WebLoader/Nette/CssUrlFilter.php b/src/Nette/CssUrlFilter.php similarity index 100% rename from WebLoader/Nette/CssUrlFilter.php rename to src/Nette/CssUrlFilter.php diff --git a/WebLoader/Nette/Diagnostics/Panel.php b/src/Nette/Diagnostics/Panel.php similarity index 100% rename from WebLoader/Nette/Diagnostics/Panel.php rename to src/Nette/Diagnostics/Panel.php diff --git a/WebLoader/Nette/Diagnostics/panel.latte b/src/Nette/Diagnostics/panel.latte similarity index 100% rename from WebLoader/Nette/Diagnostics/panel.latte rename to src/Nette/Diagnostics/panel.latte diff --git a/WebLoader/Nette/Extension.php b/src/Nette/Extension.php similarity index 100% rename from WebLoader/Nette/Extension.php rename to src/Nette/Extension.php diff --git a/WebLoader/Nette/JavaScriptLoader.php b/src/Nette/JavaScriptLoader.php similarity index 100% rename from WebLoader/Nette/JavaScriptLoader.php rename to src/Nette/JavaScriptLoader.php diff --git a/WebLoader/Nette/LoaderFactory.php b/src/Nette/LoaderFactory.php similarity index 100% rename from WebLoader/Nette/LoaderFactory.php rename to src/Nette/LoaderFactory.php diff --git a/WebLoader/Nette/SymfonyConsole/GenerateCommand.php b/src/Nette/SymfonyConsole/GenerateCommand.php similarity index 100% rename from WebLoader/Nette/SymfonyConsole/GenerateCommand.php rename to src/Nette/SymfonyConsole/GenerateCommand.php diff --git a/WebLoader/Nette/WebLoader.php b/src/Nette/WebLoader.php similarity index 100% rename from WebLoader/Nette/WebLoader.php rename to src/Nette/WebLoader.php diff --git a/WebLoader/Path.php b/src/Path.php similarity index 100% rename from WebLoader/Path.php rename to src/Path.php diff --git a/WebLoader/WebLoaderException.php b/src/WebLoaderException.php similarity index 100% rename from WebLoader/WebLoaderException.php rename to src/WebLoaderException.php From c5a57c4b3f2a51b36a1ecec416fea67ac6f4f3ac Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Wed, 29 Apr 2020 12:07:12 +0200 Subject: [PATCH 06/66] phpstan fixes --- composer.json | 3 +- phpstan/phpstan.neon | 12 ++++---- src/Compiler.php | 6 ++-- src/DefaultOutputNamingConvention.php | 8 +++--- src/FileCollection.php | 7 ++++- src/Filter/LessFilter.php | 6 ++++ src/Nette/Diagnostics/Panel.php | 6 ++++ src/Nette/LoaderFactory.php | 22 ++++++++++---- src/Nette/SymfonyConsole/GenerateCommand.php | 30 +++++++++++--------- src/Nette/WebLoader.php | 2 +- 10 files changed, 68 insertions(+), 34 deletions(-) diff --git a/composer.json b/composer.json index 859b2ef..90d9784 100755 --- a/composer.json +++ b/composer.json @@ -51,7 +51,8 @@ "phpstan/phpstan": "^0.12.0", "phpstan/phpstan-nette": "^0.12.0", "roave/security-advisories": "dev-master", - "kylekatarnls/coffeescript": "1.3.*" + "kylekatarnls/coffeescript": "1.3.*", + "symfony/console": "^4.2.9|^5.0.0" }, "scripts": { "phpstan": "./phpstan/phpstan", diff --git a/phpstan/phpstan.neon b/phpstan/phpstan.neon index 331b515..980ed2a 100755 --- a/phpstan/phpstan.neon +++ b/phpstan/phpstan.neon @@ -1,13 +1,13 @@ parameters: + tmpDir: %currentWorkingDirectory%/temp/phpstan/ autoload_directories: - - WebLoader/ + - %currentWorkingDirectory%/src/ +# - %currentWorkingDirectory%/tests/ paths: - - WebLoader/ + - %currentWorkingDirectory%/src/ +# - %currentWorkingDirectory%/tests/ level: 7 - -includes: - - ../vendor/phpstan/phpstan-nette/extension.neon - - ../vendor/phpstan/phpstan-nette/rules.neon + checkMissingIterableValueType: false diff --git a/src/Compiler.php b/src/Compiler.php index 38a06dc..83da13c 100755 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -62,7 +62,7 @@ public function __construct(IFileCollection $files, IOutputNamingConvention $con */ public static function createCssCompiler(IFileCollection $files, string $outputDir): self { - return new static($files, DefaultOutputNamingConvention::createCssConvention(), $outputDir); + return new self($files, DefaultOutputNamingConvention::createCssConvention(), $outputDir); } @@ -71,7 +71,7 @@ public static function createCssCompiler(IFileCollection $files, string $outputD */ public static function createJsCompiler(IFileCollection $files, string $outputDir): self { - return new static($files, DefaultOutputNamingConvention::createJsConvention(), $outputDir); + return new self($files, DefaultOutputNamingConvention::createJsConvention(), $outputDir); } @@ -255,7 +255,7 @@ public function generate(): array } - protected function generateFiles(array $files, array $watchFiles = []) + protected function generateFiles(array $files, array $watchFiles = []): File { $name = $this->namingConvention->getFilename($files, $this); $path = $this->outputDir . '/' . $name; diff --git a/src/DefaultOutputNamingConvention.php b/src/DefaultOutputNamingConvention.php index b470c18..2bfeb40 100644 --- a/src/DefaultOutputNamingConvention.php +++ b/src/DefaultOutputNamingConvention.php @@ -9,7 +9,7 @@ * * @author Jan Marek */ -class DefaultOutputNamingConvention implements \WebLoader\IOutputNamingConvention +class DefaultOutputNamingConvention implements IOutputNamingConvention { /** @var string */ @@ -21,7 +21,7 @@ class DefaultOutputNamingConvention implements \WebLoader\IOutputNamingConventio public static function createCssConvention(): self { - $convention = new static(); + $convention = new self(); $convention->setSuffix('.css'); return $convention; @@ -30,7 +30,7 @@ public static function createCssConvention(): self public static function createJsConvention(): self { - $convention = new static(); + $convention = new self(); $convention->setSuffix('.js'); return $convention; @@ -84,7 +84,7 @@ public function getFilename(array $files, Compiler $compiler): string } - protected function createHash(array $files, Compiler $compiler) + protected function createHash(array $files, Compiler $compiler): string { $parts = $files; foreach ($files as $file) { diff --git a/src/FileCollection.php b/src/FileCollection.php index 9da7fe6..0e1c663 100755 --- a/src/FileCollection.php +++ b/src/FileCollection.php @@ -4,6 +4,7 @@ namespace WebLoader; +use SplFileInfo; use Traversable; /** @@ -11,7 +12,7 @@ * * @author Jan Marek */ -class FileCollection implements \WebLoader\IFileCollection +class FileCollection implements IFileCollection { /** @var string */ @@ -66,6 +67,10 @@ public function cannonicalizePath(string $path): string } + /** + * @param string|SplFileInfo $file + * @throws FileNotFoundException + */ public function addFile($file): void { $file = $this->cannonicalizePath((string) $file); diff --git a/src/Filter/LessFilter.php b/src/Filter/LessFilter.php index c775d48..eee8c7f 100644 --- a/src/Filter/LessFilter.php +++ b/src/Filter/LessFilter.php @@ -15,6 +15,8 @@ */ class LessFilter { + + /** @var lessc|null */ private $lc; @@ -37,6 +39,10 @@ private function getLessC(): lessc /** * Invoke filter + * @param string $code + * @param Compiler $loader + * @param string $file + * @return string */ public function __invoke(string $code, Compiler $loader, string $file): string { diff --git a/src/Nette/Diagnostics/Panel.php b/src/Nette/Diagnostics/Panel.php index f66088c..088279d 100644 --- a/src/Nette/Diagnostics/Panel.php +++ b/src/Nette/Diagnostics/Panel.php @@ -53,6 +53,8 @@ public function __construct(?string $appDir = null) /** * Registers a compiler. * + * @param string $name + * @param Compiler $compiler * @return Panel */ public function addLoader(string $name, Compiler $compiler): self @@ -77,6 +79,10 @@ private function compute(): array ]; $this->files = $this->sizes = []; + /** + * @var string $name + * @var Compiler $compiler + */ foreach ($this->compilers as $name => $compiler) { $group = lcfirst(substr($name, $name[0] === 'c' ? 3 : 2)); diff --git a/src/Nette/LoaderFactory.php b/src/Nette/LoaderFactory.php index 450edde..8d3d1eb 100755 --- a/src/Nette/LoaderFactory.php +++ b/src/Nette/LoaderFactory.php @@ -11,20 +11,32 @@ class LoaderFactory { - /** @var \Nette\Http\IRequest */ + /** @var IRequest */ private $httpRequest; - /** @var \Nette\DI\Container */ + /** @var Container */ private $serviceLocator; - /** @var array */ + /** @var array */ private $tempPaths; /** @var string */ private $extensionName; - public function __construct(array $tempPaths, string $extensionName, IRequest $httpRequest, Container $serviceLocator) + /** + * LoaderFactory constructor. + * @param array $tempPaths + * @param string $extensionName + * @param IRequest $httpRequest + * @param Container $serviceLocator + */ + public function __construct( + array $tempPaths, + string $extensionName, + IRequest $httpRequest, + Container $serviceLocator + ) { $this->httpRequest = $httpRequest; $this->serviceLocator = $serviceLocator; @@ -49,7 +61,7 @@ public function createJavaScriptLoader(string $name, bool $appendLastModified = } - private function formatTempPath(string $name, $absoluteUrl = false): string + private function formatTempPath(string $name, bool $absoluteUrl = false): string { $lName = strtolower($name); $tempPath = isset($this->tempPaths[$lName]) ? $this->tempPaths[$lName] : Extension::DEFAULT_TEMP_PATH; diff --git a/src/Nette/SymfonyConsole/GenerateCommand.php b/src/Nette/SymfonyConsole/GenerateCommand.php index 7b121ba..ce45673 100755 --- a/src/Nette/SymfonyConsole/GenerateCommand.php +++ b/src/Nette/SymfonyConsole/GenerateCommand.php @@ -4,29 +4,32 @@ namespace WebLoader\Nette\SymfonyConsole; -use Nette; +use Nette\DI\Container; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use WebLoader; +use WebLoader\Compiler; /** * Generate Command */ -class GenerateCommand extends \Symfony\Component\Console\Command\Command +class GenerateCommand extends Command { - /** @var \WebLoader\Compiler[] */ + /** @var Compiler[] */ private $compilers = []; + /** @var string */ protected static $defaultName = 'webloader:generate'; - public function __construct(Nette\DI\Container $container) + public function __construct(Container $container) { parent::__construct(); - $compilers = $container->findByType(WebLoader\Compiler::class); + $compilers = $container->findByType(Compiler::class); foreach ($compilers as $compilerName) { $this->compilers[$compilerName] = $container->getService($compilerName); } @@ -41,21 +44,22 @@ protected function configure(): void } - protected function execute(InputInterface $input, OutputInterface $output): void + protected function execute(InputInterface $input, OutputInterface $output): int { - $force = $input->getOption('force'); - - $nofiles = true; + $noFiles = true; foreach ($this->compilers as $compiler) { - $files = $compiler->generate(!$force); + $files = $compiler->generate(); foreach ($files as $file) { - $output->writeln($file->file); - $nofiles = false; + $output->writeln($file->getFile()); + $noFiles = false; } } - if ($nofiles) { + if ($noFiles) { $output->writeln('No files generated.'); + return 1; } + + return 0; } } diff --git a/src/Nette/WebLoader.php b/src/Nette/WebLoader.php index a3ec962..4fbc6e0 100755 --- a/src/Nette/WebLoader.php +++ b/src/Nette/WebLoader.php @@ -96,7 +96,7 @@ public function render(): void } - protected function getGeneratedFilePath(File $file) + protected function getGeneratedFilePath(File $file): string { $path = $this->tempPath . '/' . $file->getFile(); From f7f36fd9009003022b874f271564d2f43ba830a7 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Wed, 29 Apr 2020 12:10:33 +0200 Subject: [PATCH 07/66] code style --- src/Nette/LoaderFactory.php | 3 +-- src/Nette/SymfonyConsole/GenerateCommand.php | 7 +++---- 2 files changed, 4 insertions(+), 6 deletions(-) mode change 100755 => 100644 src/Nette/LoaderFactory.php mode change 100755 => 100644 src/Nette/SymfonyConsole/GenerateCommand.php diff --git a/src/Nette/LoaderFactory.php b/src/Nette/LoaderFactory.php old mode 100755 new mode 100644 index 8d3d1eb..69b8a60 --- a/src/Nette/LoaderFactory.php +++ b/src/Nette/LoaderFactory.php @@ -36,8 +36,7 @@ public function __construct( string $extensionName, IRequest $httpRequest, Container $serviceLocator - ) - { + ) { $this->httpRequest = $httpRequest; $this->serviceLocator = $serviceLocator; $this->tempPaths = $tempPaths; diff --git a/src/Nette/SymfonyConsole/GenerateCommand.php b/src/Nette/SymfonyConsole/GenerateCommand.php old mode 100755 new mode 100644 index ce45673..13a4993 --- a/src/Nette/SymfonyConsole/GenerateCommand.php +++ b/src/Nette/SymfonyConsole/GenerateCommand.php @@ -9,7 +9,6 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use WebLoader; use WebLoader\Compiler; /** @@ -18,12 +17,12 @@ class GenerateCommand extends Command { - /** @var Compiler[] */ - private $compilers = []; - /** @var string */ protected static $defaultName = 'webloader:generate'; + /** @var Compiler[] */ + private $compilers = []; + public function __construct(Container $container) { From 5b710956e2070981faa210f74ca8ed301236fb1c Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Wed, 29 Apr 2020 13:09:31 +0200 Subject: [PATCH 08/66] fix tests, phpstan --- composer.json | 3 +- phpstan/phpstan.neon | 18 ++++++++- src/FileNotFoundException.php | 2 +- src/Filter/VariablesFilter.php | 8 ++-- tests/CompilerTest.php | 42 ++++++++----------- tests/DefaultOutputNamingConventionTest.php | 6 ++- tests/FileCollectionTest.php | 45 ++++++++++++--------- tests/Filter/LessFilterTest.php | 6 ++- tests/Filter/ScssFilterTest.php | 10 +++-- tests/Filter/VariablesFilterTest.php | 1 + tests/Nette/ExtensionTest.php | 8 ++-- 11 files changed, 87 insertions(+), 62 deletions(-) diff --git a/composer.json b/composer.json index 90d9784..0dc9be8 100755 --- a/composer.json +++ b/composer.json @@ -52,7 +52,8 @@ "phpstan/phpstan-nette": "^0.12.0", "roave/security-advisories": "dev-master", "kylekatarnls/coffeescript": "1.3.*", - "symfony/console": "^4.2.9|^5.0.0" + "symfony/console": "^4.2.9|^5.0.0", + "phpstan/phpstan-mockery": "^0.12.5" }, "scripts": { "phpstan": "./phpstan/phpstan", diff --git a/phpstan/phpstan.neon b/phpstan/phpstan.neon index 980ed2a..8aa21b2 100755 --- a/phpstan/phpstan.neon +++ b/phpstan/phpstan.neon @@ -2,12 +2,26 @@ parameters: tmpDir: %currentWorkingDirectory%/temp/phpstan/ autoload_directories: - %currentWorkingDirectory%/src/ -# - %currentWorkingDirectory%/tests/ + - %currentWorkingDirectory%/tests/ paths: - %currentWorkingDirectory%/src/ -# - %currentWorkingDirectory%/tests/ + - %currentWorkingDirectory%/tests/ + + excludes_analyse: + - %currentWorkingDirectory%/tests/temp/* level: 7 checkMissingIterableValueType: false + + ignoreErrors: + - + message: '#Parameter \#1 \$filter of method WebLoader\\Compiler::addFilter\(\) expects callable\(\): mixed, 4 given\.#' + path: %currentWorkingDirectory%/tests/CompilerTest.php + - + message: '#Parameter \#1 \$filter of method WebLoader\\Compiler::addFileFilter\(\) expects callable\(\): mixed, 4 given\.#' + path: %currentWorkingDirectory%/tests/CompilerTest.php + - + message: '#Access to an undefined property WebLoader\\Filter\\VariablesFilter::\$bar\.#' + path: %currentWorkingDirectory%/tests/Filter/VariablesFilterTest.php diff --git a/src/FileNotFoundException.php b/src/FileNotFoundException.php index bda3b4b..42544ed 100644 --- a/src/FileNotFoundException.php +++ b/src/FileNotFoundException.php @@ -9,6 +9,6 @@ * * @author Jan Marek */ -class FileNotFoundException extends \WebLoader\WebLoaderException +class FileNotFoundException extends WebLoaderException { } diff --git a/src/Filter/VariablesFilter.php b/src/Filter/VariablesFilter.php index 65e0b84..4b7fc17 100644 --- a/src/Filter/VariablesFilter.php +++ b/src/Filter/VariablesFilter.php @@ -4,6 +4,8 @@ namespace WebLoader\Filter; +use WebLoader\InvalidArgumentException; + /** * Variables filter for WebLoader * @@ -37,7 +39,7 @@ public function __construct(array $variables = []) /** * Set delimiter * - * @return \WebLoader\Filter\VariablesFilter + * @return VariablesFilter */ public function setDelimiter(string $start, string $end): self { @@ -77,14 +79,14 @@ public function __set(string $name, string $value): void /** * Magic get variable, do not call directly * - * @throws \WebLoader\InvalidArgumentException + * @throws InvalidArgumentException */ public function &__get(string $name): string { if (array_key_exists($name, $this->variables)) { return $this->variables[$name]; } else { - throw new \WebLoader\InvalidArgumentException("Variable '$name' is not set."); + throw new InvalidArgumentException("Variable '$name' is not set."); } } } diff --git a/tests/CompilerTest.php b/tests/CompilerTest.php index 03ab732..d2ea0cb 100755 --- a/tests/CompilerTest.php +++ b/tests/CompilerTest.php @@ -5,8 +5,10 @@ use Mockery; use PHPUnit\Framework\TestCase; +use TypeError; use WebLoader\Compiler; use WebLoader\File; +use WebLoader\FileNotFoundException; /** * CompilerTest @@ -16,7 +18,7 @@ class CompilerTest extends TestCase { - /** @var \WebLoader\Compiler */ + /** @var Compiler */ private $object; @@ -49,7 +51,7 @@ protected function setUp(): void private function getTempFiles(): array { - return glob(__DIR__ . '/temp/webloader-*'); + return (array) glob(__DIR__ . '/temp/webloader-*'); } @@ -58,8 +60,8 @@ public function testJoinFiles(): void $this->assertTrue($this->object->getJoinFiles()); $ret = $this->object->generate(); - $this->assertEquals(1, count($ret), 'Multiple files are generated instead of join.'); - $this->assertEquals(1, count($this->getTempFiles()), 'Multiple files are generated instead of join.'); + $this->assertCount(1, $ret, 'Multiple files are generated instead of join.'); + $this->assertCount(1, $this->getTempFiles(), 'Multiple files are generated instead of join.'); } @@ -69,8 +71,8 @@ public function testEmptyFiles(): void $this->object->setFileCollection(new \WebLoader\FileCollection()); $ret = $this->object->generate(); - $this->assertEquals(0, count($ret)); - $this->assertEquals(0, count($this->getTempFiles())); + $this->assertCount(0, $ret); + $this->assertCount(0, $this->getTempFiles()); } @@ -80,16 +82,14 @@ public function testNotJoinFiles(): void $this->assertFalse($this->object->getJoinFiles()); $ret = $this->object->generate(); - $this->assertEquals(3, count($ret), 'Wrong file count generated.'); - $this->assertEquals(3, count($this->getTempFiles()), 'Wrong file count generated.'); + $this->assertCount(3, $ret, 'Wrong file count generated.'); + $this->assertCount(3, $this->getTempFiles(), 'Wrong file count generated.'); } - /** - * @expectedException \WebLoader\FileNotFoundException - */ public function testSetOutDir(): void { + $this->expectException(FileNotFoundException::class); $this->object->setOutputDir('blablabla'); } @@ -112,9 +112,6 @@ public function testGeneratingAndFilters(): void $expectedContent = '-' . PHP_EOL . 'a:cba,' . PHP_EOL . 'b:fed,' . PHP_EOL . 'c:ihg,-' . PHP_EOL . 'a:cba,' . PHP_EOL . 'b:fed,' . PHP_EOL . 'c:ihg,'; - /** - * @var $files File[] - */ $files = $this->object->generate(); $this->assertTrue(is_numeric($files[0]->getLastModified()) && $files[0]->getLastModified() > 0, 'Generate does not provide last modified timestamp correctly.'); @@ -127,11 +124,8 @@ public function testGeneratingAndFilters(): void public function testGenerateReturnsSourceFilePaths(): void { - /** - * @var $res File[] - */ $res = $this->object->generate(); - $this->assertInternalType('array', $res[0]->getSourceFiles()); + $this->assertIsArray($res[0]->getSourceFiles()); $this->assertCount(3, $res[0]->getSourceFiles()); $this->assertFileExists($res[0]->getSourceFiles()[0]); } @@ -139,7 +133,7 @@ public function testGenerateReturnsSourceFilePaths(): void public function testFilters(): void { - $filter = function ($code, \WebLoader\Compiler $loader) { + $filter = function ($code, Compiler $loader) { return $code . $code; }; $this->object->addFilter($filter); @@ -150,7 +144,7 @@ public function testFilters(): void public function testFileFilters(): void { - $filter = function ($code, \WebLoader\Compiler $loader, $file = null) { + $filter = function ($code, Compiler $loader, $file = null) { return $code . $code; }; $this->object->addFileFilter($filter); @@ -159,20 +153,16 @@ public function testFileFilters(): void } - /** - * @expectedException \TypeError - */ public function testNonCallableFilter(): void { + $this->expectException(TypeError::class); $this->object->addFilter(4); } - /** - * @expectedException \TypeError - */ public function testNonCallableFileFilter(): void { + $this->expectException(TypeError::class); $this->object->addFileFilter(4); } } diff --git a/tests/DefaultOutputNamingConventionTest.php b/tests/DefaultOutputNamingConventionTest.php index df285a1..5f94985 100755 --- a/tests/DefaultOutputNamingConventionTest.php +++ b/tests/DefaultOutputNamingConventionTest.php @@ -3,7 +3,10 @@ namespace WebLoader\Test; +use Mockery; +use Mockery\LegacyMockInterface; use PHPUnit\Framework\TestCase; +use WebLoader\Compiler; use WebLoader\DefaultOutputNamingConvention; /** @@ -17,13 +20,14 @@ class DefaultOutputNamingConventionTest extends TestCase /** @var DefaultOutputNamingConvention */ private $object; + /** @var Compiler */ private $compiler; protected function setUp(): void { $this->object = new DefaultOutputNamingConvention(); - $this->compiler = \Mockery::mock('Webloader\Compiler'); + $this->compiler = Mockery::mock(Compiler::class); } diff --git a/tests/FileCollectionTest.php b/tests/FileCollectionTest.php index c5af843..eb1ca0f 100755 --- a/tests/FileCollectionTest.php +++ b/tests/FileCollectionTest.php @@ -3,8 +3,11 @@ namespace WebLoader\Test; +use ArrayIterator; use PHPUnit\Framework\TestCase; +use SplFileInfo; use WebLoader\FileCollection; +use WebLoader\FileNotFoundException; /** * FileCollection test @@ -39,16 +42,14 @@ public function testAddGetFiles(): void } - /** - * @expectedException \Webloader\FileNotFoundException - */ - public function testAddNonExistingFile() + public function testAddNonExistingFile(): void { + $this->expectException(FileNotFoundException::class); $this->object->addFile('sdfsdg.txt'); } - public function testRemoveFile() + public function testRemoveFile(): void { $this->object->addFile(__DIR__ . '/fixtures/a.txt'); $this->object->addFile(__DIR__ . '/fixtures/b.txt'); @@ -63,7 +64,7 @@ public function testRemoveFile() } - public function testCannonicalizePath() + public function testCannonicalizePath(): void { $abs = __DIR__ . '/./fixtures/a.txt'; $rel = 'a.txt'; @@ -75,12 +76,12 @@ public function testCannonicalizePath() try { $this->object->cannonicalizePath('nesdagf'); $this->fail('Exception was not thrown.'); - } catch (\WebLoader\FileNotFoundException $e) { + } catch (FileNotFoundException $e) { } } - public function testClear() + public function testClear(): void { $this->object->addFile('a.txt'); $this->object->addRemoteFile('http://jquery.com/jquery.js'); @@ -93,7 +94,7 @@ public function testClear() } - public function testRemoteFiles() + public function testRemoteFiles(): void { $this->object->addRemoteFile('http://jquery.com/jquery.js'); $this->object->addRemoteFiles([ @@ -109,7 +110,7 @@ public function testRemoteFiles() } - public function testWatchFiles() + public function testWatchFiles(): void { $this->object->addWatchFile(__DIR__ . '/fixtures/a.txt'); $this->object->addWatchFile(__DIR__ . '/fixtures/b.txt'); @@ -123,28 +124,32 @@ public function testWatchFiles() } - public function testTraversableFiles() + public function testTraversableFiles(): void { - $this->object->addFiles(new \ArrayIterator(['a.txt'])); - $this->assertEquals(1, count($this->object->getFiles())); + $this->object->addFiles(new ArrayIterator(['a.txt'])); + $this->assertCount(1, $this->object->getFiles()); } - public function testTraversableRemoteFiles() + public function testTraversableRemoteFiles(): void { - $this->object->addRemoteFiles(new \ArrayIterator(['http://jquery.com/jquery.js'])); - $this->assertEquals(1, count($this->object->getRemoteFiles())); + $this->object->addRemoteFiles(new ArrayIterator(['http://jquery.com/jquery.js'])); + $this->assertCount(1, $this->object->getRemoteFiles()); } - public function testSplFileInfo() + public function testSplFileInfo(): void { - $this->object->addFile(new \SplFileInfo(__DIR__ . '/fixtures/a.txt')); - $this->assertEquals(1, count($this->object->getFiles())); + $this->object->addFile(new SplFileInfo(__DIR__ . '/fixtures/a.txt')); + $this->assertCount(1, $this->object->getFiles()); } - private function assertEqualPaths($expected, $actual) + /** + * @param mixed $expected + * @param mixed $actual + */ + private function assertEqualPaths($expected, $actual): void { $actual = (array) $actual; foreach ((array) $expected as $key => $path) { diff --git a/tests/Filter/LessFilterTest.php b/tests/Filter/LessFilterTest.php index e0d9c0d..ce505a2 100755 --- a/tests/Filter/LessFilterTest.php +++ b/tests/Filter/LessFilterTest.php @@ -31,7 +31,11 @@ protected function setUp(): void public function testReplace(): void { $file = __DIR__ . '/../fixtures/style.less'; - $less = $this->filter->__invoke(file_get_contents($file), $this->compiler, $file); + $less = $this->filter->__invoke( + (string) file_get_contents($file), + $this->compiler, + $file + ); $this->assertSame(file_get_contents(__DIR__ . '/../fixtures/style.less.expected'), $less); } } diff --git a/tests/Filter/ScssFilterTest.php b/tests/Filter/ScssFilterTest.php index 2ddaf3a..5af3c48 100755 --- a/tests/Filter/ScssFilterTest.php +++ b/tests/Filter/ScssFilterTest.php @@ -33,7 +33,11 @@ protected function setUp(): void public function testReplace(): void { $file = __DIR__ . '/../fixtures/style.scss'; - $less = $this->filter->__invoke(file_get_contents($file), $this->compiler, $file); + $less = $this->filter->__invoke( + (string) file_get_contents($file), + $this->compiler, + $file + ); $this->assertSame(file_get_contents(__DIR__ . '/../fixtures/style.scss.expected'), $less); } @@ -42,9 +46,9 @@ public function testImportAbsolutePath(): void { $file = __DIR__ . '/../fixtures/styleAbsolute.scss'; $filter = new VariablesFilter([ - 'fixturesAbsolutePath' => realpath(__DIR__ . '/../fixtures'), + 'fixturesAbsolutePath' => (string) realpath(__DIR__ . '/../fixtures'), ]); - $code = file_get_contents($file); + $code = (string) file_get_contents($file); $filtered = $filter($code); $less = $this->filter->__invoke($filtered, $this->compiler, $file); $this->assertSame(file_get_contents(__DIR__ . '/../fixtures/styleAbsolute.scss.expected'), $less); diff --git a/tests/Filter/VariablesFilterTest.php b/tests/Filter/VariablesFilterTest.php index 3c3b534..4870ac6 100755 --- a/tests/Filter/VariablesFilterTest.php +++ b/tests/Filter/VariablesFilterTest.php @@ -4,6 +4,7 @@ namespace WebLoader\Test\Filter; use PHPUnit\Framework\TestCase; +use stdClass; use WebLoader\Filter\VariablesFilter; class VariablesFilterTest extends TestCase diff --git a/tests/Nette/ExtensionTest.php b/tests/Nette/ExtensionTest.php index 3fbb4c6..8b2c7ae 100755 --- a/tests/Nette/ExtensionTest.php +++ b/tests/Nette/ExtensionTest.php @@ -11,7 +11,6 @@ use WebLoader\Nette\Extension; use WebLoader\Path; - class ExtensionTest extends TestCase { @@ -19,7 +18,7 @@ class ExtensionTest extends TestCase private $container; - private function prepareContainer($configFiles): void + private function prepareContainer(array $configFiles): void { $tempDir = __DIR__ . '/../temp'; foreach (Finder::findFiles('*')->exclude('.gitignore')->from($tempDir . '/cache') as $file) { @@ -29,6 +28,7 @@ private function prepareContainer($configFiles): void $configurator = new Configurator(); $configurator->setTempDirectory($tempDir); + /** @var string $file */ foreach ($configFiles as $file) { $configurator->addConfig($file); } @@ -39,7 +39,7 @@ private function prepareContainer($configFiles): void 'tempDir' => $tempDir, ]); - $extension = new Extension(__DIR__ . '/..', $configurator->isDebugMode()); + $extension = new Extension(); $extension->install($configurator); $this->container = @$configurator->createContainer(); // sends header X-Powered-By, ... @@ -180,7 +180,7 @@ public function testExtensionName(): void $configurator->setTempDirectory($tempDir); $configurator->addParameters(['container' => ['class' => $class]]); $configurator->onCompile[] = function (Configurator $configurator, Compiler $compiler) { - $extension = new Extension(__DIR__ . '/..', $configurator->isDebugMode()); + $extension = new Extension(); $compiler->addExtension('Foo', $extension); }; $configurator->addConfig(__DIR__ . '/../fixtures/extensionName.neon'); From 73ab23c5c283c06e53c89a0c256e23ef63be7c90 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Wed, 29 Apr 2020 13:10:15 +0200 Subject: [PATCH 09/66] code style --- tests/CompilerTest.php | 1 - tests/DefaultOutputNamingConventionTest.php | 1 - tests/Filter/VariablesFilterTest.php | 1 - 3 files changed, 3 deletions(-) mode change 100755 => 100644 tests/CompilerTest.php mode change 100755 => 100644 tests/DefaultOutputNamingConventionTest.php mode change 100755 => 100644 tests/Filter/VariablesFilterTest.php diff --git a/tests/CompilerTest.php b/tests/CompilerTest.php old mode 100755 new mode 100644 index d2ea0cb..079ef38 --- a/tests/CompilerTest.php +++ b/tests/CompilerTest.php @@ -7,7 +7,6 @@ use PHPUnit\Framework\TestCase; use TypeError; use WebLoader\Compiler; -use WebLoader\File; use WebLoader\FileNotFoundException; /** diff --git a/tests/DefaultOutputNamingConventionTest.php b/tests/DefaultOutputNamingConventionTest.php old mode 100755 new mode 100644 index 5f94985..d720424 --- a/tests/DefaultOutputNamingConventionTest.php +++ b/tests/DefaultOutputNamingConventionTest.php @@ -4,7 +4,6 @@ namespace WebLoader\Test; use Mockery; -use Mockery\LegacyMockInterface; use PHPUnit\Framework\TestCase; use WebLoader\Compiler; use WebLoader\DefaultOutputNamingConvention; diff --git a/tests/Filter/VariablesFilterTest.php b/tests/Filter/VariablesFilterTest.php old mode 100755 new mode 100644 index 4870ac6..3c3b534 --- a/tests/Filter/VariablesFilterTest.php +++ b/tests/Filter/VariablesFilterTest.php @@ -4,7 +4,6 @@ namespace WebLoader\Test\Filter; use PHPUnit\Framework\TestCase; -use stdClass; use WebLoader\Filter\VariablesFilter; class VariablesFilterTest extends TestCase From a7d03056e8adde6ad580a610995a1804e719140e Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Wed, 29 Apr 2020 13:55:27 +0200 Subject: [PATCH 10/66] composer.json - cleanup, added suggestions for css/js minification --- composer.json | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 0dc9be8..3c7d06a 100755 --- a/composer.json +++ b/composer.json @@ -27,20 +27,19 @@ }, "suggest": { "wikimedia/less.php": "LESS compiler written in PHP.", - "scssphp/scssphp": "SCSS compiler written in PHP." + "scssphp/scssphp": "SCSS compiler written in PHP.", + "tedivm/jshrink": "Javascript Minifier built in PHP", + "tubalmartin/cssmin": "A PHP port of the YUI CSS compressor" }, "require-dev": { - "nette/application": "^3.0", "nette/bootstrap": "^3.0", "nette/caching": "^3.0", "nette/component-model": "^3.0", - "nette/di": "^3.0", "nette/finder": "^2.5", "nette/forms": "^3.0", "nette/http": "^3.0", "nette/neon": "^3.0", "nette/robot-loader": "^3.0", - "nette/utils": "^3.0", "latte/latte": "^2.8", "tracy/tracy": "^2.7", "wikimedia/less.php": "^3.0.0", From 41098bea3c9a2aa0b2641db03298675d8604a3d0 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Wed, 29 Apr 2020 15:02:16 +0200 Subject: [PATCH 11/66] Added CssMinFilter, JsMinFilter, updated LessFilter + tests --- .editorconfig | 2 +- composer.json | 4 ++- src/Filter/CssMinFilter.php | 20 +++++++++++ src/Filter/JsMinFilter.php | 21 +++++++++++ src/Filter/LessFilter.php | 39 +++++--------------- tests/Filter/CssMinFilterTest.php | 41 +++++++++++++++++++++ tests/Filter/JsMinFilterTest.php | 41 +++++++++++++++++++++ tests/Filter/LessFilterTest.php | 7 ++-- tests/fixtures/cssmin.css | 57 ++++++++++++++++++++++++++++++ tests/fixtures/cssmin.css.expected | 1 + tests/fixtures/jsmin.js | 9 +++++ tests/fixtures/jsmin.js.expected | 1 + 12 files changed, 208 insertions(+), 35 deletions(-) create mode 100755 src/Filter/CssMinFilter.php create mode 100755 src/Filter/JsMinFilter.php create mode 100755 tests/Filter/CssMinFilterTest.php create mode 100755 tests/Filter/JsMinFilterTest.php create mode 100755 tests/fixtures/cssmin.css create mode 100755 tests/fixtures/cssmin.css.expected create mode 100755 tests/fixtures/jsmin.js create mode 100755 tests/fixtures/jsmin.js.expected diff --git a/.editorconfig b/.editorconfig index a412170..8502e51 100755 --- a/.editorconfig +++ b/.editorconfig @@ -10,5 +10,5 @@ indent_size = 3 charset = utf-8 trim_trailing_whitespace = true -[{*.svg, .htaccess}] +[{*.svg, .htaccess, *.expected}] insert_final_newline = false diff --git a/composer.json b/composer.json index 3c7d06a..cc9ab73 100755 --- a/composer.json +++ b/composer.json @@ -52,7 +52,9 @@ "roave/security-advisories": "dev-master", "kylekatarnls/coffeescript": "1.3.*", "symfony/console": "^4.2.9|^5.0.0", - "phpstan/phpstan-mockery": "^0.12.5" + "phpstan/phpstan-mockery": "^0.12.5", + "tubalmartin/cssmin": "^4.1", + "tedivm/jshrink": "^1.3" }, "scripts": { "phpstan": "./phpstan/phpstan", diff --git a/src/Filter/CssMinFilter.php b/src/Filter/CssMinFilter.php new file mode 100755 index 0000000..e28e417 --- /dev/null +++ b/src/Filter/CssMinFilter.php @@ -0,0 +1,20 @@ +run($code); + } +} diff --git a/src/Filter/JsMinFilter.php b/src/Filter/JsMinFilter.php new file mode 100755 index 0000000..4596033 --- /dev/null +++ b/src/Filter/JsMinFilter.php @@ -0,0 +1,21 @@ +lc = $lc; - } - - - private function getLessC(): lessc - { - // lazy loading - if (empty($this->lc)) { - $this->lc = new lessc(); - } - - return clone $this->lc; + return new Less_Parser; } - /** - * Invoke filter - * @param string $code - * @param Compiler $loader - * @param string $file - * @return string - */ public function __invoke(string $code, Compiler $loader, string $file): string { if (pathinfo($file, PATHINFO_EXTENSION) === 'less') { - $lessc = $this->getLessC(); - $lessc->importDir = pathinfo($file, PATHINFO_DIRNAME) . '/'; - return $lessc->compile($code); + $parser = $this->getLessParser(); + $parser->parseFile($file); + return $parser->getCss(); } return $code; diff --git a/tests/Filter/CssMinFilterTest.php b/tests/Filter/CssMinFilterTest.php new file mode 100755 index 0000000..df3dc2b --- /dev/null +++ b/tests/Filter/CssMinFilterTest.php @@ -0,0 +1,41 @@ +filter = new CssMinFilter(); + + $files = new FileCollection(__DIR__ . '/../fixtures'); + @mkdir($outputDir = __DIR__ . '/../temp/'); + $this->compiler = new Compiler($files, new DefaultOutputNamingConvention(), $outputDir); + } + + + public function testMinify(): void + { + $file = __DIR__ . '/../fixtures/cssmin.css'; + $minified = $this->filter->__invoke( + (string) file_get_contents($file), + $this->compiler, + $file + ); + $this->assertSame(file_get_contents(__DIR__ . '/../fixtures/cssmin.css.expected'), $minified); + } +} diff --git a/tests/Filter/JsMinFilterTest.php b/tests/Filter/JsMinFilterTest.php new file mode 100755 index 0000000..084a032 --- /dev/null +++ b/tests/Filter/JsMinFilterTest.php @@ -0,0 +1,41 @@ +filter = new JsMinFilter(); + + $files = new FileCollection(__DIR__ . '/../fixtures'); + @mkdir($outputDir = __DIR__ . '/../temp/'); + $this->compiler = new Compiler($files, new DefaultOutputNamingConvention(), $outputDir); + } + + + public function testMinify(): void + { + $file = __DIR__ . '/../fixtures/jsmin.js'; + $minified = $this->filter->__invoke( + (string) file_get_contents($file), + $this->compiler, + $file + ); + $this->assertSame(file_get_contents(__DIR__ . '/../fixtures/jsmin.js.expected'), $minified); + } +} diff --git a/tests/Filter/LessFilterTest.php b/tests/Filter/LessFilterTest.php index ce505a2..ebd73d8 100755 --- a/tests/Filter/LessFilterTest.php +++ b/tests/Filter/LessFilterTest.php @@ -20,7 +20,7 @@ class LessFilterTest extends TestCase protected function setUp(): void { - $this->filter = new LessFilter(new \lessc()); + $this->filter = new LessFilter(); $files = new FileCollection(__DIR__ . '/../fixtures'); @mkdir($outputDir = __DIR__ . '/../temp/'); @@ -31,11 +31,12 @@ protected function setUp(): void public function testReplace(): void { $file = __DIR__ . '/../fixtures/style.less'; - $less = $this->filter->__invoke( + $minified = $this->filter->__invoke( (string) file_get_contents($file), $this->compiler, $file ); - $this->assertSame(file_get_contents(__DIR__ . '/../fixtures/style.less.expected'), $less); + + $this->assertSame(file_get_contents(__DIR__ . '/../fixtures/style.less.expected'), $minified); } } diff --git a/tests/fixtures/cssmin.css b/tests/fixtures/cssmin.css new file mode 100755 index 0000000..6ad0010 --- /dev/null +++ b/tests/fixtures/cssmin.css @@ -0,0 +1,57 @@ +.clearFix { + display: block; + zoom: 1; +} +.clearFix:after { + content: " "; + display: block; + font-size: 0; + height: 0; + clear: both; + visibility: hidden; +} +div.banners { + display: block; + zoom: 1; + padding: 0 0 20px; + margin: 0 10px; + border-bottom: #f4f4f4 1px solid; +} +div.banners:after { + content: " "; + display: block; + font-size: 0; + height: 0; + clear: both; + visibility: hidden; +} +div.banners > div { + float: left; + width: 610px; + height: 194px; + background: #f9f2e8; + margin: 20px 0 0 20px; + position: relative; +} +div.banners > div h3 { + width: auto; + color: #be2025; + font-weight: 600; + padding: 10px 20px; + margin: 10px 10px 0 10px; + text-shadow: 0 2px 0 rgba(0, 0, 0, 0.3); + display: inline-block; + font-size: 24px; +} +div.banners > div h3, +div.banners > div p { + color: #ffffff; +} +div.banners > div p { + font-size: 13px; + max-width: 360px; + padding: 10px; +} +div.banners > div p strong { + font-weight: 600; +} diff --git a/tests/fixtures/cssmin.css.expected b/tests/fixtures/cssmin.css.expected new file mode 100755 index 0000000..10ae4dd --- /dev/null +++ b/tests/fixtures/cssmin.css.expected @@ -0,0 +1 @@ +.clearFix{display:block;zoom:1}.clearFix:after{content:" ";display:block;font-size:0;height:0;clear:both;visibility:hidden}div.banners{display:block;zoom:1;padding:0 0 20px;margin:0 10px;border-bottom:#f4f4f4 1px solid}div.banners:after{content:" ";display:block;font-size:0;height:0;clear:both;visibility:hidden}div.banners>div{float:left;width:610px;height:194px;background:#f9f2e8;margin:20px 0 0 20px;position:relative}div.banners>div h3{width:auto;color:#be2025;font-weight:600;padding:10px 20px;margin:10px 10px 0;text-shadow:0 2px 0 rgba(0,0,0,.3);display:inline-block;font-size:24px}div.banners>div h3,div.banners>div p{color:#fff}div.banners>div p{font-size:13px;max-width:360px;padding:10px}div.banners>div p strong{font-weight:600} \ No newline at end of file diff --git a/tests/fixtures/jsmin.js b/tests/fixtures/jsmin.js new file mode 100755 index 0000000..2dd0288 --- /dev/null +++ b/tests/fixtures/jsmin.js @@ -0,0 +1,9 @@ +;(function() { + window.alert('Hello World!'); + + if (true === false) { + window.alert('Paradox'); + } else { + window.alert('All is well.'); + } +})(); diff --git a/tests/fixtures/jsmin.js.expected b/tests/fixtures/jsmin.js.expected new file mode 100755 index 0000000..238dd79 --- /dev/null +++ b/tests/fixtures/jsmin.js.expected @@ -0,0 +1 @@ +;(function(){window.alert('Hello World!');if(true===false){window.alert('Paradox');}else{window.alert('All is well.');}})(); \ No newline at end of file From c39ba32ed64808ce8bc359dcf7a9d37de8e59111 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Ferkl?= Date: Mon, 15 Jul 2019 10:18:00 +0200 Subject: [PATCH 12/66] merge, fix tests --- src/Nette/Extension.php | 78 +++++++++++++++++------------ tests/Nette/ExtensionTest.php | 93 ++++++++++++++++++++++------------- 2 files changed, 105 insertions(+), 66 deletions(-) diff --git a/src/Nette/Extension.php b/src/Nette/Extension.php index 20ce4d4..3c72424 100755 --- a/src/Nette/Extension.php +++ b/src/Nette/Extension.php @@ -8,8 +8,10 @@ use Nette\DI\Compiler; use Nette\DI\CompilerExtension; use Nette\DI\ContainerBuilder; -use Nette\DI\Helpers; +use Nette\DI\Helpers as DIHelpers; +use Nette\PhpGenerator\ClassType; use Nette\Schema\Expect; +use Nette\Schema\Helpers as SchemaHelpers; use Nette\Schema\Schema; use Nette\Utils\Finder; use SplFileInfo; @@ -26,6 +28,23 @@ class Extension extends CompilerExtension /** @var string */ public const EXTENSION_NAME = 'webloader'; + /** @var string */ + private $appDir; + + /** @var string */ + private $wwwDir; + + /** @var bool */ + private $debugMode; + + + public function __construct(string $appDir, string $wwwDir, bool $debugMode) + { + $this->appDir = $appDir; + $this->wwwDir = $wwwDir; + $this->debugMode = $debugMode; + } + public function getConfigSchema(): Schema { @@ -33,8 +52,8 @@ public function getConfigSchema(): Schema 'jsDefaults' => Expect::structure([ 'checkLastModified' => Expect::bool(true), 'debug' => Expect::bool(false), - 'sourceDir' => Expect::string('%wwwDir%/js'), - 'tempDir' => Expect::string('%wwwDir%/' . self::DEFAULT_TEMP_PATH), + 'sourceDir' => Expect::string($this->wwwDir . '/js'), + 'tempDir' => Expect::string($this->wwwDir . '/' . self::DEFAULT_TEMP_PATH), 'tempPath' => Expect::string(self::DEFAULT_TEMP_PATH), 'files' => Expect::array(), 'watchFiles' => Expect::array(), @@ -51,8 +70,8 @@ public function getConfigSchema(): Schema 'cssDefaults' => Expect::structure([ 'checkLastModified' => Expect::bool(true), 'debug' => Expect::bool(false), - 'sourceDir' => Expect::string('%wwwDir%/css')->dynamic(), - 'tempDir' => Expect::string('%wwwDir%/' . self::DEFAULT_TEMP_PATH), + 'sourceDir' => Expect::string($this->wwwDir . '/css')->dynamic(), + 'tempDir' => Expect::string($this->wwwDir . '/' . self::DEFAULT_TEMP_PATH), 'tempPath' => Expect::string(self::DEFAULT_TEMP_PATH), 'files' => Expect::array(), 'watchFiles' => Expect::array(), @@ -68,7 +87,7 @@ public function getConfigSchema(): Schema ]), 'js' => Expect::array(), 'css' => Expect::array(), - 'debugger' => Expect::bool('%debugMode%'), + 'debugger' => Expect::bool($this->debugMode), ]); } @@ -76,11 +95,7 @@ public function getConfigSchema(): Schema public function loadConfiguration(): void { $builder = $this->getContainerBuilder(); - - $params = $this->getContainerBuilder()->parameters; - $json = json_encode($this->getConfig()); - $config = json_decode((string) $json, true); - $config = Helpers::expand($config, $params); + $config = json_decode((string) json_encode($this->getConfig()), true); $builder->addDefinition($this->prefix('cssNamingConvention')) ->setFactory('WebLoader\DefaultOutputNamingConvention::createCssConvention'); @@ -90,8 +105,8 @@ public function loadConfiguration(): void if ($config['debugger']) { $builder->addDefinition($this->prefix('tracyPanel')) - ->setClass('WebLoader\Nette\Diagnostics\Panel') - ->setArguments([$params['appDir']]); + ->setType('WebLoader\Nette\Diagnostics\Panel') + ->setArguments([$this->appDir]); } $builder->parameters['webloader'] = $config; @@ -101,7 +116,7 @@ public function loadConfiguration(): void foreach (['css', 'js'] as $type) { foreach ($config[$type] as $name => $wlConfig) { /** @var array $wlConfig */ - $wlConfig = \Nette\Schema\Helpers::merge($wlConfig, $config[$type . 'Defaults']); + $wlConfig = SchemaHelpers::merge($wlConfig, $config[$type . 'Defaults']); $this->addWebLoader($builder, $type . ucfirst($name), $wlConfig); $loaderFactoryTempPaths[strtolower($name)] = $wlConfig['tempPath']; @@ -112,11 +127,12 @@ public function loadConfiguration(): void } $builder->addDefinition($this->prefix('factory')) - ->setFactory('WebLoader\Nette\LoaderFactory', [$loaderFactoryTempPaths, $this->name]); + ->setType('WebLoader\Nette\LoaderFactory') + ->setArguments([$loaderFactoryTempPaths, $this->name]); if (class_exists('Symfony\Component\Console\Command\Command')) { $builder->addDefinition($this->prefix('generateCommand')) - ->setClass('WebLoader\Nette\SymfonyConsole\GenerateCommand') + ->setType('WebLoader\Nette\SymfonyConsole\GenerateCommand') ->addTag('kdyby.console.command'); } } @@ -127,7 +143,7 @@ private function addWebLoader(ContainerBuilder $builder, string $name, array $co $filesServiceName = $this->prefix($name . 'Files'); $files = $builder->addDefinition($filesServiceName) - ->setClass('WebLoader\FileCollection') + ->setType('WebLoader\FileCollection') ->setArguments([$config['sourceDir']]); foreach ($this->findFiles($config['files'], $config['sourceDir']) as $file) { @@ -141,7 +157,7 @@ private function addWebLoader(ContainerBuilder $builder, string $name, array $co $files->addSetup('addRemoteFiles', [$config['remoteFiles']]); $compiler = $builder->addDefinition($this->prefix($name . 'Compiler')) - ->setClass('WebLoader\Compiler') + ->setType('WebLoader\Compiler') ->setArguments([ '@' . $filesServiceName, $config['namingConvention'], @@ -181,19 +197,19 @@ private function addWebLoader(ContainerBuilder $builder, string $name, array $co // I have no clue what this is supposed to do... - // public function afterCompile(Nette\PhpGenerator\ClassType $class): void - // { - // $meta = $class->getProperty('meta'); - // if (array_key_exists('webloader\\nette\\loaderfactory', $meta->value['types'])) { - // $meta->value['types']['webloader\\loaderfactory'] = $meta->value['types']['webloader\\nette\\loaderfactory']; - // } - // if (array_key_exists('WebLoader\\Nette\\LoaderFactory', $meta->value['types'])) { - // $meta->value['types']['WebLoader\\LoaderFactory'] = $meta->value['types']['WebLoader\\Nette\\LoaderFactory']; - // } - // - // $init = $class->methods['initialize']; - // $init->addBody('if (!class_exists(?, ?)) class_alias(?, ?);', ['WebLoader\\LoaderFactory', false, 'WebLoader\\Nette\\LoaderFactory', 'WebLoader\\LoaderFactory']); - // } + public function afterCompile(ClassType $class): void + { + $types = $class->getProperty('types'); + if (array_key_exists('webloader\\nette\\loaderfactory', $types)) { + $types['webloader\\loaderfactory'] = $types['webloader\\nette\\loaderfactory']; + } + if (array_key_exists('WebLoader\\Nette\\LoaderFactory', $types)) { + $types['WebLoader\\LoaderFactory'] = $types['WebLoader\\Nette\\LoaderFactory']; + } + + $init = $class->methods['initialize']; + $init->addBody('if (!class_exists(?, ?)) class_alias(?, ?);', ['WebLoader\\LoaderFactory', false, 'WebLoader\\Nette\\LoaderFactory', 'WebLoader\\LoaderFactory']); + } public function install(Configurator $configurator): void diff --git a/tests/Nette/ExtensionTest.php b/tests/Nette/ExtensionTest.php index 8b2c7ae..333d099 100755 --- a/tests/Nette/ExtensionTest.php +++ b/tests/Nette/ExtensionTest.php @@ -17,16 +17,40 @@ class ExtensionTest extends TestCase /** @var Container */ private $container; + /** @var string */ + private $appDir; + + /** @var string */ + private $wwwDir; + + /** @var string */ + private $tempDir; + + /** @var string */ + private $fixturesDir; + + /** @var bool */ + private $debugMode; + + + protected function setUp() + { + $this->appDir = __DIR__; + $this->wwwDir = $this->appDir . '/..'; + $this->tempDir = $this->wwwDir . '/../temp'; + $this->fixturesDir = $this->appDir . '/../fixtures'; + $this->debugMode = false; + } + private function prepareContainer(array $configFiles): void { - $tempDir = __DIR__ . '/../temp'; - foreach (Finder::findFiles('*')->exclude('.gitignore')->from($tempDir . '/cache') as $file) { + foreach (Finder::findFiles('*')->exclude('.gitignore')->from($this->tempDir . '/cache') as $file) { unlink((string) $file); } $configurator = new Configurator(); - $configurator->setTempDirectory($tempDir); + $configurator->setTempDirectory($this->tempDir); /** @var string $file */ foreach ($configFiles as $file) { @@ -34,12 +58,12 @@ private function prepareContainer(array $configFiles): void } $configurator->addParameters([ - 'wwwDir' => __DIR__ . '/..', - 'fixturesDir' => __DIR__ . '/../fixtures', - 'tempDir' => $tempDir, + 'wwwDir' => $this->wwwDir, + 'fixturesDir' => $this->fixturesDir, + 'tempDir' => $this->tempDir, ]); - $extension = new Extension(); + $extension = new Extension($this->appDir, $this->wwwDir, $this->debugMode); $extension->install($configurator); $this->container = @$configurator->createContainer(); // sends header X-Powered-By, ... @@ -48,26 +72,26 @@ private function prepareContainer(array $configFiles): void public function testJsCompilerService(): void { - $this->prepareContainer([__DIR__ . '/../fixtures/extension.neon']); + $this->prepareContainer([$this->fixturesDir . '/extension.neon']); $this->assertInstanceOf('WebLoader\Compiler', $this->container->getService('webloader.jsDefaultCompiler')); } public function testExcludeFiles(): void { - $this->prepareContainer([__DIR__ . '/../fixtures/extension.neon']); + $this->prepareContainer([$this->fixturesDir . '/extension.neon']); $files = $this->container->getService('webloader.jsExcludeCompiler')->getFileCollection()->getFiles(); - $this->assertTrue(in_array(Path::normalize(__DIR__ . '/../fixtures/a.txt'), $files, true)); - $this->assertFalse(in_array(Path::normalize(__DIR__ . '/../fixtures/dir/one.js'), $files, true)); + $this->assertTrue(in_array(Path::normalize($this->fixturesDir . '/a.txt'), $files, true)); + $this->assertFalse(in_array(Path::normalize($this->fixturesDir . '/dir/one.js'), $files, true)); } public function testJoinFilesOn(): void { $this->prepareContainer([ - __DIR__ . '/../fixtures/extension.neon', - __DIR__ . '/../fixtures/extensionJoinFilesTrue.neon', + $this->fixturesDir . '/extension.neon', + $this->fixturesDir . '/extensionJoinFilesTrue.neon', ]); $this->assertTrue($this->container->getService('webloader.jsDefaultCompiler')->getJoinFiles()); } @@ -76,8 +100,8 @@ public function testJoinFilesOn(): void public function testJoinFilesOff(): void { $this->prepareContainer([ - __DIR__ . '/../fixtures/extension.neon', - __DIR__ . '/../fixtures/extensionJoinFilesFalse.neon', + $this->fixturesDir . '/extension.neon', + $this->fixturesDir . '/extensionJoinFilesFalse.neon', ]); $this->assertFalse($this->container->getService('webloader.jsDefaultCompiler')->getJoinFiles()); } @@ -86,7 +110,7 @@ public function testJoinFilesOff(): void public function testJoinFilesOffInOneService(): void { $this->prepareContainer([ - __DIR__ . '/../fixtures/extension.neon', + $this->fixturesDir . '/extension.neon', ]); $this->assertFalse($this->container->getService('webloader.cssJoinOffCompiler')->getJoinFiles()); } @@ -95,8 +119,8 @@ public function testJoinFilesOffInOneService(): void public function testAsyncOn(): void { $this->prepareContainer([ - __DIR__ . '/../fixtures/extension.neon', - __DIR__ . '/../fixtures/extensionAsyncTrue.neon', + $this->fixturesDir . '/extension.neon', + $this->fixturesDir . '/extensionAsyncTrue.neon', ]); $this->assertTrue($this->container->getService('webloader.jsDefaultCompiler')->isAsync()); } @@ -105,8 +129,8 @@ public function testAsyncOn(): void public function testAsyncOff(): void { $this->prepareContainer([ - __DIR__ . '/../fixtures/extension.neon', - __DIR__ . '/../fixtures/extensionAsyncFalse.neon', + $this->fixturesDir . '/extension.neon', + $this->fixturesDir . '/extensionAsyncFalse.neon', ]); $this->assertFalse($this->container->getService('webloader.jsDefaultCompiler')->isAsync()); } @@ -115,8 +139,8 @@ public function testAsyncOff(): void public function testDeferOn(): void { $this->prepareContainer([ - __DIR__ . '/../fixtures/extension.neon', - __DIR__ . '/../fixtures/extensionDeferTrue.neon', + $this->fixturesDir . '/extension.neon', + $this->fixturesDir . '/extensionDeferTrue.neon', ]); $this->assertTrue($this->container->getService('webloader.jsDefaultCompiler')->isDefer()); } @@ -125,8 +149,8 @@ public function testDeferOn(): void public function testDeferOff(): void { $this->prepareContainer([ - __DIR__ . '/../fixtures/extension.neon', - __DIR__ . '/../fixtures/extensionDeferFalse.neon', + $this->fixturesDir . '/extension.neon', + $this->fixturesDir . '/extensionDeferFalse.neon', ]); $this->assertFalse($this->container->getService('webloader.jsDefaultCompiler')->isDefer()); } @@ -135,8 +159,8 @@ public function testDeferOff(): void public function testAbsoluteUrlOn(): void { $this->prepareContainer([ - __DIR__ . '/../fixtures/extension.neon', - __DIR__ . '/../fixtures/extensionAbsoluteUrlTrue.neon', + $this->fixturesDir . '/extension.neon', + $this->fixturesDir . '/extensionAbsoluteUrlTrue.neon', ]); $this->assertTrue($this->container->getService('webloader.jsDefaultCompiler')->isAbsoluteUrl()); } @@ -145,8 +169,8 @@ public function testAbsoluteUrlOn(): void public function testAbsoluteUrlOff(): void { $this->prepareContainer([ - __DIR__ . '/../fixtures/extension.neon', - __DIR__ . '/../fixtures/extensionAbsoluteUrlFalse.neon', + $this->fixturesDir . '/extension.neon', + $this->fixturesDir . '/extensionAbsoluteUrlFalse.neon', ]); $this->assertFalse($this->container->getService('webloader.jsDefaultCompiler')->isAbsoluteUrl()); } @@ -155,8 +179,8 @@ public function testAbsoluteUrlOff(): void public function testNonceSet(): void { $this->prepareContainer([ - __DIR__ . '/../fixtures/extension.neon', - __DIR__ . '/../fixtures/extensionNonce.neon', + $this->fixturesDir . '/extension.neon', + $this->fixturesDir . '/extensionNonce.neon', ]); $this->assertEquals('rAnd0m123', $this->container->getService('webloader.jsDefaultCompiler')->getNonce()); } @@ -165,7 +189,7 @@ public function testNonceSet(): void public function testNonceNotSet(): void { $this->prepareContainer([ - __DIR__ . '/../fixtures/extension.neon', + $this->fixturesDir . '/extension.neon', ]); $this->assertNull($this->container->getService('webloader.jsDefaultCompiler')->getNonce()); } @@ -173,17 +197,16 @@ public function testNonceNotSet(): void public function testExtensionName(): void { - $tempDir = __DIR__ . '/../temp'; $class = 'ExtensionNameServiceContainer'; $configurator = new Configurator(); - $configurator->setTempDirectory($tempDir); + $configurator->setTempDirectory($this->tempDir); $configurator->addParameters(['container' => ['class' => $class]]); $configurator->onCompile[] = function (Configurator $configurator, Compiler $compiler) { - $extension = new Extension(); + $extension = new Extension($this->appDir, $this->wwwDir, $this->debugMode); $compiler->addExtension('Foo', $extension); }; - $configurator->addConfig(__DIR__ . '/../fixtures/extensionName.neon'); + $configurator->addConfig($this->fixturesDir . '/extensionName.neon'); $container = $configurator->createContainer(); $this->assertInstanceOf('WebLoader\Compiler', $container->getService('Foo.cssDefaultCompiler')); From 96be3ccd2550eac3cc245445aac9754820d070fa Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Thu, 30 Apr 2020 09:17:52 +0200 Subject: [PATCH 13/66] phpstan, fixes --- src/Nette/Extension.php | 28 +++++++++++++--------------- tests/Nette/ExtensionTest.php | 2 +- 2 files changed, 14 insertions(+), 16 deletions(-) mode change 100755 => 100644 src/Nette/Extension.php diff --git a/src/Nette/Extension.php b/src/Nette/Extension.php old mode 100755 new mode 100644 index 3c72424..8001771 --- a/src/Nette/Extension.php +++ b/src/Nette/Extension.php @@ -8,8 +8,6 @@ use Nette\DI\Compiler; use Nette\DI\CompilerExtension; use Nette\DI\ContainerBuilder; -use Nette\DI\Helpers as DIHelpers; -use Nette\PhpGenerator\ClassType; use Nette\Schema\Expect; use Nette\Schema\Helpers as SchemaHelpers; use Nette\Schema\Schema; @@ -197,19 +195,19 @@ private function addWebLoader(ContainerBuilder $builder, string $name, array $co // I have no clue what this is supposed to do... - public function afterCompile(ClassType $class): void - { - $types = $class->getProperty('types'); - if (array_key_exists('webloader\\nette\\loaderfactory', $types)) { - $types['webloader\\loaderfactory'] = $types['webloader\\nette\\loaderfactory']; - } - if (array_key_exists('WebLoader\\Nette\\LoaderFactory', $types)) { - $types['WebLoader\\LoaderFactory'] = $types['WebLoader\\Nette\\LoaderFactory']; - } - - $init = $class->methods['initialize']; - $init->addBody('if (!class_exists(?, ?)) class_alias(?, ?);', ['WebLoader\\LoaderFactory', false, 'WebLoader\\Nette\\LoaderFactory', 'WebLoader\\LoaderFactory']); - } + // public function afterCompile(ClassType $class): void + // { + // $types = $class->getProperty('types'); + // if (array_key_exists('webloader\\nette\\loaderfactory', $types)) { + // $types['webloader\\loaderfactory'] = $types['webloader\\nette\\loaderfactory']; + // } + // if (array_key_exists('WebLoader\\Nette\\LoaderFactory', $types)) { + // $types['WebLoader\\LoaderFactory'] = $types['WebLoader\\Nette\\LoaderFactory']; + // } + // + // $init = $class->methods['initialize']; + // $init->addBody('if (!class_exists(?, ?)) class_alias(?, ?);', ['WebLoader\\LoaderFactory', false, 'WebLoader\\Nette\\LoaderFactory', 'WebLoader\\LoaderFactory']); + // } public function install(Configurator $configurator): void diff --git a/tests/Nette/ExtensionTest.php b/tests/Nette/ExtensionTest.php index 333d099..e0a26a8 100755 --- a/tests/Nette/ExtensionTest.php +++ b/tests/Nette/ExtensionTest.php @@ -33,7 +33,7 @@ class ExtensionTest extends TestCase private $debugMode; - protected function setUp() + protected function setUp(): void { $this->appDir = __DIR__; $this->wwwDir = $this->appDir . '/..'; From 66126391cad7018cf85cd5105e7ec4d8b6dd76e9 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Wed, 10 Jun 2020 12:26:06 +0200 Subject: [PATCH 14/66] phpstan.neon --- phpstan/phpstan.neon | 3 --- 1 file changed, 3 deletions(-) diff --git a/phpstan/phpstan.neon b/phpstan/phpstan.neon index 8aa21b2..4f7061c 100755 --- a/phpstan/phpstan.neon +++ b/phpstan/phpstan.neon @@ -1,8 +1,5 @@ parameters: tmpDir: %currentWorkingDirectory%/temp/phpstan/ - autoload_directories: - - %currentWorkingDirectory%/src/ - - %currentWorkingDirectory%/tests/ paths: - %currentWorkingDirectory%/src/ From c8fa12375e9af43abc88c749b2c8e159e64c926a Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Wed, 10 Jun 2020 12:42:08 +0200 Subject: [PATCH 15/66] refactoring --- src/Nette/LoaderFactory.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Nette/LoaderFactory.php b/src/Nette/LoaderFactory.php index 69b8a60..a800ceb 100644 --- a/src/Nette/LoaderFactory.php +++ b/src/Nette/LoaderFactory.php @@ -15,7 +15,7 @@ class LoaderFactory private $httpRequest; /** @var Container */ - private $serviceLocator; + private $diContainer; /** @var array */ private $tempPaths; @@ -29,16 +29,16 @@ class LoaderFactory * @param array $tempPaths * @param string $extensionName * @param IRequest $httpRequest - * @param Container $serviceLocator + * @param Container $diContainer */ public function __construct( array $tempPaths, string $extensionName, IRequest $httpRequest, - Container $serviceLocator + Container $diContainer ) { $this->httpRequest = $httpRequest; - $this->serviceLocator = $serviceLocator; + $this->diContainer = $diContainer; $this->tempPaths = $tempPaths; $this->extensionName = $extensionName; } @@ -47,7 +47,7 @@ public function __construct( public function createCssLoader(string $name, bool $appendLastModified = false): CssLoader { /** @var Compiler $compiler */ - $compiler = $this->serviceLocator->getService($this->extensionName . '.css' . ucfirst($name) . 'Compiler'); + $compiler = $this->diContainer->getService($this->extensionName . '.css' . ucfirst($name) . 'Compiler'); return new CssLoader($compiler, $this->formatTempPath($name, $compiler->isAbsoluteUrl()), $appendLastModified); } @@ -55,7 +55,7 @@ public function createCssLoader(string $name, bool $appendLastModified = false): public function createJavaScriptLoader(string $name, bool $appendLastModified = false): JavaScriptLoader { /** @var Compiler $compiler */ - $compiler = $this->serviceLocator->getService($this->extensionName . '.js' . ucfirst($name) . 'Compiler'); + $compiler = $this->diContainer->getService($this->extensionName . '.js' . ucfirst($name) . 'Compiler'); return new JavaScriptLoader($compiler, $this->formatTempPath($name, $compiler->isAbsoluteUrl()), $appendLastModified); } From 489e0ab6ca7b7e5b0a9ceeacf227027938f9f181 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Wed, 10 Jun 2020 12:43:22 +0200 Subject: [PATCH 16/66] createCssLoader & createJavaScriptLoader: $appendLastModified default is now true --- src/Nette/LoaderFactory.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Nette/LoaderFactory.php b/src/Nette/LoaderFactory.php index a800ceb..43f5130 100644 --- a/src/Nette/LoaderFactory.php +++ b/src/Nette/LoaderFactory.php @@ -44,7 +44,7 @@ public function __construct( } - public function createCssLoader(string $name, bool $appendLastModified = false): CssLoader + public function createCssLoader(string $name, bool $appendLastModified = true): CssLoader { /** @var Compiler $compiler */ $compiler = $this->diContainer->getService($this->extensionName . '.css' . ucfirst($name) . 'Compiler'); @@ -52,7 +52,7 @@ public function createCssLoader(string $name, bool $appendLastModified = false): } - public function createJavaScriptLoader(string $name, bool $appendLastModified = false): JavaScriptLoader + public function createJavaScriptLoader(string $name, bool $appendLastModified = true): JavaScriptLoader { /** @var Compiler $compiler */ $compiler = $this->diContainer->getService($this->extensionName . '.js' . ucfirst($name) . 'Compiler'); From b10b2a7432c9f758cdcb49ab12fafe87f7d93199 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Wed, 10 Jun 2020 13:29:54 +0200 Subject: [PATCH 17/66] DefaultOutputNamingConvetion - insert batch name by default --- src/Nette/LoaderFactory.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Nette/LoaderFactory.php b/src/Nette/LoaderFactory.php index 43f5130..ccf3891 100644 --- a/src/Nette/LoaderFactory.php +++ b/src/Nette/LoaderFactory.php @@ -6,7 +6,10 @@ use Nette\DI\Container; use Nette\Http\IRequest; +use Nette\Utils\Strings; use WebLoader\Compiler; +use WebLoader\DefaultOutputNamingConvention; +use WebLoader\IOutputNamingConvention; class LoaderFactory { @@ -48,6 +51,7 @@ public function createCssLoader(string $name, bool $appendLastModified = true): { /** @var Compiler $compiler */ $compiler = $this->diContainer->getService($this->extensionName . '.css' . ucfirst($name) . 'Compiler'); + $this->modifyConvention($compiler->getOutputNamingConvention(), $name); return new CssLoader($compiler, $this->formatTempPath($name, $compiler->isAbsoluteUrl()), $appendLastModified); } @@ -56,6 +60,7 @@ public function createJavaScriptLoader(string $name, bool $appendLastModified = { /** @var Compiler $compiler */ $compiler = $this->diContainer->getService($this->extensionName . '.js' . ucfirst($name) . 'Compiler'); + $this->modifyConvention($compiler->getOutputNamingConvention(), $name); return new JavaScriptLoader($compiler, $this->formatTempPath($name, $compiler->isAbsoluteUrl()), $appendLastModified); } @@ -67,4 +72,14 @@ private function formatTempPath(string $name, bool $absoluteUrl = false): string $method = $absoluteUrl ? 'getBaseUrl' : 'getBasePath'; return rtrim($this->httpRequest->getUrl()->{$method}(), '/') . '/' . $tempPath; } + + + private function modifyConvention(IOutputNamingConvention $convention, string $name): IOutputNamingConvention + { + if ($convention instanceof DefaultOutputNamingConvention) { + $convention->setPrefix($name . '-'); + } + + return $convention; + } } From 40d86a422ca577562acde902df5826f2d967ee88 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Wed, 10 Jun 2020 13:34:17 +0200 Subject: [PATCH 18/66] remove dead code --- src/Nette/LoaderFactory.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Nette/LoaderFactory.php b/src/Nette/LoaderFactory.php index ccf3891..35dfab0 100644 --- a/src/Nette/LoaderFactory.php +++ b/src/Nette/LoaderFactory.php @@ -6,7 +6,6 @@ use Nette\DI\Container; use Nette\Http\IRequest; -use Nette\Utils\Strings; use WebLoader\Compiler; use WebLoader\DefaultOutputNamingConvention; use WebLoader\IOutputNamingConvention; From 828741c09e9fe221777c4203343a2cd3cdd05e2b Mon Sep 17 00:00:00 2001 From: Adam Zemek Date: Mon, 19 Oct 2020 17:19:03 +0200 Subject: [PATCH 19/66] WebLoader component: compatibility with Nette 3 --- WebLoader/Nette/WebLoader.php | 1 - 1 file changed, 1 deletion(-) diff --git a/WebLoader/Nette/WebLoader.php b/WebLoader/Nette/WebLoader.php index 6a3fb48..4e2a93a 100644 --- a/WebLoader/Nette/WebLoader.php +++ b/WebLoader/Nette/WebLoader.php @@ -22,7 +22,6 @@ abstract class WebLoader extends \Nette\Application\UI\Control public function __construct(Compiler $compiler, $tempPath) { - parent::__construct(); $this->compiler = $compiler; $this->tempPath = $tempPath; } From 724d91f27b818a95b65c77e45c05fa3a6ae18e2f Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Sat, 27 Feb 2021 19:42:32 +0100 Subject: [PATCH 20/66] remove coffeescript support --- composer.json | 1 - src/Filter/CoffeeScriptFilter.php | 53 ---------------------- src/Filter/PHPCoffeeScriptFilter.php | 38 ---------------- src/Nette/Diagnostics/Panel.php | 1 - tests/Filter/PHPCoffeeScriptFilterTest.php | 51 --------------------- 5 files changed, 144 deletions(-) delete mode 100755 src/Filter/CoffeeScriptFilter.php delete mode 100644 src/Filter/PHPCoffeeScriptFilter.php delete mode 100755 tests/Filter/PHPCoffeeScriptFilterTest.php diff --git a/composer.json b/composer.json index cc9ab73..50c87ff 100755 --- a/composer.json +++ b/composer.json @@ -50,7 +50,6 @@ "phpstan/phpstan": "^0.12.0", "phpstan/phpstan-nette": "^0.12.0", "roave/security-advisories": "dev-master", - "kylekatarnls/coffeescript": "1.3.*", "symfony/console": "^4.2.9|^5.0.0", "phpstan/phpstan-mockery": "^0.12.5", "tubalmartin/cssmin": "^4.1", diff --git a/src/Filter/CoffeeScriptFilter.php b/src/Filter/CoffeeScriptFilter.php deleted file mode 100755 index 5b21881..0000000 --- a/src/Filter/CoffeeScriptFilter.php +++ /dev/null @@ -1,53 +0,0 @@ -bin = $bin; - } - - - public function __invoke(string $code, Compiler $loader, ?string $file = null): string - { - $file = (string) $file; - - if (pathinfo($file, PATHINFO_EXTENSION) === 'coffee') { - $code = $this->compileCoffee($code); - } - - return $code; - } - - - public function compileCoffee(string $source, ?bool $bare = null): string - { - if ($bare === null) { - $bare = $this->bare; - } - - $cmd = $this->bin . ' -p -s' . ($bare ? ' -b' : ''); - - return Process::run($cmd, $source); - } -} diff --git a/src/Filter/PHPCoffeeScriptFilter.php b/src/Filter/PHPCoffeeScriptFilter.php deleted file mode 100644 index 988022f..0000000 --- a/src/Filter/PHPCoffeeScriptFilter.php +++ /dev/null @@ -1,38 +0,0 @@ -compileCoffee($code, $file); - } - - return $code; - } - - - public function compileCoffee(string $source, ?string $file): string - { - try { - return Compiler::compile($source, ['filename' => $file]); - } catch (Throwable $e) { - throw new WebLoaderException('CoffeeScript Filter Error: ' . $e->getMessage(), 0, $e); - } - } -} diff --git a/src/Nette/Diagnostics/Panel.php b/src/Nette/Diagnostics/Panel.php index 088279d..3e80da5 100644 --- a/src/Nette/Diagnostics/Panel.php +++ b/src/Nette/Diagnostics/Panel.php @@ -24,7 +24,6 @@ class Panel implements IBarPanel 'js' => 'JavaScript files', 'less' => 'Less files', 'scss' => 'Sass files', - 'coffee' => 'CoffeeScript files', ]; /** @var Compiler[] */ diff --git a/tests/Filter/PHPCoffeeScriptFilterTest.php b/tests/Filter/PHPCoffeeScriptFilterTest.php deleted file mode 100755 index 3114ccf..0000000 --- a/tests/Filter/PHPCoffeeScriptFilterTest.php +++ /dev/null @@ -1,51 +0,0 @@ -object = new PHPCoffeeScriptFilter(); - } - - - public function testSimpleLoadAndParse(): void - { - if (!class_exists('CoffeeScript\Compiler')) { - $this->markTestSkipped('Missing CoffeeScript compiler.'); - } - - $compiler = new PHPCoffeeScriptFilter(); - $coffee = $compiler->compileCoffee('number = -42 if opposite', null); - - $version = COFFEESCRIPT_VERSION; - $expected = <<assertEquals($expected, $coffee); - } -} From 8947cc1b4f95575940af3625c7e8f777b09e30e2 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Sat, 27 Feb 2021 19:51:58 +0100 Subject: [PATCH 21/66] fix scss tests --- tests/fixtures/style.scss | 23 +++++++++++----------- tests/fixtures/style.scss.expected | 16 ++++++++------- tests/fixtures/style2.scss | 4 ++-- tests/fixtures/styleAbsolute.scss | 22 ++++++++++----------- tests/fixtures/styleAbsolute.scss.expected | 16 ++++++++------- 5 files changed, 42 insertions(+), 39 deletions(-) diff --git a/tests/fixtures/style.scss b/tests/fixtures/style.scss index 2e98946..2334920 100644 --- a/tests/fixtures/style.scss +++ b/tests/fixtures/style.scss @@ -1,19 +1,18 @@ - @import 'style2.scss'; .navigation { - ul { - line-height: 20px; - color: blue; - a { - color: red; - } - } + ul { + line-height: 20px; + color: blue; + + a { + color: red; + } + } } .footer { - .copyright { - color: silver; - } + .copyright { + color: silver; + } } - diff --git a/tests/fixtures/style.scss.expected b/tests/fixtures/style.scss.expected index d4249e7..dd4b040 100644 --- a/tests/fixtures/style.scss.expected +++ b/tests/fixtures/style.scss.expected @@ -1,12 +1,14 @@ .clearFix { display: block; - zoom: 1; } - + zoom: 1; +} .navigation ul { line-height: 20px; - color: blue; } - .navigation ul a { - color: red; } - + color: blue; +} +.navigation ul a { + color: red; +} .footer .copyright { - color: silver; } + color: silver; +} diff --git a/tests/fixtures/style2.scss b/tests/fixtures/style2.scss index 1155d28..8de238e 100644 --- a/tests/fixtures/style2.scss +++ b/tests/fixtures/style2.scss @@ -1,4 +1,4 @@ .clearFix { - display: block; - zoom: 1; + display: block; + zoom: 1; } diff --git a/tests/fixtures/styleAbsolute.scss b/tests/fixtures/styleAbsolute.scss index e4ed002..2958f1d 100644 --- a/tests/fixtures/styleAbsolute.scss +++ b/tests/fixtures/styleAbsolute.scss @@ -1,19 +1,19 @@ - @import '{{$fixturesAbsolutePath}}/style2.scss'; .navigation { - ul { - line-height: 20px; - color: blue; - a { - color: red; - } - } + ul { + line-height: 20px; + color: blue; + + a { + color: red; + } + } } .footer { - .copyright { - color: silver; - } + .copyright { + color: silver; + } } diff --git a/tests/fixtures/styleAbsolute.scss.expected b/tests/fixtures/styleAbsolute.scss.expected index d4249e7..dd4b040 100644 --- a/tests/fixtures/styleAbsolute.scss.expected +++ b/tests/fixtures/styleAbsolute.scss.expected @@ -1,12 +1,14 @@ .clearFix { display: block; - zoom: 1; } - + zoom: 1; +} .navigation ul { line-height: 20px; - color: blue; } - .navigation ul a { - color: red; } - + color: blue; +} +.navigation ul a { + color: red; +} .footer .copyright { - color: silver; } + color: silver; +} From ee9cd418c74969e64f9644a9e109f773a7899719 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Sat, 27 Feb 2021 20:07:27 +0100 Subject: [PATCH 22/66] remove nette/forms dev dependency --- composer.json | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index 50c87ff..d9ca550 100755 --- a/composer.json +++ b/composer.json @@ -20,9 +20,9 @@ }, "require": { "php": ">= 7.1", - "nette/application": "^3.0", + "nette/application": "^3.1", "nette/di": "^3.0", - "nette/utils": "^3.0", + "nette/utils": "^3.2", "ext-json": "*" }, "suggest": { @@ -32,11 +32,10 @@ "tubalmartin/cssmin": "A PHP port of the YUI CSS compressor" }, "require-dev": { - "nette/bootstrap": "^3.0", - "nette/caching": "^3.0", + "nette/bootstrap": "^3.1", + "nette/caching": "^3.1", "nette/component-model": "^3.0", "nette/finder": "^2.5", - "nette/forms": "^3.0", "nette/http": "^3.0", "nette/neon": "^3.0", "nette/robot-loader": "^3.0", From 9a6ba129863f5c423ec3f67309b3a1826e6e0d7e Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Sat, 27 Feb 2021 20:18:45 +0100 Subject: [PATCH 23/66] phpstan - increase memory --- phpstan/phpstan | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpstan/phpstan b/phpstan/phpstan index 9e146d0..7ab99e2 100755 --- a/phpstan/phpstan +++ b/phpstan/phpstan @@ -1,3 +1,3 @@ #!/bin/bash -./vendor/bin/phpstan analyse --configuration ./phpstan/phpstan.neon +./vendor/bin/phpstan analyse --configuration ./phpstan/phpstan.neon --memory-limit 512M From 9a7b828b871f82944fcf012804fdc085fcd0602b Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Sat, 27 Feb 2021 23:51:56 +0100 Subject: [PATCH 24/66] php 7.4, add hidden dependencies --- composer.json | 14 +++--- src/Compiler.php | 47 +++++--------------- src/DefaultOutputNamingConvention.php | 7 +-- src/File.php | 23 ++++------ src/FileCollection.php | 15 ++----- src/Filter/CssUrlsFilter.php | 7 +-- src/Filter/LessBinFilter.php | 7 +-- src/Filter/Process.php | 7 ++- src/Filter/ScssFilter.php | 3 +- src/Filter/StylusFilter.php | 11 ++--- src/Filter/TypeScriptFilter.php | 7 +-- src/Filter/VariablesFilter.php | 11 ++--- src/Nette/CssLoader.php | 16 ++----- src/Nette/Diagnostics/Panel.php | 20 +++------ src/Nette/Extension.php | 14 ++---- src/Nette/LoaderFactory.php | 13 ++---- src/Nette/SymfonyConsole/GenerateCommand.php | 2 +- src/Nette/WebLoader.php | 11 ++--- 18 files changed, 75 insertions(+), 160 deletions(-) diff --git a/composer.json b/composer.json index d9ca550..80edb74 100755 --- a/composer.json +++ b/composer.json @@ -19,28 +19,30 @@ "psr-4": { "WebLoader\\Test\\": "tests/" } }, "require": { - "php": ">= 7.1", + "php": ">= 7.4", "nette/application": "^3.1", "nette/di": "^3.0", "nette/utils": "^3.2", - "ext-json": "*" + "ext-json": "*", + "nette/schema": "^1.2", + "nette/finder": "^2.5", + "latte/latte": "^2.10", + "tracy/tracy": "^2.8" }, "suggest": { "wikimedia/less.php": "LESS compiler written in PHP.", "scssphp/scssphp": "SCSS compiler written in PHP.", "tedivm/jshrink": "Javascript Minifier built in PHP", - "tubalmartin/cssmin": "A PHP port of the YUI CSS compressor" + "tubalmartin/cssmin": "A PHP port of the YUI CSS compressor", + "symfony/console": "For pre-generating files form the CLI" }, "require-dev": { "nette/bootstrap": "^3.1", "nette/caching": "^3.1", "nette/component-model": "^3.0", - "nette/finder": "^2.5", "nette/http": "^3.0", "nette/neon": "^3.0", "nette/robot-loader": "^3.0", - "latte/latte": "^2.8", - "tracy/tracy": "^2.7", "wikimedia/less.php": "^3.0.0", "scssphp/scssphp": "^1.1.0", "mockery/mockery": "1.*", diff --git a/src/Compiler.php b/src/Compiler.php index 83da13c..e871c2c 100755 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -12,41 +12,18 @@ class Compiler { - /** @var string */ - private $outputDir; - - /** @var bool */ - private $joinFiles = true; - - /** @var array */ - private $filters = []; - - /** @var array */ - private $fileFilters = []; - - /** @var IFileCollection */ - private $collection; - - /** @var IOutputNamingConvention */ - private $namingConvention; - - /** @var bool */ - private $checkLastModified = true; - - /** @var bool */ - private $debugging = false; - - /** @var bool */ - private $async = false; - - /** @var bool */ - private $defer = false; - - /** @var string|null */ - private $nonce; - - /** @var bool */ - private $absoluteUrl = false; + private IFileCollection $collection; + private IOutputNamingConvention $namingConvention; + private string $outputDir; + private bool $joinFiles = true; + private array $filters = []; + private array $fileFilters = []; + private bool $checkLastModified = true; + private bool $debugging = false; + private bool $async = false; + private bool $defer = false; + private bool $absoluteUrl = false; + private ?string $nonce; public function __construct(IFileCollection $files, IOutputNamingConvention $convention, string $outputDir) diff --git a/src/DefaultOutputNamingConvention.php b/src/DefaultOutputNamingConvention.php index 2bfeb40..48da64d 100644 --- a/src/DefaultOutputNamingConvention.php +++ b/src/DefaultOutputNamingConvention.php @@ -12,11 +12,8 @@ class DefaultOutputNamingConvention implements IOutputNamingConvention { - /** @var string */ - private $prefix = ''; - - /** @var string */ - private $suffix = ''; + private string $prefix = ''; + private string $suffix = ''; public static function createCssConvention(): self diff --git a/src/File.php b/src/File.php index bc21f6a..833fae1 100644 --- a/src/File.php +++ b/src/File.php @@ -7,23 +7,18 @@ class File { - /** @var string */ - protected $file; + protected string $file; + protected ?int $lastModified; + protected array $sourceFiles; - /** @var int|null */ - protected $lastModified; - /** @var array */ - protected $sourceFiles; - - - public function __construct( - string $file, - ?int $lastModified, - array $sourceFiles + public function __construct( + string $file, + ?int $lastModified, + array $sourceFiles ) { - $this->file = $file; - $this->lastModified = $lastModified; + $this->file = $file; + $this->lastModified = $lastModified; $this->sourceFiles = $sourceFiles; } diff --git a/src/FileCollection.php b/src/FileCollection.php index 0e1c663..1c55eea 100755 --- a/src/FileCollection.php +++ b/src/FileCollection.php @@ -15,17 +15,10 @@ class FileCollection implements IFileCollection { - /** @var string */ - private $root; - - /** @var array */ - private $files = []; - - /** @var array */ - private $watchFiles = []; - - /** @var array */ - private $remoteFiles = []; + private string $root; + private array $files = []; + private array $watchFiles = []; + private array $remoteFiles = []; /** diff --git a/src/Filter/CssUrlsFilter.php b/src/Filter/CssUrlsFilter.php index fd505ef..5686315 100755 --- a/src/Filter/CssUrlsFilter.php +++ b/src/Filter/CssUrlsFilter.php @@ -17,11 +17,8 @@ class CssUrlsFilter { - /** @var string */ - protected $basePath; - - /** @var string */ - private $docRoot; + protected string $basePath; + private string $docRoot; public function __construct(string $docRoot, string $basePath = '/') diff --git a/src/Filter/LessBinFilter.php b/src/Filter/LessBinFilter.php index 10afa28..b56a873 100644 --- a/src/Filter/LessBinFilter.php +++ b/src/Filter/LessBinFilter.php @@ -15,11 +15,8 @@ class LessBinFilter { - /** @var string */ - private $bin; - - /** @var array */ - private $env; + private string $bin; + private array $env; public function __construct(string $bin = 'lessc', array $env = []) diff --git a/src/Filter/Process.php b/src/Filter/Process.php index ea41bb9..9eadfc4 100644 --- a/src/Filter/Process.php +++ b/src/Filter/Process.php @@ -14,7 +14,12 @@ */ class Process { - public static function run(string $cmd, ?string $stdin = null, ?string $cwd = null, ?array $env = null): string + public static function run( + string $cmd, + ?string $stdin = null, + ?string $cwd = null, + ?array $env = null + ): string { $descriptorspec = [ 0 => ['pipe', 'r'], // stdin diff --git a/src/Filter/ScssFilter.php b/src/Filter/ScssFilter.php index 51d1595..b73eaae 100644 --- a/src/Filter/ScssFilter.php +++ b/src/Filter/ScssFilter.php @@ -16,8 +16,7 @@ class ScssFilter { - /** @var ScssCompiler|null */ - private $sc; + private ?ScssCompiler $sc; public function __construct(?ScssCompiler $sc = null) diff --git a/src/Filter/StylusFilter.php b/src/Filter/StylusFilter.php index 330f180..f1f74ad 100755 --- a/src/Filter/StylusFilter.php +++ b/src/Filter/StylusFilter.php @@ -15,14 +15,9 @@ class StylusFilter { - /** @var bool */ - public $compress = false; - - /** @var bool */ - public $includeCss = false; - - /** @var string */ - private $bin; + public bool $compress = false; + public bool $includeCss = false; + private string $bin; public function __construct(string $bin = 'stylus') diff --git a/src/Filter/TypeScriptFilter.php b/src/Filter/TypeScriptFilter.php index 81b0c89..833106a 100755 --- a/src/Filter/TypeScriptFilter.php +++ b/src/Filter/TypeScriptFilter.php @@ -15,11 +15,8 @@ class TypeScriptFilter { - /** @var string|null */ - private $bin; - - /** @var array|null */ - private $env; + private ?string $bin; + private ?array $env; public function __construct(string $bin = 'tsc', array $env = []) diff --git a/src/Filter/VariablesFilter.php b/src/Filter/VariablesFilter.php index 4b7fc17..7851bab 100644 --- a/src/Filter/VariablesFilter.php +++ b/src/Filter/VariablesFilter.php @@ -15,14 +15,9 @@ class VariablesFilter { - /** @var string */ - private $startVariable = '{{$'; - - /** @var string */ - private $endVariable = '}}'; - - /** @var array */ - private $variables; + private string $startVariable = '{{$'; + private string $endVariable = '}}'; + private array $variables; /** diff --git a/src/Nette/CssLoader.php b/src/Nette/CssLoader.php index 7e02647..d1c20ec 100755 --- a/src/Nette/CssLoader.php +++ b/src/Nette/CssLoader.php @@ -14,18 +14,10 @@ */ class CssLoader extends WebLoader { - - /** @var string */ - private $media; - - /** @var string */ - private $title; - - /** @var string */ - private $type = 'text/css'; - - /** @var bool */ - private $alternate = false; + private string $media; + private string $title; + private string $type = 'text/css'; + private bool $alternate = false; public function getMedia(): string diff --git a/src/Nette/Diagnostics/Panel.php b/src/Nette/Diagnostics/Panel.php index 3e80da5..02b250b 100644 --- a/src/Nette/Diagnostics/Panel.php +++ b/src/Nette/Diagnostics/Panel.php @@ -18,8 +18,7 @@ class Panel implements IBarPanel { - /** @var array */ - public static $types = [ + public static array $types = [ 'css' => 'CSS files', 'js' => 'JavaScript files', 'less' => 'Less files', @@ -27,19 +26,12 @@ class Panel implements IBarPanel ]; /** @var Compiler[] */ - private $compilers = []; + private array $compilers = []; - /** @var array */ - private $size; - - /** @var array */ - private $files; - - /** @var array */ - private $sizes; - - /** @var string */ - private $root; + private ?array $size; + private array $files; + private array $sizes; + private string $root; public function __construct(?string $appDir = null) diff --git a/src/Nette/Extension.php b/src/Nette/Extension.php index 8001771..7ede679 100644 --- a/src/Nette/Extension.php +++ b/src/Nette/Extension.php @@ -20,20 +20,12 @@ */ class Extension extends CompilerExtension { - /** @var string */ public const DEFAULT_TEMP_PATH = 'webtemp'; - - /** @var string */ public const EXTENSION_NAME = 'webloader'; - /** @var string */ - private $appDir; - - /** @var string */ - private $wwwDir; - - /** @var bool */ - private $debugMode; + private string $appDir; + private string $wwwDir; + private bool $debugMode; public function __construct(string $appDir, string $wwwDir, bool $debugMode) diff --git a/src/Nette/LoaderFactory.php b/src/Nette/LoaderFactory.php index 35dfab0..fdbee21 100644 --- a/src/Nette/LoaderFactory.php +++ b/src/Nette/LoaderFactory.php @@ -13,17 +13,12 @@ class LoaderFactory { - /** @var IRequest */ - private $httpRequest; - - /** @var Container */ - private $diContainer; + private IRequest $httpRequest; + private Container $diContainer; + private string $extensionName; /** @var array */ - private $tempPaths; - - /** @var string */ - private $extensionName; + private array $tempPaths; /** diff --git a/src/Nette/SymfonyConsole/GenerateCommand.php b/src/Nette/SymfonyConsole/GenerateCommand.php index 13a4993..a1601dd 100644 --- a/src/Nette/SymfonyConsole/GenerateCommand.php +++ b/src/Nette/SymfonyConsole/GenerateCommand.php @@ -21,7 +21,7 @@ class GenerateCommand extends Command protected static $defaultName = 'webloader:generate'; /** @var Compiler[] */ - private $compilers = []; + private array $compilers = []; public function __construct(Container $container) diff --git a/src/Nette/WebLoader.php b/src/Nette/WebLoader.php index 4fbc6e0..23e66b2 100755 --- a/src/Nette/WebLoader.php +++ b/src/Nette/WebLoader.php @@ -19,14 +19,9 @@ abstract class WebLoader extends Control { - /** @var Compiler */ - private $compiler; - - /** @var string */ - private $tempPath; - - /** @var bool */ - private $appendLastModified; + private Compiler $compiler; + private string $tempPath; + private bool $appendLastModified; public function __construct(Compiler $compiler, string $tempPath, bool $appendLastModified) From 17f7f5650ac3f244acd97a6c258fcc6066eae47a Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Sun, 28 Feb 2021 16:18:55 +0100 Subject: [PATCH 25/66] php 7.4, remove useless stuff --- src/FileCollection.php | 6 ------ src/Filter/CssMinFilter.php | 3 --- src/Filter/VariablesFilter.php | 11 ---------- src/Nette/LoaderFactory.php | 1 - tests/CompilerTest.php | 3 +-- tests/DefaultOutputNamingConventionTest.php | 7 ++----- tests/FileCollectionTest.php | 3 +-- tests/Filter/CssMinFilterTest.php | 7 ++----- tests/Filter/CssUrlsFilterTest.php | 3 +-- tests/Filter/JsMinFilterTest.php | 7 ++----- tests/Filter/LessFilterTest.php | 7 ++----- tests/Filter/ScssFilterTest.php | 7 ++----- tests/Filter/VariablesFilterTest.php | 3 +-- tests/Nette/ExtensionTest.php | 23 ++++++--------------- 14 files changed, 20 insertions(+), 71 deletions(-) diff --git a/src/FileCollection.php b/src/FileCollection.php index 1c55eea..46bd4dc 100755 --- a/src/FileCollection.php +++ b/src/FileCollection.php @@ -21,18 +21,12 @@ class FileCollection implements IFileCollection private array $remoteFiles = []; - /** - * @param string|null $root files root for relative paths - */ public function __construct(?string $root = null) { $this->root = (string) $root; } - /** - * Get file list - */ public function getFiles(): array { return array_values($this->files); diff --git a/src/Filter/CssMinFilter.php b/src/Filter/CssMinFilter.php index e28e417..fd0dcd0 100755 --- a/src/Filter/CssMinFilter.php +++ b/src/Filter/CssMinFilter.php @@ -9,9 +9,6 @@ class CssMinFilter { - /** - * Minify code - */ public function __invoke(string $code, Compiler $compiler, string $file = ''): string { $minifier = new Minifier; diff --git a/src/Filter/VariablesFilter.php b/src/Filter/VariablesFilter.php index 7851bab..3bf0426 100644 --- a/src/Filter/VariablesFilter.php +++ b/src/Filter/VariablesFilter.php @@ -20,9 +20,6 @@ class VariablesFilter private array $variables; - /** - * Construct - */ public function __construct(array $variables = []) { foreach ($variables as $key => $value) { @@ -31,11 +28,6 @@ public function __construct(array $variables = []) } - /** - * Set delimiter - * - * @return VariablesFilter - */ public function setDelimiter(string $start, string $end): self { $this->startVariable = (string) $start; @@ -44,9 +36,6 @@ public function setDelimiter(string $start, string $end): self } - /** - * Invoke filter - */ public function __invoke(string $code): string { $start = $this->startVariable; diff --git a/src/Nette/LoaderFactory.php b/src/Nette/LoaderFactory.php index fdbee21..092da2e 100644 --- a/src/Nette/LoaderFactory.php +++ b/src/Nette/LoaderFactory.php @@ -22,7 +22,6 @@ class LoaderFactory /** - * LoaderFactory constructor. * @param array $tempPaths * @param string $extensionName * @param IRequest $httpRequest diff --git a/tests/CompilerTest.php b/tests/CompilerTest.php index 079ef38..2d20730 100644 --- a/tests/CompilerTest.php +++ b/tests/CompilerTest.php @@ -17,8 +17,7 @@ class CompilerTest extends TestCase { - /** @var Compiler */ - private $object; + private Compiler $object; protected function setUp(): void diff --git a/tests/DefaultOutputNamingConventionTest.php b/tests/DefaultOutputNamingConventionTest.php index d720424..2f47cad 100644 --- a/tests/DefaultOutputNamingConventionTest.php +++ b/tests/DefaultOutputNamingConventionTest.php @@ -16,11 +16,8 @@ class DefaultOutputNamingConventionTest extends TestCase { - /** @var DefaultOutputNamingConvention */ - private $object; - - /** @var Compiler */ - private $compiler; + private DefaultOutputNamingConvention $object; + protected Compiler $compiler; protected function setUp(): void diff --git a/tests/FileCollectionTest.php b/tests/FileCollectionTest.php index eb1ca0f..436bbd9 100755 --- a/tests/FileCollectionTest.php +++ b/tests/FileCollectionTest.php @@ -17,8 +17,7 @@ class FileCollectionTest extends TestCase { - /** @var FileCollection */ - private $object; + private FileCollection $object; protected function setUp(): void diff --git a/tests/Filter/CssMinFilterTest.php b/tests/Filter/CssMinFilterTest.php index df3dc2b..aeb8b9f 100755 --- a/tests/Filter/CssMinFilterTest.php +++ b/tests/Filter/CssMinFilterTest.php @@ -11,11 +11,8 @@ class CssMinFilterTest extends TestCase { - /** @var CssMinFilter */ - private $filter; - - /** @var Compiler */ - private $compiler; + private CssMinFilter $filter; + private Compiler $compiler; protected function setUp(): void diff --git a/tests/Filter/CssUrlsFilterTest.php b/tests/Filter/CssUrlsFilterTest.php index 9113527..8d1b6d7 100755 --- a/tests/Filter/CssUrlsFilterTest.php +++ b/tests/Filter/CssUrlsFilterTest.php @@ -9,8 +9,7 @@ class CssUrlsFilterTest extends TestCase { - /** @var CssUrlsFilter */ - private $object; + private CssUrlsFilter $object; protected function setUp(): void diff --git a/tests/Filter/JsMinFilterTest.php b/tests/Filter/JsMinFilterTest.php index 084a032..4bdb734 100755 --- a/tests/Filter/JsMinFilterTest.php +++ b/tests/Filter/JsMinFilterTest.php @@ -11,11 +11,8 @@ class JsMinFilterTest extends TestCase { - /** @var JsMinFilter */ - private $filter; - - /** @var Compiler */ - private $compiler; + private JsMinFilter $filter; + private Compiler $compiler; protected function setUp(): void diff --git a/tests/Filter/LessFilterTest.php b/tests/Filter/LessFilterTest.php index ebd73d8..20c9079 100755 --- a/tests/Filter/LessFilterTest.php +++ b/tests/Filter/LessFilterTest.php @@ -11,11 +11,8 @@ class LessFilterTest extends TestCase { - /** @var LessFilter */ - private $filter; - - /** @var Compiler */ - private $compiler; + private LessFilter $filter; + private Compiler $compiler; protected function setUp(): void diff --git a/tests/Filter/ScssFilterTest.php b/tests/Filter/ScssFilterTest.php index 5af3c48..5e643b1 100755 --- a/tests/Filter/ScssFilterTest.php +++ b/tests/Filter/ScssFilterTest.php @@ -13,11 +13,8 @@ class ScssFilterTest extends TestCase { - /** @var ScssFilter */ - private $filter; - - /** @var Compiler */ - private $compiler; + private ScssFilter $filter; + private Compiler $compiler; protected function setUp(): void diff --git a/tests/Filter/VariablesFilterTest.php b/tests/Filter/VariablesFilterTest.php index 3c3b534..9e720ee 100644 --- a/tests/Filter/VariablesFilterTest.php +++ b/tests/Filter/VariablesFilterTest.php @@ -9,8 +9,7 @@ class VariablesFilterTest extends TestCase { - /** @var VariablesFilter */ - private $object; + private VariablesFilter $object; protected function setUp(): void diff --git a/tests/Nette/ExtensionTest.php b/tests/Nette/ExtensionTest.php index e0a26a8..9ec3a01 100755 --- a/tests/Nette/ExtensionTest.php +++ b/tests/Nette/ExtensionTest.php @@ -14,23 +14,12 @@ class ExtensionTest extends TestCase { - /** @var Container */ - private $container; - - /** @var string */ - private $appDir; - - /** @var string */ - private $wwwDir; - - /** @var string */ - private $tempDir; - - /** @var string */ - private $fixturesDir; - - /** @var bool */ - private $debugMode; + private Container $container; + private string $appDir; + private string $wwwDir; + private string $tempDir; + private string $fixturesDir; + private bool $debugMode; protected function setUp(): void From c579c7363306360bfb1c7e4a5df8546205eed31b Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Sun, 28 Feb 2021 16:20:34 +0100 Subject: [PATCH 26/66] Suggest nette/safe-stream --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 80edb74..c08f503 100755 --- a/composer.json +++ b/composer.json @@ -34,7 +34,8 @@ "scssphp/scssphp": "SCSS compiler written in PHP.", "tedivm/jshrink": "Javascript Minifier built in PHP", "tubalmartin/cssmin": "A PHP port of the YUI CSS compressor", - "symfony/console": "For pre-generating files form the CLI" + "symfony/console": "For pre-generating files form the CLI", + "nette/safe-stream": "Atomic and safe manipulation with files via native PHP functions." }, "require-dev": { "nette/bootstrap": "^3.1", From a0472b963b793f63b228a73f8984015d71b0bf35 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Sun, 28 Feb 2021 16:21:30 +0100 Subject: [PATCH 27/66] fix typo --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index c08f503..d89ddd9 100755 --- a/composer.json +++ b/composer.json @@ -34,7 +34,7 @@ "scssphp/scssphp": "SCSS compiler written in PHP.", "tedivm/jshrink": "Javascript Minifier built in PHP", "tubalmartin/cssmin": "A PHP port of the YUI CSS compressor", - "symfony/console": "For pre-generating files form the CLI", + "symfony/console": "For pre-generating files from CLI", "nette/safe-stream": "Atomic and safe manipulation with files via native PHP functions." }, "require-dev": { From fe9aa5bc9cee5efaac2fb55672c156a6f83f1527 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Sun, 28 Feb 2021 16:26:29 +0100 Subject: [PATCH 28/66] Use Nette\Utils\FileSystem::write instead of plain file_put_contents --- src/Compiler.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Compiler.php b/src/Compiler.php index e871c2c..101fcd6 100755 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -4,6 +4,8 @@ namespace WebLoader; +use Nette\Utils\FileSystem; + /** * Compiler * @@ -240,7 +242,7 @@ protected function generateFiles(array $files, array $watchFiles = []): File if (!file_exists($path) || $lastModified > filemtime($path) || $this->debugging === true) { $outPath = in_array('nette.safe', stream_get_wrappers(), true) ? 'nette.safe://' . $path : $path; - file_put_contents($outPath, $this->getContent($files)); + FileSystem::write($outPath, $this->getContent($files)); } return new File($name, (int) filemtime($path), $files); From 4663865b6fed87126463bd37779722008007e11f Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Sun, 28 Feb 2021 16:33:37 +0100 Subject: [PATCH 29/66] Use Nette\Utils\FileSystem::normalizePath() instead of a custom one --- src/Path.php | 20 +++----------------- tests/Path/PathTest.php | 7 +++++++ 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/src/Path.php b/src/Path.php index 7b95eb6..5dd5b80 100755 --- a/src/Path.php +++ b/src/Path.php @@ -4,26 +4,12 @@ namespace WebLoader; +use Nette\Utils\FileSystem; + class Path { public static function normalize(string $path): string { - $path = strtr($path, '\\', '/'); - $root = (strpos($path, '/') === 0) ? '/' : ''; - $pieces = explode('/', trim($path, '/')); - $res = []; - - foreach ($pieces as $piece) { - if ($piece === '.' || $piece === '') { - continue; - } - if ($piece === '..') { - array_pop($res); - } else { - array_push($res, $piece); - } - } - - return $root . implode('/', $res); + return FileSystem::normalizePath($path); } } diff --git a/tests/Path/PathTest.php b/tests/Path/PathTest.php index b8f25dd..1fabe27 100755 --- a/tests/Path/PathTest.php +++ b/tests/Path/PathTest.php @@ -13,4 +13,11 @@ public function testNormalize(): void $normalized = Path::normalize('/path/to//project//that/contains/0/in/it'); $this->assertEquals('/path/to/project/that/contains/0/in/it', $normalized); } + + + public function testDirectoryJump(): void + { + $normalized = Path::normalize('/path/to/my/project/../../wrong/path/../../correct/one/'); + $this->assertEquals('/path/to/correct/one/', $normalized); + } } From 067924c09504de5d6982d9c304215a0cf8e55289 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Sun, 28 Feb 2021 16:36:17 +0100 Subject: [PATCH 30/66] update .gitignore --- .gitignore | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 1c00021..5fa635d 100755 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,12 @@ -vendor -composer.lock -.idea +# Masks *.bak + +# Files +composer.lock +.phpunit.result.cache + +# Dirs +vendor/ temp/ +.idea/ + From 7306e0c56af8740971eaee0ef2f6a366511bd907 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Sun, 28 Feb 2021 16:43:14 +0100 Subject: [PATCH 31/66] Update README.md --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 354ab36..ec29728 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,13 @@ WebLoader [![Build Status](https://secure.travis-ci.org/janmarek/WebLoader.png?branch=master)](http://travis-ci.org/janmarek/WebLoader) ======================= -Component for CSS and JS files loading +Component for CSS and JS files loading. -Author: Jan Marek +Author: [Jan Marek](https://github.com/janmarek) Licence: MIT +Updated for Nette 3/3.1 and PHP 7.4/8.0 by [Gappa](https://github.com/Gappa). + Example ------- From 86c976fe8cbaf821e38dffe3aeff753ad96d2c48 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Sun, 28 Feb 2021 16:54:57 +0100 Subject: [PATCH 32/66] Fix PHP 7.4 errors --- src/Nette/CssLoader.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Nette/CssLoader.php b/src/Nette/CssLoader.php index d1c20ec..7483b03 100755 --- a/src/Nette/CssLoader.php +++ b/src/Nette/CssLoader.php @@ -14,8 +14,8 @@ */ class CssLoader extends WebLoader { - private string $media; - private string $title; + private ?string $media = null; + private ?string $title = null; private string $type = 'text/css'; private bool $alternate = false; From e21edb73bf0f66223ac5d4ced55d488155861a24 Mon Sep 17 00:00:00 2001 From: Ciki Date: Tue, 25 May 2021 11:41:47 +0200 Subject: [PATCH 33/66] initialize typed property --- src/Compiler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Compiler.php b/src/Compiler.php index 101fcd6..529fa44 100755 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -25,7 +25,7 @@ class Compiler private bool $async = false; private bool $defer = false; private bool $absoluteUrl = false; - private ?string $nonce; + private ?string $nonce = null; public function __construct(IFileCollection $files, IOutputNamingConvention $convention, string $outputDir) From f878a6ca9f13e6d8e23531b81005072f36e3dcf5 Mon Sep 17 00:00:00 2001 From: Ciki Date: Tue, 25 May 2021 11:42:21 +0200 Subject: [PATCH 34/66] initialize typed property --- src/Filter/VariablesFilter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Filter/VariablesFilter.php b/src/Filter/VariablesFilter.php index 3bf0426..faeb7a5 100644 --- a/src/Filter/VariablesFilter.php +++ b/src/Filter/VariablesFilter.php @@ -17,7 +17,7 @@ class VariablesFilter private string $startVariable = '{{$'; private string $endVariable = '}}'; - private array $variables; + private array $variables = []; public function __construct(array $variables = []) From 2c9929f5c83418194d725c7c83059306173b1ccd Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Tue, 27 Jul 2021 15:36:44 +0200 Subject: [PATCH 35/66] remove joinfiles --- src/Compiler.php | 39 +++------------------ src/Nette/Extension.php | 3 -- tests/CompilerTest.php | 22 ------------ tests/Nette/ExtensionTest.php | 29 --------------- tests/fixtures/extension.neon | 9 +---- tests/fixtures/extensionJoinFilesFalse.neon | 3 -- tests/fixtures/extensionJoinFilesTrue.neon | 3 -- 7 files changed, 5 insertions(+), 103 deletions(-) delete mode 100644 tests/fixtures/extensionJoinFilesFalse.neon delete mode 100644 tests/fixtures/extensionJoinFilesTrue.neon diff --git a/src/Compiler.php b/src/Compiler.php index 101fcd6..3cd7209 100755 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -17,7 +17,6 @@ class Compiler private IFileCollection $collection; private IOutputNamingConvention $namingConvention; private string $outputDir; - private bool $joinFiles = true; private array $filters = []; private array $fileFilters = []; private bool $checkLastModified = true; @@ -94,24 +93,6 @@ public function setOutputDir(string $tempPath): void } - /** - * Get join files - */ - public function getJoinFiles(): bool - { - return $this->joinFiles; - } - - - /** - * Set join files - */ - public function setJoinFiles(bool $joinFiles): void - { - $this->joinFiles = $joinFiles; - } - - public function isAsync(): bool { return $this->async; @@ -214,23 +195,11 @@ public function generate(): array return []; } - if ($this->joinFiles) { - $watchFiles = $this->checkLastModified ? array_unique(array_merge($files, $this->collection->getWatchFiles())) : []; + $watchFiles = $this->checkLastModified ? array_unique(array_merge($files, $this->collection->getWatchFiles())) : []; - return [ - $this->generateFiles($files, $watchFiles), - ]; - - } else { - $arr = []; - - foreach ($files as $file) { - $watchFiles = $this->checkLastModified ? array_unique(array_merge([$file], $this->collection->getWatchFiles())) : []; - $arr[] = $this->generateFiles([$file], $watchFiles); - } - - return $arr; - } + return [ + $this->generateFiles($files, $watchFiles), + ]; } diff --git a/src/Nette/Extension.php b/src/Nette/Extension.php index 7ede679..f52e536 100644 --- a/src/Nette/Extension.php +++ b/src/Nette/Extension.php @@ -50,7 +50,6 @@ public function getConfigSchema(): Schema 'remoteFiles' => Expect::array(), 'filters' => Expect::array(), 'fileFilters' => Expect::array(), - 'joinFiles' => Expect::bool(true), 'async' => Expect::bool(false), 'defer' => Expect::bool(false), 'nonce' => Expect::string()->nullable(), @@ -68,7 +67,6 @@ public function getConfigSchema(): Schema 'remoteFiles' => Expect::array(), 'filters' => Expect::array(), 'fileFilters' => Expect::array(), - 'joinFiles' => Expect::bool(true), 'async' => Expect::bool(false), 'defer' => Expect::bool(false), 'nonce' => Expect::string()->nullable(), @@ -155,7 +153,6 @@ private function addWebLoader(ContainerBuilder $builder, string $name, array $co ]); $compiler - ->addSetup('setJoinFiles', [$config['joinFiles']]) ->addSetup('setAsync', [$config['async']]) ->addSetup('setDefer', [$config['defer']]) ->addSetup('setNonce', [$config['nonce']]) diff --git a/tests/CompilerTest.php b/tests/CompilerTest.php index 2d20730..3aebe63 100644 --- a/tests/CompilerTest.php +++ b/tests/CompilerTest.php @@ -53,19 +53,8 @@ private function getTempFiles(): array } - public function testJoinFiles(): void - { - $this->assertTrue($this->object->getJoinFiles()); - - $ret = $this->object->generate(); - $this->assertCount(1, $ret, 'Multiple files are generated instead of join.'); - $this->assertCount(1, $this->getTempFiles(), 'Multiple files are generated instead of join.'); - } - - public function testEmptyFiles(): void { - $this->assertTrue($this->object->getJoinFiles()); $this->object->setFileCollection(new \WebLoader\FileCollection()); $ret = $this->object->generate(); @@ -74,17 +63,6 @@ public function testEmptyFiles(): void } - public function testNotJoinFiles(): void - { - $this->object->setJoinFiles(false); - $this->assertFalse($this->object->getJoinFiles()); - - $ret = $this->object->generate(); - $this->assertCount(3, $ret, 'Wrong file count generated.'); - $this->assertCount(3, $this->getTempFiles(), 'Wrong file count generated.'); - } - - public function testSetOutDir(): void { $this->expectException(FileNotFoundException::class); diff --git a/tests/Nette/ExtensionTest.php b/tests/Nette/ExtensionTest.php index 9ec3a01..bad0ad8 100755 --- a/tests/Nette/ExtensionTest.php +++ b/tests/Nette/ExtensionTest.php @@ -76,35 +76,6 @@ public function testExcludeFiles(): void } - public function testJoinFilesOn(): void - { - $this->prepareContainer([ - $this->fixturesDir . '/extension.neon', - $this->fixturesDir . '/extensionJoinFilesTrue.neon', - ]); - $this->assertTrue($this->container->getService('webloader.jsDefaultCompiler')->getJoinFiles()); - } - - - public function testJoinFilesOff(): void - { - $this->prepareContainer([ - $this->fixturesDir . '/extension.neon', - $this->fixturesDir . '/extensionJoinFilesFalse.neon', - ]); - $this->assertFalse($this->container->getService('webloader.jsDefaultCompiler')->getJoinFiles()); - } - - - public function testJoinFilesOffInOneService(): void - { - $this->prepareContainer([ - $this->fixturesDir . '/extension.neon', - ]); - $this->assertFalse($this->container->getService('webloader.cssJoinOffCompiler')->getJoinFiles()); - } - - public function testAsyncOn(): void { $this->prepareContainer([ diff --git a/tests/fixtures/extension.neon b/tests/fixtures/extension.neon index d8ac1d8..b2f0fef 100755 --- a/tests/fixtures/extension.neon +++ b/tests/fixtures/extension.neon @@ -1,6 +1,3 @@ -parameters: - cssJoinFiles: false - http: frames: yes @@ -20,10 +17,6 @@ webloader: files: - style.css - {files: ["*.css"], from: %fixturesDir%/dir} - joinOff: - joinFiles: %cssJoinFiles% - files: - - style.css js: default: @@ -37,4 +30,4 @@ webloader: - @variablesFilter exclude: files: - - {files: ["*"], from: %fixturesDir%, exclude: [dir/*]} \ No newline at end of file + - {files: ["*"], from: %fixturesDir%, exclude: [dir/*]} diff --git a/tests/fixtures/extensionJoinFilesFalse.neon b/tests/fixtures/extensionJoinFilesFalse.neon deleted file mode 100644 index 8a3f58d..0000000 --- a/tests/fixtures/extensionJoinFilesFalse.neon +++ /dev/null @@ -1,3 +0,0 @@ -webloader: - jsDefaults: - joinFiles: false \ No newline at end of file diff --git a/tests/fixtures/extensionJoinFilesTrue.neon b/tests/fixtures/extensionJoinFilesTrue.neon deleted file mode 100644 index 1523860..0000000 --- a/tests/fixtures/extensionJoinFilesTrue.neon +++ /dev/null @@ -1,3 +0,0 @@ -webloader: - jsDefaults: - joinFiles: true \ No newline at end of file From 36c7b50ff662e6cf5c772169387ebe0965530545 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Wed, 28 Jul 2021 11:33:33 +0200 Subject: [PATCH 36/66] Added renderInline, renderUrl, refactoring --- src/Compiler.php | 13 +++---- src/File.php | 28 ++++++++------- src/Nette/CssLoader.php | 20 +++++++++-- src/Nette/Diagnostics/Panel.php | 2 +- src/Nette/JavaScriptLoader.php | 17 +++++++-- src/Nette/LoaderFactory.php | 26 +++++++++----- src/Nette/SymfonyConsole/GenerateCommand.php | 2 +- src/Nette/WebLoader.php | 36 +++++++++++++++++--- tests/CompilerTest.php | 19 ++++++----- 9 files changed, 115 insertions(+), 48 deletions(-) diff --git a/src/Compiler.php b/src/Compiler.php index 3cd7209..d2d727c 100755 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -184,22 +184,17 @@ public function getContent(?array $files = null): string } - /** - * Load content and save file - */ - public function generate(): array + public function generate(): ?File { $files = $this->collection->getFiles(); if (!count($files)) { - return []; + return null; } $watchFiles = $this->checkLastModified ? array_unique(array_merge($files, $this->collection->getWatchFiles())) : []; - return [ - $this->generateFiles($files, $watchFiles), - ]; + return $this->generateFiles($files, $watchFiles); } @@ -214,7 +209,7 @@ protected function generateFiles(array $files, array $watchFiles = []): File FileSystem::write($outPath, $this->getContent($files)); } - return new File($name, (int) filemtime($path), $files); + return new File($path, $files); } diff --git a/src/File.php b/src/File.php index 833fae1..337e8b4 100644 --- a/src/File.php +++ b/src/File.php @@ -4,34 +4,38 @@ namespace WebLoader; -class File -{ +use SplFileInfo; - protected string $file; - protected ?int $lastModified; - protected array $sourceFiles; +final class File +{ + private SplFileInfo $file; + private array $sourceFiles; public function __construct( - string $file, - ?int $lastModified, + string $path, array $sourceFiles ) { - $this->file = $file; - $this->lastModified = $lastModified; + $this->file = new SplFileInfo($path); $this->sourceFiles = $sourceFiles; } - public function getFile(): string + public function getFileName(): string + { + return $this->file->getBasename(); + } + + + public function getPath(): string { - return $this->file; + return $this->file->getPathname(); } public function getLastModified(): ?int { - return $this->lastModified; + return $this->file->getMTime(); } diff --git a/src/Nette/CssLoader.php b/src/Nette/CssLoader.php index 7483b03..b8687c6 100755 --- a/src/Nette/CssLoader.php +++ b/src/Nette/CssLoader.php @@ -4,7 +4,9 @@ namespace WebLoader\Nette; +use Nette\Utils\FileSystem; use Nette\Utils\Html; +use WebLoader\File; /** * Css loader @@ -72,7 +74,7 @@ public function setAlternate(bool $alternate): self } - public function getElement(string $source): Html + public function getElement(File $file): Html { if ($this->alternate) { $alternate = ' alternate'; @@ -86,8 +88,22 @@ public function getElement(string $source): Html $el->setAttribute('media', $this->media); $el->setAttribute('title', $this->title); $el->setAttribute('nonce', $this->getCompiler()->getNonce()); - $el->setAttribute('href', $source); + $el->setAttribute('href', $this->getGeneratedFilePath($file)); return $el; } + + + public function getInlineElement(File $file): Html + { + $el = Html::el('style'); + $el->setAttribute('type', $this->type); + $el->setAttribute('media', $this->media); + $el->setAttribute('title', $this->title); + $el->setAttribute('nonce', $this->getCompiler()->getNonce()); + $el->setHtml(FileSystem::read($file->getPath())); + + return $el; + } + } diff --git a/src/Nette/Diagnostics/Panel.php b/src/Nette/Diagnostics/Panel.php index 02b250b..f80f527 100644 --- a/src/Nette/Diagnostics/Panel.php +++ b/src/Nette/Diagnostics/Panel.php @@ -88,7 +88,7 @@ private function compute(): array /** @var File $generated */ foreach ($compiler->generate() as $generated) { - $generatedSize = filesize($compiler->getOutputDir() . DIRECTORY_SEPARATOR . $generated->getFile()); + $generatedSize = filesize($compiler->getOutputDir() . DIRECTORY_SEPARATOR . $generated->getFileName()); $size['combined'] += $generatedSize; $compilerCombinedSize += $generatedSize; diff --git a/src/Nette/JavaScriptLoader.php b/src/Nette/JavaScriptLoader.php index a752f17..8833ba4 100644 --- a/src/Nette/JavaScriptLoader.php +++ b/src/Nette/JavaScriptLoader.php @@ -4,7 +4,9 @@ namespace WebLoader\Nette; +use Nette\Utils\FileSystem; use Nette\Utils\Html; +use WebLoader\File; /** * JavaScript loader @@ -14,14 +16,25 @@ */ class JavaScriptLoader extends WebLoader { - public function getElement(string $source): Html + public function getElement(File $file): Html { $el = Html::el('script'); $el->setAttribute('async', $this->getCompiler()->isAsync()); $el->setAttribute('defer', $this->getCompiler()->isDefer()); $el->setAttribute('nonce', $this->getCompiler()->getNonce()); - $el->setAttribute('src', $source); + $el->setAttribute('src', $this->getGeneratedFilePath($file)); return $el; } + + + public function getInlineElement(File $file): Html + { + $el = Html::el('script'); + $el->setAttribute('nonce', $this->getCompiler()->getNonce()); + $el->setHtml(FileSystem::read($file->getPath())); + + return $el; + } + } diff --git a/src/Nette/LoaderFactory.php b/src/Nette/LoaderFactory.php index 092da2e..f814878 100644 --- a/src/Nette/LoaderFactory.php +++ b/src/Nette/LoaderFactory.php @@ -40,10 +40,23 @@ public function __construct( } - public function createCssLoader(string $name, bool $appendLastModified = true): CssLoader + private function getCompiler(string $name, string $type): Compiler { /** @var Compiler $compiler */ - $compiler = $this->diContainer->getService($this->extensionName . '.css' . ucfirst($name) . 'Compiler'); + $compiler = $this->diContainer->getService( + $this->extensionName . + '.' . + $type . + ucfirst($name) . + 'Compiler' + ); + return $compiler; + } + + + public function createCssLoader(string $name, bool $appendLastModified = true): CssLoader + { + $compiler = $this->getCompiler($name, 'css'); $this->modifyConvention($compiler->getOutputNamingConvention(), $name); return new CssLoader($compiler, $this->formatTempPath($name, $compiler->isAbsoluteUrl()), $appendLastModified); } @@ -51,8 +64,7 @@ public function createCssLoader(string $name, bool $appendLastModified = true): public function createJavaScriptLoader(string $name, bool $appendLastModified = true): JavaScriptLoader { - /** @var Compiler $compiler */ - $compiler = $this->diContainer->getService($this->extensionName . '.js' . ucfirst($name) . 'Compiler'); + $compiler = $this->getCompiler($name, 'js'); $this->modifyConvention($compiler->getOutputNamingConvention(), $name); return new JavaScriptLoader($compiler, $this->formatTempPath($name, $compiler->isAbsoluteUrl()), $appendLastModified); } @@ -61,18 +73,16 @@ public function createJavaScriptLoader(string $name, bool $appendLastModified = private function formatTempPath(string $name, bool $absoluteUrl = false): string { $lName = strtolower($name); - $tempPath = isset($this->tempPaths[$lName]) ? $this->tempPaths[$lName] : Extension::DEFAULT_TEMP_PATH; + $tempPath = $this->tempPaths[$lName] ?? Extension::DEFAULT_TEMP_PATH; $method = $absoluteUrl ? 'getBaseUrl' : 'getBasePath'; return rtrim($this->httpRequest->getUrl()->{$method}(), '/') . '/' . $tempPath; } - private function modifyConvention(IOutputNamingConvention $convention, string $name): IOutputNamingConvention + private function modifyConvention(IOutputNamingConvention $convention, string $name): void { if ($convention instanceof DefaultOutputNamingConvention) { $convention->setPrefix($name . '-'); } - - return $convention; } } diff --git a/src/Nette/SymfonyConsole/GenerateCommand.php b/src/Nette/SymfonyConsole/GenerateCommand.php index a1601dd..aca7edc 100644 --- a/src/Nette/SymfonyConsole/GenerateCommand.php +++ b/src/Nette/SymfonyConsole/GenerateCommand.php @@ -49,7 +49,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int foreach ($this->compilers as $compiler) { $files = $compiler->generate(); foreach ($files as $file) { - $output->writeln($file->getFile()); + $output->writeln($file->getFileName()); $noFiles = false; } } diff --git a/src/Nette/WebLoader.php b/src/Nette/WebLoader.php index 23e66b2..b996021 100755 --- a/src/Nette/WebLoader.php +++ b/src/Nette/WebLoader.php @@ -59,7 +59,16 @@ public function setTempPath(string $tempPath): void /** * Get html element including generated content */ - abstract public function getElement(string $source): Html; + abstract public function getElement(File $file): Html; + + + abstract public function getInlineElement(File $file): Html; + + + protected function getUrl(File $file): string + { + return $this->getGeneratedFilePath($file); + } /** @@ -81,8 +90,9 @@ public function render(): void echo $this->getElement($file), PHP_EOL; } - foreach ($this->compiler->generate() as $file) { - echo $this->getElement($this->getGeneratedFilePath($file)), PHP_EOL; + $file = $this->compiler->generate(); + if ($file) { + echo $this->getElement($file), PHP_EOL; } if ($hasArgs && !empty($backup)) { @@ -91,9 +101,27 @@ public function render(): void } + public function renderInline(): void + { + $file = $this->compiler->generate(); + if ($file) { + echo $this->getInlineElement($file), PHP_EOL; + } + } + + + public function renderUrl(): void + { + $file = $this->compiler->generate(); + if ($file) { + echo $this->getUrl($file), PHP_EOL; + } + } + + protected function getGeneratedFilePath(File $file): string { - $path = $this->tempPath . '/' . $file->getFile(); + $path = $this->tempPath . '/' . $file->getFileName(); if ($this->appendLastModified) { $path .= '?' . $file->getLastModified(); diff --git a/tests/CompilerTest.php b/tests/CompilerTest.php index 3aebe63..8d406bf 100644 --- a/tests/CompilerTest.php +++ b/tests/CompilerTest.php @@ -7,6 +7,7 @@ use PHPUnit\Framework\TestCase; use TypeError; use WebLoader\Compiler; +use WebLoader\FileCollection; use WebLoader\FileNotFoundException; /** @@ -55,10 +56,10 @@ private function getTempFiles(): array public function testEmptyFiles(): void { - $this->object->setFileCollection(new \WebLoader\FileCollection()); + $this->object->setFileCollection(new FileCollection()); $ret = $this->object->generate(); - $this->assertCount(0, $ret); + $this->assertNull($ret); $this->assertCount(0, $this->getTempFiles()); } @@ -88,11 +89,11 @@ public function testGeneratingAndFilters(): void $expectedContent = '-' . PHP_EOL . 'a:cba,' . PHP_EOL . 'b:fed,' . PHP_EOL . 'c:ihg,-' . PHP_EOL . 'a:cba,' . PHP_EOL . 'b:fed,' . PHP_EOL . 'c:ihg,'; - $files = $this->object->generate(); + $file = $this->object->generate(); - $this->assertTrue(is_numeric($files[0]->getLastModified()) && $files[0]->getLastModified() > 0, 'Generate does not provide last modified timestamp correctly.'); + $this->assertTrue(is_numeric($file->getLastModified()) && $file->getLastModified() > 0, 'Generate does not provide last modified timestamp correctly.'); - $content = file_get_contents($this->object->getOutputDir() . '/' . $files[0]->getFile()); + $content = file_get_contents($this->object->getOutputDir() . '/' . $file->getFileName()); $this->assertEquals($expectedContent, $content); } @@ -100,10 +101,10 @@ public function testGeneratingAndFilters(): void public function testGenerateReturnsSourceFilePaths(): void { - $res = $this->object->generate(); - $this->assertIsArray($res[0]->getSourceFiles()); - $this->assertCount(3, $res[0]->getSourceFiles()); - $this->assertFileExists($res[0]->getSourceFiles()[0]); + $file = $this->object->generate(); + $this->assertIsArray($file->getSourceFiles()); + $this->assertCount(3, $file->getSourceFiles()); + $this->assertFileExists($file->getSourceFiles()[0]); } From 4b18c8877411a6f3acd4bafab37f971592170739 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Wed, 28 Jul 2021 12:55:10 +0200 Subject: [PATCH 37/66] Update Compiler.php Fixes #3 --- src/Compiler.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Compiler.php b/src/Compiler.php index 529fa44..7fa0829 100755 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -241,8 +241,9 @@ protected function generateFiles(array $files, array $watchFiles = []): File $lastModified = $this->checkLastModified ? $this->getLastModified($watchFiles) : 0; if (!file_exists($path) || $lastModified > filemtime($path) || $this->debugging === true) { - $outPath = in_array('nette.safe', stream_get_wrappers(), true) ? 'nette.safe://' . $path : $path; - FileSystem::write($outPath, $this->getContent($files)); + // disabled: https://github.com/nette/safe-stream/pull/5 + // $outPath = in_array('nette.safe', stream_get_wrappers(), true) ? 'nette.safe://' . $path : $path; + FileSystem::write($path, $this->getContent($files)); } return new File($name, (int) filemtime($path), $files); From 243b64f9b655f3c4cad75b609fa1f96e58aa2063 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Wed, 28 Jul 2021 14:04:19 +0200 Subject: [PATCH 38/66] fixes --- src/Nette/Diagnostics/Panel.php | 58 +++++++++++--------- src/Nette/SymfonyConsole/GenerateCommand.php | 4 +- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/Nette/Diagnostics/Panel.php b/src/Nette/Diagnostics/Panel.php index f80f527..fe7093b 100644 --- a/src/Nette/Diagnostics/Panel.php +++ b/src/Nette/Diagnostics/Panel.php @@ -28,7 +28,7 @@ class Panel implements IBarPanel /** @var Compiler[] */ private array $compilers = []; - private ?array $size; + private ?array $size = null; private array $files; private array $sizes; private string $root; @@ -86,33 +86,37 @@ private function compute(): array $compilerCombinedSize = 0; - /** @var File $generated */ - foreach ($compiler->generate() as $generated) { - $generatedSize = filesize($compiler->getOutputDir() . DIRECTORY_SEPARATOR . $generated->getFileName()); - $size['combined'] += $generatedSize; - $compilerCombinedSize += $generatedSize; - - foreach ($generated->getSourceFiles() as $file) { - $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION)); - $file = str_replace('\\', DIRECTORY_SEPARATOR, (string) realpath($file)); - - if (!isset($this->files[$group][$extension])) { - $this->files[$group][$extension] = []; - } - if (!isset($this->sizes[$group][$extension])) { - $this->sizes[$group][$extension] = ['original' => 0, 'combined' => 0]; - } - - $this->files[$group][$extension][] = [ - 'name' => basename($file), - 'full' => $file, - 'size' => $fileSize = filesize($file), - ]; - - $size['original'] += $fileSize; - $this->sizes[$group][$extension]['original'] += $fileSize; - $this->sizes[$group]['.']['original'] += $fileSize; + $generated = $compiler->generate(); + + if (is_null($generated)) { + continue; + } + + $generatedSize = filesize($compiler->getOutputDir() . DIRECTORY_SEPARATOR . $generated->getFileName()); + $size['combined'] += $generatedSize; + + $compilerCombinedSize += $generatedSize; + + foreach ($generated->getSourceFiles() as $file) { + $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION)); + $file = str_replace('\\', DIRECTORY_SEPARATOR, (string) realpath($file)); + + if (!isset($this->files[$group][$extension])) { + $this->files[$group][$extension] = []; + } + if (!isset($this->sizes[$group][$extension])) { + $this->sizes[$group][$extension] = ['original' => 0, 'combined' => 0]; } + + $this->files[$group][$extension][] = [ + 'name' => basename($file), + 'full' => $file, + 'size' => $fileSize = filesize($file), + ]; + + $size['original'] += $fileSize; + $this->sizes[$group][$extension]['original'] += $fileSize; + $this->sizes[$group]['.']['original'] += $fileSize; } $this->sizes[$group]['.']['combined'] += $compilerCombinedSize; diff --git a/src/Nette/SymfonyConsole/GenerateCommand.php b/src/Nette/SymfonyConsole/GenerateCommand.php index aca7edc..eaa1a74 100644 --- a/src/Nette/SymfonyConsole/GenerateCommand.php +++ b/src/Nette/SymfonyConsole/GenerateCommand.php @@ -47,8 +47,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int { $noFiles = true; foreach ($this->compilers as $compiler) { - $files = $compiler->generate(); - foreach ($files as $file) { + $file = $compiler->generate(); + if (!is_null($file)) { $output->writeln($file->getFileName()); $noFiles = false; } From b3989948a617eb3dd6e447178f51c7e41f54699d Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Wed, 24 Nov 2021 09:48:25 +0100 Subject: [PATCH 39/66] use ::class --- src/Nette/Extension.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Nette/Extension.php b/src/Nette/Extension.php index f52e536..5111e19 100644 --- a/src/Nette/Extension.php +++ b/src/Nette/Extension.php @@ -13,7 +13,12 @@ use Nette\Schema\Schema; use Nette\Utils\Finder; use SplFileInfo; +use WebLoader\Compiler as WebloaderCompiler; +use WebLoader\DefaultOutputNamingConvention; +use WebLoader\FileCollection; use WebLoader\FileNotFoundException; +use WebLoader\Nette\Diagnostics\Panel; +use WebLoader\Nette\SymfonyConsole\GenerateCommand; /** * @author Jan Marek @@ -93,7 +98,7 @@ public function loadConfiguration(): void if ($config['debugger']) { $builder->addDefinition($this->prefix('tracyPanel')) - ->setType('WebLoader\Nette\Diagnostics\Panel') + ->setType(Panel::class) ->setArguments([$this->appDir]); } @@ -115,12 +120,12 @@ public function loadConfiguration(): void } $builder->addDefinition($this->prefix('factory')) - ->setType('WebLoader\Nette\LoaderFactory') + ->setType(LoaderFactory::class) ->setArguments([$loaderFactoryTempPaths, $this->name]); if (class_exists('Symfony\Component\Console\Command\Command')) { $builder->addDefinition($this->prefix('generateCommand')) - ->setType('WebLoader\Nette\SymfonyConsole\GenerateCommand') + ->setType(GenerateCommand::class) ->addTag('kdyby.console.command'); } } @@ -131,7 +136,7 @@ private function addWebLoader(ContainerBuilder $builder, string $name, array $co $filesServiceName = $this->prefix($name . 'Files'); $files = $builder->addDefinition($filesServiceName) - ->setType('WebLoader\FileCollection') + ->setType(FileCollection::class) ->setArguments([$config['sourceDir']]); foreach ($this->findFiles($config['files'], $config['sourceDir']) as $file) { @@ -145,7 +150,7 @@ private function addWebLoader(ContainerBuilder $builder, string $name, array $co $files->addSetup('addRemoteFiles', [$config['remoteFiles']]); $compiler = $builder->addDefinition($this->prefix($name . 'Compiler')) - ->setType('WebLoader\Compiler') + ->setType(WebloaderCompiler::class) ->setArguments([ '@' . $filesServiceName, $config['namingConvention'], From 8e5b39c8d7f42341ce04755970f7156b112c2cf0 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Wed, 24 Nov 2021 09:56:23 +0100 Subject: [PATCH 40/66] update packages & phpunit --- composer.json | 8 ++++---- tests/DefaultOutputNamingConventionTest.php | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index d89ddd9..67f2a82 100755 --- a/composer.json +++ b/composer.json @@ -47,13 +47,13 @@ "wikimedia/less.php": "^3.0.0", "scssphp/scssphp": "^1.1.0", "mockery/mockery": "1.*", - "phpunit/phpunit": "7.*", + "phpunit/phpunit": "9.*", "phpstan/extension-installer": "^1.0", - "phpstan/phpstan": "^0.12.0", - "phpstan/phpstan-nette": "^0.12.0", + "phpstan/phpstan": "^1.0.0", + "phpstan/phpstan-nette": "^1.0.0", "roave/security-advisories": "dev-master", "symfony/console": "^4.2.9|^5.0.0", - "phpstan/phpstan-mockery": "^0.12.5", + "phpstan/phpstan-mockery": "^1.0.0", "tubalmartin/cssmin": "^4.1", "tedivm/jshrink": "^1.3" }, diff --git a/tests/DefaultOutputNamingConventionTest.php b/tests/DefaultOutputNamingConventionTest.php index 2f47cad..3e7fee5 100644 --- a/tests/DefaultOutputNamingConventionTest.php +++ b/tests/DefaultOutputNamingConventionTest.php @@ -35,7 +35,7 @@ public function testMultipleFiles(): void ]; $name = $this->object->getFilename($files, $this->compiler); - $this->assertRegExp('/^[0-9a-f]{12}$/', $name); + $this->assertMatchesRegularExpression('/^[0-9a-f]{12}$/', $name); // another hash $files[] = __DIR__ . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR . 'c.txt'; @@ -51,7 +51,7 @@ public function testOneFile(): void ]; $name = $this->object->getFilename($files, $this->compiler); - $this->assertRegExp('/^[0-9a-f]{12}$/', $name); + $this->assertMatchesRegularExpression('/^[0-9a-f]{12}$/', $name); } @@ -62,7 +62,7 @@ public function testCssConvention(): void ]; $name = DefaultOutputNamingConvention::createCssConvention()->getFilename($files, $this->compiler); - $this->assertRegExp('/^[0-9a-f]{12}.css$/', $name); + $this->assertMatchesRegularExpression('/^[0-9a-f]{12}.css$/', $name); } @@ -73,6 +73,6 @@ public function testJsConvention(): void ]; $name = DefaultOutputNamingConvention::createJsConvention()->getFilename($files, $this->compiler); - $this->assertRegExp('/^[0-9a-f]{12}.js$/', $name); + $this->assertMatchesRegularExpression('/^[0-9a-f]{12}.js$/', $name); } } From e243f182972d459fa5b7b56eaa63d29e711ae31a Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Wed, 24 Nov 2021 10:15:42 +0100 Subject: [PATCH 41/66] update phpstan --- phpstan/phpstan.neon | 2 +- src/Nette/WebLoader.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/phpstan/phpstan.neon b/phpstan/phpstan.neon index 4f7061c..8491a0a 100755 --- a/phpstan/phpstan.neon +++ b/phpstan/phpstan.neon @@ -5,7 +5,7 @@ parameters: - %currentWorkingDirectory%/src/ - %currentWorkingDirectory%/tests/ - excludes_analyse: + excludePaths: - %currentWorkingDirectory%/tests/temp/* level: 7 diff --git a/src/Nette/WebLoader.php b/src/Nette/WebLoader.php index b996021..7fbe903 100755 --- a/src/Nette/WebLoader.php +++ b/src/Nette/WebLoader.php @@ -95,7 +95,7 @@ public function render(): void echo $this->getElement($file), PHP_EOL; } - if ($hasArgs && !empty($backup)) { + if ($hasArgs) { $this->compiler->setFileCollection($backup); } } From 9eac7fdc5d05ad5667dd26bdab20ffc944b1037a Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Wed, 24 Nov 2021 10:54:34 +0100 Subject: [PATCH 42/66] refactoring --- src/Compiler.php | 2 ++ src/{ => Contract}/IFileCollection.php | 2 +- src/{ => Contract}/IOutputNamingConvention.php | 4 +++- src/DefaultOutputNamingConvention.php | 2 ++ src/FileCollection.php | 1 + src/Nette/Extension.php | 2 ++ src/Nette/LoaderFactory.php | 2 +- tests/CompilerTest.php | 6 ++++-- 8 files changed, 16 insertions(+), 5 deletions(-) rename src/{ => Contract}/IFileCollection.php (89%) rename src/{ => Contract}/IOutputNamingConvention.php (78%) diff --git a/src/Compiler.php b/src/Compiler.php index 40fa300..9db06d6 100755 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -5,6 +5,8 @@ namespace WebLoader; use Nette\Utils\FileSystem; +use WebLoader\Contract\IFileCollection; +use WebLoader\Contract\IOutputNamingConvention; /** * Compiler diff --git a/src/IFileCollection.php b/src/Contract/IFileCollection.php similarity index 89% rename from src/IFileCollection.php rename to src/Contract/IFileCollection.php index 1e49601..7aaa3e8 100755 --- a/src/IFileCollection.php +++ b/src/Contract/IFileCollection.php @@ -2,7 +2,7 @@ declare(strict_types = 1); -namespace WebLoader; +namespace WebLoader\Contract; /** * @author Jan Marek diff --git a/src/IOutputNamingConvention.php b/src/Contract/IOutputNamingConvention.php similarity index 78% rename from src/IOutputNamingConvention.php rename to src/Contract/IOutputNamingConvention.php index 866ca08..e838f00 100755 --- a/src/IOutputNamingConvention.php +++ b/src/Contract/IOutputNamingConvention.php @@ -2,7 +2,9 @@ declare(strict_types = 1); -namespace WebLoader; +namespace WebLoader\Contract; + +use WebLoader\Compiler; /** * IOutputNamingConvention diff --git a/src/DefaultOutputNamingConvention.php b/src/DefaultOutputNamingConvention.php index 48da64d..97b6674 100644 --- a/src/DefaultOutputNamingConvention.php +++ b/src/DefaultOutputNamingConvention.php @@ -4,6 +4,8 @@ namespace WebLoader; +use WebLoader\Contract\IOutputNamingConvention; + /** * DefaultNamingConvention * diff --git a/src/FileCollection.php b/src/FileCollection.php index 46bd4dc..47bbd59 100755 --- a/src/FileCollection.php +++ b/src/FileCollection.php @@ -6,6 +6,7 @@ use SplFileInfo; use Traversable; +use WebLoader\Contract\IFileCollection; /** * FileCollection diff --git a/src/Nette/Extension.php b/src/Nette/Extension.php index 5111e19..2e33385 100644 --- a/src/Nette/Extension.php +++ b/src/Nette/Extension.php @@ -106,6 +106,8 @@ public function loadConfiguration(): void $loaderFactoryTempPaths = []; + bdump($config); + foreach (['css', 'js'] as $type) { foreach ($config[$type] as $name => $wlConfig) { /** @var array $wlConfig */ diff --git a/src/Nette/LoaderFactory.php b/src/Nette/LoaderFactory.php index f814878..bc9ca46 100644 --- a/src/Nette/LoaderFactory.php +++ b/src/Nette/LoaderFactory.php @@ -8,7 +8,7 @@ use Nette\Http\IRequest; use WebLoader\Compiler; use WebLoader\DefaultOutputNamingConvention; -use WebLoader\IOutputNamingConvention; +use WebLoader\Contract\IOutputNamingConvention; class LoaderFactory { diff --git a/tests/CompilerTest.php b/tests/CompilerTest.php index 8d406bf..de636ba 100644 --- a/tests/CompilerTest.php +++ b/tests/CompilerTest.php @@ -7,6 +7,8 @@ use PHPUnit\Framework\TestCase; use TypeError; use WebLoader\Compiler; +use WebLoader\Contract\IFileCollection; +use WebLoader\Contract\IOutputNamingConvention; use WebLoader\FileCollection; use WebLoader\FileNotFoundException; @@ -23,7 +25,7 @@ class CompilerTest extends TestCase protected function setUp(): void { - $fileCollection = Mockery::mock('WebLoader\IFileCollection'); + $fileCollection = Mockery::mock(IFileCollection::class); $fileCollection->shouldReceive('getFiles')->andReturn([ __DIR__ . '/fixtures/a.txt', __DIR__ . '/fixtures/b.txt', @@ -35,7 +37,7 @@ protected function setUp(): void __DIR__ . '/fixtures/c.txt', ]); - $convention = Mockery::mock('WebLoader\IOutputNamingConvention'); + $convention = Mockery::mock(IOutputNamingConvention::class); $convention->shouldReceive('getFilename')->andReturnUsing(function ($files, $compiler) { return 'webloader-' . md5(join(',', $files)); }); From 9939fc45013530117a8e1fe464f77ac6c0df8c13 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Wed, 24 Nov 2021 13:28:24 +0100 Subject: [PATCH 43/66] Extension refactoring --- src/Nette/Extension.php | 112 ++++++++++++++++++++++++++-------------- 1 file changed, 74 insertions(+), 38 deletions(-) diff --git a/src/Nette/Extension.php b/src/Nette/Extension.php index 2e33385..62bb968 100644 --- a/src/Nette/Extension.php +++ b/src/Nette/Extension.php @@ -41,45 +41,77 @@ public function __construct(string $appDir, string $wwwDir, bool $debugMode) } + private function getJsConfigSchema(bool $useDefaults = false): Schema + { + $checkLastModified = $useDefaults ? true : null; + $debug = $useDefaults ? false : null; + $sourceDir = $useDefaults ? ($this->wwwDir . '/js') : null; + $tempDir = $useDefaults ? ($this->wwwDir . '/' . self::DEFAULT_TEMP_PATH) : null; + $tempPath = $useDefaults ? self::DEFAULT_TEMP_PATH : null; + $async = $useDefaults ? false : null; + $defer = $useDefaults ? false : null; + $absoluteUrl = $useDefaults ? false : null; + $namingConvention = $useDefaults ? ('@' . $this->prefix('jsNamingConvention')) : null; + + return Expect::structure([ + 'checkLastModified' => Expect::bool($checkLastModified), + 'debug' => Expect::bool($debug), + 'sourceDir' => Expect::string($sourceDir), + 'tempDir' => Expect::string($tempDir), + 'tempPath' => Expect::string($tempPath), + 'files' => Expect::array(), + 'watchFiles' => Expect::array(), + 'remoteFiles' => Expect::array(), + 'filters' => Expect::array(), + 'fileFilters' => Expect::array(), + 'async' => Expect::bool($async), + 'defer' => Expect::bool($defer), + 'nonce' => Expect::string()->nullable(), + 'absoluteUrl' => Expect::bool($absoluteUrl), + 'namingConvention' => Expect::string($namingConvention), + ]); + } + + + private function getCssConfigSchema(bool $useDefaults = false): Schema + { + $checkLastModified = $useDefaults ? true : null; + $debug = $useDefaults ? false : null; + $sourceDir = $useDefaults ? ($this->wwwDir . '/css') : null; + $tempDir = $useDefaults ? ($this->wwwDir . '/' . self::DEFAULT_TEMP_PATH) : null; + $tempPath = $useDefaults ? self::DEFAULT_TEMP_PATH : null; + $async = $useDefaults ? false : null; + $defer = $useDefaults ? false : null; + $absoluteUrl = $useDefaults ? false : null; + $namingConvention = $useDefaults ? ('@' . $this->prefix('cssNamingConvention')) : null; + + return Expect::structure([ + 'checkLastModified' => Expect::bool($checkLastModified), + 'debug' => Expect::bool($debug), + 'sourceDir' => Expect::string($sourceDir), + 'tempDir' => Expect::string($tempDir), + 'tempPath' => Expect::string($tempPath), + 'files' => Expect::array(), + 'watchFiles' => Expect::array(), + 'remoteFiles' => Expect::array(), + 'filters' => Expect::array(), + 'fileFilters' => Expect::array(), + 'async' => Expect::bool($async), + 'defer' => Expect::bool($defer), + 'nonce' => Expect::string()->nullable(), + 'absoluteUrl' => Expect::bool($absoluteUrl), + 'namingConvention' => Expect::string($namingConvention), + ]); + } + + public function getConfigSchema(): Schema { return Expect::structure([ - 'jsDefaults' => Expect::structure([ - 'checkLastModified' => Expect::bool(true), - 'debug' => Expect::bool(false), - 'sourceDir' => Expect::string($this->wwwDir . '/js'), - 'tempDir' => Expect::string($this->wwwDir . '/' . self::DEFAULT_TEMP_PATH), - 'tempPath' => Expect::string(self::DEFAULT_TEMP_PATH), - 'files' => Expect::array(), - 'watchFiles' => Expect::array(), - 'remoteFiles' => Expect::array(), - 'filters' => Expect::array(), - 'fileFilters' => Expect::array(), - 'async' => Expect::bool(false), - 'defer' => Expect::bool(false), - 'nonce' => Expect::string()->nullable(), - 'absoluteUrl' => Expect::bool(false), - 'namingConvention' => Expect::string('@' . $this->prefix('jsNamingConvention')), - ]), - 'cssDefaults' => Expect::structure([ - 'checkLastModified' => Expect::bool(true), - 'debug' => Expect::bool(false), - 'sourceDir' => Expect::string($this->wwwDir . '/css')->dynamic(), - 'tempDir' => Expect::string($this->wwwDir . '/' . self::DEFAULT_TEMP_PATH), - 'tempPath' => Expect::string(self::DEFAULT_TEMP_PATH), - 'files' => Expect::array(), - 'watchFiles' => Expect::array(), - 'remoteFiles' => Expect::array(), - 'filters' => Expect::array(), - 'fileFilters' => Expect::array(), - 'async' => Expect::bool(false), - 'defer' => Expect::bool(false), - 'nonce' => Expect::string()->nullable(), - 'absoluteUrl' => Expect::bool(false), - 'namingConvention' => Expect::string('@' . $this->prefix('cssNamingConvention')), - ]), - 'js' => Expect::array(), - 'css' => Expect::array(), + 'jsDefaults' => $this->getJsConfigSchema(true), + 'cssDefaults' => $this->getCssConfigSchema(true), + 'js' => Expect::arrayOf($this->getJsConfigSchema()), + 'css' => Expect::arrayOf($this->getCssConfigSchema()), 'debugger' => Expect::bool($this->debugMode), ]); } @@ -106,12 +138,16 @@ public function loadConfiguration(): void $loaderFactoryTempPaths = []; - bdump($config); - foreach (['css', 'js'] as $type) { foreach ($config[$type] as $name => $wlConfig) { /** @var array $wlConfig */ + $wlConfig = array_filter($wlConfig); $wlConfig = SchemaHelpers::merge($wlConfig, $config[$type . 'Defaults']); + + if (!is_array($wlConfig)) { + throw new CompilationException('Batch config not valid.'); + } + $this->addWebLoader($builder, $type . ucfirst($name), $wlConfig); $loaderFactoryTempPaths[strtolower($name)] = $wlConfig['tempPath']; From d18d66a35929fd8b6771198d732d1306cee98899 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Wed, 24 Nov 2021 15:12:26 +0100 Subject: [PATCH 44/66] Experimental feature to enable adding batches from other extensions --- src/BatchCollection.php | 35 ++++++++++ src/Contract/IBatchCollection.php | 10 +++ src/Contract/IBatchProvider.php | 9 +++ src/Exception/BatchAlreadyExistsException.php | 10 +++ src/Nette/Extension.php | 70 +++++++++++++++---- 5 files changed, 121 insertions(+), 13 deletions(-) create mode 100755 src/BatchCollection.php create mode 100755 src/Contract/IBatchCollection.php create mode 100755 src/Contract/IBatchProvider.php create mode 100755 src/Exception/BatchAlreadyExistsException.php diff --git a/src/BatchCollection.php b/src/BatchCollection.php new file mode 100755 index 0000000..fda11f5 --- /dev/null +++ b/src/BatchCollection.php @@ -0,0 +1,35 @@ +batches; + } + + + public function addBatch(string $type, string $name, array $batch): void + { + if (isset($this->batches[$type][$name])) { + throw new BatchAlreadyExistsException( + sprintf("Batch '%s' of type '%s', already exists.", $name, $type) + ); + } + + $this->batches[$type][$name] = $batch; + } +} diff --git a/src/Contract/IBatchCollection.php b/src/Contract/IBatchCollection.php new file mode 100755 index 0000000..e986330 --- /dev/null +++ b/src/Contract/IBatchCollection.php @@ -0,0 +1,10 @@ +appDir = $appDir; $this->wwwDir = $wwwDir; $this->debugMode = $debugMode; + $this->batchCollection = new BatchCollection; } @@ -110,8 +115,8 @@ public function getConfigSchema(): Schema return Expect::structure([ 'jsDefaults' => $this->getJsConfigSchema(true), 'cssDefaults' => $this->getCssConfigSchema(true), - 'js' => Expect::arrayOf($this->getJsConfigSchema()), - 'css' => Expect::arrayOf($this->getCssConfigSchema()), + 'js' => Expect::arrayOf($this->getJsConfigSchema())->nullable(), + 'css' => Expect::arrayOf($this->getCssConfigSchema())->nullable(), 'debugger' => Expect::bool($this->debugMode), ]); } @@ -138,21 +143,25 @@ public function loadConfiguration(): void $loaderFactoryTempPaths = []; - foreach (['css', 'js'] as $type) { - foreach ($config[$type] as $name => $wlConfig) { - /** @var array $wlConfig */ - $wlConfig = array_filter($wlConfig); - $wlConfig = SchemaHelpers::merge($wlConfig, $config[$type . 'Defaults']); - if (!is_array($wlConfig)) { + $this->extractBatchesFromExtensions(); + $this->extractNormalBatches($config); + + $batchTypes = $this->batchCollection->getBatches(); + foreach ($batchTypes as $type => $batches) { + foreach ($batches as $name => $batch) { + $batch = array_filter($batch); + $batch = SchemaHelpers::merge($batch, $config[$type . 'Defaults']); + + if (!is_array($batch)) { throw new CompilationException('Batch config not valid.'); } - $this->addWebLoader($builder, $type . ucfirst($name), $wlConfig); - $loaderFactoryTempPaths[strtolower($name)] = $wlConfig['tempPath']; + $this->addWebLoader($builder, $type . ucfirst($name), $batch); + $loaderFactoryTempPaths[strtolower($name)] = $batch['tempPath']; - if (!is_dir($wlConfig['tempDir']) || !is_writable($wlConfig['tempDir'])) { - throw new CompilationException(sprintf("You must create a writable directory '%s'", $wlConfig['tempDir'])); + if (!is_dir($batch['tempDir']) || !is_writable($batch['tempDir'])) { + throw new CompilationException(sprintf("You must create a writable directory '%s'", $batch['tempDir'])); } } } @@ -320,4 +329,39 @@ protected function fileExists(string $file): bool return file_exists($file); } + + + private function extractBatchesFromExtensions(): void + { + // Extension batches + /** @var array $batchProviders */ + $batchProviders = $this->compiler->getExtensions(IBatchProvider::class); + + if (empty($batchProviders)) { + return; + } + + $schemaProcessor = new Processor; + + foreach($batchProviders as $batchProvider) { + $batchCollections = $batchProvider->getBatches(); + $schemaProcessor->process($this->getConfigSchema(), $batchCollections); + + foreach ($batchCollections as $type => $batches) { + foreach ($batches as $name => $batch) { + $this->batchCollection->addBatch($type, $name, $batch); + } + } + } + } + + + private function extractNormalBatches(array $config): void + { + foreach (['css', 'js'] as $type) { + foreach ($config[$type] as $name => $batch) { + $this->batchCollection->addBatch($type, $name, $batch); + } + } + } } From 5f34bd9c55e3520a215f9feba7d4bc3649a9d73c Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Wed, 24 Nov 2021 15:16:25 +0100 Subject: [PATCH 45/66] refactoring --- src/Compiler.php | 2 ++ src/Exception/BatchAlreadyExistsException.php | 2 +- src/Exception/CompilationException.php | 12 ++++++++++++ src/{ => Exception}/FileNotFoundException.php | 4 +++- src/Exception/InvalidArgumentException.php | 14 ++++++++++++++ src/{ => Exception}/WebLoaderException.php | 2 +- src/FileCollection.php | 1 + src/Filter/CssUrlsFilter.php | 2 +- src/Filter/StylusFilter.php | 2 +- src/Filter/VariablesFilter.php | 4 ++-- src/InvalidArgumentException.php | 14 -------------- src/Nette/CompilationException.php | 12 ------------ src/Nette/Extension.php | 3 ++- tests/CompilerTest.php | 2 +- tests/FileCollectionTest.php | 2 +- 15 files changed, 42 insertions(+), 36 deletions(-) create mode 100755 src/Exception/CompilationException.php rename src/{ => Exception}/FileNotFoundException.php (66%) create mode 100644 src/Exception/InvalidArgumentException.php rename src/{ => Exception}/WebLoaderException.php (81%) delete mode 100644 src/InvalidArgumentException.php delete mode 100755 src/Nette/CompilationException.php diff --git a/src/Compiler.php b/src/Compiler.php index 9db06d6..78a89c3 100755 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -7,6 +7,8 @@ use Nette\Utils\FileSystem; use WebLoader\Contract\IFileCollection; use WebLoader\Contract\IOutputNamingConvention; +use WebLoader\Exception\FileNotFoundException; +use WebLoader\Exception\InvalidArgumentException; /** * Compiler diff --git a/src/Exception/BatchAlreadyExistsException.php b/src/Exception/BatchAlreadyExistsException.php index 29e51c7..7117731 100755 --- a/src/Exception/BatchAlreadyExistsException.php +++ b/src/Exception/BatchAlreadyExistsException.php @@ -3,7 +3,7 @@ namespace WebLoader\Exception; -use WebLoader\WebLoaderException; +use WebLoader\Exception\WebLoaderException; class BatchAlreadyExistsException extends WebLoaderException { diff --git a/src/Exception/CompilationException.php b/src/Exception/CompilationException.php new file mode 100755 index 0000000..9a128fd --- /dev/null +++ b/src/Exception/CompilationException.php @@ -0,0 +1,12 @@ + + */ +class CompilationException extends \WebLoader\Exception\WebLoaderException +{ +} diff --git a/src/FileNotFoundException.php b/src/Exception/FileNotFoundException.php similarity index 66% rename from src/FileNotFoundException.php rename to src/Exception/FileNotFoundException.php index 42544ed..9be7131 100644 --- a/src/FileNotFoundException.php +++ b/src/Exception/FileNotFoundException.php @@ -2,7 +2,9 @@ declare(strict_types = 1); -namespace WebLoader; +namespace WebLoader\Exception; + +use WebLoader\Exception\WebLoaderException; /** * FileNotFoundException diff --git a/src/Exception/InvalidArgumentException.php b/src/Exception/InvalidArgumentException.php new file mode 100644 index 0000000..3c924ed --- /dev/null +++ b/src/Exception/InvalidArgumentException.php @@ -0,0 +1,14 @@ + - */ -class CompilationException extends \WebLoader\WebLoaderException -{ -} diff --git a/src/Nette/Extension.php b/src/Nette/Extension.php index 36613ac..7cfe316 100644 --- a/src/Nette/Extension.php +++ b/src/Nette/Extension.php @@ -18,8 +18,9 @@ use WebLoader\BatchCollection; use WebLoader\Compiler as WebloaderCompiler; use WebLoader\Contract\IBatchProvider; +use WebLoader\Exception\CompilationException; use WebLoader\FileCollection; -use WebLoader\FileNotFoundException; +use WebLoader\Exception\FileNotFoundException; use WebLoader\Nette\Diagnostics\Panel; use WebLoader\Nette\SymfonyConsole\GenerateCommand; diff --git a/tests/CompilerTest.php b/tests/CompilerTest.php index de636ba..65a3412 100644 --- a/tests/CompilerTest.php +++ b/tests/CompilerTest.php @@ -10,7 +10,7 @@ use WebLoader\Contract\IFileCollection; use WebLoader\Contract\IOutputNamingConvention; use WebLoader\FileCollection; -use WebLoader\FileNotFoundException; +use WebLoader\Exception\FileNotFoundException; /** * CompilerTest diff --git a/tests/FileCollectionTest.php b/tests/FileCollectionTest.php index 436bbd9..49dda53 100755 --- a/tests/FileCollectionTest.php +++ b/tests/FileCollectionTest.php @@ -7,7 +7,7 @@ use PHPUnit\Framework\TestCase; use SplFileInfo; use WebLoader\FileCollection; -use WebLoader\FileNotFoundException; +use WebLoader\Exception\FileNotFoundException; /** * FileCollection test From b9f756473abc57521d0a230fe09c52365934a000 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Wed, 24 Nov 2021 15:24:39 +0100 Subject: [PATCH 46/66] update phpunit xsd --- tests/phpunit.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit.xml b/tests/phpunit.xml index 9e655c9..65e3f36 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -1,6 +1,6 @@ Date: Thu, 25 Nov 2021 10:05:44 +0100 Subject: [PATCH 47/66] refactoring, add test --- src/Nette/Extension.php | 1 - tests/BatchCollectionTest.php | 44 +++++++++++++++++++++++++++++++++++ tests/Nette/ExtensionTest.php | 18 ++++++++++---- 3 files changed, 58 insertions(+), 5 deletions(-) create mode 100755 tests/BatchCollectionTest.php diff --git a/src/Nette/Extension.php b/src/Nette/Extension.php index 7cfe316..c4ce41f 100644 --- a/src/Nette/Extension.php +++ b/src/Nette/Extension.php @@ -144,7 +144,6 @@ public function loadConfiguration(): void $loaderFactoryTempPaths = []; - $this->extractBatchesFromExtensions(); $this->extractNormalBatches($config); diff --git a/tests/BatchCollectionTest.php b/tests/BatchCollectionTest.php new file mode 100755 index 0000000..99090da --- /dev/null +++ b/tests/BatchCollectionTest.php @@ -0,0 +1,44 @@ +batchCollection = new BatchCollection(); + } + + + public function testAddGetBatches(): void + { + $this->batchCollection->addBatch('css', 'front.screen', []); + $this->batchCollection->addBatch('js', 'front.head', []); + + $expected = [ + 'css' => [ + 'front.screen' => [], + ], + 'js' => [ + 'front.head' => [], + ], + ]; + + $this->assertSame($expected, $this->batchCollection->getBatches()); + } + + + public function testAddBatchException(): void + { + $this->expectException(BatchAlreadyExistsException::class); + $this->batchCollection->addBatch('css', 'front.screen', []); + $this->batchCollection->addBatch('css', 'front.screen', []); + } +} diff --git a/tests/Nette/ExtensionTest.php b/tests/Nette/ExtensionTest.php index bad0ad8..ad1dfc2 100755 --- a/tests/Nette/ExtensionTest.php +++ b/tests/Nette/ExtensionTest.php @@ -8,12 +8,12 @@ use Nette\DI\Container; use Nette\Utils\Finder; use PHPUnit\Framework\TestCase; +use WebLoader\Compiler as WebloaderCompiler; use WebLoader\Nette\Extension; use WebLoader\Path; class ExtensionTest extends TestCase { - private Container $container; private string $appDir; private string $wwwDir; @@ -34,7 +34,11 @@ protected function setUp(): void private function prepareContainer(array $configFiles): void { - foreach (Finder::findFiles('*')->exclude('.gitignore')->from($this->tempDir . '/cache') as $file) { + $finder = Finder::findFiles('*') + ->exclude('.gitignore') + ->from($this->tempDir . '/cache'); + + foreach ($finder as $file) { unlink((string) $file); } @@ -169,7 +173,13 @@ public function testExtensionName(): void $configurator->addConfig($this->fixturesDir . '/extensionName.neon'); $container = $configurator->createContainer(); - $this->assertInstanceOf('WebLoader\Compiler', $container->getService('Foo.cssDefaultCompiler')); - $this->assertInstanceOf('WebLoader\Compiler', $container->getService('Foo.jsDefaultCompiler')); + $this->assertInstanceOf( + WebloaderCompiler::class, + $container->getService('Foo.cssDefaultCompiler') + ); + $this->assertInstanceOf( + WebloaderCompiler::class, + $container->getService('Foo.jsDefaultCompiler') + ); } } From d851cd306df1cc0be38fa5445601416c9fad8b95 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Thu, 25 Nov 2021 10:47:14 +0100 Subject: [PATCH 48/66] refactoring --- src/Contract/IBatchProvider.php | 9 --------- src/Contract/IWebloaderAssetProvider.php | 9 +++++++++ src/Nette/Extension.php | 12 ++++++------ 3 files changed, 15 insertions(+), 15 deletions(-) delete mode 100755 src/Contract/IBatchProvider.php create mode 100755 src/Contract/IWebloaderAssetProvider.php diff --git a/src/Contract/IBatchProvider.php b/src/Contract/IBatchProvider.php deleted file mode 100755 index f7795cb..0000000 --- a/src/Contract/IBatchProvider.php +++ /dev/null @@ -1,9 +0,0 @@ - $batchProviders */ - $batchProviders = $this->compiler->getExtensions(IBatchProvider::class); + /** @var array $batchProviders */ + $batchProviders = $this->compiler->getExtensions(IWebloaderAssetProvider::class); if (empty($batchProviders)) { return; @@ -344,10 +344,10 @@ private function extractBatchesFromExtensions(): void $schemaProcessor = new Processor; foreach($batchProviders as $batchProvider) { - $batchCollections = $batchProvider->getBatches(); - $schemaProcessor->process($this->getConfigSchema(), $batchCollections); + $assets = $batchProvider->getWebloaderAssets(); + $schemaProcessor->process($this->getConfigSchema(), $assets); - foreach ($batchCollections as $type => $batches) { + foreach ($assets as $type => $batches) { foreach ($batches as $name => $batch) { $this->batchCollection->addBatch($type, $name, $batch); } From b73fda4ffe72a268e5886558e71f2e59af94b678 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Thu, 25 Nov 2021 14:10:24 +0100 Subject: [PATCH 49/66] phpstan ignore error --- phpstan/phpstan.neon | 3 +++ 1 file changed, 3 insertions(+) diff --git a/phpstan/phpstan.neon b/phpstan/phpstan.neon index 8491a0a..3299687 100755 --- a/phpstan/phpstan.neon +++ b/phpstan/phpstan.neon @@ -22,3 +22,6 @@ parameters: - message: '#Access to an undefined property WebLoader\\Filter\\VariablesFilter::\$bar\.#' path: %currentWorkingDirectory%/tests/Filter/VariablesFilterTest.php + - + message: '#Property WebLoader\\Nette\\Diagnostics\\Panel::\$root is never read, only written.#' + path: %currentWorkingDirectory%/src/Nette/Diagnostics/Panel.php From 387a3d270ca8b261e202afc85b9bef74527f5773 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Mon, 6 Jun 2022 14:43:19 +0200 Subject: [PATCH 50/66] composer updates --- composer.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/composer.json b/composer.json index 67f2a82..489edc3 100755 --- a/composer.json +++ b/composer.json @@ -60,5 +60,10 @@ "scripts": { "phpstan": "./phpstan/phpstan", "tests": "./vendor/bin/phpunit --configuration tests/phpunit.xml tests" + }, + "config": { + "allow-plugins": { + "phpstan/extension-installer": true + } } } From 1eb74550a949dd08ec720a71f8f2d89feefc5b19 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Mon, 6 Jun 2022 15:24:03 +0200 Subject: [PATCH 51/66] bump deps, fixes --- composer.json | 6 +++--- src/Nette/Diagnostics/Panel.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 489edc3..c0acd36 100755 --- a/composer.json +++ b/composer.json @@ -19,14 +19,14 @@ "psr-4": { "WebLoader\\Test\\": "tests/" } }, "require": { - "php": ">= 7.4", + "php": "^8.0", "nette/application": "^3.1", "nette/di": "^3.0", "nette/utils": "^3.2", "ext-json": "*", "nette/schema": "^1.2", "nette/finder": "^2.5", - "latte/latte": "^2.10", + "latte/latte": "^3.0", "tracy/tracy": "^2.8" }, "suggest": { @@ -52,7 +52,7 @@ "phpstan/phpstan": "^1.0.0", "phpstan/phpstan-nette": "^1.0.0", "roave/security-advisories": "dev-master", - "symfony/console": "^4.2.9|^5.0.0", + "symfony/console": "^4.2.9|^5.0.0|^6.0.0", "phpstan/phpstan-mockery": "^1.0.0", "tubalmartin/cssmin": "^4.1", "tedivm/jshrink": "^1.3" diff --git a/src/Nette/Diagnostics/Panel.php b/src/Nette/Diagnostics/Panel.php index fe7093b..e49b614 100644 --- a/src/Nette/Diagnostics/Panel.php +++ b/src/Nette/Diagnostics/Panel.php @@ -5,7 +5,7 @@ namespace WebLoader\Nette\Diagnostics; use Latte; -use Latte\Runtime\Filters; +use Latte\Essential\Filters; use Tracy\Debugger; use Tracy\IBarPanel; use WebLoader\Compiler; From 20c8b1c433206aeb7d72690dcf67c888a364713a Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Tue, 7 Jun 2022 07:52:57 +0200 Subject: [PATCH 52/66] syntax update, phpstan, code style --- phpstan/phpstan.neon | 2 +- src/Compiler.php | 7 ++-- src/Contract/IBatchCollection.php | 3 +- src/Contract/IFileCollection.php | 2 +- src/Contract/IOutputNamingConvention.php | 2 +- src/Contract/IWebloaderAssetProvider.php | 2 +- src/DefaultOutputNamingConvention.php | 7 ++-- src/Exception/BatchAlreadyExistsException.php | 3 +- src/Exception/CompilationException.php | 2 +- src/Exception/FileNotFoundException.php | 3 +- src/Exception/InvalidArgumentException.php | 2 +- src/Exception/WebLoaderException.php | 2 +- src/File.php | 2 +- src/FileCollection.php | 7 ++-- src/Filter/CssMinFilter.php | 1 - src/Filter/CssUrlsFilter.php | 16 ++++------ src/Filter/LessBinFilter.php | 7 ++-- src/Filter/Process.php | 5 ++- src/Filter/ScssFilter.php | 11 ++----- src/Filter/StylusFilter.php | 7 ++-- src/Filter/TypeScriptFilter.php | 7 ++-- src/Filter/VariablesFilter.php | 14 +++----- src/Nette/CssLoader.php | 7 ++-- src/Nette/CssUrlFilter.php | 2 +- src/Nette/Diagnostics/Panel.php | 18 ++++++----- src/Nette/Extension.php | 15 +++++---- src/Nette/JavaScriptLoader.php | 3 +- src/Nette/LoaderFactory.php | 32 ++++--------------- src/Nette/SymfonyConsole/GenerateCommand.php | 4 +-- src/Nette/WebLoader.php | 18 ++++------- src/Path.php | 2 +- tests/CompilerTest.php | 23 ++++++++++--- 32 files changed, 102 insertions(+), 136 deletions(-) diff --git a/phpstan/phpstan.neon b/phpstan/phpstan.neon index 3299687..c3eacd4 100755 --- a/phpstan/phpstan.neon +++ b/phpstan/phpstan.neon @@ -8,7 +8,7 @@ parameters: excludePaths: - %currentWorkingDirectory%/tests/temp/* - level: 7 + level: 8 checkMissingIterableValueType: false diff --git a/src/Compiler.php b/src/Compiler.php index 78a89c3..717be7c 100755 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -1,6 +1,6 @@ namingConvention->getFilename($files, $this); $path = $this->outputDir . '/' . $name; - $lastModified = $this->checkLastModified ? $this->getLastModified($watchFiles) : 0; + $lastModified = $this->checkLastModified + ? $this->getLastModified($watchFiles) + : 0; if (!file_exists($path) || $lastModified > filemtime($path) || $this->debugging === true) { // disabled: https://github.com/nette/safe-stream/pull/5 diff --git a/src/Contract/IBatchCollection.php b/src/Contract/IBatchCollection.php index e986330..a8a40a4 100755 --- a/src/Contract/IBatchCollection.php +++ b/src/Contract/IBatchCollection.php @@ -1,10 +1,11 @@ setSuffix('.css'); return $convention; @@ -29,7 +28,7 @@ public static function createCssConvention(): self public static function createJsConvention(): self { - $convention = new self(); + $convention = new self; $convention->setSuffix('.js'); return $convention; diff --git a/src/Exception/BatchAlreadyExistsException.php b/src/Exception/BatchAlreadyExistsException.php index 7117731..718be6f 100755 --- a/src/Exception/BatchAlreadyExistsException.php +++ b/src/Exception/BatchAlreadyExistsException.php @@ -1,9 +1,8 @@ root = (string) $root; } diff --git a/src/Filter/CssMinFilter.php b/src/Filter/CssMinFilter.php index fd0dcd0..0b72468 100755 --- a/src/Filter/CssMinFilter.php +++ b/src/Filter/CssMinFilter.php @@ -8,7 +8,6 @@ class CssMinFilter { - public function __invoke(string $code, Compiler $compiler, string $file = ''): string { $minifier = new Minifier; diff --git a/src/Filter/CssUrlsFilter.php b/src/Filter/CssUrlsFilter.php index 1aad49f..02beef0 100755 --- a/src/Filter/CssUrlsFilter.php +++ b/src/Filter/CssUrlsFilter.php @@ -1,6 +1,6 @@ docRoot = Path::normalize($docRoot); if (!is_dir($this->docRoot)) { throw new InvalidArgumentException('Given document root is not directory.'); } - - $this->basePath = $basePath; } @@ -109,9 +107,7 @@ public function __invoke(string $code, Compiler $loader, ?string $file = null): $self = $this; - $return = preg_replace_callback($regexp, function ($matches) use ($self, $file) { - return "url('" . $self->absolutizeUrl($matches[2], $matches[1], $file) . "')"; - }, $code); + $return = preg_replace_callback($regexp, fn($matches) => "url('" . $self->absolutizeUrl($matches[2], $matches[1], $file) . "')", $code); return (string) $return; } diff --git a/src/Filter/LessBinFilter.php b/src/Filter/LessBinFilter.php index b56a873..a09ee1f 100644 --- a/src/Filter/LessBinFilter.php +++ b/src/Filter/LessBinFilter.php @@ -1,6 +1,6 @@ bin = $bin; $this->env = $env + $_ENV; unset($this->env['argv'], $this->env['argc']); } diff --git a/src/Filter/Process.php b/src/Filter/Process.php index 9eadfc4..f504558 100644 --- a/src/Filter/Process.php +++ b/src/Filter/Process.php @@ -1,6 +1,6 @@ ['pipe', 'r'], // stdin 1 => ['pipe', 'w'], // stdout diff --git a/src/Filter/ScssFilter.php b/src/Filter/ScssFilter.php index b73eaae..43fb255 100644 --- a/src/Filter/ScssFilter.php +++ b/src/Filter/ScssFilter.php @@ -1,6 +1,6 @@ sc = $sc; } @@ -29,7 +24,7 @@ private function getScssC(): ScssCompiler { // lazy loading if (empty($this->sc)) { - $this->sc = new ScssCompiler(); + $this->sc = new ScssCompiler; } return $this->sc; diff --git a/src/Filter/StylusFilter.php b/src/Filter/StylusFilter.php index ccf6f47..7ac4b30 100755 --- a/src/Filter/StylusFilter.php +++ b/src/Filter/StylusFilter.php @@ -1,6 +1,6 @@ bin = $bin; } diff --git a/src/Filter/TypeScriptFilter.php b/src/Filter/TypeScriptFilter.php index 833106a..b11f174 100755 --- a/src/Filter/TypeScriptFilter.php +++ b/src/Filter/TypeScriptFilter.php @@ -1,6 +1,6 @@ bin = $bin; $this->env = $env + $_ENV; unset($this->env['argv'], $this->env['argc']); } diff --git a/src/Filter/VariablesFilter.php b/src/Filter/VariablesFilter.php index be326d4..3ec1203 100644 --- a/src/Filter/VariablesFilter.php +++ b/src/Filter/VariablesFilter.php @@ -1,6 +1,6 @@ $value) { $this->$key = $value; @@ -30,8 +28,8 @@ public function __construct(array $variables = []) public function setDelimiter(string $start, string $end): self { - $this->startVariable = (string) $start; - $this->endVariable = (string) $end; + $this->startVariable = $start; + $this->endVariable = $end; return $this; } @@ -41,9 +39,7 @@ public function __invoke(string $code): string $start = $this->startVariable; $end = $this->endVariable; - $variables = array_map(function ($key) use ($start, $end) { - return $start . $key . $end; - }, array_keys($this->variables)); + $variables = array_map(fn($key) => $start . $key . $end, array_keys($this->variables)); $values = array_values($this->variables); diff --git a/src/Nette/CssLoader.php b/src/Nette/CssLoader.php index b8687c6..8b77dab 100755 --- a/src/Nette/CssLoader.php +++ b/src/Nette/CssLoader.php @@ -1,6 +1,6 @@ media; } @@ -34,7 +34,7 @@ public function getType(): string } - public function getTitle(): string + public function getTitle(): ?string { return $this->title; } @@ -105,5 +105,4 @@ public function getInlineElement(File $file): Html return $el; } - } diff --git a/src/Nette/CssUrlFilter.php b/src/Nette/CssUrlFilter.php index 17e5091..2733164 100755 --- a/src/Nette/CssUrlFilter.php +++ b/src/Nette/CssUrlFilter.php @@ -1,6 +1,6 @@ 'CSS files', 'js' => 'JavaScript files', @@ -36,7 +34,9 @@ class Panel implements IBarPanel public function __construct(?string $appDir = null) { - $this->root = $appDir ? str_replace('\\', DIRECTORY_SEPARATOR, (string) realpath(dirname($appDir))) : ''; + $this->root = $appDir + ? str_replace('\\', DIRECTORY_SEPARATOR, (string) realpath(dirname($appDir))) + : ''; Debugger::getBar()->addPanel($this); } @@ -88,7 +88,7 @@ private function compute(): array $generated = $compiler->generate(); - if (is_null($generated)) { + if (null === $generated) { continue; } @@ -133,9 +133,7 @@ private function getTable(): string { $latte = new Latte\Engine; - $latte->addFilter('extension', function ($extension) { - return isset(self::$types[$extension]) ? self::$types[$extension] : $extension; - }); + $latte->addFilter('extension', fn($extension) => self::$types[$extension] ?? $extension); return $latte->renderToString(__DIR__ . '/panel.latte', [ 'files' => $this->files, @@ -161,6 +159,10 @@ public function getTab(): string { $this->compute(); + if (empty($this->size['combined'])) { + return ''; + } + return '' . '' . Filters::bytes($this->size['combined']) diff --git a/src/Nette/Extension.php b/src/Nette/Extension.php index 6f93257..c258f7f 100644 --- a/src/Nette/Extension.php +++ b/src/Nette/Extension.php @@ -1,6 +1,6 @@ prefix('jsNamingConvention')) : null; + $namingConvention = $useDefaults + ? ('@' . $this->prefix('jsNamingConvention')) + : null; return Expect::structure([ 'checkLastModified' => Expect::bool($checkLastModified), @@ -89,7 +90,9 @@ private function getCssConfigSchema(bool $useDefaults = false): Schema $async = $useDefaults ? false : null; $defer = $useDefaults ? false : null; $absoluteUrl = $useDefaults ? false : null; - $namingConvention = $useDefaults ? ('@' . $this->prefix('cssNamingConvention')) : null; + $namingConvention = $useDefaults + ? ('@' . $this->prefix('cssNamingConvention')) + : null; return Expect::structure([ 'checkLastModified' => Expect::bool($checkLastModified), @@ -343,7 +346,7 @@ private function extractBatchesFromExtensions(): void $schemaProcessor = new Processor; - foreach($batchProviders as $batchProvider) { + foreach ($batchProviders as $batchProvider) { $assets = $batchProvider->getWebloaderAssets(); $schemaProcessor->process($this->getConfigSchema(), $assets); diff --git a/src/Nette/JavaScriptLoader.php b/src/Nette/JavaScriptLoader.php index 8833ba4..45ee64e 100644 --- a/src/Nette/JavaScriptLoader.php +++ b/src/Nette/JavaScriptLoader.php @@ -1,6 +1,6 @@ */ - private array $tempPaths; - - - /** - * @param array $tempPaths - * @param string $extensionName - * @param IRequest $httpRequest - * @param Container $diContainer - */ + /** @param array $tempPaths */ public function __construct( - array $tempPaths, - string $extensionName, - IRequest $httpRequest, - Container $diContainer + private array $tempPaths, + private string $extensionName, + private IRequest $httpRequest, + private Container $diContainer ) { - $this->httpRequest = $httpRequest; - $this->diContainer = $diContainer; - $this->tempPaths = $tempPaths; - $this->extensionName = $extensionName; } diff --git a/src/Nette/SymfonyConsole/GenerateCommand.php b/src/Nette/SymfonyConsole/GenerateCommand.php index eaa1a74..2cd5ac4 100644 --- a/src/Nette/SymfonyConsole/GenerateCommand.php +++ b/src/Nette/SymfonyConsole/GenerateCommand.php @@ -1,6 +1,6 @@ compilers as $compiler) { $file = $compiler->generate(); - if (!is_null($file)) { + if (null !== $file) { $output->writeln($file->getFileName()); $noFiles = false; } diff --git a/src/Nette/WebLoader.php b/src/Nette/WebLoader.php index 7fbe903..d0a9b63 100755 --- a/src/Nette/WebLoader.php +++ b/src/Nette/WebLoader.php @@ -1,6 +1,6 @@ compiler = $compiler; - $this->tempPath = $tempPath; - $this->appendLastModified = $appendLastModified; + public function __construct( + private Compiler $compiler, + private string $tempPath, + private bool $appendLastModified + ) { } diff --git a/src/Path.php b/src/Path.php index 5dd5b80..1b9a650 100755 --- a/src/Path.php +++ b/src/Path.php @@ -1,6 +1,6 @@ object->setFileCollection(new FileCollection()); + $this->object->setFileCollection(new FileCollection('')); $ret = $this->object->generate(); $this->assertNull($ret); @@ -91,7 +93,7 @@ public function testGeneratingAndFilters(): void $expectedContent = '-' . PHP_EOL . 'a:cba,' . PHP_EOL . 'b:fed,' . PHP_EOL . 'c:ihg,-' . PHP_EOL . 'a:cba,' . PHP_EOL . 'b:fed,' . PHP_EOL . 'c:ihg,'; - $file = $this->object->generate(); + $file = $this->generateFile(); $this->assertTrue(is_numeric($file->getLastModified()) && $file->getLastModified() > 0, 'Generate does not provide last modified timestamp correctly.'); @@ -103,7 +105,8 @@ public function testGeneratingAndFilters(): void public function testGenerateReturnsSourceFilePaths(): void { - $file = $this->object->generate(); + $file = $this->generateFile(); + $this->assertIsArray($file->getSourceFiles()); $this->assertCount(3, $file->getSourceFiles()); $this->assertFileExists($file->getSourceFiles()[0]); @@ -144,4 +147,16 @@ public function testNonCallableFileFilter(): void $this->expectException(TypeError::class); $this->object->addFileFilter(4); } + + + private function generateFile(): File + { + $file = $this->object->generate(); + + if ($file === null) { + throw new Exception('Should not be empty'); + } + + return $file; + } } From 284e22bab723a7e19b51c7fea93c19357edc9478 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Tue, 6 Sep 2022 13:18:21 +0200 Subject: [PATCH 53/66] update php version --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ec29728..dccdeab 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -WebLoader [![Build Status](https://secure.travis-ci.org/janmarek/WebLoader.png?branch=master)](http://travis-ci.org/janmarek/WebLoader) +WebLoader ======================= Component for CSS and JS files loading. @@ -6,7 +6,7 @@ Component for CSS and JS files loading. Author: [Jan Marek](https://github.com/janmarek) Licence: MIT -Updated for Nette 3/3.1 and PHP 7.4/8.0 by [Gappa](https://github.com/Gappa). +Updated for Nette 3/3.1 and PHP 8.0 by [Gappa](https://github.com/Gappa). Example ------- From ac5fa2cecca129f8e4c0eb225abb9371d401458c Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Tue, 6 Sep 2022 13:18:28 +0200 Subject: [PATCH 54/66] add return typehint --- src/Contract/IWebloaderAssetProvider.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Contract/IWebloaderAssetProvider.php b/src/Contract/IWebloaderAssetProvider.php index 47ef041..f18af55 100755 --- a/src/Contract/IWebloaderAssetProvider.php +++ b/src/Contract/IWebloaderAssetProvider.php @@ -5,5 +5,6 @@ interface IWebloaderAssetProvider { + /** @return array> */ public function getWebloaderAssets(): array; } From 0224bee17bb1e0bab723771fa256a86866d2df9f Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Mon, 23 Jan 2023 10:24:29 +0100 Subject: [PATCH 55/66] Allow nette/utils 4.x, cleanup --- composer.json | 10 +++++----- phpstan/phpstan | 3 --- src/Nette/Extension.php | 2 +- tests/Nette/ExtensionTest.php | 2 +- 4 files changed, 7 insertions(+), 10 deletions(-) delete mode 100755 phpstan/phpstan diff --git a/composer.json b/composer.json index c0acd36..b7ccfde 100755 --- a/composer.json +++ b/composer.json @@ -22,10 +22,10 @@ "php": "^8.0", "nette/application": "^3.1", "nette/di": "^3.0", - "nette/utils": "^3.2", + "nette/utils": "^3.2 || ^4.0", "ext-json": "*", "nette/schema": "^1.2", - "nette/finder": "^2.5", + "nette/finder": "^2.5 || ^3.0", "latte/latte": "^3.0", "tracy/tracy": "^2.8" }, @@ -43,7 +43,7 @@ "nette/component-model": "^3.0", "nette/http": "^3.0", "nette/neon": "^3.0", - "nette/robot-loader": "^3.0", + "nette/robot-loader": "^3.0 || ^4.0", "wikimedia/less.php": "^3.0.0", "scssphp/scssphp": "^1.1.0", "mockery/mockery": "1.*", @@ -58,8 +58,8 @@ "tedivm/jshrink": "^1.3" }, "scripts": { - "phpstan": "./phpstan/phpstan", - "tests": "./vendor/bin/phpunit --configuration tests/phpunit.xml tests" + "phpstan": "@php ./vendor/bin/phpstan analyse --configuration ./phpstan/phpstan.neon --memory-limit 512M", + "tests": "@php ./vendor/bin/phpunit --configuration tests/phpunit.xml tests" }, "config": { "allow-plugins": { diff --git a/phpstan/phpstan b/phpstan/phpstan deleted file mode 100755 index 7ab99e2..0000000 --- a/phpstan/phpstan +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash - -./vendor/bin/phpstan analyse --configuration ./phpstan/phpstan.neon --memory-limit 512M diff --git a/src/Nette/Extension.php b/src/Nette/Extension.php index c258f7f..7968619 100644 --- a/src/Nette/Extension.php +++ b/src/Nette/Extension.php @@ -4,7 +4,7 @@ namespace WebLoader\Nette; -use Nette\Configurator; +use Nette\Bootstrap\Configurator; use Nette\DI\Compiler; use Nette\DI\CompilerExtension; use Nette\DI\ContainerBuilder; diff --git a/tests/Nette/ExtensionTest.php b/tests/Nette/ExtensionTest.php index ad1dfc2..9d67bb5 100755 --- a/tests/Nette/ExtensionTest.php +++ b/tests/Nette/ExtensionTest.php @@ -3,7 +3,7 @@ namespace WebLoader\Test\Nette; -use Nette\Configurator; +use Nette\Bootstrap\Configurator; use Nette\DI\Compiler; use Nette\DI\Container; use Nette\Utils\Finder; From be7a69d81a61157fe260f425277b36ea33986907 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Thu, 8 Aug 2024 12:27:34 +0200 Subject: [PATCH 56/66] fixes #6 --- src/Nette/Diagnostics/Panel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Nette/Diagnostics/Panel.php b/src/Nette/Diagnostics/Panel.php index e239733..49cc409 100644 --- a/src/Nette/Diagnostics/Panel.php +++ b/src/Nette/Diagnostics/Panel.php @@ -165,7 +165,7 @@ public function getTab(): string return '' . '' - . Filters::bytes($this->size['combined']) + . (new Filters())->bytes($this->size['combined']) . ''; } } From 9f5a72d252c12be6c7105c562dd2044ac34b3473 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Thu, 8 Aug 2024 12:36:17 +0200 Subject: [PATCH 57/66] update deps and tools --- .gitignore | 4 ++++ composer.json | 12 ++++++------ phpstan/phpstan.neon | 2 -- tests/phpunit.xml | 13 ++----------- 4 files changed, 12 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 5fa635d..bfbbf45 100755 --- a/.gitignore +++ b/.gitignore @@ -4,9 +4,13 @@ # Files composer.lock .phpunit.result.cache +tests/.phpunit.cache/ # Dirs vendor/ temp/ .idea/ +# Custom +run +composer.phar diff --git a/composer.json b/composer.json index b7ccfde..3100ca0 100755 --- a/composer.json +++ b/composer.json @@ -19,13 +19,13 @@ "psr-4": { "WebLoader\\Test\\": "tests/" } }, "require": { - "php": "^8.0", + "php": "^8.3", "nette/application": "^3.1", "nette/di": "^3.0", - "nette/utils": "^3.2 || ^4.0", + "nette/utils": "^4.0", "ext-json": "*", "nette/schema": "^1.2", - "nette/finder": "^2.5 || ^3.0", + "nette/finder": "^3.0", "latte/latte": "^3.0", "tracy/tracy": "^2.8" }, @@ -44,15 +44,15 @@ "nette/http": "^3.0", "nette/neon": "^3.0", "nette/robot-loader": "^3.0 || ^4.0", - "wikimedia/less.php": "^3.0.0", + "wikimedia/less.php": "^5.0.0", "scssphp/scssphp": "^1.1.0", "mockery/mockery": "1.*", - "phpunit/phpunit": "9.*", + "phpunit/phpunit": "11.*", "phpstan/extension-installer": "^1.0", "phpstan/phpstan": "^1.0.0", "phpstan/phpstan-nette": "^1.0.0", "roave/security-advisories": "dev-master", - "symfony/console": "^4.2.9|^5.0.0|^6.0.0", + "symfony/console": "^4.2.9|^5.0.0|^6.0.0||^7.0.0", "phpstan/phpstan-mockery": "^1.0.0", "tubalmartin/cssmin": "^4.1", "tedivm/jshrink": "^1.3" diff --git a/phpstan/phpstan.neon b/phpstan/phpstan.neon index c3eacd4..eefcbfc 100755 --- a/phpstan/phpstan.neon +++ b/phpstan/phpstan.neon @@ -10,8 +10,6 @@ parameters: level: 8 - checkMissingIterableValueType: false - ignoreErrors: - message: '#Parameter \#1 \$filter of method WebLoader\\Compiler::addFilter\(\) expects callable\(\): mixed, 4 given\.#' diff --git a/tests/phpunit.xml b/tests/phpunit.xml index 65e3f36..0cfdb62 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -1,12 +1,3 @@ - + + From 747adf03d0e58fb49ed3b2a352beec77e60e3204 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Thu, 8 Aug 2024 13:27:51 +0200 Subject: [PATCH 58/66] phpstan fixes --- src/BatchCollection.php | 6 ++++++ src/Compiler.php | 14 ++++++++++++ src/Contract/IBatchCollection.php | 2 ++ src/Contract/IFileCollection.php | 3 +++ src/Contract/IOutputNamingConvention.php | 1 + src/DefaultOutputNamingConvention.php | 2 ++ src/File.php | 6 +++++- src/FileCollection.php | 27 ++++++++++++++++-------- src/Filter/LessBinFilter.php | 2 ++ src/Filter/Process.php | 1 + src/Filter/TypeScriptFilter.php | 2 ++ src/Filter/VariablesFilter.php | 4 ++-- src/Nette/Diagnostics/Panel.php | 10 ++++++++- src/Nette/Extension.php | 20 +++++++++++++++++- tests/CompilerTest.php | 9 +++++++- tests/Nette/ExtensionTest.php | 1 + 16 files changed, 95 insertions(+), 15 deletions(-) diff --git a/src/BatchCollection.php b/src/BatchCollection.php index fda11f5..e225375 100755 --- a/src/BatchCollection.php +++ b/src/BatchCollection.php @@ -8,6 +8,7 @@ class BatchCollection implements IBatchCollection { + /** @var array */ private array $batches = []; @@ -16,12 +17,17 @@ public function __construct() } + /** @return array */ public function getBatches(): array { return $this->batches; } + /** + * @param array $batch + * @throws BatchAlreadyExistsException + */ public function addBatch(string $type, string $name, array $batch): void { if (isset($this->batches[$type][$name])) { diff --git a/src/Compiler.php b/src/Compiler.php index 717be7c..4a37010 100755 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -20,8 +20,13 @@ class Compiler private IFileCollection $collection; private IOutputNamingConvention $namingConvention; private string $outputDir; + + /** @var list */ private array $filters = []; + + /** @var list */ private array $fileFilters = []; + private bool $checkLastModified = true; private bool $debugging = false; private bool $async = false; @@ -146,6 +151,7 @@ public function setCheckLastModified(bool $checkLastModified): void /** * Get last modified timestamp of newest file + * @param list|null $files */ public function getLastModified(?array $files = null): int { @@ -155,6 +161,7 @@ public function getLastModified(?array $files = null): int $modified = 0; + /** @var string $file */ foreach ($files as $file) { $modified = max($modified, filemtime((string) realpath($file))); } @@ -165,6 +172,7 @@ public function getLastModified(?array $files = null): int /** * Get joined content of all files + * @param array|null $files */ public function getContent(?array $files = null): string { @@ -201,6 +209,10 @@ public function generate(): ?File } + /** + * @param list $files + * @param list $watchFiles + */ protected function generateFiles(array $files, array $watchFiles = []): File { $name = $this->namingConvention->getFilename($files, $this); @@ -261,6 +273,7 @@ public function addFilter(callable $filter): void } + /** @return list */ public function getFilters(): array { return $this->filters; @@ -273,6 +286,7 @@ public function addFileFilter(callable $filter): void } + /** @return list */ public function getFileFilters(): array { return $this->fileFilters; diff --git a/src/Contract/IBatchCollection.php b/src/Contract/IBatchCollection.php index a8a40a4..ffe2dab 100755 --- a/src/Contract/IBatchCollection.php +++ b/src/Contract/IBatchCollection.php @@ -5,7 +5,9 @@ interface IBatchCollection { + /** @return array */ public function getBatches(): array; + /** @param array $batch */ public function addBatch(string $type, string $name, array $batch): void; } diff --git a/src/Contract/IFileCollection.php b/src/Contract/IFileCollection.php index 506d77f..d0c62cb 100755 --- a/src/Contract/IFileCollection.php +++ b/src/Contract/IFileCollection.php @@ -11,9 +11,12 @@ interface IFileCollection { public function getRoot(): string; + /** @return array */ public function getFiles(): array; + /** @return array */ public function getRemoteFiles(): array; + /** @return array */ public function getWatchFiles(): array; } diff --git a/src/Contract/IOutputNamingConvention.php b/src/Contract/IOutputNamingConvention.php index c8158b9..252eac8 100755 --- a/src/Contract/IOutputNamingConvention.php +++ b/src/Contract/IOutputNamingConvention.php @@ -13,5 +13,6 @@ */ interface IOutputNamingConvention { + /** @param array $files */ public function getFilename(array $files, Compiler $compiler): string; } diff --git a/src/DefaultOutputNamingConvention.php b/src/DefaultOutputNamingConvention.php index 070e9d7..ee821fb 100644 --- a/src/DefaultOutputNamingConvention.php +++ b/src/DefaultOutputNamingConvention.php @@ -75,6 +75,7 @@ public function setSuffix(string $suffix): void /** * Filename of generated file + * @param array $files */ public function getFilename(array $files, Compiler $compiler): string { @@ -82,6 +83,7 @@ public function getFilename(array $files, Compiler $compiler): string } + /** @param array $files */ protected function createHash(array $files, Compiler $compiler): string { $parts = $files; diff --git a/src/File.php b/src/File.php index acebbbc..e1dd8c7 100644 --- a/src/File.php +++ b/src/File.php @@ -9,12 +9,15 @@ final class File { private SplFileInfo $file; + + /** @var array */ private array $sourceFiles; + /** @param array $sourceFiles */ public function __construct( string $path, - array $sourceFiles + array $sourceFiles, ) { $this->file = new SplFileInfo($path); $this->sourceFiles = $sourceFiles; @@ -39,6 +42,7 @@ public function getLastModified(): ?int } + /** @return array */ public function getSourceFiles(): array { return $this->sourceFiles; diff --git a/src/FileCollection.php b/src/FileCollection.php index 1b38bfa..ac4567e 100755 --- a/src/FileCollection.php +++ b/src/FileCollection.php @@ -16,16 +16,22 @@ */ class FileCollection implements IFileCollection { + /** @var array */ private array $files = []; + + /** @var array */ private array $watchFiles = []; + + /** @var array */ private array $remoteFiles = []; - public function __construct(private string $root) + public function __construct(private readonly string $root) { } + /** @return list */ public function getFiles(): array { return array_values($this->files); @@ -54,10 +60,10 @@ public function cannonicalizePath(string $path): string /** - * @param string|SplFileInfo $file + * @param SplFileInfo|string $file * @throws FileNotFoundException */ - public function addFile($file): void + public function addFile(SplFileInfo|string $file): void { $file = $this->cannonicalizePath((string) $file); @@ -71,9 +77,9 @@ public function addFile($file): void /** * Add files - * @param array|Traversable $files array list of files + * @param iterable $files array list of files */ - public function addFiles($files): void + public function addFiles(iterable $files): void { foreach ($files as $file) { $this->addFile($file); @@ -87,6 +93,7 @@ public function removeFile(string $file): void } + /** @param array $files */ public function removeFiles(array $files): void { $files = array_map([$this, 'cannonicalizePath'], $files); @@ -110,9 +117,9 @@ public function addRemoteFile(string $file): void /** * Add multiple remote files - * @param array|Traversable $files + * @param iterable $files */ - public function addRemoteFiles($files): void + public function addRemoteFiles(iterable $files): void { foreach ($files as $file) { $this->addRemoteFile($file); @@ -131,6 +138,7 @@ public function clear(): void } + /** @return array */ public function getRemoteFiles(): array { return $this->remoteFiles; @@ -157,9 +165,9 @@ public function addWatchFile(string $file): void /** * Add watch files - * @param array|Traversable $files array list of files + * @param iterable $files array list of files */ - public function addWatchFiles($files): void + public function addWatchFiles(iterable $files): void { foreach ($files as $file) { $this->addWatchFile($file); @@ -167,6 +175,7 @@ public function addWatchFiles($files): void } + /** @return list */ public function getWatchFiles(): array { return array_values($this->watchFiles); diff --git a/src/Filter/LessBinFilter.php b/src/Filter/LessBinFilter.php index a09ee1f..c69776b 100644 --- a/src/Filter/LessBinFilter.php +++ b/src/Filter/LessBinFilter.php @@ -14,9 +14,11 @@ */ class LessBinFilter { + /** @var array */ private array $env; + /** @param array $env */ public function __construct(private string $bin = 'lessc', array $env = []) { $this->env = $env + $_ENV; diff --git a/src/Filter/Process.php b/src/Filter/Process.php index f504558..b297a01 100644 --- a/src/Filter/Process.php +++ b/src/Filter/Process.php @@ -14,6 +14,7 @@ */ class Process { + /** @param array $env */ public static function run( string $cmd, ?string $stdin = null, diff --git a/src/Filter/TypeScriptFilter.php b/src/Filter/TypeScriptFilter.php index b11f174..4b359e7 100755 --- a/src/Filter/TypeScriptFilter.php +++ b/src/Filter/TypeScriptFilter.php @@ -14,9 +14,11 @@ */ class TypeScriptFilter { + /** @var array|null */ private ?array $env; + /** @param array $env */ public function __construct(private string $bin = 'tsc', array $env = []) { $this->env = $env + $_ENV; diff --git a/src/Filter/VariablesFilter.php b/src/Filter/VariablesFilter.php index 3ec1203..e97895a 100644 --- a/src/Filter/VariablesFilter.php +++ b/src/Filter/VariablesFilter.php @@ -18,6 +18,7 @@ class VariablesFilter private string $endVariable = '}}'; + /** @param array $variables */ public function __construct(private array $variables = []) { foreach ($variables as $key => $value) { @@ -58,8 +59,7 @@ public function __set(string $name, string $value): void /** * Magic get variable, do not call directly - * - * @throws \WebLoader\Exception\InvalidArgumentException + * @throws InvalidArgumentException */ public function &__get(string $name): string { diff --git a/src/Nette/Diagnostics/Panel.php b/src/Nette/Diagnostics/Panel.php index 49cc409..2579496 100644 --- a/src/Nette/Diagnostics/Panel.php +++ b/src/Nette/Diagnostics/Panel.php @@ -16,6 +16,7 @@ */ class Panel implements IBarPanel { + /** @var array */ public static array $types = [ 'css' => 'CSS files', 'js' => 'JavaScript files', @@ -26,9 +27,15 @@ class Panel implements IBarPanel /** @var Compiler[] */ private array $compilers = []; + /** @var array{original: int, combined: int, ratio: float}|null */ private ?array $size = null; + + /** @var array */ private array $files; + + /** @var array */ private array $sizes; + private string $root; @@ -57,6 +64,7 @@ public function addLoader(string $name, Compiler $compiler): self /** * Computes the info. + * @return array{original: int, combined: int, ratio: float} */ private function compute(): array { @@ -148,7 +156,7 @@ private function getTable(): string */ public function getPanel(): string { - return $this->compute() ? $this->getTable() : ''; + return $this->compute() ? $this->getTable() : ''; //@phpstan-ignore ternary.alwaysTrue } diff --git a/src/Nette/Extension.php b/src/Nette/Extension.php index 7968619..00c02d0 100644 --- a/src/Nette/Extension.php +++ b/src/Nette/Extension.php @@ -17,6 +17,7 @@ use WebLoader\BatchCollection; use WebLoader\Compiler as WebloaderCompiler; use WebLoader\Contract\IWebloaderAssetProvider; +use WebLoader\Exception\BatchAlreadyExistsException; use WebLoader\Exception\CompilationException; use WebLoader\Exception\FileNotFoundException; use WebLoader\FileCollection; @@ -181,6 +182,13 @@ public function loadConfiguration(): void } + /** + * @param ContainerBuilder $builder + * @param string $name + * @param array $config + * @return void + * @throws FileNotFoundException + */ private function addWebLoader(ContainerBuilder $builder, string $name, array $config): void { $filesServiceName = $this->prefix($name . 'Files'); @@ -263,11 +271,17 @@ public function install(Configurator $configurator): void } + /** + * @param array $filesConfig + * @param string $sourceDir + * @return array + * @throws FileNotFoundException + */ private function findFiles(array $filesConfig, string $sourceDir): array { $normalizedFiles = []; - /** @var array|string $file */ + /** @var array|string $file */ foreach ($filesConfig as $file) { // finder support if (is_array($file) && isset($file['files']) && (isset($file['in']) || isset($file['from']))) { @@ -359,6 +373,10 @@ private function extractBatchesFromExtensions(): void } + /** + * @param array $config + * @throws BatchAlreadyExistsException + */ private function extractNormalBatches(array $config): void { foreach (['css', 'js'] as $type) { diff --git a/tests/CompilerTest.php b/tests/CompilerTest.php index b3104bc..e00fcfa 100644 --- a/tests/CompilerTest.php +++ b/tests/CompilerTest.php @@ -52,9 +52,16 @@ protected function setUp(): void } + /** @return list */ private function getTempFiles(): array { - return (array)glob(__DIR__ . '/temp/webloader-*'); + $files = glob(__DIR__ . '/temp/webloader-*'); + + if ($files === false) { + return []; + } + + return $files; } diff --git a/tests/Nette/ExtensionTest.php b/tests/Nette/ExtensionTest.php index 9d67bb5..2856126 100755 --- a/tests/Nette/ExtensionTest.php +++ b/tests/Nette/ExtensionTest.php @@ -32,6 +32,7 @@ protected function setUp(): void } + /** @param list $configFiles */ private function prepareContainer(array $configFiles): void { $finder = Finder::findFiles('*') From 6bbe918235bd86312b356c8951d23b3af6566cfe Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Thu, 8 Aug 2024 13:33:54 +0200 Subject: [PATCH 59/66] fixes #4 - this feature has been removed --- README.md | 3 --- src/Contract/IFileCollection.php | 3 --- src/FileCollection.php | 37 -------------------------------- src/Nette/Extension.php | 4 ---- src/Nette/WebLoader.php | 5 ----- tests/FileCollectionTest.php | 25 --------------------- tests/fixtures/extension.neon | 3 --- 7 files changed, 80 deletions(-) diff --git a/README.md b/README.md index dccdeab..b52187f 100644 --- a/README.md +++ b/README.md @@ -77,9 +77,6 @@ webloader: js: default: - remoteFiles: - - http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js - - http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js files: - %appDir%/../libs/nette/nette/client-side/netteForms.js - web.js diff --git a/src/Contract/IFileCollection.php b/src/Contract/IFileCollection.php index d0c62cb..14a5ae0 100755 --- a/src/Contract/IFileCollection.php +++ b/src/Contract/IFileCollection.php @@ -14,9 +14,6 @@ public function getRoot(): string; /** @return array */ public function getFiles(): array; - /** @return array */ - public function getRemoteFiles(): array; - /** @return array */ public function getWatchFiles(): array; } diff --git a/src/FileCollection.php b/src/FileCollection.php index ac4567e..dad99d5 100755 --- a/src/FileCollection.php +++ b/src/FileCollection.php @@ -22,9 +22,6 @@ class FileCollection implements IFileCollection /** @var array */ private array $watchFiles = []; - /** @var array */ - private array $remoteFiles = []; - public function __construct(private readonly string $root) { @@ -101,32 +98,6 @@ public function removeFiles(array $files): void } - /** - * Add file in remote repository (for example Google CDN). - * @param string $file URL address - */ - public function addRemoteFile(string $file): void - { - if (in_array($file, $this->remoteFiles, true)) { - return; - } - - $this->remoteFiles[] = $file; - } - - - /** - * Add multiple remote files - * @param iterable $files - */ - public function addRemoteFiles(iterable $files): void - { - foreach ($files as $file) { - $this->addRemoteFile($file); - } - } - - /** * Remove all files */ @@ -134,14 +105,6 @@ public function clear(): void { $this->files = []; $this->watchFiles = []; - $this->remoteFiles = []; - } - - - /** @return array */ - public function getRemoteFiles(): array - { - return $this->remoteFiles; } diff --git a/src/Nette/Extension.php b/src/Nette/Extension.php index 00c02d0..f1fbea1 100644 --- a/src/Nette/Extension.php +++ b/src/Nette/Extension.php @@ -69,7 +69,6 @@ private function getJsConfigSchema(bool $useDefaults = false): Schema 'tempPath' => Expect::string($tempPath), 'files' => Expect::array(), 'watchFiles' => Expect::array(), - 'remoteFiles' => Expect::array(), 'filters' => Expect::array(), 'fileFilters' => Expect::array(), 'async' => Expect::bool($async), @@ -103,7 +102,6 @@ private function getCssConfigSchema(bool $useDefaults = false): Schema 'tempPath' => Expect::string($tempPath), 'files' => Expect::array(), 'watchFiles' => Expect::array(), - 'remoteFiles' => Expect::array(), 'filters' => Expect::array(), 'fileFilters' => Expect::array(), 'async' => Expect::bool($async), @@ -205,8 +203,6 @@ private function addWebLoader(ContainerBuilder $builder, string $name, array $co $files->addSetup('addWatchFile', [$file]); } - $files->addSetup('addRemoteFiles', [$config['remoteFiles']]); - $compiler = $builder->addDefinition($this->prefix($name . 'Compiler')) ->setType(WebloaderCompiler::class) ->setArguments([ diff --git a/src/Nette/WebLoader.php b/src/Nette/WebLoader.php index d0a9b63..03f74a1 100755 --- a/src/Nette/WebLoader.php +++ b/src/Nette/WebLoader.php @@ -79,11 +79,6 @@ public function render(): void $this->compiler->setFileCollection($newFiles); } - // remote files - foreach ($this->compiler->getFileCollection()->getRemoteFiles() as $file) { - echo $this->getElement($file), PHP_EOL; - } - $file = $this->compiler->generate(); if ($file) { echo $this->getElement($file), PHP_EOL; diff --git a/tests/FileCollectionTest.php b/tests/FileCollectionTest.php index 49dda53..2f8496f 100755 --- a/tests/FileCollectionTest.php +++ b/tests/FileCollectionTest.php @@ -83,32 +83,14 @@ public function testCannonicalizePath(): void public function testClear(): void { $this->object->addFile('a.txt'); - $this->object->addRemoteFile('http://jquery.com/jquery.js'); $this->object->addWatchFile('b.txt'); $this->object->clear(); $this->assertEquals([], $this->object->getFiles()); - $this->assertEquals([], $this->object->getRemoteFiles()); $this->assertEquals([], $this->object->getWatchFiles()); } - public function testRemoteFiles(): void - { - $this->object->addRemoteFile('http://jquery.com/jquery.js'); - $this->object->addRemoteFiles([ - 'http://jquery.com/jquery.js', - 'http://google.com/angular.js', - ]); - - $expected = [ - 'http://jquery.com/jquery.js', - 'http://google.com/angular.js', - ]; - $this->assertEquals($expected, $this->object->getRemoteFiles()); - } - - public function testWatchFiles(): void { $this->object->addWatchFile(__DIR__ . '/fixtures/a.txt'); @@ -130,13 +112,6 @@ public function testTraversableFiles(): void } - public function testTraversableRemoteFiles(): void - { - $this->object->addRemoteFiles(new ArrayIterator(['http://jquery.com/jquery.js'])); - $this->assertCount(1, $this->object->getRemoteFiles()); - } - - public function testSplFileInfo(): void { $this->object->addFile(new SplFileInfo(__DIR__ . '/fixtures/a.txt')); diff --git a/tests/fixtures/extension.neon b/tests/fixtures/extension.neon index b2f0fef..b71f33c 100755 --- a/tests/fixtures/extension.neon +++ b/tests/fixtures/extension.neon @@ -20,9 +20,6 @@ webloader: js: default: - remoteFiles: - - http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js - - http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js files: - %fixturesDir%/dir/one.js - dir/two.js From 37ff51c681d7573a26ad80f1838aeefcb476947c Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Thu, 8 Aug 2024 13:43:08 +0200 Subject: [PATCH 60/66] dev stuff --- .dev/phpcs.xml | 285 +++++++++++++++++++++++++ {phpstan => .dev/phpstan}/phpstan.neon | 0 composer.json | 6 +- 3 files changed, 289 insertions(+), 2 deletions(-) create mode 100644 .dev/phpcs.xml rename {phpstan => .dev/phpstan}/phpstan.neon (100%) diff --git a/.dev/phpcs.xml b/.dev/phpcs.xml new file mode 100644 index 0000000..5821512 --- /dev/null +++ b/.dev/phpcs.xml @@ -0,0 +1,285 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/phpstan/phpstan.neon b/.dev/phpstan/phpstan.neon similarity index 100% rename from phpstan/phpstan.neon rename to .dev/phpstan/phpstan.neon diff --git a/composer.json b/composer.json index 3100ca0..485e968 100755 --- a/composer.json +++ b/composer.json @@ -55,7 +55,8 @@ "symfony/console": "^4.2.9|^5.0.0|^6.0.0||^7.0.0", "phpstan/phpstan-mockery": "^1.0.0", "tubalmartin/cssmin": "^4.1", - "tedivm/jshrink": "^1.3" + "tedivm/jshrink": "^1.3", + "slevomat/coding-standard": "^8.15" }, "scripts": { "phpstan": "@php ./vendor/bin/phpstan analyse --configuration ./phpstan/phpstan.neon --memory-limit 512M", @@ -63,7 +64,8 @@ }, "config": { "allow-plugins": { - "phpstan/extension-installer": true + "phpstan/extension-installer": true, + "dealerdirect/phpcodesniffer-composer-installer": true } } } From 552c7cf27ad5fed019cb8ad2a8270abda2fca01e Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Fri, 9 Aug 2024 09:30:42 +0200 Subject: [PATCH 61/66] remove arguments from render method --- src/Nette/WebLoader.php | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/Nette/WebLoader.php b/src/Nette/WebLoader.php index 03f74a1..8694fd5 100755 --- a/src/Nette/WebLoader.php +++ b/src/Nette/WebLoader.php @@ -70,23 +70,10 @@ protected function getUrl(File $file): string */ public function render(): void { - $hasArgs = func_num_args() > 0; - - if ($hasArgs) { - $backup = $this->compiler->getFileCollection(); - $newFiles = new FileCollection($backup->getRoot()); - $newFiles->addFiles(func_get_args()); - $this->compiler->setFileCollection($newFiles); - } - $file = $this->compiler->generate(); if ($file) { echo $this->getElement($file), PHP_EOL; } - - if ($hasArgs) { - $this->compiler->setFileCollection($backup); - } } From ee59aefb2043d924829b2caeec26ec6673dd35e4 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Fri, 9 Aug 2024 09:58:17 +0200 Subject: [PATCH 62/66] Switchable output type during component creation --- src/Enum/RenderMode.php | 11 +++++++++++ src/Nette/WebLoader.php | 37 +++++++++++++++++++++++++------------ 2 files changed, 36 insertions(+), 12 deletions(-) create mode 100644 src/Enum/RenderMode.php diff --git a/src/Enum/RenderMode.php b/src/Enum/RenderMode.php new file mode 100644 index 0000000..918998d --- /dev/null +++ b/src/Enum/RenderMode.php @@ -0,0 +1,11 @@ +renderMode = $renderMode; + } + + protected function getUrl(File $file): string { return $this->getGeneratedFilePath($file); @@ -71,27 +79,32 @@ protected function getUrl(File $file): string public function render(): void { $file = $this->compiler->generate(); - if ($file) { - echo $this->getElement($file), PHP_EOL; + + if ($file === null) { + return; } + + $output = match ($this->renderMode) { + RenderMode::URL => $this->getUrl($file), + RenderMode::LINK => $this->getElement($file), + RenderMode::INLINE => $this->getInlineElement($file), + }; + + echo $output, PHP_EOL; } public function renderInline(): void { - $file = $this->compiler->generate(); - if ($file) { - echo $this->getInlineElement($file), PHP_EOL; - } + $this->setRenderMode(RenderMode::INLINE); + $this->render(); } public function renderUrl(): void { - $file = $this->compiler->generate(); - if ($file) { - echo $this->getUrl($file), PHP_EOL; - } + $this->setRenderMode(renderMode::URL); + $this->render(); } From dfafe201f86f890ac03007bae75d4590110218ff Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Fri, 30 Aug 2024 08:47:05 +0200 Subject: [PATCH 63/66] GenerateCommand - symfony/console 7 update --- src/Nette/SymfonyConsole/GenerateCommand.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Nette/SymfonyConsole/GenerateCommand.php b/src/Nette/SymfonyConsole/GenerateCommand.php index 2cd5ac4..e4050d0 100644 --- a/src/Nette/SymfonyConsole/GenerateCommand.php +++ b/src/Nette/SymfonyConsole/GenerateCommand.php @@ -5,6 +5,7 @@ namespace WebLoader\Nette\SymfonyConsole; use Nette\DI\Container; +use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -14,12 +15,10 @@ /** * Generate Command */ +#[AsCommand(name: 'webloader:generate', description: 'Generate files.')] class GenerateCommand extends Command { - /** @var string */ - protected static $defaultName = 'webloader:generate'; - /** @var Compiler[] */ private array $compilers = []; @@ -37,9 +36,7 @@ public function __construct(Container $container) protected function configure(): void { - $this->setName(self::$defaultName) - ->setDescription('Generates files.') - ->addOption('force', 'f', InputOption::VALUE_NONE, 'Generate if not modified.'); + $this->addOption('force', 'f', InputOption::VALUE_NONE, 'Generate if not modified.'); } From 49666480ead28cee6399ec5dd35ac7f7b9cfcc5b Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Fri, 28 Feb 2025 09:25:20 +0100 Subject: [PATCH 64/66] remove http auth from url (cherry picked from commit 1e7cdf84ae034207601cda6d338e0e82be0db7e9) --- src/Nette/LoaderFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Nette/LoaderFactory.php b/src/Nette/LoaderFactory.php index 6c51603..51ac554 100644 --- a/src/Nette/LoaderFactory.php +++ b/src/Nette/LoaderFactory.php @@ -57,7 +57,7 @@ private function formatTempPath(string $name, bool $absoluteUrl = false): string $lName = strtolower($name); $tempPath = $this->tempPaths[$lName] ?? Extension::DEFAULT_TEMP_PATH; $method = $absoluteUrl ? 'getBaseUrl' : 'getBasePath'; - return rtrim($this->httpRequest->getUrl()->{$method}(), '/') . '/' . $tempPath; + return rtrim($this->httpRequest->getUrl()->withoutUserInfo()->{$method}(), '/') . '/' . $tempPath; } From c1f269c37413152425184b7bb2b333b17137b4ae Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Tue, 25 Mar 2025 08:43:08 +0100 Subject: [PATCH 65/66] update deps, phpstan fixes --- .editorconfig | 2 +- composer.json | 10 +++--- src/Compiler.php | 7 +++- src/Contract/IFileCollection.php | 4 +-- src/File.php | 2 +- src/Filter/ScssFilter.php | 37 +++++++++++++++++++--- src/Filter/TypeScriptFilter.php | 4 +-- src/Nette/Extension.php | 4 +-- tests/CompilerTest.php | 3 +- tests/fixtures/style.scss.expected | 4 ++- tests/fixtures/styleAbsolute.scss.expected | 4 ++- 11 files changed, 58 insertions(+), 23 deletions(-) diff --git a/.editorconfig b/.editorconfig index 8502e51..fb6c057 100755 --- a/.editorconfig +++ b/.editorconfig @@ -10,5 +10,5 @@ indent_size = 3 charset = utf-8 trim_trailing_whitespace = true -[{*.svg, .htaccess, *.expected}] +[{*.svg,.htaccess,*.expected}] insert_final_newline = false diff --git a/composer.json b/composer.json index 485e968..0f6daa6 100755 --- a/composer.json +++ b/composer.json @@ -45,15 +45,15 @@ "nette/neon": "^3.0", "nette/robot-loader": "^3.0 || ^4.0", "wikimedia/less.php": "^5.0.0", - "scssphp/scssphp": "^1.1.0", + "scssphp/scssphp": "^2.0.0", "mockery/mockery": "1.*", - "phpunit/phpunit": "11.*", + "phpunit/phpunit": "12.*", "phpstan/extension-installer": "^1.0", - "phpstan/phpstan": "^1.0.0", - "phpstan/phpstan-nette": "^1.0.0", + "phpstan/phpstan": "^2.0.0", + "phpstan/phpstan-nette": "^2.0.0", "roave/security-advisories": "dev-master", "symfony/console": "^4.2.9|^5.0.0|^6.0.0||^7.0.0", - "phpstan/phpstan-mockery": "^1.0.0", + "phpstan/phpstan-mockery": "^2.0.0", "tubalmartin/cssmin": "^4.1", "tedivm/jshrink": "^1.3", "slevomat/coding-standard": "^8.15" diff --git a/src/Compiler.php b/src/Compiler.php index 4a37010..6319598 100755 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -203,7 +203,12 @@ public function generate(): ?File return null; } - $watchFiles = $this->checkLastModified ? array_unique(array_merge($files, $this->collection->getWatchFiles())) : []; + if ($this->checkLastModified) { + $watchFiles = array_unique(array_merge($files, $this->collection->getWatchFiles())); + $watchFiles = array_values($watchFiles); + } else { + $watchFiles = []; + } return $this->generateFiles($files, $watchFiles); } diff --git a/src/Contract/IFileCollection.php b/src/Contract/IFileCollection.php index 14a5ae0..dcb62e8 100755 --- a/src/Contract/IFileCollection.php +++ b/src/Contract/IFileCollection.php @@ -11,9 +11,9 @@ interface IFileCollection { public function getRoot(): string; - /** @return array */ + /** @return list */ public function getFiles(): array; - /** @return array */ + /** @return list */ public function getWatchFiles(): array; } diff --git a/src/File.php b/src/File.php index e1dd8c7..f114b54 100644 --- a/src/File.php +++ b/src/File.php @@ -36,7 +36,7 @@ public function getPath(): string } - public function getLastModified(): ?int + public function getLastModified(): int { return $this->file->getMTime(); } diff --git a/src/Filter/ScssFilter.php b/src/Filter/ScssFilter.php index 43fb255..5d11ab3 100644 --- a/src/Filter/ScssFilter.php +++ b/src/Filter/ScssFilter.php @@ -33,13 +33,40 @@ private function getScssC(): ScssCompiler public function __invoke(string $code, Compiler $loader, string $file): string { - $file = (string) $file; - if (pathinfo($file, PATHINFO_EXTENSION) === 'scss') { - $this->getScssC()->setImportPaths(['', pathinfo($file, PATHINFO_DIRNAME) . '/']); - return $this->getScssC()->compile($code); + + $paths = []; + + $cwd = getcwd(); + + if ($cwd === false) { + return $code; + } + + $this->getScssC()->setImportPaths($this->getImportPaths($file)); + $result = $this->getScssC()->compileString($code); + return $result->getCss(); + } + + return $code; + } + + + /** + * @return list + */ + private function getImportPaths(string $file): array + { + $paths = []; + + $cwd = getcwd(); + + if ($cwd !== false) { + $paths[] = $cwd; } - return (string) $code; + $paths[] = pathinfo($file, PATHINFO_DIRNAME) . '/'; + + return $paths; } } diff --git a/src/Filter/TypeScriptFilter.php b/src/Filter/TypeScriptFilter.php index 4b359e7..88764f2 100755 --- a/src/Filter/TypeScriptFilter.php +++ b/src/Filter/TypeScriptFilter.php @@ -14,8 +14,8 @@ */ class TypeScriptFilter { - /** @var array|null */ - private ?array $env; + /** @var array */ + private array $env; /** @param array $env */ diff --git a/src/Nette/Extension.php b/src/Nette/Extension.php index f1fbea1..b42baf0 100644 --- a/src/Nette/Extension.php +++ b/src/Nette/Extension.php @@ -12,8 +12,8 @@ use Nette\Schema\Helpers as SchemaHelpers; use Nette\Schema\Processor; use Nette\Schema\Schema; +use Nette\Utils\FileInfo; use Nette\Utils\Finder; -use SplFileInfo; use WebLoader\BatchCollection; use WebLoader\Compiler as WebloaderCompiler; use WebLoader\Contract\IWebloaderAssetProvider; @@ -295,7 +295,7 @@ private function findFiles(array $filesConfig, string $sourceDir): array $foundFilesList = []; foreach ($finder as $foundFile) { - /** @var SplFileInfo $foundFile */ + /** @var FileInfo $foundFile */ $foundFilesList[] = $foundFile->getPathname(); } diff --git a/tests/CompilerTest.php b/tests/CompilerTest.php index e00fcfa..cf703e2 100644 --- a/tests/CompilerTest.php +++ b/tests/CompilerTest.php @@ -102,7 +102,7 @@ public function testGeneratingAndFilters(): void $file = $this->generateFile(); - $this->assertTrue(is_numeric($file->getLastModified()) && $file->getLastModified() > 0, 'Generate does not provide last modified timestamp correctly.'); + $this->assertTrue($file->getLastModified() && $file->getLastModified() > 0, 'Generate does not provide last modified timestamp correctly.'); $content = file_get_contents($this->object->getOutputDir() . '/' . $file->getFileName()); @@ -114,7 +114,6 @@ public function testGenerateReturnsSourceFilePaths(): void { $file = $this->generateFile(); - $this->assertIsArray($file->getSourceFiles()); $this->assertCount(3, $file->getSourceFiles()); $this->assertFileExists($file->getSourceFiles()[0]); } diff --git a/tests/fixtures/style.scss.expected b/tests/fixtures/style.scss.expected index dd4b040..fe3ebaf 100644 --- a/tests/fixtures/style.scss.expected +++ b/tests/fixtures/style.scss.expected @@ -2,6 +2,7 @@ display: block; zoom: 1; } + .navigation ul { line-height: 20px; color: blue; @@ -9,6 +10,7 @@ .navigation ul a { color: red; } + .footer .copyright { color: silver; -} +} \ No newline at end of file diff --git a/tests/fixtures/styleAbsolute.scss.expected b/tests/fixtures/styleAbsolute.scss.expected index dd4b040..fe3ebaf 100644 --- a/tests/fixtures/styleAbsolute.scss.expected +++ b/tests/fixtures/styleAbsolute.scss.expected @@ -2,6 +2,7 @@ display: block; zoom: 1; } + .navigation ul { line-height: 20px; color: blue; @@ -9,6 +10,7 @@ .navigation ul a { color: red; } + .footer .copyright { color: silver; -} +} \ No newline at end of file From 565cd110d7952527bf3284c609c40971894d6130 Mon Sep 17 00:00:00 2001 From: Pavel Linhart Date: Thu, 22 May 2025 13:10:36 +0200 Subject: [PATCH 66/66] ScssFilter - update --- src/Filter/ScssFilter.php | 56 +++++---------------------------------- 1 file changed, 7 insertions(+), 49 deletions(-) diff --git a/src/Filter/ScssFilter.php b/src/Filter/ScssFilter.php index 5d11ab3..039afa9 100644 --- a/src/Filter/ScssFilter.php +++ b/src/Filter/ScssFilter.php @@ -4,69 +4,27 @@ namespace WebLoader\Filter; -use ScssPhp\ScssPhp\Compiler as ScssCompiler; use WebLoader\Compiler; +use ScssPhp\ScssPhp\Compiler as ScssCompiler; -/** - * Scss CSS filter - * - * @author Roman Matěna - * @license MIT - */ -class ScssFilter +final class ScssFilter { - public function __construct(private ?ScssCompiler $sc = null) - { - } - - private function getScssC(): ScssCompiler + private function getCompiler(): ScssCompiler { - // lazy loading - if (empty($this->sc)) { - $this->sc = new ScssCompiler; - } - - return $this->sc; + return new ScssCompiler; } public function __invoke(string $code, Compiler $loader, string $file): string { if (pathinfo($file, PATHINFO_EXTENSION) === 'scss') { - - $paths = []; - - $cwd = getcwd(); - - if ($cwd === false) { - return $code; - } - - $this->getScssC()->setImportPaths($this->getImportPaths($file)); - $result = $this->getScssC()->compileString($code); + $compiler = $this->getCompiler(); + $compiler->setImportPaths([pathinfo($file, PATHINFO_DIRNAME) . '/']); + $result = $compiler->compileString($code); return $result->getCss(); } return $code; } - - - /** - * @return list - */ - private function getImportPaths(string $file): array - { - $paths = []; - - $cwd = getcwd(); - - if ($cwd !== false) { - $paths[] = $cwd; - } - - $paths[] = pathinfo($file, PATHINFO_DIRNAME) . '/'; - - return $paths; - } }