diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..019bb8a --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,36 @@ +name: HTMLMinTests +on: [push, pull_request] +jobs: + htmlmin: + name: PHP ${{ matrix.php-versions }} + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + php-versions: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1'] + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Setup PHP with Composer and extensions + with: + php-version: ${{ matrix.php-versions }} + uses: shivammathur/setup-php@v2 + - name: Get Composer cache directory + id: composercache + run: echo "::set-output name=dir::$(composer config cache-files-dir)" + - name: Cache Composer dependencies + uses: actions/cache@v2 + with: + php-version: ${{ matrix.php-versions }} + path: ${{ steps.composercache.outputs.dir }} + key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} + restore-keys: ${{ runner.os }}-composer- + - name: Install Composer dependencies + env: + PHP_VERSION: ${{ matrix.php-versions }} + run: composer config --no-plugins allow-plugins.kylekatarnls/update-helper true && composer install --no-progress --prefer-dist --optimize-autoloader $(if [ "$PHP_VERSION" == "8.0" || "$PHP_VERSION" == "8.1" ]; then echo "--ignore-platform-reqs"; fi;) + - name: Run tests with code coverage + env: + PHP_VERSION: ${{ matrix.php-versions }} + run: vendor/bin/phpunit --coverage-clover build/logs/clover.xml + continue-on-error: true diff --git a/README.md b/README.md index 106d614..7194a4c 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ Laravel HTMLMin =============== -Laravel HTMLMin is currently maintained by [Raza Mehdi](https://github.com/srmklive), and is a simple HTML minifier for [Laravel 5](http://laravel.com). It utilises Mr Clay's [Minify](https://github.com/mrclay/minify) package to minify entire responses, but can also minify blade at compile time. Feel free to check out the [change log](CHANGELOG.md), [releases](https://github.com/HTMLMin/Laravel-HTMLMin/releases), [license](LICENSE), and [contribution guidelines](CONTRIBUTING.md). +Laravel HTMLMin is currently maintained by [Raza Mehdi](https://github.com/srmklive), and is a simple HTML minifier for [Laravel](http://laravel.com). It utilises Mr Clay's [Minify](https://github.com/mrclay/minify) package to minify entire responses, but can also minify blade at compile time. Feel free to check out the [change log](CHANGELOG.md), [releases](https://github.com/HTMLMin/Laravel-HTMLMin/releases), [license](LICENSE), and [contribution guidelines](CONTRIBUTING.md).

-Build Status +Tests Software License Latest Version

@@ -12,7 +12,7 @@ Laravel HTMLMin is currently maintained by [Raza Mehdi](https://github.com/srmkl ## Installation -Laravel HTMLMin requires [PHP](https://php.net) 5.5+. This particular version supports Laravel 5.1, 5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.8 and 6.0 only. +Laravel HTMLMin requires [PHP](https://php.net) 5.5+. This particular version supports Laravel 5.1-5.8, 6.x, 7.x and 8.x. To get the latest version, simply require the project using [Composer](https://getcomposer.org): @@ -20,12 +20,21 @@ To get the latest version, simply require the project using [Composer](https://g $ composer require htmlmin/htmlmin ``` -Once installed, you need to register the `HTMLMin\HTMLMin\HTMLMinServiceProvider` service provider in your `config/app.php`, and optionally alias our facade: +Once installed, register the service provider in your `config/app.php` ```php - 'HTMLMin' => HTMLMin\HTMLMin\Facades\HTMLMin::class, +'providers' => [ + HTMLMin\HTMLMin\HTMLMinServiceProvider::class +] ``` +If you want, a facade is available to alias + +```php +'aliases' => [ + 'HTMLMin' => HTMLMin\HTMLMin\Facades\HTMLMin::class +] +``` ## Configuration diff --git a/composer.json b/composer.json index 317c670..a686cac 100644 --- a/composer.json +++ b/composer.json @@ -15,18 +15,18 @@ ], "require": { "php": ">=5.5.9", - "illuminate/contracts": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0", - "illuminate/filesystem": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0", - "illuminate/http": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0", - "illuminate/routing": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0", - "illuminate/support": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0", - "illuminate/view": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0", - "mrclay/minify": "^2.2" + "illuminate/contracts": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0", + "illuminate/filesystem": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0", + "illuminate/http": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0", + "illuminate/routing": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0", + "illuminate/support": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0", + "illuminate/view": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0", + "mrclay/minify": "^2.2|^3.0" }, "require-dev": { "graham-campbell/testbench": "^3.1|^4.0|^5.0", "mockery/mockery": "^0.9.4|^1.0", - "phpunit/phpunit": "^4.8|^5.0|^6.0|^7.0|^8.0" + "phpunit/phpunit": "^4.8|^5.0|^6.0|^7.0|^8.0|^9.0" }, "autoload": { "psr-4": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 986bc30..bd51815 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,5 +1,7 @@ -./tests - - + + ./src - - + + diff --git a/src/Compilers/MinifyCompiler.php b/src/Compilers/MinifyCompiler.php index 3c441e1..c0164ba 100644 --- a/src/Compilers/MinifyCompiler.php +++ b/src/Compilers/MinifyCompiler.php @@ -36,6 +36,10 @@ class MinifyCompiler extends BladeCompiler * @var string */ protected $ignoredPaths; + /** + * @var \Illuminate\View\Compilers\BladeCompiler|null + */ + private $previousCompiler; /** * Create a new instance. @@ -44,15 +48,30 @@ class MinifyCompiler extends BladeCompiler * @param \Illuminate\Filesystem\Filesystem $files * @param string $cachePath * @param array $ignoredPaths + * @param \Illuminate\View\Compilers\BladeCompiler $previousCompiler + * @param array $customDirectives * * @return void */ - public function __construct(BladeMinifier $blade, Filesystem $files, $cachePath, $ignoredPaths = []) - { + public function __construct( + BladeMinifier $blade, + Filesystem $files, + $cachePath, + $ignoredPaths = [], + $previousCompiler = null, + $customDirectives = [] + ) { parent::__construct($files, $cachePath); $this->blade = $blade; $this->ignoredPaths = $ignoredPaths; $this->compilers[] = 'Minify'; + $this->previousCompiler = $previousCompiler; + + if (count($customDirectives)) { + foreach ($customDirectives as $key => $value) { + $this->directive($key, $value); + } + } } /** @@ -96,4 +115,26 @@ public function getBladeMinifier() { return $this->blade; } + + /** + * @return void + */ + public function initMinifyCompiler() + { + if ($this->previousCompiler === null) { + return; + } + + if (property_exists($this->previousCompiler, 'customDirectives')) { + foreach ($this->previousCompiler->customDirectives as $name => $handler) { + $this->directive($name, $handler); + } + } + + if (property_exists($this->previousCompiler, 'classComponentAliases')) { + foreach ($this->previousCompiler->classComponentAliases as $name => $handler) { + $this->component($handler, $name); + } + } + } } diff --git a/src/HTMLMinServiceProvider.php b/src/HTMLMinServiceProvider.php index aeee44e..7eed413 100644 --- a/src/HTMLMinServiceProvider.php +++ b/src/HTMLMinServiceProvider.php @@ -30,6 +30,11 @@ */ class HTMLMinServiceProvider extends ServiceProvider { + /** + * @var \Illuminate\View\Compilers\CompilerInterface|null + */ + protected $previousCompiler; + /** * Boot the service provider. * @@ -73,6 +78,7 @@ protected function enableBladeOptimisations() $app->view->getEngineResolver()->register('blade', function () use ($app) { $compiler = $app['htmlmin.compiler']; + $compiler->initMinifyCompiler(); return new CompilerEngine($compiler); }); @@ -163,13 +169,22 @@ protected function registerBladeMinifier() */ protected function registerMinifyCompiler() { + if (method_exists($this, 'callAfterResolving')) { + $this->callAfterResolving('view', function () { + $this->previousCompiler = $this->app->make('view') + ->getEngineResolver() + ->resolve('blade') + ->getCompiler(); + }); + } + $this->app->singleton('htmlmin.compiler', function (Container $app) { $blade = $app['htmlmin.blade']; $files = $app['files']; $storagePath = $app->config->get('view.compiled'); $ignoredPaths = $app->config->get('htmlmin.ignore', []); - return new MinifyCompiler($blade, $files, $storagePath, $ignoredPaths); + return new MinifyCompiler($blade, $files, $storagePath, $ignoredPaths, $this->previousCompiler, $this->previousCompiler->getCustomDirectives()); }); $this->app->alias('htmlmin.compiler', MinifyCompiler::class); diff --git a/src/Minifiers/JsMinifier.php b/src/Minifiers/JsMinifier.php index df30c7c..66ec879 100644 --- a/src/Minifiers/JsMinifier.php +++ b/src/Minifiers/JsMinifier.php @@ -12,7 +12,7 @@ namespace HTMLMin\HTMLMin\Minifiers; -use JSMin; +use JSMin\JSMin; /** * This is the js minifier class. diff --git a/tests/AbstractTestCase.php b/tests/AbstractTestCase.php index 2a1cd4d..cd75a82 100644 --- a/tests/AbstractTestCase.php +++ b/tests/AbstractTestCase.php @@ -29,7 +29,7 @@ abstract class AbstractTestCase extends AbstractPackageTestCase * * @return string */ - protected function getServiceProviderClass($app) + protected function getServiceProviderClass() { return HTMLMinServiceProvider::class; } diff --git a/tests/Functional/ComponentsTest.php b/tests/Functional/ComponentsTest.php new file mode 100644 index 0000000..2313fc3 --- /dev/null +++ b/tests/Functional/ComponentsTest.php @@ -0,0 +1,37 @@ +app->version(), '7.0', '<')) { + $this->markTestSkipped('Class components were released in Laravel version 7.0.0'); + } + + /** @var MinifyCompiler $minifyCompiler */ + $minifyCompiler = $this->app->make('view') + ->getEngineResolver() + ->resolve('blade') + ->getCompiler(); + + $this->assertArrayHasKey('test-component', $minifyCompiler->getClassComponentAliases()); + } +} + diff --git a/tests/Functional/DirectivesTest.php b/tests/Functional/DirectivesTest.php new file mode 100644 index 0000000..45f3369 --- /dev/null +++ b/tests/Functional/DirectivesTest.php @@ -0,0 +1,34 @@ +app->make('view') + ->getEngineResolver() + ->resolve('blade') + ->getCompiler(); + + $compilerMock = MinifyCompilerMock::newInstance(); + $this->assertArrayHasKey('test_directive', $compilerMock->getCompilerCustomDirectives($minifyCompiler)); + } +} diff --git a/tests/Functional/Provider/TestComponentsProvider.php b/tests/Functional/Provider/TestComponentsProvider.php new file mode 100644 index 0000000..f98d25f --- /dev/null +++ b/tests/Functional/Provider/TestComponentsProvider.php @@ -0,0 +1,28 @@ + true]); + } + + public function boot() + { + /** @var BladeCompiler $previousCompiler */ + $previousCompiler = $this->app->make('view') + ->getEngineResolver() + ->resolve('blade') + ->getCompiler(); + + if (version_compare($this->app->version(), '7.0', '>=')) { + $previousCompiler->component(ViewComponentMock::class, 'test-component'); + } + } +} diff --git a/tests/Functional/Provider/TestDirectivesProvider.php b/tests/Functional/Provider/TestDirectivesProvider.php new file mode 100644 index 0000000..e9b4896 --- /dev/null +++ b/tests/Functional/Provider/TestDirectivesProvider.php @@ -0,0 +1,29 @@ +app->make('view') + ->getEngineResolver() + ->resolve('blade') + ->getCompiler(); + + $previousCompiler->directive('test_directive', [$this, 'testDirective']); + } +} diff --git a/tests/Mock/MinifyCompilerMock.php b/tests/Mock/MinifyCompilerMock.php new file mode 100644 index 0000000..fd0eba8 --- /dev/null +++ b/tests/Mock/MinifyCompilerMock.php @@ -0,0 +1,29 @@ +customDirectives; + } +} diff --git a/tests/Mock/ViewComponentMock.php b/tests/Mock/ViewComponentMock.php new file mode 100644 index 0000000..6f98482 --- /dev/null +++ b/tests/Mock/ViewComponentMock.php @@ -0,0 +1,27 @@ +content = $content; + } + + public function render() + { + return $this->content; + } +}