Skip to content

Commit

Permalink
Merge pull request #245 from WebFiori/refactoring
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
usernane authored Nov 27, 2024
2 parents ffba40f + 2808d71 commit 4b789c6
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/php83.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ jobs:
- name: Benchmarking
run: phpbench run tests/webfiori/benchmark --report=default
release_staging:
name: Publish Beta
needs:
- "test"
- "coding_standards_check"
Expand All @@ -134,6 +135,7 @@ jobs:
manifest-file: .release-please-manifest.json
token: ${{ secrets.GITHUB_TOKEN }}
release_prod:
name: Publish Production
needs:
- "test"
- "coding_standards_check"
Expand Down
122 changes: 122 additions & 0 deletions .github/workflows/php84.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
name: Build PHP 8.4

on:
push:
branches: [ master, dev ]
pull_request:
branches: [ master ]

jobs:

test:
runs-on: ${{ matrix.os }}
services:
sql.data:
image: mcr.microsoft.com/mssql/server:2019-latest
env:
SA_PASSWORD: 1234567890@Eu
ACCEPT_EULA: Y
MSSQL_PID: Express
ports:
- "1433:1433"
strategy:
fail-fast: true
matrix:
os: [ ubuntu-latest ]
php: [8.4]

name: PHP${{matrix.php}} - ${{matrix.os}}

steps:
- name: Clone Repo
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: mysqli, mbstring, sqlsrv
tools: phpunit:9.5.20, composer, symplify/easy-coding-standard:12.0.6, phpbench/phpbench:1.2.14

- name: Shutdown Ubuntu MySQL
run: sudo service mysql stop

- name: Set up MySQL
uses: mirromutth/[email protected]
with:
mysql version: '5.7'
mysql database: 'testing_db'
mysql root password: 123456
mysql user: 'root'
mysql password: 123456

- name: Wait for MySQL
run: |
while ! mysqladmin ping --host=127.0.0.1 --password=123456 --silent; do
sleep 1
done
- name: Setup MSSQL
run: sqlcmd -S localhost -U SA -P 1234567890@Eu -Q 'create database testing_db'

- name: Install Dependencies
run: composer install --prefer-dist --no-interaction --no-dev

- name: Execute Tests
run: phpunit

- name: CodeCov
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}

coding_standards_check:
name: "Coding Standards Check"
needs:
- "test"

runs-on: "ubuntu-latest"
steps:
- name: "Set up PHP"
uses: "shivammathur/setup-php@v2"
with:
php-version: "8.1"
extensions: "mbstring"
tools: composer, symplify/easy-coding-standard:12.0.6

- name: "Checkout code"
uses: "actions/checkout@v3"

- name: Install Dependencies
run: composer install --prefer-dist --no-interaction --no-dev

- name: "Check Style"
run: "ecs check"

benchmarking:
needs:
- "test"
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
os: [ ubuntu-latest ]
php: [8.0,8.1,8.2,8.3,8.4]
steps:
- name: Clone Repo
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: mbstring
tools: composer, phpbench/phpbench

- name: Install Dependencies
run: composer install --prefer-dist --no-interaction --no-dev

- name: Benchmarking
run: phpbench run tests/webfiori/benchmark --report=default


5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</p>

<p align="center">
<a href="https://github.com/WebFiori/framework/actions"><img src="https://github.com/WebFiori/framework/actions/workflows/php83.yml/badge.svg?branch=master"></a>
<a href="https://github.com/WebFiori/framework/actions"><img src="https://github.com/WebFiori/framework/actions/workflows/php84.yml/badge.svg?branch=master"></a>
<a href="https://codecov.io/gh/WebFiori/framework">
<img src="https://codecov.io/gh/WebFiori/framework/branch/master/graph/badge.svg" />
</a>
Expand Down Expand Up @@ -37,7 +37,8 @@ WebFiori Framework is a mini web development framework which is built using PHP
|<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>|
|<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>|
|<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>|
|<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>|
|<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>|
|<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>|


## Key Features
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"ext-mbstring": "*",
"ext-fileinfo": "*",
"ext-openssl": "*",
"webfiori/http": "v3.3.15",
"webfiori/http": "v3.4.0",
"webfiori/file": "v1.3.6",
"webfiori/jsonx": "v3.3.0",
"webfiori/ui": "v2.6.3",
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<file>./webfiori/framework/router/Router.php</file>

<file>./webfiori/framework/cache/AbstractCacheStore.php</file>
<file>./webfiori/framework/cache/FileCacheStore.php</file>
<file>./webfiori/framework/cache/FileStorage.php</file>
<file>./webfiori/framework/cache/Cache.php</file>

<file>./webfiori/framework/session/Session.php</file>
Expand Down
59 changes: 59 additions & 0 deletions tests/webfiori/framework/test/cache/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class CacheTest extends TestCase {
*/
public function test00() {
$key = 'first';
$this->assertTrue(Cache::isEnabled());
$data = Cache::get($key, function () {
return 'This is a test.';
});
Expand Down Expand Up @@ -84,4 +85,62 @@ public function test05() {
$this->assertFalse(Cache::has($key));
}
}
/**
* @test
*/
public function test06() {
$key = 'bbuu';
$this->assertTrue(Cache::isEnabled());
Cache::setEnabled(false);
$data = Cache::get($key, function () {
return 'This is a test.';
});
$this->assertEquals('This is a test.', $data);
$this->assertNull(Cache::get($key));
$this->assertFalse(Cache::isEnabled());
}
/**
* @test
*/
public function testSet00() {
$key = 'new_cool_key';
Cache::setEnabled(true);
$this->assertTrue(Cache::isEnabled());
$this->assertTrue(Cache::set($key, 'This is a test.', 60, false));
$this->assertEquals('This is a test.', Cache::get($key));
$item = Cache::getItem($key);
$this->assertEquals(60, $item->getTTL());

$this->assertFalse(Cache::set($key, 'This is a test.', 60, false));
$this->assertEquals('This is a test.', Cache::get($key));
$item = Cache::getItem($key);
$this->assertEquals(60, $item->getTTL());

$this->assertTrue(Cache::set($key, 'This is a test 2.', 660, true));
$this->assertEquals('This is a test 2.', Cache::get($key));
$item = Cache::getItem($key);
$this->assertEquals(660, $item->getTTL());
}
/**
* @test
*/
public function testSetTTL00() {
$key = 'new_cool_key2';
Cache::setEnabled(true);
$this->assertTrue(Cache::isEnabled());
$this->assertTrue(Cache::set($key, 'This is a test.', 60, false));
$item = Cache::getItem($key);
$this->assertEquals(60, $item->getTTL());
Cache::setTTL($key, 700);

$item = Cache::getItem($key);
$this->assertEquals(700, $item->getTTL());
}
/**
* @test
*/
public function testSetTTL01() {
$key = 'not exist cool';
$this->assertFalse(Cache::setTTL($key, 700));
}
}
28 changes: 26 additions & 2 deletions webfiori/framework/cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Cache {
* @var Storage
*/
private $driver;
private $isEnabled;
private static $inst;
/**
* Removes an item from the cache given its unique identifier.
Expand All @@ -28,6 +29,15 @@ class Cache {
public static function delete(string $key) {
self::getDriver()->delete($key);
}
/**
* Enable or disable caching.
*
* @param bool $enable If set to true, caching will be enabled. Other than
* that, caching will be disabled.
*/
public static function setEnabled(bool $enable) {
self::getInst()->isEnabled = $enable;
}
/**
* Removes all items from the cache.
*/
Expand Down Expand Up @@ -61,8 +71,11 @@ public static function get(string $key, callable $generator = null, int $ttl = 6
return null;
}
$newData = call_user_func_array($generator, $params);
$item = new Item($key, $newData, $ttl, defined('CACHE_SECRET') ? CACHE_SECRET : '');
self::getDriver()->cache($item);

if (self::isEnabled()) {
$item = new Item($key, $newData, $ttl, defined('CACHE_SECRET') ? CACHE_SECRET : '');
self::getDriver()->cache($item);
}

return $newData;
}
Expand Down Expand Up @@ -122,10 +135,20 @@ public static function set(string $key, $data, int $ttl = 60, bool $override = f
if (!self::has($key) || $override === true) {
$item = new Item($key, $data, $ttl, defined('CACHE_SECRET') ? CACHE_SECRET : '');
self::getDriver()->cache($item);

return true;
}

return false;
}
/**
* Checks if caching is enabled or not.
*
* @return bool True if enabled. False otherwise.
*/
public static function isEnabled() : bool {
return self::getInst()->isEnabled;
}
/**
* Sets storage engine which is used to store, read, update and delete items
* from the cache.
Expand Down Expand Up @@ -165,6 +188,7 @@ private static function getInst() : Cache {
if (self::$inst === null) {
self::$inst = new Cache();
self::setDriver(new FileStorage());
self::setEnabled(true);
}

return self::$inst;
Expand Down

0 comments on commit 4b789c6

Please sign in to comment.