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

Commit

Permalink
Merge pull request #50 from apility/release/1.0.11
Browse files Browse the repository at this point in the history
Release/1.0.11
  • Loading branch information
thomas-alrek authored Jun 17, 2019
2 parents 35d8bf1 + 4a653f0 commit 3b50bc2
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 40 deletions.
26 changes: 12 additions & 14 deletions src/NF.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Netflex\Site\Commerce;
use Netflex\Site\Search;
use Netflex\Site\JWT;
use PhpConsole\Handler;
use Netflex\Site\Console;

class NF
{
Expand All @@ -35,7 +35,10 @@ class NF
public static $config;
/** @var array[string]string */
public static $routes;
/** @var Handler */
/**
* @deprecated 1.0.11
* @var Console
* */
public static $console;
/** @var string */
public static $sitename;
Expand Down Expand Up @@ -83,7 +86,7 @@ public static function init($site = null, $branch = null, $path = [])
self::clearCache();
}

self::$console = self::startPhpConsole();
self::$console = Console::getInstance();
self::$site = new Site();

// Datastore for Netflex
Expand Down Expand Up @@ -165,17 +168,13 @@ public static function nfPath($file)
/**
* Instantiates a PHPConsole session
*
* @return PhpConsole
* @deprecated 1.0.11
* @return Console
*/
public static function startPhpConsole()
{
$console = null;
if (getenv('ENV') !== 'master') {
$console = Handler::getInstance();
$console->getConnector()->setSourcesBasePath($_SERVER['DOCUMENT_ROOT']);
$console->start();
}
return $console;
trigger_error('NF::startPhpConsole is deprecated', E_USER_DEPRECATED);
return new Console();
}

/**
Expand All @@ -186,9 +185,8 @@ public static function startPhpConsole()
*/
public static function debug($text, $label = null)
{
if (self::$console) {
self::$console->debug($text, $label);
}
$console = Console::getInstance();
$console->log($text, $label);
}

/**
Expand Down
128 changes: 119 additions & 9 deletions src/Netflex/Site/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,46 @@

use NF;
use Exception;
use ReflectionFunction;
use Phpfastcache\CacheManager;
use Phpfastcache\Drivers\Files\Config as FilesConfig;
use Phpfastcache\Drivers\Memcached\Config as MemcachedConfig;


class Cache
{
/** @var string */
private static $key;

private static $key;
/** @var \Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface */
private static $cache;

public function __construct() {

self::$key = NF::$sitename;

$config = null;
$driver = class_exists('Memcached') ? 'memcached' : 'files';

switch ($driver) {
case 'memcached':
$memcached_hostname = getenv("MEMCACHED_HOST") ? getenv("MEMCACHED_HOST") : '127.0.0.1';
$memcached_port = getenv("MEMCACHED_PORT") ? intval(getenv("MEMCACHED_PORT")) : 11211;
$memcached_hostname = getenv('MEMCACHED_HOST') ? getenv('MEMCACHED_HOST') : '127.0.0.1';
$memcached_port = getenv('MEMCACHED_PORT') ? intval(getenv('MEMCACHED_PORT')) : 11211;

$config = new MemcachedConfig([
'host' => $memcached_hostname,
'port' => $memcached_port,
]);

break;
case 'files':
if (!file_exists(NF::$cacheDir . 'cache/')) {
mkdir(NF::$cacheDir. 'cache/', 0755, true);
}

$config = new FilesConfig([
'path' => NF::$cacheDir . 'cache/'
]);

break;
default:
throw new Exception('Invalid Cache driver');
Expand All @@ -46,68 +51,173 @@ public function __construct() {
self::$cache = CacheManager::getInstance($driver, $config);
}

/**
* Creates a prefixed cache key
*
* @param string $key
* @return string
*/
public function getCacheKey($key) {
return md5(self::$key . $key);
}

/**
* This function is only an alias for fetch.
*
* @see fetch
* @param string $key
* @return mixed
*/
public function get ($key) {
return $this->fetch($key);
}

/**
* This function is only an alias for save.
*
* @see save
* @return void
* @return bool
*/
public function set($key, $value, $_=false, $ttl) {
return $this->save($key, $value, $ttl, null);
}

/**
* This function is only an alias for save.
*
* @see save
* @return bool
*/
public function add ($key, $value, $_ = false, $ttl) {
return $this->save($key, $value, $ttl, null);
}

/**
* Fetches item from cache by $key
*
* @param string $key
* @return mixed
*/
public function fetch($key) {
$item = self::$cache->getItem(self::getCacheKey($key));
$item = unserialize($item->get());
return $item;
}

/**
* Checks is $key exists in the cache
*
* @param string $key
* @return bool
*/
public function has($key) {
$item = self::$cache->getItem(self::getCacheKey($key));
return !is_null($item->get());
}

/**
* Stores an item in the cache
*
* @param string $key
* @param mixed $value
* @param int $ttl = 0
* @param string $tag = null
* @return bool
*/
public function save($key, $value, $ttl = 0, $tag = null) {
$value = serialize($value);
$item = self::$cache->getItem(self::getCacheKey($key));
$item->set($value)->expiresAfter($ttl);
self::$cache->save($item);
return self::$cache->save($item);
}

/**
* Stores an array of items in the cache
*
* @param array $items
* @return void
*/
public function saveMultiple(array $items) {
foreach($items as $item) {
$this->save($item['key'], $item['value'], $item['ttl'], $item['tag']);
}
}

/**
* Deletes the item from the cache
*
* @param string $key
* @return bool
*/
public function delete($key) {
$key = self::getCacheKey($key);
self::$cache->deleteItem($key);
return self::$cache->deleteItem($key);
}

/**
* Deletes an array of items from the cache
*
* @param array $keys
* @return void
*/
public function deleteMultiple(array $keys) {
foreach($keys as $key) {
$this->delete($key);
}
}

/**
* Deletes items by tag
*
* @param string|array $tag
* @return bool
*/
public function deleteTag($tag) {
self::$cache->deleteItemsByTags($tags);
if (is_array($tag)) {
return self::$cache->deleteItemsByTags($tag);
}

return self::$cache->deleteItemsByTag($tag);
}

/**
* Purges all items from the cache
*
* @return bool
*/
public function purge() {
self::$cache->clear();
return self::$cache->clear();
}

/**
* One line fetch or set cache function
*
* @param $key string Name of the cache key
* @param $ttl int Cache time to live
* @param $callback function A function that resolves the value if it is not already cached
*/
public static function resolve($key, $ttl = 3600, $callback) {
if (self::$cache->has($key)) {
return self::$cache->get($key);
}

$ttlValue = null;
$ttlFunction = function(int $value) use (&$ttlValue) {
$ttlValue = $value;
};

$args = (new ReflectionFunction($callback))->getNumberOfParameters();

if ($args == 1) {
$response = $callback($ttlFunction);
$ttlValue = is_int($ttlValue) ? max(1, $ttlValue) : NULL;
NF::debug($ttlValue, 'Cache->resolve(' . $key . ') dynamic duration');
} else {
$response = $callback();
}

self::$cache->set($key, $response, '_', $ttlValue ?? $ttl);

return self::$cache->get($key);
}
}
61 changes: 61 additions & 0 deletions src/Netflex/Site/Console.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Netflex\Site;

use PhpConsole\Handler;

class Console {
/** @var Handler */
private $handler;

/** @var static */
private static $instance;

protected function __construct()
{
if (getenv('ENV') !== 'master') {
$this->handler = Handler::getInstance();;
$this->handler->getConnector()->setSourcesBasePath($_SERVER['DOCUMENT_ROOT']);
$this->handler->start();
}
}

/**
* Instantiates a Console instance
*
* @return Console
*/
public static function getInstance () {
if (!self::$instance) {
self::$instance = new static;
}

return self::$instance;
}

/**
* Logs the text to console (if not in production)
*
* @param string $text
* @param string $labels One or more labels separated by .
* @return void
*/
public function log ($text, $labels = null) {
if ($this->handler) {
$this->handler->debug($text, $labels);
}
}

/**
* @deprecated 1.0.11
*
* @param string $text
* @param string $label
* @return void
*/
public function debug ($text, $label)
{
trigger_error('NF::$console->debug is deprecated', E_USER_DEPRECATED);
return $this->log($text, $label);
}
}
13 changes: 8 additions & 5 deletions src/Netflex/Site/Support/CaptchaV2.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,15 @@ public static function checkBox()
*/
public static function isValid()
{
if (array_key_exists('g-recaptcha-response', $_GET))
$response = $_GET['g-recaptcha-response'];
if (array_key_exists('g-recaptcha-response', $_POST))
$response = $_POST['g-recaptcha-response'];;
if (!isset($response))
$response = null;

if (array_key_exists('g-recaptcha-response', $_REQUEST)) {
$response = $_REQUEST['g-recaptcha-response'];
}

if (is_null($response)) {
throw new ResponseMissingException('g-recaptcha-response is missing from $_GET and $_POST');
}

$response = json_decode(NF::$capi->post('https://www.google.com/recaptcha/api/siteverify', [
'form_params' => [
Expand Down
Loading

0 comments on commit 3b50bc2

Please sign in to comment.