Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Commit

Permalink
1.1.1: added Browser
Browse files Browse the repository at this point in the history
  • Loading branch information
vl committed Jun 5, 2018
1 parent 6d08f71 commit e4b56cf
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "playtini/console-pack",
"version": "1.0.1",
"version": "1.1.1",
"description": "Playtini Console Pack",
"type": "library",
"license": "proprietary",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions src/Browser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Playtini\ConsolePack;

use Psr\Cache\InvalidArgumentException;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

class Browser
{
/** @var null */
private $cacheDirectory = null;

public function __construct(string $cacheDirectory = null)
{
$this->cacheDirectory = $cacheDirectory;
}

public function get(string $url): ?string
{
$cache = new FilesystemAdapter('browser', 86400 * 7, __DIR__ . '/../../var/cache');

$result = null;
try {
$cacheItem = $cache->getItem(md5($url));
} catch (InvalidArgumentException $e) {
return null;
}
$result = $cacheItem->get();
if ($result === null) {
$result = $this->doGet($url);
$cacheItem->set($result);
$cache->save($cacheItem);
}
if ($result === false || $result === null) {
$result = null;
}

return $result;
}

/**
* @param string $url
* @return bool|string
*/
private function doGet(string $url)
{
try {
$result = file_get_contents($url);
} catch (\Exception $e) {
return null;
}

return $result;
}
}

0 comments on commit e4b56cf

Please sign in to comment.