Skip to content

Commit

Permalink
Update CRUtils.php
Browse files Browse the repository at this point in the history
  • Loading branch information
MrFiregore authored May 31, 2018
1 parent bb5b878 commit 72fde8c
Showing 1 changed file with 89 additions and 72 deletions.
161 changes: 89 additions & 72 deletions src/CRUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,98 +13,115 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

namespace CR;
use ReflectionMethod;
use ReflectionFunction;
use CR\CRCache;
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use CR\HttpClients\GuzzleHttpClient;


/**
*
*/
class CRUtils
class CRVersion
{
protected static $composer;
protected static $root;

/**
* @return \Composer\Autoload\ClassLoader|false
*/
public static function getComposer(){
if (self::$composer !== false){
$composer_path = self::getRoot()."vendor".DIRECTORY_SEPARATOR."autoload.php";
self::$composer = (file_exists($composer_path)) ? require $composer_path : false;
}
return self::$composer;
}
const API_VERSION = "1.3.1";
/**
* @var GuzzleHttpClient HTTP Client
*/
protected static $httpClientHandler;



/**
* @return string
*/
public static function getRoot(){
if (!self::$root){
self::$root = substr(__DIR__,0,strpos(__DIR__,"vendor") ?: strpos(__DIR__,"src"));
}
return self::$root;
}
public static function delTree($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (is_dir($dir."/".$object))
self::delTree($dir."/".$object);
else
unlink($dir."/".$object);
}
}
rmdir($dir);
}
}
/**
* Check if the given string is a HTML page
* @method isHTMLPage
* @param string $string The string to check
* @return bool Returns true if is a HTML page, otherwise returns false
* Returns the HTTP client handler.
*
* @return GuzzleHttpClient
*/
public static function isHTMLPage($string)
protected static function getHttpClientHandler()
{
return preg_match('/<html.*>/', $string) != 0;
if (is_null(self::$httpClientHandler)) {
$client = new Client();

self::$httpClientHandler = new GuzzleHttpClient($client);
}
return self::$httpClientHandler;
}
public static function getFunctionDefaultValues()
/**
* [checkGithub description]
* @param string $version [description]
* @return [type] [description]
*/
public static function checkGithub(string $version)
{
$values = [];
$debug_backtrace = debug_backtrace();

if (!isset($debug_backtrace[1])) {
return $values;
}
$rawResponse = self::getHttpClientHandler()->send(
"https://api.github.com/repos/firegore2/clash-royale-php/releases/tags/".$version,
"GET",
[RequestOptions::HEADERS=>["Accept: application/vnd.github.v3+json"]],
30,
false,
60
);
return $rawResponse->getStatusCode() == 200 ? $rawResponse->getBody()->getContents() : false;
}
/**
* [checkPackagist description]
* @return [type] [description]
*/
public static function checkPackagist()
{

$caller = $debug_backtrace[1];
$type = (isset($caller['class']) ? "Method" : "Function");
$func_name = ($type === "Method") ? $caller['class']."::".$caller['function'] : $caller['function'];
$rawResponse = self::getHttpClientHandler()->send(
"https://packagist.org/p/firegore2/clash-royale-php.json",
"GET",
[],
30,
false,
60
);
return $rawResponse->getStatusCode() == 200 ? $rawResponse->getBody()->getContents() : false;
}

/**
* [checkVersion description]
* @return [type] [description]
*/
public static function checkVersion()
{
if (!CRCache::exists("APIVERSION".self::API_VERSION) || version_compare(self::API_VERSION,CRCache::get("APIVERSION".self::API_VERSION),">")) {
CRUtils::delTree(CRCache::getPath());
CRCache::write("APIVERSION".self::API_VERSION,self::API_VERSION);
}

$reflection_type = "Reflection".$type;
$reflection = new $reflection_type($func_name);
if (!CRCache::exists("checkVersion", ["maxage"=>60])) {
CRCache::write("checkVersion","1");
if ($packagist = self::checkPackagist()) {
$packagist = collect(json_decode($packagist,true)['packages']['firegore2/clash-royale-php'])
->reject(function ($item,$key)
{
return strpos($key, "dev-") !== false ;
});

$max_version = $packagist->max("version");
d($max_version);
if (version_compare($max_version,self::API_VERSION,">")) {
$new_version = $packagist->get($max_version);
unset($packagist);
$alert = "New version ** $max_version ** available of ** ".$new_version['name']." ** - ".$new_version['description']."\n";

foreach ($reflection->getParameters() as $param) {
if ($github = self::checkGithub($max_version)) {
$github = json_decode($github,true);
$alert .= "###".$github['name']."\n ---\n".$github['body']." \n ---\n";
}
$alert .= "Run `composer update firegore2/clash-royale-php` to update the package.\n";
echo CRUtils::markdownToHTML($alert);
}
}
}

$arg_sent = isset($caller["args"][$param->getPosition()]) && !is_null($caller["args"][$param->getPosition()]) ?
$caller["args"][$param->getPosition()] :
(
($param->isOptional() && $param->isDefaultValueAvailable()) ?
$param->getDefaultValue() : null
);

$values[$param->getName()] = $arg_sent;
}

}

return $values;

}
public static function isAssoc(array $arr)
{
if([] === $arr) return false;
return array_keys($arr) !== range(0, count($arr) - 1 );
}
}

0 comments on commit 72fde8c

Please sign in to comment.