Skip to content

Commit 692aa45

Browse files
authored
Merge pull request #2 from flightphp/updated-to-flight-cache
updated to flight cache module
2 parents 4bca423 + d67b4f8 commit 692aa45

File tree

3 files changed

+56
-35
lines changed

3 files changed

+56
-35
lines changed

composer.json

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22
"name": "flightphp/permissions",
33
"type": "library",
44
"description": "Library for managing permissions in Flight Applications",
5-
"keywords": ["permissions", "authentication","lite","simple"],
5+
"keywords": [
6+
"permissions",
7+
"authentication",
8+
"lite",
9+
"simple"
10+
],
611
"homepage": "https://docs.flightphp.com",
712
"license": "MIT",
8-
"authors": [
13+
"authors": [
914
{
1015
"name": "n0nag0n",
1116
"email": "n0nag0n@sky-9.com",
@@ -20,18 +25,25 @@
2025
"squizlabs/php_codesniffer": "^3.8",
2126
"rregeer/phpunit-coverage-check": "^0.3.1",
2227
"flightphp/core": "^3.10",
23-
"wruczek/php-file-cache": "^0.0.5"
28+
"flightphp/cache": "^1.1"
29+
},
30+
"suggest": {
31+
"flightphp/cache": "Required for caching permissions"
32+
},
33+
"autoload": {
34+
"psr-4": {
35+
"flight\\": "src/"
36+
}
37+
},
38+
"autoload-dev": {
39+
"psr-4": {
40+
"flight\\tests\\": "tests/"
41+
}
2442
},
25-
"autoload": {
26-
"psr-4": {"flight\\": "src/"}
27-
},
28-
"autoload-dev": {
29-
"psr-4": {"flight\\tests\\": "tests/"}
30-
},
31-
"scripts": {
32-
"test": "phpunit",
33-
"test-coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html=coverage --coverage-clover=clover.xml && vendor/bin/coverage-check clover.xml 100",
34-
"beautify": "phpcbf --standard=phpcs.xml",
35-
"phpcs": "phpcs --standard=phpcs.xml"
36-
}
37-
}
43+
"scripts": {
44+
"test": "phpunit",
45+
"test-coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html=coverage --coverage-clover=clover.xml && vendor/bin/coverage-check clover.xml 100",
46+
"beautify": "phpcbf --standard=phpcs.xml",
47+
"phpcs": "phpcs --standard=phpcs.xml"
48+
}
49+
}

src/Permission.php

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
use Exception;
88
use Flight;
99
use flight\Engine;
10-
use Wruczek\PhpFileCache\PhpFileCache;
1110

12-
class Permission {
11+
class Permission
12+
{
1313
/** @var array */
1414
protected $rules = [];
1515

@@ -31,7 +31,8 @@ class Permission {
3131
* @param string $currentRole
3232
* @param Engine $f3
3333
*/
34-
public function __construct(string $currentRole = '', ?Engine $app = null, $Cache = null) {
34+
public function __construct(string $currentRole = '', ?Engine $app = null, $Cache = null)
35+
{
3536
$this->currentRole = $currentRole;
3637
$this->app = $app === null ? Flight::app() : $app;
3738
$this->Cache = $Cache;
@@ -43,7 +44,8 @@ public function __construct(string $currentRole = '', ?Engine $app = null, $Cach
4344
* @param string $currentRole the current role of the logged in user
4445
* @return void
4546
*/
46-
public function setCurrentRole(string $currentRole) {
47+
public function setCurrentRole(string $currentRole)
48+
{
4749
$this->currentRole = $currentRole;
4850
}
4951

@@ -52,7 +54,8 @@ public function setCurrentRole(string $currentRole) {
5254
*
5355
* @return string
5456
*/
55-
public function getCurrentRole(): string {
57+
public function getCurrentRole(): string
58+
{
5659
return $this->currentRole;
5760
}
5861

@@ -61,7 +64,8 @@ public function getCurrentRole(): string {
6164
*
6265
* @return array
6366
*/
64-
public function getRules(): array {
67+
public function getRules(): array
68+
{
6569
return $this->rules;
6670
}
6771

@@ -82,7 +86,8 @@ public function getRules(): array {
8286
* @param bool $overwrite if true, will overwrite any existing rule with the same name
8387
* @return void
8488
*/
85-
public function defineRule(string $rule, $callableOrClassString, bool $overwrite = false) {
89+
public function defineRule(string $rule, $callableOrClassString, bool $overwrite = false)
90+
{
8691
if ($overwrite === false && isset($this->rules[$rule]) === true) {
8792
throw new \Exception('Rule already defined: ' . $rule);
8893
}
@@ -95,15 +100,16 @@ public function defineRule(string $rule, $callableOrClassString, bool $overwrite
95100
* @param string $className the name of the class to define rules from
96101
* @return void
97102
*/
98-
public function defineRulesFromClassMethods(string $className, int $ttl = 0): void {
103+
public function defineRulesFromClassMethods(string $className, int $ttl = 0): void
104+
{
99105

100106
$useCache = false;
101107
if ($this->Cache !== null && $ttl > 0) {
102108
$useCache = true;
103109
$Cache = $this->Cache;
104110
$cacheKey = 'flight_permissions_class_methods_' . $className;
105-
if (is_a($Cache, PhpFileCache::class) === true) {
106-
/** @var PhpFileCache $Cache */
111+
if (is_a($Cache, Cache::class) === true) {
112+
/** @var Cache $Cache */
107113
$isCached = $Cache->isCached($cacheKey);
108114
if ($isCached === true) {
109115
$this->rules = $Cache->retrieve($cacheKey);
@@ -124,8 +130,8 @@ public function defineRulesFromClassMethods(string $className, int $ttl = 0): vo
124130
}
125131

126132
if ($useCache === true) {
127-
if (is_a($Cache, PhpFileCache::class) === true) {
128-
/** @var PhpFileCache $Cache */
133+
if (is_a($Cache, Cache::class) === true) {
134+
/** @var Cache $Cache */
129135
$Cache->store($cacheKey, $classRules, $ttl);
130136
}
131137
}
@@ -141,7 +147,8 @@ public function defineRulesFromClassMethods(string $className, int $ttl = 0): vo
141147
* @param mixed $additionalArgs any additional arguments to pass to the callback or method.
142148
* @return bool
143149
*/
144-
public function can(string $permission, ...$additionalArgs): bool {
150+
public function can(string $permission, ...$additionalArgs): bool
151+
{
145152
$allowed = false;
146153
$action = '';
147154
if (strpos($permission, '.') !== false) {
@@ -187,7 +194,8 @@ public function can(string $permission, ...$additionalArgs): bool {
187194
* @param mixed $additionalArgs any additional arguments to pass to the callback or method.
188195
* @return boolean
189196
*/
190-
public function has(string $permission, ...$additionalArgs): bool {
197+
public function has(string $permission, ...$additionalArgs): bool
198+
{
191199
return $this->can($permission, ...$additionalArgs);
192200
}
193201

@@ -197,7 +205,8 @@ public function has(string $permission, ...$additionalArgs): bool {
197205
* @param string $role [description]
198206
* @return boolean
199207
*/
200-
public function is(string $role): bool {
208+
public function is(string $role): bool
209+
{
201210
return $this->currentRole === $role;
202211
}
203212
}

tests/PermissionTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace flight\tests;
44

55
use Exception;
6+
use flight\Cache;
67
use flight\Permission;
7-
use Wruczek\PhpFileCache\PhpFileCache;
88

99
class PermissionTest extends \PHPUnit\Framework\TestCase
1010
{
@@ -27,7 +27,7 @@ public function testDefineRule()
2727
{
2828
$permission = new Permission('admin');
2929
$permission->defineRule('createOrder', 'Some_Permissions_Class->createOrder');
30-
$this->assertSame([ 'createOrder' => 'Some_Permissions_Class->createOrder' ], $permission->getRules());
30+
$this->assertSame(['createOrder' => 'Some_Permissions_Class->createOrder'], $permission->getRules());
3131
}
3232

3333
public function testDefineDuplicateRule()
@@ -62,7 +62,7 @@ public function testDefineRulesFromClassMethodsNoCacheArray()
6262

6363
public function testDefineRulesFromClassMethodsWithCache()
6464
{
65-
$PhpFileCache = new PhpFileCache(__DIR__, 'my_test');
65+
$PhpFileCache = new Cache(__DIR__, 'my_test');
6666
$permission = new Permission('public', null, $PhpFileCache);
6767
$permission->defineRulesFromClassMethods(FakePermissionsClass::class, 60);
6868
$this->assertTrue($permission->can('order.create', 1, 1));
@@ -77,7 +77,7 @@ public function testDefineRulesFromClassMethodsWithCache()
7777

7878
public function testDefineRulesFromClassMethodsWithCacheTouchCache()
7979
{
80-
$PhpFileCache = new PhpFileCache(__DIR__, 'my_test');
80+
$PhpFileCache = new Cache(__DIR__, 'my_test');
8181
$permission = new Permission('public', null, $PhpFileCache);
8282
$permission->defineRulesFromClassMethods(FakePermissionsClass::class, 60);
8383
// Make sure it works

0 commit comments

Comments
 (0)