Skip to content

Instantly share code, notes, and snippets.

@gcphost
Last active March 8, 2022 18:04
Show Gist options
  • Select an option

  • Save gcphost/ce29cd837eabdcf50793bd76fb3fce47 to your computer and use it in GitHub Desktop.

Select an option

Save gcphost/ce29cd837eabdcf50793bd76fb3fce47 to your computer and use it in GitHub Desktop.
<?php
/**
* Created by Will Bowman, 2016, twitter.com/asked_io & asked.io.
*/
$Cacher = new Cacher;
$Cacher->add('js/analytics.js', 'http://www.google-analytics.com/analytics.js');
$Cacher->fetch()->withErrors();
/**
* Cache remote files to a local file.
*/
class Cacher
{
/**
* Files and Urls to save.
* @var array
*/
protected $scripts = [];
/**
* List of errors.
* @var array
*/
protected $errors = [];
/**
* File name.
* @var string
*/
protected $file;
/**
* Url.
* @var string
*/
protected $url;
/**
* Contents of a file
* @var string
*/
protected $contents;
/**
* Fetch urls and save to a file.
* @return self
*/
public function fetch()
{
foreach ($this->scripts as $file => $url) {
$this->setData($file, $url);
$this->save();
}
return $this;
}
/**
* Add file and url to fetch.
* @param string $file path/to/file.js
* @param string $url http://example.org/file.js
* @return self
*/
public function add($file, $url)
{
$this->scripts = array_merge($this->scripts, [$file => $url]);
return $this;
}
/**
* Get errors.
* @return array
*/
public function getErrors()
{
return $this->errors;
}
/**
* Check if there are errors.
* @return boolean
*/
public function hasErrors()
{
return !empty($this->errors);
}
/**
* Fetch with error reporting.
* @return boolean
*/
public function withErrors()
{
if (!$this->hasErrors()) {
return false;
}
foreach ($this->getErrors() as $error) {
echo sprintf('%s[91m %s ERROR: %s %s[0m%s', chr(27), strtoupper($error['type']), $error['file'], chr(27), PHP_EOL);
}
return true;
}
/**
* Set data to be parsed.
* @param string $file path/to/file.js
* @param string $url http://example.org/file.js
*/
private function setData($file, $url)
{
$this->file = $file;
$this->url = $url;
$this->contents = null;
}
/**
* Add an error.
* @param string $type type of error.
* @return void
*/
private function addError($type)
{
array_push($this->errors, [
'type' => $type ,
'file' => $this->file,
]);
}
/**
* Save a file.
* @return boolean
*/
private function save()
{
if (!$this->getContents()) {
$this->addError('get');
return false;
}
if (!$this->checkFolder()) {
$this->addError('folder');
return false;
}
if (!$this->putContents()) {
$this->addError('put');
return false;
}
return true;
}
/**
* Get contents of a file.
* @return boolean
*/
private function getContents()
{
try {
$this->contents = file_get_contents($this->url);
} catch (Exception $exception) {
return false;
}
return true;
}
/**
* Check if a folder exists and is writable.
* @return boolean
*/
private function checkFolder()
{
$path = pathinfo($this->file, PATHINFO_DIRNAME);
if (!is_dir($path) || !is_writable($path)) {
return false;
}
return true;
}
/**
* Save contents to a file.
* @return boolean
*/
private function putContents()
{
try {
file_put_contents($this->file, $this->contents);
} catch (Exception $exception) {
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment