Skip to content

Commit 8d11adf

Browse files
authored
Merge pull request #6 from Flagsmith/feature/v2
Feature/v2
2 parents 95e0181 + 8ef112a commit 8d11adf

File tree

14 files changed

+1282
-87
lines changed

14 files changed

+1282
-87
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
vendor
2+
package-lock.json
3+
composer.lock
4+
/node_modules/

.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"trailingCommaPHP": true,
3+
"singleQuote": true,
4+
"useTabs": false,
5+
"braceStyle": "psr-2"
6+
}

.vscode/extensions.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"recommendations": [
3+
"felixfbecker.php-debug",
4+
"neilbrayfield.php-docblocker",
5+
"esbenp.prettier-vscode",
6+
"eamodio.gitlens",
7+
"bmewburn.vscode-intelephense-client",
8+
"ryanluker.vscode-coverage-gutters"
9+
]
10+
}

.vscode/settings.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
// I recommend to disable VS Code's built-in PHP IntelliSense by setting php.suggest.basic to false to avoid duplicate suggestions.
3+
"php.suggest.basic": false,
4+
"editor.formatOnSave": true,
5+
"[php]": {
6+
"editor.defaultFormatter": "esbenp.prettier-vscode"
7+
},
8+
"workbench.colorCustomizations": {},
9+
"taskExplorer.pathToMake": "make",
10+
"coverage-gutters.showLineCoverage": true,
11+
"coverage-gutters.showRulerCoverage": true
12+
}

composer.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,17 @@
88
"email": "[email protected]"
99
}
1010
],
11-
"require": {},
11+
"require": {
12+
"php": ">=7.4",
13+
"php-http/discovery": "^1.14",
14+
"psr/simple-cache": "^1.0",
15+
"psr/http-factory-implementation": "1.0",
16+
"psr/http-client-implementation": "1.0",
17+
"psr/http-message-implementation": "1.0"
18+
},
19+
"require-dev": {
20+
"guzzlehttp/guzzle": "^7.3"
21+
},
1222
"autoload": {
1323
"psr-4": {
1424
"Flagsmith\\": "src"

package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "@clearlyip/laravel-flagsmith",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"directories": {
6+
"test": "tests"
7+
},
8+
"private": true,
9+
"repository": {
10+
"type": "git",
11+
"url": "ssh://[email protected]:clearlyip/laravel-flagsmith.git"
12+
},
13+
"author": "",
14+
"license": "BSD 3-Clause",
15+
"dependencies": {
16+
"@prettier/plugin-php": "^0.14.3",
17+
"prettier": "^2.1.2"
18+
}
19+
}

src/Cache.php

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
namespace Flagsmith;
4+
5+
use Psr\SimpleCache\CacheInterface;
6+
7+
/**
8+
* This class is a wrapper for the PSR-16 cache interface.
9+
*
10+
* It exists as an easy way to allow use to set global Prefix and TTL.
11+
*/
12+
class Cache
13+
{
14+
private CacheInterface $cache;
15+
private string $prefix;
16+
private ?int $ttl = null;
17+
18+
public function __construct(
19+
CacheInterface $cache,
20+
string $prefix,
21+
?int $ttl = null
22+
) {
23+
$this->cache = $cache;
24+
$this->prefix = $prefix;
25+
$this->ttl = $ttl;
26+
}
27+
28+
/**
29+
* Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
30+
*
31+
* @param string $key The key of the item to store.
32+
* @param mixed $value The value of the item to store. Must be serializable.
33+
* @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
34+
* the driver supports TTL then the library may set a default value
35+
* for it or let the driver take care of that.
36+
*
37+
* @return bool True on success and false on failure.
38+
*
39+
* @throws \Psr\SimpleCache\InvalidArgumentException
40+
* MUST be thrown if the $key string is not a legal value.
41+
*/
42+
public function set(string $key, $value): bool
43+
{
44+
return $this->cache->set(
45+
$this->getKeyWithPrefix($key),
46+
$value,
47+
$this->ttl
48+
);
49+
}
50+
51+
/**
52+
* Determines whether an item is present in the cache.
53+
*
54+
* NOTE: It is recommended that has() is only to be used for cache warming type purposes
55+
* and not to be used within your live applications operations for get/set, as this method
56+
* is subject to a race condition where your has() will return true and immediately after,
57+
* another script can remove it, making the state of your app out of date.
58+
*
59+
* @param string $key The cache item key.
60+
*
61+
* @return bool
62+
*
63+
* @throws \Psr\SimpleCache\InvalidArgumentException
64+
* MUST be thrown if the $key string is not a legal value.
65+
*/
66+
public function has(string $key): bool
67+
{
68+
return $this->cache->has($this->getKeyWithPrefix($key));
69+
}
70+
71+
/**
72+
* Fetches a value from the cache.
73+
*
74+
* @param string $key The unique key of this item in the cache.
75+
* @param mixed $default Default value to return if the key does not exist.
76+
*
77+
* @return mixed The value of the item from the cache, or $default in case of cache miss.
78+
*
79+
* @throws \Psr\SimpleCache\InvalidArgumentException
80+
* MUST be thrown if the $key string is not a legal value.
81+
*/
82+
public function get(string $key, $default = null)
83+
{
84+
return $this->cache->get($this->getKeyWithPrefix($key), $default);
85+
}
86+
87+
/**
88+
* Persists a set of key => value pairs in the cache, with an optional TTL.
89+
*
90+
* @param iterable $values A list of key => value pairs for a multiple-set operation.
91+
* @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
92+
* the driver supports TTL then the library may set a default value
93+
* for it or let the driver take care of that.
94+
*
95+
* @return bool True on success and false on failure.
96+
*
97+
* @throws \Psr\SimpleCache\InvalidArgumentException
98+
* MUST be thrown if $values is neither an array nor a Traversable,
99+
* or if any of the $values are not a legal value.
100+
*/
101+
public function setMultiple(array $values): bool
102+
{
103+
$newValues = [];
104+
foreach ($values as $key => $value) {
105+
$newValues[$this->getKeyWithPrefix($key)] = $value;
106+
}
107+
108+
return $this->cache->setMultiple($newValues, $this->ttl);
109+
}
110+
111+
/**
112+
* Obtains multiple cache items by their unique keys.
113+
*
114+
* @param iterable $keys A list of keys that can obtained in a single operation.
115+
* @param mixed $default Default value to return for keys that do not exist.
116+
*
117+
* @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value.
118+
*
119+
* @throws \Psr\SimpleCache\InvalidArgumentException
120+
* MUST be thrown if $keys is neither an array nor a Traversable,
121+
* or if any of the $keys are not a legal value.
122+
*/
123+
public function getMultiple(array $keys, $default = null)
124+
{
125+
return $this->cache->getMultiple(
126+
array_map(fn($key) => $this->getKeyWithPrefix($key), $keys),
127+
$default
128+
);
129+
}
130+
131+
/**
132+
* Get the full Key name including Prefix
133+
*
134+
* @param string $key
135+
* @return string
136+
*/
137+
public function getKeyWithPrefix(string $key): string
138+
{
139+
return $this->prefix . '.' . $key;
140+
}
141+
}

src/Concerns/HasWith.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Flagsmith\Concerns;
4+
5+
trait HasWith
6+
{
7+
/**
8+
* Clones class with new property
9+
*
10+
* @param string $property
11+
* @param mixed $value
12+
* @return self
13+
*/
14+
protected function with(string $property, $value): self
15+
{
16+
$self = clone $this;
17+
$self->{$property} = $value;
18+
return $self;
19+
}
20+
}

src/Exceptions/APIException.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Flagsmith\Exceptions;
4+
5+
use Exception;
6+
7+
class APIException extends Exception
8+
{
9+
}

0 commit comments

Comments
 (0)