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 #17 from CodeIncHQ/2.x-new-src
Browse files Browse the repository at this point in the history
v2.1.0
  • Loading branch information
Joan Fabrégat authored Oct 9, 2018
2 parents 64778f9 + e8a8af1 commit b79f312
Show file tree
Hide file tree
Showing 19 changed files with 1,056 additions and 238 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codeinc/assets-middleware",
"version": "2.0.5",
"version": "2.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 @@ -15,7 +15,8 @@
"codeinc/psr7-responses": "^2",
"matthiasmullie/minify": "^1.3",
"codeinc/media-types": "^1.0",
"enshrined/svg-sanitize": "^0.8.2"
"enshrined/svg-sanitize": "^0.8.2",
"codeinc/collection-interface": "^1.1"
},
"require-dev": {
"phpunit/phpunit": "^7",
Expand Down
75 changes: 75 additions & 0 deletions src/Assets/AssetInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
//
// +---------------------------------------------------------------------+
// | CODE INC. SOURCE CODE |
// +---------------------------------------------------------------------+
// | Copyright (c) 2018 - 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: 09/10/2018
// Project: AssetsMiddleware
//
declare(strict_types=1);
namespace CodeInc\AssetsMiddleware\Assets;
use Psr\Http\Message\StreamInterface;


/**
* Interface AssetInterface
*
* @package CodeInc\AssetsMiddleware
* @author Joan Fabrégat <[email protected]>
*/
interface AssetInterface
{
/**
* Returns the asset's filename.
*
* @return string
*/
public function getFilename():string;

/**
* Returns the asset's size or NULL if unknown.
*
* @return int|null
*/
public function getSize():?int;

/**
* Returns the last modification time or NULL if unknown.
*
* @return \DateTime|null
*/
public function getMTime():?\DateTime;

/**
* Verifies if the assets must be downloaded as an attachment.
*
* @return bool
*/
public function asAttachment():bool;

/**
* Returns the assets media type or NULL if unknown.
*
* @return string
*/
public function getMediaType():string;

/**
* Returns a stream to the assets interface.
*
* @return StreamInterface
*/
public function getContent():StreamInterface;
}
104 changes: 104 additions & 0 deletions src/Assets/LocalAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
//
// +---------------------------------------------------------------------+
// | CODE INC. SOURCE CODE |
// +---------------------------------------------------------------------+
// | Copyright (c) 2018 - 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: 09/10/2018
// Project: AssetsMiddleware
//
declare(strict_types=1);
namespace CodeInc\AssetsMiddleware\Assets;
use CodeInc\AssetsMiddleware\Exceptions\AssetReadException;
use function GuzzleHttp\Psr7\stream_for;
use Psr\Http\Message\StreamInterface;


/**
* Class LocalAsset
*
* @package CodeInc\AssetsMiddleware
* @author Joan Fabrégat <[email protected]>
*/
class LocalAsset extends StreamAsset
{
/**
* @var string
*/
private $path;

/**
* StreamAsset constructor.
*
* @param string $path
* @param null|string $filename
* @param bool $asAttachment
* @param null|string $mediaType
* @param int|null $size
* @throws \CodeInc\MediaTypes\Exceptions\MediaTypesException
* @throws AssetReadException
*/
public function __construct(string $path, ?string $filename = null, bool $asAttachment = false,
?string $mediaType = null, ?int $size = null)
{
$this->path = $path;
parent::__construct(
$this->getPathStream($path),
$filename ?? basename($path),
$this->readMTime($path),
$asAttachment,
$mediaType,
$size
);
}

/**
* Returns the local asset's path.
*
* @return string
*/
public function getPath():string
{
return $this->path;
}

/**
* Returns the last modification time for the given path.
*
* @param string $path
* @return \DateTime|null
*/
private function readMTime(string $path):?\DateTime
{
if (($mTime = filemtime($path)) !== false) {
return new \DateTime('@'.$mTime);
}
return null;
}

/**
* Returns the stream for the given path.
*
* @param string $path
* @return StreamInterface
* @throws AssetReadException
*/
private function getPathStream(string $path):StreamInterface
{
if (($f = fopen($path, 'r')) === false) {
throw new AssetReadException($path);
}
return stream_for($f);
}
}
145 changes: 145 additions & 0 deletions src/Assets/StreamAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php
//
// +---------------------------------------------------------------------+
// | CODE INC. SOURCE CODE |
// +---------------------------------------------------------------------+
// | Copyright (c) 2018 - 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: 09/10/2018
// Project: AssetsMiddleware
//
declare(strict_types=1);
namespace CodeInc\AssetsMiddleware\Assets;
use CodeInc\MediaTypes\MediaTypes;
use Psr\Http\Message\StreamInterface;


/**
* Class LocalAsset
*
* @package CodeInc\AssetsMiddleware
* @author Joan Fabrégat <[email protected]>
*/
class StreamAsset implements AssetInterface
{
/**
* @var null|string
*/
private $filename;

/**
* @var int|null
*/
private $size;

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

/**
* @var string
*/
private $mediaType;

/**
* @var \GuzzleHttp\Psr7\Stream
*/
private $content;

/**
* @var \DateTime
*/
private $mTime;

/**
* StreamAsset constructor.
*
* @param StreamInterface $stream
* @param null|string $filename
* @param \DateTime|null $mTime
* @param bool $asAttachment
* @param null|string $mediaType
* @param int|null $size
* @throws \CodeInc\MediaTypes\Exceptions\MediaTypesException
*/
public function __construct(StreamInterface $stream, string $filename, ?\DateTime $mTime = null,
bool $asAttachment = false, ?string $mediaType = null, ?int $size = null)
{
$this->filename = $filename;
$this->asAttachment = $asAttachment;
$this->mTime = $mTime;
$this->mediaType = $mediaType ?? MediaTypes::getFilenameMediaType($filename) ?? 'application/octet-stream';
$this->size = $size ?? $stream->getSize();
$this->content = $stream;
}

/**
* Returns the asset's filename.
*
* @return string
*/
public function getFilename():string
{
return $this->filename;
}

/**
* Returns the asset's size or NULL if unknown.
*
* @return int|null
*/
public function getSize():?int
{
return $this->size;
}

/**
* Verifies if the assets must be downloaded as an attachment.
*
* @return bool
*/
public function asAttachment():bool
{
return $this->asAttachment;
}

/**
* Returns the assets media type or NULL if unknown.
*
* @return string
*/
public function getMediaType():string
{
return $this->mediaType;
}

/**
* Returns a stream to the assets interface.
*
* @return StreamInterface
*/
public function getContent():StreamInterface
{
return $this->content;
}

/**
* @inheritdoc
* @return \DateTime|null
*/
public function getMTime():?\DateTime
{
return $this->mTime;
}
}
Loading

0 comments on commit b79f312

Please sign in to comment.