This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
vl
committed
Jun 5, 2018
1 parent
6d08f71
commit e4b56cf
Showing
3 changed files
with
57 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |