Skip to content
This repository has been archived by the owner on Feb 5, 2019. It is now read-only.

Commit

Permalink
Merge pull request #3 from CodeIncHQ/1.x
Browse files Browse the repository at this point in the history
v1.1.0
  • Loading branch information
Joan Fabrégat authored Jul 3, 2018
2 parents f618b6e + 8571f19 commit 26c3e6d
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 25 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codeinc/assets-middleware",
"version": "1.0.1",
"version": "1.1.0",
"description": "A PSR-15 middleware to server static assets (CSS, JS, images, etc.)",
"homepage": "https://github.com/CodeIncHQ/AssetsMiddleware",
"type": "library",
Expand All @@ -11,7 +11,9 @@
"psr/http-server-middleware": "^1.0",
"psr/http-server-handler": "^1.0",
"micheh/psr7-cache": "^0.5.0",
"codeinc/psr7-responses": "^1.2"
"codeinc/psr7-responses": "^1.2",
"matthiasmullie/minify": "^1.3",
"codeinc/media-types": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^7",
Expand Down
107 changes: 107 additions & 0 deletions src/Assets/AssetMinifiedResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
//
// +---------------------------------------------------------------------+
// | CODE INC. SOURCE CODE |
// +---------------------------------------------------------------------+
// | Copyright (c) 2017 - Code Inc. SAS - All Rights Reserved. |
// | Visit https://www.codeinc.fr for more information about licensing. |
// +---------------------------------------------------------------------+
// | NOTICE: All information contained herein is, and remains the |
// | property of Code Inc. SAS. The intellectual and technical concepts |
// | contained herein are proprietary to Code Inc. SAS are protected by |
// | trade secret or copyright law. Dissemination of this information or |
// | reproduction of this material is strictly forbidden unless prior |
// | written permission is obtained from Code Inc. SAS. |
// +---------------------------------------------------------------------+
//
// Author: Joan Fabrégat <[email protected]>
// Date: 03/05/2018
// Time: 16:30
// Project: AssetsMiddleware
//
declare(strict_types=1);
namespace CodeInc\AssetsMiddleware\Assets;
use CodeInc\MediaTypes\MediaTypes;
use CodeInc\Psr7Responses\StreamResponse;
use function GuzzleHttp\Psr7\stream_for;
use MatthiasMullie\Minify;


/**
* Class AssetMinifiedResponse
*
* @uses Minify\CSS
* @uses Minify\JS
* @package CodeInc\AssetsMiddleware\Assets
* @author Joan Fabrégat <[email protected]>
*/
class AssetMinifiedResponse extends StreamResponse implements AssetResponseInterface
{
/**
* @var string
*/
private $assetName;

/**
* AssetResponse constructor.
*
* @param string $filePath
* @param string $assetName
* @param null|string $fileName
* @param null|string $mimeType
* @param bool $asAttachment
* @param int $status
* @param array $headers
* @param string $version
* @param null|string $reason
* @throws \CodeInc\MediaTypes\Exceptions\MediaTypesException
*/
public function __construct(string $filePath, string $assetName, ?string $fileName = null, ?string $mimeType = null,
bool $asAttachment = false, int $status = 200, array $headers = [],
string $version = '1.1', ?string $reason = null)
{
$this->assetName = $assetName;

if (!$fileName) {
$fileName = basename($assetName);
}
if (!$mimeType) {
$mimeType = MediaTypes::getFilenameMediaType($fileName);
}

switch ($mimeType) {
case 'text/css':
$stream = stream_for((new Minify\CSS($filePath))->minify());
break;

case 'text/javascript':
$stream = stream_for((new Minify\JS($filePath))->minify());
break;

default:
$stream = stream_for($filePath);
break;
}

parent::__construct(
$stream,
$mimeType,
null,
$fileName,
$asAttachment,
$status,
$headers,
$version,
$reason
);
}

/**
* @inheritdoc
* @return string
*/
public function getAssetName():string
{
return $this->assetName;
}
}
4 changes: 2 additions & 2 deletions src/Assets/AssetResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class AssetResponse extends FileResponse implements AssetResponseInterface
* @throws \CodeInc\MediaTypes\Exceptions\MediaTypesException
* @throws \CodeInc\Psr7Responses\ResponseException
*/
public function __construct(string $filePath, string $assetName, ?string $fileName = null, ?string $mimeType = null,
bool $asAttachment = false, int $status = 200, array $headers = [],
public function __construct(string $filePath, string $assetName, ?string $fileName = null,
?string $mimeType = null, bool $asAttachment = false, int $status = 200, array $headers = [],
string $version = '1.1', ?string $reason = null)
{
$this->assetName = $assetName;
Expand Down
76 changes: 55 additions & 21 deletions src/AssetsMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
//
declare(strict_types = 1);
namespace CodeInc\AssetsMiddleware;
use CodeInc\AssetsMiddleware\Assets\AssetMinifiedResponse;
use CodeInc\AssetsMiddleware\Assets\AssetNotModifiedResponse;
use CodeInc\AssetsMiddleware\Assets\AssetResponse;
use CodeInc\AssetsMiddleware\Test\AssetsMiddlewareTest;
Expand Down Expand Up @@ -55,16 +56,24 @@ class AssetsMiddleware implements MiddlewareInterface
/**
* @var bool
*/
private $allowAssetsCache = true;
private $allowAssetsCache;

/**
* @var bool
*/
private $minifyJsAndCss;

/**
* AssetsMiddleware constructor.
*
* @param string $assetsLocalPath
* @param string $assetsUriPath
* @param bool $allowAssetsCache
* @param bool $minifyJsAndCss
* @throws AssetsMiddlewareException
*/
public function __construct(string $assetsLocalPath, string $assetsUriPath)
public function __construct(string $assetsLocalPath, string $assetsUriPath,
bool $allowAssetsCache = true, bool $minifyJsAndCss = false)
{
if (!is_dir($assetsLocalPath) || ($assetsLocalPath = realpath($assetsLocalPath)) === null) {
throw new AssetsMiddlewareException(
Expand All @@ -74,22 +83,8 @@ public function __construct(string $assetsLocalPath, string $assetsUriPath)
}
$this->assetsLocalPath = $assetsLocalPath;
$this->assetsUriPath = $assetsUriPath;
}

/**
* Enables the assets cache (enabled by default).
*/
public function enableAssetsCache():void
{
$this->allowAssetsCache = false;
}

/**
* Disables the assets cache (enabled by default).
*/
public function disableAssetsCache():void
{
$this->allowAssetsCache = false;
$this->allowAssetsCache = $allowAssetsCache;
$this->minifyJsAndCss = $minifyJsAndCss;
}

/**
Expand All @@ -106,8 +101,16 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
if (($assetName = $this->getAssetName($request)) !== null) {
$assetPath = $this->getAssetPath($assetName);
if (file_exists($assetPath)) {
$response = new AssetResponse($assetPath, $assetName);

// builds the response
if (!$this->minifyJsAndCss) {
$response = new AssetResponse($assetPath, $assetName);
}
else {
$response = new AssetMinifiedResponse($assetPath, $assetName);
}

// enables the cache
if ($this->allowAssetsCache) {
$assetMTime = filemtime($assetPath);
$cache = new CacheUtil();
Expand All @@ -123,11 +126,42 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
}
}


// retruns the handler respnse
// returns the handler response
return $handler->handle($request);
}

/**
* @return string
*/
public function getAssetsLocalPath():string
{
return $this->assetsLocalPath;
}

/**
* @return string
*/
public function getAssetsUriPath():string
{
return $this->assetsUriPath;
}

/**
* Enables the assets cache (enabled by default).
*/
public function enableAssetsCache():void
{
$this->allowAssetsCache = false;
}

/**
* Disables the assets cache (enabled by default).
*/
public function disableAssetsCache():void
{
$this->allowAssetsCache = false;
}

/**
* Returns an asset's name from a request or null if the request does'nt points toward an asset.
*
Expand Down

0 comments on commit 26c3e6d

Please sign in to comment.