Kiri Core Framework Documentation
Project Introduction
Kiri is a high-performance PHP-based core framework designed to provide developers with a simple and efficient development experience. The framework includes a variety of practical utility classes, exception handling mechanisms, configuration management, and a dependency injection container, making it suitable for building various types of applications.
Key Features
- Dependency Injection Container: Provides powerful dependency management via
Kiri\Di\Container. - Configuration Management: Supports reading configuration from files and environment variables.
- Exception Handling: Offers a unified exception handling interface with support for registering custom exception handlers.
- Logging: Built-in
StdoutLoggersupporting multiple log levels. - Utility Classes:
Str: String operations such as encryption and random string generation.Json: JSON encoding/decoding and response construction.Help: Auxiliary functions including XML conversion, random number generation, and email sending.DateFormat: Date and time formatting tools.
- Networking: Supports local IP retrieval and Socket programming.
- NoSQL Support: Provides client wrappers for Redis and MongoDB with connection pool management.
Installation
System Requirements
- PHP 7.4 or higher
- Composer (for dependency management)
Installation Steps
- Ensure Composer is installed.
- Run the following command to install dependencies:
composer install
Usage Guide
Initialize the Framework
use Kiri\Kiri;
use Kiri\Application;
// Initialize the container
Kiri::setContainer(new Container());
// Create an application instance
$app = new Application(
new EventProvider(),
new ConfigProvider(),
Kiri::getContainer()
);
// Initialize the application
$app->init();
Configuration Management
Configuration files can be loaded and accessed via the ConfigProvider class:
$config = new ConfigProvider();
$databaseHost = $config->get('database.host');
Dependency Injection
Use the container to inject dependencies:
$service = Kiri::getContainer()->get(MyService::class);
Logging
Use the built-in logger to output messages:
Kiri::getLogger()->info('This is an info message.');
Exception Handling
Register a custom exception handler:
$errorHandler = new ErrorHandler();
$errorHandler->registerExceptionHandler(function (Throwable $e) {
echo "Unhandled exception: " . $e->getMessage();
});
Database Connections
Redis
$redis = new Redis();
$redis->connect();
$redis->set('key', 'value');
echo $redis->get('key');
MongoDB
$mongo = new MongoDB();
$collection = $mongo->getCollection('users');
$result = $collection->find([]);
Utility Class Examples
String Operations
echo Str::rand(10); // Generate a random string
JSON Response
echo Json::jsonSuccess(['data' => 'example'], 'Operation successful');
Date Formatting
echo DateFormat::DaySecond(); // Get the number of seconds in a day
Contribution Guidelines
Contributions are welcome! Please follow these steps:
- Fork the project repository.
- Create a new branch (
git checkout -b feature/new-feature) - Commit your changes (
git commit -am 'Add new feature') - Push to the branch (
git push origin feature/new-feature) - Create a Pull Request
License
This project is licensed under the MIT License. For details, see the LICENSE file.
Contact
If you have any questions or suggestions, please open an Issue or contact the project maintainers.
Thank you for choosing Kiri Core Framework! We look forward to your feedback and contributions.