Skip to content
This repository has been archived by the owner on Apr 28, 2024. It is now read-only.

Partial caching support, backward compatible #9

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Open
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
53 changes: 0 additions & 53 deletions .appveyor.yml

This file was deleted.

36 changes: 36 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Continuous Integration
on:
# Every push
push:
schedule:
# Every Monday and Thursday at 12PM UTC
- cron: '0 12 * * 1,4'

jobs:
test:
strategy:
matrix:
os: [ ubuntu-latest, macos-latest, windows-latest ]
php-version: [ '7.1', '7.2', '7.3', '7.4' ]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
coverage: xdebug
extensions: mbstring
- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Retain composer cache
uses: actions/cache@v1
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-
- run: composer install
- run: vendor/bin/phpunit --coverage-clover=coverage.xml
# - uses: codecov/codecov-action@v1
# with:
# token: ${{ secrets.CODECOV_UPLOAD_TOKEN }}
21 changes: 0 additions & 21 deletions .travis.yml

This file was deleted.

3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unknown]
- Added partial caching support to development server

## [6.0.0] - 2019-06-22
- Dropping support for PHP 5.6 & 7.0
- Updated PHPUnit to 7.5
Expand Down
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,24 @@

| Branch | Build | Coverage | Style |
| ------ | ----- | -------- | ----- |
| [master][master] | [![Build Status][master-build-travis-badge]][travis] [![Windows Build][master-build-appveyor-badge]][appveyor] | [![codecov][master-codecov-badge]][codecov] | [![][master-styleci-badge]][styleci] |
| [develop][develop] | [![Build Status][develop-build-travis-badge]][travis] [![Windows Build][develop-build-appveyor-badge]][appveyor] | [![codecov][develop-codecov-badge]][codecov] | [![][develop-styleci-badge]][styleci] |
| [master][master] | [![Continuous Integration][master-gh-ci-badge]](master-gh-ci) | [![codecov][master-codecov-badge]][codecov] | [![][master-styleci-badge]][styleci] |
| [develop][develop] | [![Continuous Integration][develop-gh-ci-badge]](develop-gh-ci) | [![codecov][develop-codecov-badge]][codecov] | [![][develop-styleci-badge]][styleci] |

<!-- Links -->
[codecov]: https://codecov.io/gh/userfrosting/assets
[releases]: https://github.com/userfrosting/assets/releases
[styleci]: https://github.styleci.io/repos/55460230
[travis]: https://travis-ci.org/userfrosting/assets
[appveyor]: https://ci.appveyor.com/project/userfrosting/assets
[version-badge]: https://img.shields.io/github/release/userfrosting/assets.svg
[master]: https://github.com/userfrosting/assets/tree/master
[master-gh-ci]: https://github.com/userfrosting/assets/actions?query=branch:master+workflow:"Continuous+Integration"
[master-gh-ci-badge]: https://github.com/userfrosting/assets/workflows/Continuous%20Integration/badge.svg?branch=master
[master-styleci-badge]: https://github.styleci.io/repos/55460230/shield?branch=master&style=flat
[master-codecov-badge]: https://codecov.io/gh/userfrosting/assets/branch/master/graph/badge.svg
[master-build-travis-badge]: https://travis-ci.org/userfrosting/assets.svg?branch=master
[master-build-appveyor-badge]: https://ci.appveyor.com/api/projects/status/github/userfrosting/assets?branch=master&svg=true
[develop]: https://github.com/userfrosting/assets/tree/develop
[develop-gh-ci]: https://github.com/userfrosting/assets/actions?query=branch:develop+workflow:"Continuous+Integration"
[develop-gh-ci-badge]: https://github.com/userfrosting/assets/workflows/Continuous%20Integration/badge.svg?branch=develop
[develop-styleci-badge]: https://github.styleci.io/repos/55460230/shield?branch=develop&style=flat
[develop-codecov-badge]: https://codecov.io/gh/userfrosting/assets/branch/develop/graph/badge.svg
[develop-build-travis-badge]: https://travis-ci.org/userfrosting/assets.svg?branch=develop
[develop-build-appveyor-badge]: https://ci.appveyor.com/api/projects/status/github/userfrosting/assets?branch=develop&svg=true

**Assets** is a library originally created for UserFrosting 4 to make it much easier to reference frontend assets in both production and development contexts.

Expand Down
10 changes: 10 additions & 0 deletions src/AssetLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,14 @@ public function getType()
{
return MimeType::detectByFilename($this->fullPath);
}

/**
* Get the last modified date and time.
*
* @return \DateTime
*/
public function getLastModified(): \DateTime
{
return new \DateTime('@'.filemtime($this->fullPath));
}
}
2 changes: 1 addition & 1 deletion src/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Assets
/** @var ResourceLocatorInterface Resource locator used to find assets. */
protected $locator;

/** @var string Scheme used when finding assets via $locator. */
/** @var string Scheme used when finding assets via the provided locator. */
protected $locatorScheme;

/** @var string Sites base URL and optionally assets directory. */
Expand Down
21 changes: 18 additions & 3 deletions src/ServeAsset/SlimServeAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,24 @@ public function serveAsset(RequestInterface $request, ResponseInterface $respons
return $response->withStatus(404);
}

return $response
->withHeader('Content-Type', $assetLoader->getType())
// Generate file last modified
$lastModified = $assetLoader->getLastModified()->format('D, d M Y H:i:s \G\M\T');

// Return 304 if asset not modified
try {
$clientLastModified = $request->getHeader('If-Modified-Since')[0] ?? false;
if ($lastModified === $clientLastModified) {
return $response->withStatus(304);
}
} catch (\Exception $e) {
// Fallback to regular response
}

$response->getBody()->write($assetLoader->getContent());

return $response->withHeader('Content-Type', $assetLoader->getType())
->withHeader('Content-Length', $assetLoader->getLength())
->write($assetLoader->getContent());
->withHeader('Cache-Control', 'no-cache')
->withHeader('Last-Modified', $lastModified);
}
}
18 changes: 14 additions & 4 deletions tests/ServeAsset/SlimServeAssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ public function testAssetMatchesExpectations(SlimServeAsset $controller)
$response = new Response();

// Invoke controller method.
$response = $controller->serveAsset($request, $response, [
'url' => 'allowed.txt',
]);
$response = $controller->serveAsset($request, $response, ['url' => 'allowed.txt']);

// Assert 200 response
$this->assertSame($response->getStatusCode(), 200);
Expand All @@ -122,10 +120,22 @@ public function testAssetMatchesExpectations(SlimServeAsset $controller)

// Assert correct MIME
$this->assertSame($response->getHeader('Content-Type'), ['text/plain']);

// Asset last-modified set
$this->assertStringEndsWith('GMT', $response->getHeader('Last-Modified')[0] ?? false);

// Last modified support
$request = Request::createFromEnvironment($environment)
->withHeader('If-Modified-Since', $response->getHeader('Last-Modified')[0]);
$response = new Response();
$response = $controller->serveAsset($request, $response, ['url' => 'allowed.txt']);

// Assert 304 response
$this->assertSame($response->getStatusCode(), 304);
}

/**
* Test with existent asset.
* Test with existent asset of unknown type.
*
* @param SlimServeAsset $controller
*
Expand Down