This repository has been archived by the owner on Feb 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from CodeIncHQ/2.x-new-src
v2.1.0
- Loading branch information
Showing
19 changed files
with
1,056 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.