Permalink
Browse files

Merge branch 'master' into Bootstrap337

  • Loading branch information...
Art4 committed Jul 28, 2017
1 parent b5ed1aa commit fcb185c8bb1f78cc9e7c35c3bd6e982244b015b6
View
@@ -1,6 +1,6 @@
/Deciphers.log
/playerscript/*
/config/custom.php
/vendor
cache/*
composer.phar
composer.lock
config/custom.php
Deciphers.log
vendor
View
@@ -74,6 +74,13 @@ function($custom = 'custom')
// Create Toolkit
$container->set('toolkit', new \YoutubeDownloader\Toolkit);
// Create Cache
$cache = \YoutubeDownloader\Cache\FileCache::createFromDirectory(
__DIR__ . DIRECTORY_SEPARATOR . 'cache'
);
$container->set('cache', $cache);
return $container;
},
[getenv('CONFIG_ENV') ?: 'custom']
File renamed without changes.
View
@@ -22,7 +22,8 @@
"require-dev": {
"mikey179/vfsStream": "^1.6",
"phpunit/phpunit": "^4.8.35 || ^6.0",
"psr/container": "^1.0"
"psr/container": "^1.0",
"psr/simple-cache": "^1.0"
},
"autoload": {
"psr-4": {
@@ -67,6 +67,7 @@ public function execute()
$video_info_url = 'https://www.youtube.com/get_video_info?&video_id=' . $url. '&asv=3&el=detailpage&hl=en_US';
$video_info_string = $toolkit->curlGet($video_info_url, $config);
$video_info = \YoutubeDownloader\VideoInfo::createFromStringWithConfig($video_info_string, $config);
$video_info->setCache($this->get('cache'));
try
{
@@ -61,6 +61,7 @@ public function execute()
/* TODO: Check return from curl for status code */
$video_info = \YoutubeDownloader\VideoInfo::createFromStringWithConfig($video_info_string, $config);
$video_info->setCache($this->get('cache'));
if ($video_info->getStatus() == 'fail')
{
View
@@ -0,0 +1,53 @@
<?php
namespace YoutubeDownloader\Cache;
/**
* Describes the interface of a container that exposes methods to read its entries.
*
* This interface is compatible with PSR-16 Psr\SimpleCache\CacheInterface
*/
interface Cache
{
/**
* Fetches a value from the cache.
*
* @param string $key The unique key of this item in the cache.
* @param mixed $default Default value to return if the key does not exist.
*
* @return mixed The value of the item from the cache, or $default in case of cache miss.
*
* @throws \Psr\SimpleCache\InvalidArgumentException
* MUST be thrown if the $key string is not a legal value.
*/
public function get($key, $default = null);
/**
* Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
*
* @param string $key The key of the item to store.
* @param mixed $value The value of the item to store, must be serializable.
* @param null|int|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
* the driver supports TTL then the library may set a default value
* for it or let the driver take care of that.
*
* @return bool True on success and false on failure.
*
* @throws \Psr\SimpleCache\InvalidArgumentException
* MUST be thrown if the $key string is not a legal value.
*/
public function set($key, $value, $ttl = null);
/**
* Delete an item from the cache by its unique key.
*
* @param string $key The unique cache key of the item to delete.
*
* @return bool True if the item was successfully removed. False if there was an error.
*
* @throws \Psr\SimpleCache\InvalidArgumentException
* MUST be thrown if the $key string is not a legal value.
*/
public function delete($key);
}
@@ -0,0 +1,14 @@
<?php
namespace YoutubeDownloader\Cache;
use Exception;
/**
* Base exception in a cache.
*
* This interface must be compatible with PSR-16 Psr\SimpleCache\CacheException
*/
class CacheException extends Exception
{
}
View
@@ -0,0 +1,240 @@
<?php
namespace YoutubeDownloader\Cache;
use DateTimeInterface;
/**
* Describes the interface of a container that exposes methods to read its entries.
*
* This interface must be compatible with PSR-16 Psr\SimpleCache\CacheInterface
*/
class FileCache implements Cache
{
/**
* Create the cache with a directory
*
* @param string $directory The cache root directory
* @param array $config some configuration
*
* @param CacheException If $directory not exists
* @param CacheException If $directory is not writable
*
* @return FileCache
*/
public static function createFromDirectory($directory, array $config = [])
{
$directory = rtrim(strval($directory), '/\\');
if ( ! file_exists($directory) )
{
throw new CacheException(
sprintf('cache directory "%s" does not exist.', $directory)
);
}
if ( ! is_dir($directory) )
{
throw new CacheException(
sprintf('cache directory "%s" is not a directory.', $directory)
);
}
if ( ! is_readable($directory) )
{
throw new CacheException(
sprintf('cache directory "%s" is not readable.', $directory)
);
}
if ( ! is_writable($directory) )
{
throw new CacheException(
sprintf('cache directory "%s" is not writable.', $directory)
);
}
return new self($directory, $config);
}
/**
* @var string
*/
private $root;
/**
* @var array
*/
private $config = [
'writeFlags' => LOCK_EX,
];
/**
* Fetches a value from the cache.
*
* @param string $directory The cache root directory
* @param array $config some configuration
*
* @return void
*/
public function __construct($directory, array $config = [])
{
if ( array_key_exists('writeFlags', $config) )
{
$this->config['writeFlags'] = $config['writeFlags'];
}
$this->root = rtrim(strval($directory), '/\\');
}
/**
* Fetches a value from the cache.
*
* @param string $key The unique key of this item in the cache.
* @param mixed $default Default value to return if the key does not exist.
*
* @return mixed The value of the item from the cache, or $default in case of cache miss.
*
* @throws InvalidArgumentException
* MUST be thrown if the $key string is not a legal value.
*/
public function get($key, $default = null)
{
$this->validateKey($key);
$location = $this->root . \DIRECTORY_SEPARATOR . $key;
$data = false;
if ( file_exists($location) )
{
$data = file_get_contents($location);
}
if ( $data === false )
{
return $default;
}
$data = @unserialize($data);
if ( $data === false )
{
return $default;
}
$expirationTimestamp = $data[1] ?: null;
if ($expirationTimestamp !== null && time() > $expirationTimestamp)
{
$this->delete($key);
return $default;
}
return $data[0];
}
/**
* Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
*
* @param string $key The key of the item to store.
* @param mixed $value The value of the item to store, must be serializable.
* @param null|int|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
* the driver supports TTL then the library may set a default value
* for it or let the driver take care of that.
*
* @return bool True on success and false on failure.
*
* @throws InvalidArgumentException
* MUST be thrown if the $key string is not a legal value.
*/
public function set($key, $value, $ttl = null)
{
$this->validateKey($key);
if ($ttl instanceof DateTimeInterface)
{
$expirationTimestamp = $ttl->getTimestamp();
}
elseif (is_int($ttl))
{
$expirationTimestamp = time() + $ttl;
}
elseif (null === $ttl)
{
$expirationTimestamp = $ttl;
}
else
{
throw new InvalidArgumentException('$ttl must be of type null, integer or \DateTimeInterface.');
}
$data = serialize([
$value,
$expirationTimestamp,
]);
$location = $this->root . \DIRECTORY_SEPARATOR . $key;
$size = file_put_contents($location, $data, $this->config['writeFlags']);
if ( $size === false )
{
return false;
}
return true;
}
/**
* Delete an item from the cache by its unique key.
*
* @param string $key The unique cache key of the item to delete.
*
* @return bool True if the item was successfully removed. False if there was an error.
*
* @throws InvalidArgumentException
* MUST be thrown if the $key string is not a legal value.
*/
public function delete($key)
{
$this->validateKey($key);
$location = $this->root . \DIRECTORY_SEPARATOR . $key;
return unlink($location);
}
/**
* @param string $key
*
* @throws InvalidArgumentException
*/
private function validateKey($key)
{
if ( ! is_string($key) )
{
throw new InvalidArgumentException(
sprintf('Cache key must be string, "%s" given', gettype($key))
);
}
if ( ! isset($key[0]) )
{
throw new InvalidArgumentException(
'Cache key cannot be an empty string'
);
}
if ( preg_match('~[^a-zA-Z0-9_\\-$]+~', $key) !== 0 )
{
throw new InvalidArgumentException(sprintf(
'Invalid key: "%s". The key contains one or more not allowed characters, allowed are only %s',
$key,
'`a-z`, `A-Z`, `0-9`, `_` and `-`'
));
}
}
}
@@ -0,0 +1,12 @@
<?php
namespace YoutubeDownloader\Cache;
/**
* invalid argument exception in a cache.
*
* This interface must be compatible with PSR-16 Psr\SimpleCache\InvalidArgumentException
*/
class InvalidArgumentException extends CacheException
{
}
Oops, something went wrong.

0 comments on commit fcb185c

Please sign in to comment.