Skip to content

Commit

Permalink
response 304, load resources once
Browse files Browse the repository at this point in the history
  • Loading branch information
Recca Tsai committed Feb 21, 2016
1 parent 2c0f197 commit e76eeff
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 47 deletions.
33 changes: 3 additions & 30 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,8 @@ php:
- 7.0
- hhvm

env:
global:
- setup=basic
- coverage=no
sudo: false

before_script:
- travis_retry composer self-update
- composer config discard-changes true
- if [[ $setup = 'basic' ]]; then travis_retry composer install --prefer-source --no-interaction; fi
- if [[ $setup = 'stable' ]]; then travis_retry composer update --prefer-source --no-interaction --prefer-stable; fi
- if [[ $setup = 'lowest' ]]; then travis_retry composer update --prefer-source --no-interaction --prefer-lowest --prefer-stable; fi
- if [[ $setup = 'coveralls' ]]; then travis_retry composer require "psr/log=1.0.0" "satooshi/php-coveralls=~0.6" "symfony/yaml=~2.0" --prefer-source --no-interaction --dev; fi
install: travis_retry composer install --no-interaction --prefer-source

script:
- if [[ $coverage = 'yes' ]]; then ./vendor/bin/phpunit -c phpunit.xml --coverage-clover build/logs/clover.xml; fi
- if [[ $coverage = 'no' ]]; then ./vendor/bin/phpunit -c phpunit.xml; fi

after_script:
- if [[ $setup = 'coveralls' ]]; then php vendor/bin/coveralls -v; fi

matrix:
include:
- php: 5.5
env: setup=lowest
- php: 5.5
env: setup=stable
- php: 5.5
env: setup=coveralls coverage=yes
allow_failures:
- env: setup=stable
- env: setup=coveralls coverage=yes
fast_finish: true
script: vendor/bin/phpunit --verbose
32 changes: 22 additions & 10 deletions resources/views/panel.blade.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<textarea id="panel-editor" style="display: none;"></textarea>
<div id="panel-shell"></div>


<script>
(function() {
var loadStyle = function(filename) {
var loadStyle = function(id, filename) {
id = "laravel-terminal-"+id;
if (document.getElementById(id)) {
return;
}
var link = document.createElement('link');
link.setAttribute("rel", "stylesheet")
link.setAttribute("type", "text/css")
link.setAttribute("href", filename)
link.setAttribute("rel", "stylesheet");
link.setAttribute("type", "text/css");
link.setAttribute("href", filename);
link.setAttribute("id", id)
var head = document.getElementsByTagName('head');
if (head.length > 0) {
Expand All @@ -18,10 +22,18 @@
}
}
var loadScript = function(filename, callback) {
var loadScript = function(id, filename, callback) {
id = "laravel-terminal-"+id;
if (document.getElementById(id)) {
if (callback) {
callback();
}
return;
}
var js = document.createElement('script');
js.setAttribute("type","text/javascript");
js.setAttribute("src", filename);
js.setAttribute("id", id);
if (callback) {
js.onload = callback;
}
Expand All @@ -33,16 +45,16 @@
}
}
loadStyle("{{ action('\Recca0120\Terminal\Http\Controllers\TerminalController@media', ['file' => 'css/app.css']) }}");
loadStyle('css', "{{ action('\Recca0120\Terminal\Http\Controllers\TerminalController@media', ['file' => 'css/app.css']) }}");
var scripts = {
jquery: "{{ action('\Recca0120\Terminal\Http\Controllers\TerminalController@media', ['file' => 'js/jquery.min.js']) }}",
terminal: "{{ action('\Recca0120\Terminal\Http\Controllers\TerminalController@media', ['file' => 'js/terminal.js']) }}",
app: "{{ action('\Recca0120\Terminal\Http\Controllers\TerminalController@media', ['file' => 'js/app.js']) }}"
};
var callback = function () {
loadScript(scripts.terminal, function () {
loadScript(scripts.app, function() {
loadScript('terminal' ,scripts.terminal, function () {
loadScript('app', scripts.app, function() {
new Term("#panel-shell", $.extend({!! $options !!}, {
editor: "#panel-editor"
}));
Expand All @@ -51,7 +63,7 @@
}
if (!window.jQuery) {
loadScript(scripts.jquery, callback);
loadScript('jquery', scripts.jquery, callback);
} else {
callback();
}
Expand Down
39 changes: 32 additions & 7 deletions src/Http/Controllers/TerminalController.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,40 @@ public function endPoint(Request $request)
]);
}

public function media(Filesystem $filesystem, $file)
/**
* media.
*
* @param \Illuminate\Filesystem\Filesystem $filesystem
* @param \Illuminate\Http\Request $request
* @param string $file
*
* @return \Illuminate\Http\Response
*/
public function media(Filesystem $filesystem, Request $request, $file)
{
$mimeType = strpos($file, '.css') !== false ? 'text/css' : 'application/javascript';
$filename = __DIR__.'/../../../public/'.$file;

return response($filesystem->get($filename), 200, [
$mimeType = strpos($filename, '.css') !== false ? 'text/css' : 'application/javascript';
$lastModified = $filesystem->lastModified($filename);
$eTag = sha1_file($filename);
$headers = [
'content-type' => $mimeType,
'last-modified' => date('D, d M Y H:i:s ', $filesystem->lastModified($filename)).'GMT',
])
->setEtag(sha1_file($filename));
'last-modified' => date('D, d M Y H:i:s ', $lastModified).'GMT',
];

if (@strtotime($request->server('HTTP_IF_MODIFIED_SINCE')) === $lastModified ||
trim($request->server('HTTP_IF_NONE_MATCH'), '"') === $eTag
) {
$response = response(null, 304, $headers);
} else {
$response = response()->stream(function () use ($filename) {
$out = fopen('php://output', 'wb');
$file = fopen($filename, 'rb');
stream_copy_to_stream($file, $out, filesize($filename));
fclose($out);
fclose($file);
}, 200, $headers);
}

return $response->setEtag($eTag);
}
}

0 comments on commit e76eeff

Please sign in to comment.