Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions config/minify.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
| Default: true
|
*/
'enabled' => env('MINFY_ENABLED', true),
'enabled' => env('MINIFY_ENABLED', true),

/*
|--------------------------------------------------------------------------
Expand All @@ -41,7 +41,7 @@
| Default: false
|
*/
'assets_enabled' => env('MINFY_ASSETS_ENABLED', false),
'assets_enabled' => env('MINIFY_ASSETS_ENABLED', false),

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -85,6 +85,33 @@
*/
'obfuscate' => env('MINIFY_OBFUSCATE', true),

/*
|--------------------------------------------------------------------------
| Route Prefix
|--------------------------------------------------------------------------
|
| This option allows you to specify a route prefix for the minify route.
| For example, if the route prefix is set to "_minify", then the minify
| route will be accessible at http://example.com/_minify/{hash}
|
| Default: "_minify"
|
*/
'route_prefix' => env('MINIFY_ROUTE_PREFIX', '_minify'),

/*
|--------------------------------------------------------------------------
| Assets Caching
|--------------------------------------------------------------------------
|
| This option allows you to specify a cache lifetime for the minified
| assets. The default is 1 week. Set to false to disable caching.
|
| Default: 604800
|
*/
'cache_lifetime' => env('MINIFY_CACHE_LIFETIME', 604800),

/*
|--------------------------------------------------------------------------
| Ignore Routes
Expand Down
10 changes: 10 additions & 0 deletions src/Exceptions/InvalidMinifyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Fahlisaputra\Minify\Exceptions;

use Exception;

class InvalidMinifyException extends Exception
{
//
}
9 changes: 6 additions & 3 deletions src/Helpers.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
<?php

use Fahlisaputra\Minify\Exceptions\InvalidMinifyException;

function minify($file)
{
$path = resource_path($file);
$path = base_path('/assets/'.$file);
if (!file_exists($path)) {
throw new \Exception('File not found');
throw new InvalidMinifyException('File not found: '.$path);
}

// remove slash or backslash from the beginning of the file path
$file = ltrim($file, '/\\');

$path = '_minify/'.$file;
$prefix = config('minify.route_prefix', '_minify');
$path = '/' . $prefix . '/'.$file;

return $path;
}
24 changes: 23 additions & 1 deletion src/MinifyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Fahlisaputra\Minify\Controllers\HttpConnectionHandler;
use Illuminate\Support\Facades\Route as RouteFacade;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
use Fahlisaputra\Minify\Exceptions\InvalidMinifyException;

class MinifyServiceProvider extends BaseServiceProvider
{
Expand Down Expand Up @@ -37,6 +38,9 @@ protected function registerPublishables()
$this->publishes([
__DIR__.'/../config/minify.php' => config_path('minify.php'),
], 'config');

$this->createAssetDirectory();

}

public function registerConfig()
Expand All @@ -46,8 +50,26 @@ public function registerConfig()

public function registerRoutes()
{
RouteFacade::get('/_minify/{file?}', HttpConnectionHandler::class)
// get the route prefix from the config file
$prefix = config('minify.route_prefix', '_minify');

RouteFacade::get('/' . $prefix . '/{file?}', HttpConnectionHandler::class)
->where('file', '(.*)')
->name('minify.assets');
}

public function createAssetDirectory()
{
$path = base_path('/assets');
if (!file_exists($path)) {
mkdir($path, 0777, true);
}

$js = $path . '/js';
if (!file_exists($js)) {
mkdir($js, 0777, true);
}

file_put_contents($js . '/example.js', "console.log('Hello World!');");
}
}