first commit

This commit is contained in:
2024-07-15 11:28:08 +02:00
commit f52d538ea5
21891 changed files with 6161164 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
{
"name": "wpdesk\/wp-persistence",
"authors": [
{
"name": "Krzysiek",
"email": "krzysiek@wpdesk.pl"
}
],
"require": {
"php": ">=5.6"
},
"require-dev": {
"phpunit\/phpunit": "^5",
"wp-coding-standards\/wpcs": "^0.14.1",
"squizlabs\/php_codesniffer": "^3.0.2",
"wimg\/php-compatibility": "^8"
},
"autoload": {
"psr-4": {
"FSVendor\\WPDesk\\Persistence\\": "src\/"
}
},
"autoload-dev": {
"psr-4": {
"FSVendor\\WPDesk\\Persistence\\Tests\\Integration\\": "tests\/integration\/"
}
},
"scripts": {
"phpunit-unit": "phpunit --configuration phpunit-unit.xml --coverage-text --colors=never",
"phpunit-unit-fast": "phpunit --configuration phpunit-unit.xml --no-coverage",
"phpunit-integration": "phpunit --configuration phpunit-integration.xml --coverage-text --colors=never",
"phpunit-integration-fast": "phpunit --configuration phpunit-integration.xml --no-coverage"
}
}

View File

@@ -0,0 +1,12 @@
<?php
namespace FSVendor\WPDesk\Persistence;
/**
* Class ElementNotExistsException
*
* @package WPDesk\Persistence
*/
class ElementNotExistsException extends \RuntimeException
{
}

View File

@@ -0,0 +1,35 @@
<?php
namespace FSVendor\WPDesk\Persistence;
/**
* Class MemoryContainer
* @package WPDesk\Persistence
*/
class MemoryContainer implements \FSVendor\WPDesk\Persistence\PersistentContainer
{
private $array;
/**
* Persist value for key
*
* @param string $key
* @param mixed $value
*/
public function set($key, $value)
{
$this->array[$key] = $value;
}
/**
* Get persistent value for key
*
* @param string $key
* @return mixed
*/
public function get($key)
{
if (!isset($this->array[$key])) {
throw new \FSVendor\WPDesk\Persistence\ElementNotExistsException(\sprintf('Element %s not exists!', $key));
}
return $this->array[$key];
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace FSVendor\WPDesk\Persistence;
interface PersistentContainer
{
/**
* @param string $key
* @param mixed $value
* @return void
*/
public function set($key, $value);
/**
* @param string $key
* @return mixed
*/
public function get($key);
}

View File

@@ -0,0 +1,51 @@
<?php
namespace FSVendor\WPDesk\Persistence\Wordpress;
use FSVendor\WPDesk\Persistence\ElementNotExistsException;
use FSVendor\WPDesk\Persistence\PersistentContainer;
/**
* Class WordpressOptionsContainer
* @package WPDesk\Persistence\Wordpress
*/
class WordpressOptionsContainer implements \FSVendor\WPDesk\Persistence\PersistentContainer
{
const OPTION_PREFIX = 'saas-platform-client';
/**
* Set value.
*
* @param string $key Key.
* @param mixed $value Value.
*/
public function set($key, $value)
{
\update_option($this->prepareKeyName($key), $value);
}
/**
* Prepare transient name for key.
*
* @param string $key Key.
*
* @return string
*/
private function prepareKeyName($key)
{
return self::OPTION_PREFIX . '-' . $key;
}
/**
* Get value.
*
* @param string $key Key.
*
* @return mixed
* @throws ElementNotExistsException Element not found.
*/
public function get($key)
{
$value = \get_option($this->prepareKeyName($key));
if (\false === $value) {
throw new \FSVendor\WPDesk\Persistence\ElementNotExistsException(\sprintf('Element %s not exists!', $key));
}
return $value;
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace FSVendor\WPDesk\Persistence\Wordpress;
use FSVendor\WPDesk\Persistence\ElementNotExistsException;
use FSVendor\WPDesk\Persistence\PersistentContainer;
/**
* Class WordpressTransientContainer
* @package WPDesk\Persistence\Wordpress
*/
class WordpressTransientContainer implements \FSVendor\WPDesk\Persistence\PersistentContainer
{
const TRANSIENT_NAME = 'wsp_';
const TRANSIENT_TIMEOUT = 86400;
/**
* Set value.
*
* @param string $key Key.
* @param mixed $value Value.
*/
public function set($key, $value)
{
\set_transient($this->prepareTransientName($key), $value, self::TRANSIENT_TIMEOUT);
}
/**
* Prepare transient name for key.
*
* @param string $key Key.
*
* @return string
*/
private function prepareTransientName($key)
{
return self::TRANSIENT_NAME . \md5($key);
}
/**
* Get value.
*
* @param string $key Key.
*
* @return mixed
* @throws ElementNotExistsException Element not found.
*/
public function get($key)
{
$value = \get_transient($this->prepareTransientName($key));
if (\false === $value) {
throw new \FSVendor\WPDesk\Persistence\ElementNotExistsException(\sprintf('Element %s not exists!', $key));
}
return $value;
}
}