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 #14 from CodeIncHQ/2.x
Browse files Browse the repository at this point in the history
v2.0.3
  • Loading branch information
Joan Fabrégat authored Oct 8, 2018
2 parents 2ca017d + d0dacbb commit 1445911
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codeinc/assets-middleware",
"version": "2.0.2",
"version": "2.0.3",
"description": "A PSR-15 middleware to server static assets (CSS, JS, images, etc.)",
"homepage": "https://github.com/CodeIncHQ/AssetsMiddleware",
"type": "library",
Expand Down
23 changes: 21 additions & 2 deletions src/AssetsMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,27 @@ public function getAssetUri(string $assetPath):?string
}
foreach ($this->getAssetsDirectories() as $directoryKey => $directoryPath) {
if (substr($assetPath, 0, strlen($directoryPath)) == $directoryPath) {
return $this->assetsUriPrefix.urlencode($directoryKey)
.str_replace('\\', '/', substr($assetPath, strlen($directoryPath)));
return $this->assetsUriPrefix.urlencode($directoryKey).'/'
.str_replace('\\', '/', substr($assetPath, strlen($directoryPath) + 1));
}
}
return null;
}

/**
* Returns the URI of an assets directory.
*
* @param string $dirPath
* @return null|string
*/
public function getAssetsDirectoryUri(string $dirPath):?string
{
if (($realDirPath = realpath($dirPath)) === false) {
throw new InvalidAssetPathException($dirPath);
}
foreach ($this->getAssetsDirectories() as $directoryKey => $directoryPath) {
if ($realDirPath = $directoryPath) {
return $this->assetsUriPrefix.urlencode($directoryKey).'/';
}
}
return null;
Expand Down
63 changes: 63 additions & 0 deletions src/Exceptions/InvalidAssetDirectoryPathException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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: 28/09/2018
// Project: AssetsMiddleware
//
declare(strict_types=1);
namespace CodeInc\AssetsMiddleware\Exceptions;
use Throwable;


/**
* Class InvalidAssetDirectoryPathException
*
* @package CodeInc\AssetsMiddleware\Exceptions
* @author Joan Fabrégat <[email protected]>
*/
class InvalidAssetDirectoryPathException extends \RuntimeException implements AssetsMiddlewareException
{
/**
* @var string
*/
private $dirPath;

/**
* InvalidAssetDirectoryPathException constructor.
*
* @param string $dirPath
* @param int $code
* @param Throwable|null $previous
*/
public function __construct(string $dirPath, int $code = 0, Throwable $previous = null)
{
$this->dirPath = $dirPath;
parent::__construct(
sprintf("The assets directory path '%s' is not valid.", $dirPath),
$code,
$previous
);
}

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

0 comments on commit 1445911

Please sign in to comment.