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

Commit

Permalink
M erge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Kündig committed Feb 16, 2016
2 parents 1f3468e + 7f192a7 commit 1e69151
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 17 deletions.
29 changes: 20 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,34 @@ this location).

To cache a file use the `getCachedHtml` method. The file will be downloaded and stored to disk.
The method returns the local URL for your file.

```
$string = 'http://www.offlinegmbh.ch/file.jpg';
$string = 'http://www.offlinegmbh.ch/file.jpg';

// returns http://yoursite/cache/{hash}
var_dump(LocalCache::getCachedHtml($string));
// returns http://yoursite/cache/{hash}
var_dump(LocalCache::getCachedHtml($string));
```

By default, a `/cache/{hash}` route is generated which serves the file's contents with the correct mime type.
To change the route, edit the `route` setting in `config/localcache.php`.

The `getCachedHtml` method works with any string that contains any number of URLs. It extracts and replaces the links
accordingly.

$string = '<p>http://www.offlinegmbh.ch/file.jpg</p><p>http://www.offlinegmbh.ch/file2.jpg</p>';

// <p>http://yoursite/cache/{hash1}</p><p>http://yoursite/cache/{hash2}</p>
var_dump(LocalCache::getCachedHtml($string));
```php
$string = '<p>http://www.offlinegmbh.ch/file.jpg</p><p>http://www.offlinegmbh.ch/file2.jpg</p>';

// <p>http://yoursite/cache/{hash1}</p><p>http://yoursite/cache/{hash2}</p>
var_dump(LocalCache::getCachedHtml($string));
```

To prevent URLs from being cached, prefix them with a `@` symbol:

```php
$string = '<p>@http://dont-cache-me/file.jpg</p>';
// <p>http://dont-cache-me/file.jpg</p>
var_dump(LocalCache::getCachedHtml($string));
```
### Example Middleware

This example middleware caches all external files referenced in your template and replaces
Expand Down Expand Up @@ -107,4 +118,4 @@ And use it in your `routes.php` or controller.
'uses' => 'PageController@index',
'middleware' => ['localcache']
]);


7 changes: 7 additions & 0 deletions spec/Offline/LocalCache/ValueObjects/UrlSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class UrlSpec extends ObjectBehavior
{
const validURL = 'http://www.offlinegmbh.ch/file.jpg';
const invalidURL = 'http:/invalid/url';
const escapedUrl = '@http://escaped';

function let()
{
Expand All @@ -31,4 +32,10 @@ function it_returns_a_hash()
{
$this->toHash()->shouldReturn(md5(static::validURL));
}

function it_recognises_escaped_url()
{
$this->beConstructedWith(self::escapedUrl);
$this->__toString()->shouldBe("http://escaped");
}
}
5 changes: 5 additions & 0 deletions src/Offline/LocalCache/CacheObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ public function isValid()
*/
private function downloadRemoteFile()
{

if ($this->url->replace === false) {
return ['', true];
}

if ($this->isValid()) {
return [$this->getContents(), true];
}
Expand Down
6 changes: 4 additions & 2 deletions src/Offline/LocalCache/LocalCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class LocalCache
*
* @var string
*/
protected $urlRegEx = '/https?\:\\\?\/\\\?\/[^\"\'\<]+/i';
protected $urlRegEx = '/@?https?\:\\\?\/\\\?\/[^\"\'\<]+/i';
/**
* Maximum file size.
*
Expand Down Expand Up @@ -161,7 +161,9 @@ private function cleanUp()
{
foreach (glob($this->getCachePath() . '/*') as $file) {

if(strpos('mimeMap.json', $file) !== false) continue;
if (strpos('mimeMap.json', $file) !== false) {
continue;
}

if (time() - filemtime($file) >= $this->ttl->inSeconds()) {
@unlink($file);
Expand Down
24 changes: 18 additions & 6 deletions src/Offline/LocalCache/ValueObjects/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,35 @@ class Url
* @var bool
*/
public $escape;
/**
* Whether or not the URL has to be replaced.
*
* @var bool
*/
public $replace = true;

/**
* @param $url
*/
public function __construct($url)
{
if(strpos($url, '\/') !== false) {
if (strpos($url, '\/') !== false) {
$this->escape = true;
$url = str_replace('\/', '/', $url);
$url = str_replace('\/', '/', $url);
}

if ( ! filter_var($url, FILTER_VALIDATE_URL)) {
throw new InvalidArgumentException("${url} is not a valid url");
$checkUrl = $url;
if (starts_with($url, '@')) {
$checkUrl = substr($url, 1);
$this->replace = false;
}

if ( ! filter_var($checkUrl, FILTER_VALIDATE_URL)) {
throw new InvalidArgumentException("${checkUrl} is not a valid url");
}
$this->url = $url;
}

$this->url = $checkUrl;
}

/**
* @return string
Expand Down

0 comments on commit 1e69151

Please sign in to comment.