Skip to content

Commit 4b789c6

Browse files
authored
Merge pull request #245 from WebFiori/refactoring
Refactoring
2 parents ffba40f + 2808d71 commit 4b789c6

File tree

7 files changed

+214
-6
lines changed

7 files changed

+214
-6
lines changed

.github/workflows/php83.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ jobs:
119119
- name: Benchmarking
120120
run: phpbench run tests/webfiori/benchmark --report=default
121121
release_staging:
122+
name: Publish Beta
122123
needs:
123124
- "test"
124125
- "coding_standards_check"
@@ -134,6 +135,7 @@ jobs:
134135
manifest-file: .release-please-manifest.json
135136
token: ${{ secrets.GITHUB_TOKEN }}
136137
release_prod:
138+
name: Publish Production
137139
needs:
138140
- "test"
139141
- "coding_standards_check"

.github/workflows/php84.yml

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
name: Build PHP 8.4
2+
3+
on:
4+
push:
5+
branches: [ master, dev ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
11+
test:
12+
runs-on: ${{ matrix.os }}
13+
services:
14+
sql.data:
15+
image: mcr.microsoft.com/mssql/server:2019-latest
16+
env:
17+
SA_PASSWORD: 1234567890@Eu
18+
ACCEPT_EULA: Y
19+
MSSQL_PID: Express
20+
ports:
21+
- "1433:1433"
22+
strategy:
23+
fail-fast: true
24+
matrix:
25+
os: [ ubuntu-latest ]
26+
php: [8.4]
27+
28+
name: PHP${{matrix.php}} - ${{matrix.os}}
29+
30+
steps:
31+
- name: Clone Repo
32+
uses: actions/checkout@v3
33+
34+
- name: Setup PHP
35+
uses: shivammathur/setup-php@v2
36+
with:
37+
php-version: ${{ matrix.php }}
38+
extensions: mysqli, mbstring, sqlsrv
39+
tools: phpunit:9.5.20, composer, symplify/easy-coding-standard:12.0.6, phpbench/phpbench:1.2.14
40+
41+
- name: Shutdown Ubuntu MySQL
42+
run: sudo service mysql stop
43+
44+
- name: Set up MySQL
45+
uses: mirromutth/[email protected]
46+
with:
47+
mysql version: '5.7'
48+
mysql database: 'testing_db'
49+
mysql root password: 123456
50+
mysql user: 'root'
51+
mysql password: 123456
52+
53+
- name: Wait for MySQL
54+
run: |
55+
while ! mysqladmin ping --host=127.0.0.1 --password=123456 --silent; do
56+
sleep 1
57+
done
58+
59+
- name: Setup MSSQL
60+
run: sqlcmd -S localhost -U SA -P 1234567890@Eu -Q 'create database testing_db'
61+
62+
- name: Install Dependencies
63+
run: composer install --prefer-dist --no-interaction --no-dev
64+
65+
- name: Execute Tests
66+
run: phpunit
67+
68+
- name: CodeCov
69+
uses: codecov/codecov-action@v4
70+
with:
71+
token: ${{ secrets.CODECOV_TOKEN }}
72+
73+
coding_standards_check:
74+
name: "Coding Standards Check"
75+
needs:
76+
- "test"
77+
78+
runs-on: "ubuntu-latest"
79+
steps:
80+
- name: "Set up PHP"
81+
uses: "shivammathur/setup-php@v2"
82+
with:
83+
php-version: "8.1"
84+
extensions: "mbstring"
85+
tools: composer, symplify/easy-coding-standard:12.0.6
86+
87+
- name: "Checkout code"
88+
uses: "actions/checkout@v3"
89+
90+
- name: Install Dependencies
91+
run: composer install --prefer-dist --no-interaction --no-dev
92+
93+
- name: "Check Style"
94+
run: "ecs check"
95+
96+
benchmarking:
97+
needs:
98+
- "test"
99+
runs-on: ${{ matrix.os }}
100+
strategy:
101+
fail-fast: true
102+
matrix:
103+
os: [ ubuntu-latest ]
104+
php: [8.0,8.1,8.2,8.3,8.4]
105+
steps:
106+
- name: Clone Repo
107+
uses: actions/checkout@v4
108+
109+
- name: Setup PHP
110+
uses: shivammathur/setup-php@v2
111+
with:
112+
php-version: ${{ matrix.php }}
113+
extensions: mbstring
114+
tools: composer, phpbench/phpbench
115+
116+
- name: Install Dependencies
117+
run: composer install --prefer-dist --no-interaction --no-dev
118+
119+
- name: Benchmarking
120+
run: phpbench run tests/webfiori/benchmark --report=default
121+
122+

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</p>
66

77
<p align="center">
8-
<a href="https://github.com/WebFiori/framework/actions"><img src="https://github.com/WebFiori/framework/actions/workflows/php83.yml/badge.svg?branch=master"></a>
8+
<a href="https://github.com/WebFiori/framework/actions"><img src="https://github.com/WebFiori/framework/actions/workflows/php84.yml/badge.svg?branch=master"></a>
99
<a href="https://codecov.io/gh/WebFiori/framework">
1010
<img src="https://codecov.io/gh/WebFiori/framework/branch/master/graph/badge.svg" />
1111
</a>
@@ -37,7 +37,8 @@ WebFiori Framework is a mini web development framework which is built using PHP
3737
|<a target="_blank" href="https://github.com/WebFiori/framework/actions/workflows/php80.yml"><img src="https://github.com/WebFiori/framework/actions/workflows/php80.yml/badge.svg?branch=master"></a>|
3838
|<a target="_blank" href="https://github.com/WebFiori/framework/actions/workflows/php81.yml"><img src="https://github.com/WebFiori/framework/actions/workflows/php81.yml/badge.svg?branch=master"></a>|
3939
|<a target="_blank" href="https://github.com/WebFiori/framework/actions/workflows/php82.yml"><img src="https://github.com/WebFiori/framework/actions/workflows/php82.yml/badge.svg?branch=master"></a>|
40-
|<a target="_blank" href="https://github.com/WebFiori/framework/actions/workflows/php83.yml"><img src="https://github.com/WebFiori/framework/actions/workflows/php83.yml/badge.svg?branch=master"></a><br>|
40+
|<a target="_blank" href="https://github.com/WebFiori/framework/actions/workflows/php83.yml"><img src="https://github.com/WebFiori/framework/actions/workflows/php83.yml/badge.svg?branch=master"></a>|
41+
|<a target="_blank" href="https://github.com/WebFiori/framework/actions/workflows/php84.yml"><img src="https://github.com/WebFiori/framework/actions/workflows/php84.yml/badge.svg?branch=master"><br>|
4142

4243

4344
## Key Features

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"ext-mbstring": "*",
2323
"ext-fileinfo": "*",
2424
"ext-openssl": "*",
25-
"webfiori/http": "v3.3.15",
25+
"webfiori/http": "v3.4.0",
2626
"webfiori/file": "v1.3.6",
2727
"webfiori/jsonx": "v3.3.0",
2828
"webfiori/ui": "v2.6.3",

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<file>./webfiori/framework/router/Router.php</file>
3030

3131
<file>./webfiori/framework/cache/AbstractCacheStore.php</file>
32-
<file>./webfiori/framework/cache/FileCacheStore.php</file>
32+
<file>./webfiori/framework/cache/FileStorage.php</file>
3333
<file>./webfiori/framework/cache/Cache.php</file>
3434

3535
<file>./webfiori/framework/session/Session.php</file>

tests/webfiori/framework/test/cache/CacheTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class CacheTest extends TestCase {
1111
*/
1212
public function test00() {
1313
$key = 'first';
14+
$this->assertTrue(Cache::isEnabled());
1415
$data = Cache::get($key, function () {
1516
return 'This is a test.';
1617
});
@@ -84,4 +85,62 @@ public function test05() {
8485
$this->assertFalse(Cache::has($key));
8586
}
8687
}
88+
/**
89+
* @test
90+
*/
91+
public function test06() {
92+
$key = 'bbuu';
93+
$this->assertTrue(Cache::isEnabled());
94+
Cache::setEnabled(false);
95+
$data = Cache::get($key, function () {
96+
return 'This is a test.';
97+
});
98+
$this->assertEquals('This is a test.', $data);
99+
$this->assertNull(Cache::get($key));
100+
$this->assertFalse(Cache::isEnabled());
101+
}
102+
/**
103+
* @test
104+
*/
105+
public function testSet00() {
106+
$key = 'new_cool_key';
107+
Cache::setEnabled(true);
108+
$this->assertTrue(Cache::isEnabled());
109+
$this->assertTrue(Cache::set($key, 'This is a test.', 60, false));
110+
$this->assertEquals('This is a test.', Cache::get($key));
111+
$item = Cache::getItem($key);
112+
$this->assertEquals(60, $item->getTTL());
113+
114+
$this->assertFalse(Cache::set($key, 'This is a test.', 60, false));
115+
$this->assertEquals('This is a test.', Cache::get($key));
116+
$item = Cache::getItem($key);
117+
$this->assertEquals(60, $item->getTTL());
118+
119+
$this->assertTrue(Cache::set($key, 'This is a test 2.', 660, true));
120+
$this->assertEquals('This is a test 2.', Cache::get($key));
121+
$item = Cache::getItem($key);
122+
$this->assertEquals(660, $item->getTTL());
123+
}
124+
/**
125+
* @test
126+
*/
127+
public function testSetTTL00() {
128+
$key = 'new_cool_key2';
129+
Cache::setEnabled(true);
130+
$this->assertTrue(Cache::isEnabled());
131+
$this->assertTrue(Cache::set($key, 'This is a test.', 60, false));
132+
$item = Cache::getItem($key);
133+
$this->assertEquals(60, $item->getTTL());
134+
Cache::setTTL($key, 700);
135+
136+
$item = Cache::getItem($key);
137+
$this->assertEquals(700, $item->getTTL());
138+
}
139+
/**
140+
* @test
141+
*/
142+
public function testSetTTL01() {
143+
$key = 'not exist cool';
144+
$this->assertFalse(Cache::setTTL($key, 700));
145+
}
87146
}

webfiori/framework/cache/Cache.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Cache {
1919
* @var Storage
2020
*/
2121
private $driver;
22+
private $isEnabled;
2223
private static $inst;
2324
/**
2425
* Removes an item from the cache given its unique identifier.
@@ -28,6 +29,15 @@ class Cache {
2829
public static function delete(string $key) {
2930
self::getDriver()->delete($key);
3031
}
32+
/**
33+
* Enable or disable caching.
34+
*
35+
* @param bool $enable If set to true, caching will be enabled. Other than
36+
* that, caching will be disabled.
37+
*/
38+
public static function setEnabled(bool $enable) {
39+
self::getInst()->isEnabled = $enable;
40+
}
3141
/**
3242
* Removes all items from the cache.
3343
*/
@@ -61,8 +71,11 @@ public static function get(string $key, callable $generator = null, int $ttl = 6
6171
return null;
6272
}
6373
$newData = call_user_func_array($generator, $params);
64-
$item = new Item($key, $newData, $ttl, defined('CACHE_SECRET') ? CACHE_SECRET : '');
65-
self::getDriver()->cache($item);
74+
75+
if (self::isEnabled()) {
76+
$item = new Item($key, $newData, $ttl, defined('CACHE_SECRET') ? CACHE_SECRET : '');
77+
self::getDriver()->cache($item);
78+
}
6679

6780
return $newData;
6881
}
@@ -122,10 +135,20 @@ public static function set(string $key, $data, int $ttl = 60, bool $override = f
122135
if (!self::has($key) || $override === true) {
123136
$item = new Item($key, $data, $ttl, defined('CACHE_SECRET') ? CACHE_SECRET : '');
124137
self::getDriver()->cache($item);
138+
139+
return true;
125140
}
126141

127142
return false;
128143
}
144+
/**
145+
* Checks if caching is enabled or not.
146+
*
147+
* @return bool True if enabled. False otherwise.
148+
*/
149+
public static function isEnabled() : bool {
150+
return self::getInst()->isEnabled;
151+
}
129152
/**
130153
* Sets storage engine which is used to store, read, update and delete items
131154
* from the cache.
@@ -165,6 +188,7 @@ private static function getInst() : Cache {
165188
if (self::$inst === null) {
166189
self::$inst = new Cache();
167190
self::setDriver(new FileStorage());
191+
self::setEnabled(true);
168192
}
169193

170194
return self::$inst;

0 commit comments

Comments
 (0)