scriptmancer/kiler 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

scriptmancer/kiler

Composer 安装命令:

composer require scriptmancer/kiler

包简介

A lightweight, attribute-based dependency injection container for PHP

README 文档

README

PHP Version License

Kiler is a lightweight, feature-rich dependency injection container for PHP applications. It provides a simple yet powerful way to manage dependencies and services in your application.

Features

  • 🚀 Simple and intuitive API
  • 🔄 Support for both dynamic and compiled containers
  • 🏷️ Service registration with attributes
  • 🔌 Interface-based dependency injection
  • 🏷️ Service tagging and grouping
  • 🔒 Singleton and factory services
  • 📦 PSR-11 Compliance & Framework Compatibility

Kiler implements the PSR-11 Container Interface, making it compatible with any library or framework that supports PSR-11 containers. You can use Kiler as a drop-in DI container for custom projects or integrate it with popular frameworks (such as Laravel, Symfony, Slim, etc.) as long as they support PSR-11 or allow custom containers.

  • Service Providers: Extend AbstractServiceProvider for modular service registration and boot logic.
  • Optional Priority & Dependencies: Providers can optionally override getPriority() and getDependencies() for advanced initialization order control.
  • No Framework Lock-in: Kiler does not require any framework-specific code, ensuring portability and reusability.

Installation

composer require scriptmancer/kiler

Quick Start

use Scriptmancer\Kiler\Container;
use Scriptmancer\Kiler\Attributes\Service;

// Define a service
#[Service]
class Database
{
    public function __construct(
        private readonly string $dsn = 'mysql:host=localhost;dbname=test'
    ) {}
}

// Get container instance
$container = Container::getInstance();

// Register service
$container->register(Database::class);

// Use service
$db = $container->get(Database::class);

Service Registration

Using Attributes

use Scriptmancer\Kiler\Attributes\Service;

#[Service(
    implements: DatabaseInterface::class,
    group: 'database',
    tags: ['storage', 'persistence'],
    singleton: true
)]
class Database implements DatabaseInterface
{
    // ...
}

Manual Registration

$container->register(Database::class, 'db');
$container->registerFactory('db.factory', fn() => new Database());

Interface Resolution

interface LoggerInterface
{
    public function log(string $message): void;
}

#[Service(implements: LoggerInterface::class)]
class FileLogger implements LoggerInterface
{
    public function log(string $message): void
    {
        // ...
    }
}

// Resolve interface to implementation
$logger = $container->get(LoggerInterface::class);

Service Groups and Tags

// Register service with group and tags
$container->register(Database::class);

// Get all services in a group
$databaseServices = $container->getServicesByGroup('database');

// Get all services with a tag
$storageServices = $container->getServicesByTag('storage');

Compiled Container

For production environments, Kiler provides a compiled container that improves performance:

use Scriptmancer\Kiler\Container;
use Scriptmancer\Kiler\ContainerCompiler;

// In development
$container = Container::getInstance();
$container->register(Database::class);

// Compile container
$compiler = new ContainerCompiler('/path/to/cache', 'App\\Bootstrap');
$containerPath = $compiler->compile($container);

// In production
$container = new App\Bootstrap\Container();
$db = $container->get(Database::class);

Event System

Kiler integrates with event systems through the EventDispatcherInterface:

use Scriptmancer\Kiler\Event\EventDispatcherInterface;

$container->setEventDispatcher($eventDispatcher);

Available events:

  • container.service.registered: Fired when a service is registered
  • container.service.resolved: Fired when a service is resolved

Service Providers

A service provider is a class responsible for registering related services in the container. You can create your own providers by extending the AbstractServiceProvider class.

use Scriptmancer\Kiler\AbstractServiceProvider;
use Scriptmancer\Kiler\Container;

class MyServiceProvider extends AbstractServiceProvider
{
    public function register(Container $container): void
    {
        // Register your services here
    }
    public function boot(Container $container): void
    {
        // Perform initialization after all services are registered
    }
}

$container->addServiceProvider(new DatabaseServiceProvider());
$container->registerProviders();

Best Practices

  1. Use attributes for service configuration
  2. Register services in service providers
  3. Use interface-based dependency injection
  4. Compile container for production
  5. Use groups and tags for service organization
  6. Keep services stateless when possible

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for details.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Credits

Kiler is part of the Nazım Framework project.

scriptmancer/kiler 适用场景与选型建议

scriptmancer/kiler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 05 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 scriptmancer/kiler 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 scriptmancer/kiler 我们能提供哪些服务?
定制开发 / 二次开发

基于 scriptmancer/kiler 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-05-07