websk / php-keyvalue
KeyValue
Requires
- php: ^8.3
- ext-mbstring: *
- ext-pdo: *
- ext-pdo_mysql: *
- fig/http-message-util: ^1.1
- juliangut/slim-php-di: ^4.2
- psr/container: ^2.0
- psr/http-message: ^2.0
- slim/psr7: ^1.8
- slim/slim: ^4.15
- websk/php-cache: 3.0.*
- websk/php-config: 2.2.*
- websk/php-crud: 3.0.*
- websk/php-db: 3.0.*
- websk/php-entity: 3.0.*
- websk/php-logger: 3.0.*
- websk/php-slim: 2.4.*
- websk/php-utils: 3.0.*
- websk/php-view: 3.1.*
Requires (Dev)
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^12.5
- websk/php-auth: 3.0.*
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Key/value settings storage for WebSK applications, with CRUD administration, caching, and change logging.
Requirements
- PHP 8.3 or later, including PHP 8.5
ext-mbstringext-pdoext-pdo_mysql- MySQL or MariaDB
- A cache backend supported by
websk/php-cache
Installation
Install the package with Composer:
composer require websk/php-keyvalue
Register CacheServiceProvider, LoggerServiceProvider, and KeyValueServiceProvider in the application's mutable container. The demo application in src/WebSK/KeyValue/Demo shows the complete PHP-DI 7 setup.
The package does not require an authentication library. By default, change-log entries have no user identifier. Applications using websk/php-auth can supply it while registering the provider:
use WebSK\Auth\Auth; use WebSK\KeyValue\KeyValueServiceProvider; use WebSK\Utils\FullObjectId; KeyValueServiceProvider::register( $mutable_container, static fn(): ?string => FullObjectId::getFullObjectId(Auth::getCurrentUserObj()) );
The demo keeps websk/php-auth as a development dependency and uses this integration.
Read a setting through the service:
use WebSK\KeyValue\KeyValueService; $value = $container->get(KeyValueService::class) ->getOptionalValueForKey('feature.enabled', 'no');
Store or update a setting:
$container->get(KeyValueService::class) ->setValueForKey('feature.enabled', 'yes', 'Feature switch');
Configuration
See config/config.default.php for the complete example. At minimum, configure the KeyValue database and a cache engine:
'db' => [ 'db_keyvalue' => [ 'host' => 'localhost', 'db_name' => 'keyvalue', 'user' => 'root', 'password' => '', 'dump_file_path' => \WebSK\KeyValue\KeyValueServiceProvider::DUMP_FILE_PATH, ], ], 'keyvalue' => [ 'layout_admin' => '/absolute/path/to/layout.main.tpl.php', 'admin_main_page_url' => '/admin', ],
Create the schema with the WebSK DB migration command:
php vendor/bin/websk_db_migration.php migrations:migration_auto
You can also apply dumps/db_keyvalue.sql to a new database directly.
Demo
- Copy
config/config.default.phptoconfig/config.php. - Adjust the database, cache, and absolute layout paths.
- Run the database migrations.
- Start the built-in server from the project root:
php -S localhost:8000 -t public
Open http://localhost:8000.
Development
Run the default verification suite:
composer check
MySQL integration tests are opt-in:
MYSQL_TEST_HOST=127.0.0.1 \ MYSQL_TEST_DB=keyvalue_test \ MYSQL_TEST_USER=root \ MYSQL_TEST_PASSWORD=secret \ composer test:mysql
The MySQL suite recreates the key_value table in the configured test database.
License
MIT. See LICENSE.