damarbob/starcore 问题修复 & 功能扩展

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

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

damarbob/starcore

Composer 安装命令:

composer require damarbob/starcore

包简介

The HMVC and Hook Orchestration Kernel for CodeIgniter 4

README 文档

README

The HMVC and Hook Orchestration Kernel for CodeIgniter 4

StarCore is a lightweight, powerful library designed to bring Hierarchical Model-View-Controller (HMVC) architecture and a robust Hook Orchestration system to CodeIgniter 4 applications. It enables modular application development and provides a flexible event-driven architecture.

Features

  • HMVC Architecture: Organize your application into reusable modules with their own routes, controllers, models, and views.
  • Hook Orchestration: A powerful event system supporting Actions, Filters, and Triggers with priority management.
  • Module Autoloading: Automatically discovers and loads modules from modules/ directory.
  • Zero Configuration: Works out of the box with sensible defaults, but fully configurable.

Installation

Install via Composer:

composer require damarbob/starcore

Configuration

Active Modules

By default, StarCore looks for modules in the modules/ directory. You can configure active modules in Config/Star.php (publish this file to your application's app/Config directory if needed, or modify it in place for the library).

// app/Config/Star.php

protected string $activeModules = 'Blog,Auth,Shop';

Alternatively, you can set this in your .env file:

Star.activeModules = 'Blog,Auth,Shop'

Development Modules

Development modules are located in the modules/.star-dev/ directory. These modules are either experimental or intended for development tools and features, and should not be enabled in production.

You can configure active development modules in Config/Star.php:

protected string $activeDevModules = 'DebugToolbar,Generator';

Alternatively, you can set this in your .env file:

Star.activeDevModules = 'DebugToolbar,Generator'

Note: Development modules are automatically disabled when safeMode is enabled.

Safe Mode

Safe mode disables all modules, useful for debugging or maintenance.

public bool $safeMode = true;

Usage

Hooks

StarCore provides a global helper function hook() and a service HyperHooks to manage events.

Registering a Hook

You can register hooks in your module's init.php or any other loaded file.

use StarCore\Service\HyperHooks;

// Register an action (side-effect)
HyperHooks::getInstance()->register('user.login', function($user) {
    log_message('info', 'User logged in: ' . $user->id);
});

// Register a filter (modify value)
HyperHooks::getInstance()->register('content.render', function($content) {
    return strtoupper($content);
});

Triggering a Hook

// Trigger an action
$hooks = service('hooks');
$hooks->action('user.login', [$currentUser]);

// Apply filters
$content = "hello world";
$content = $hooks->filter('content.render', $content); // Returns "HELLO WORLD"

Using the hook() Helper

The hook() helper is used to retrieve hook configurations or values from Hooks/ files in your modules.

// Returns the value of 'header' from 'Hooks/Frontend.php'
$headerHook = hook('Frontend.header');

Creating a Hook

To create a hook that can be retrieved via the hook() helper, create a file in your module's Hooks/ directory (e.g., modules/Blog/Hooks/Frontend.php). This file should return an associative array of Star\HyperHook objects.

<?php

use StarCore\Star\HyperHook;

return [
    'header' => new HyperHook('header', 'Header Hook', 'This hook is used to display the header.'),
    'footer' => new HyperHook('footer', 'Footer Hook', 'This hook is used to display the footer.'),
];

You can then access these hooks using hook('Frontend.header') or hook('Frontend.footer').

Dynamic Hook Names

You can use placeholders in your hook values to make them dynamic. Placeholders are defined using curly braces, e.g., {name}.

1. Create a hook with a placeholder:

// modules/Blog/Hooks/Greetings.php
return [
    'welcome' => new HyperHook('welcome', 'Welcome Message', 'Welcome back, {name}!'),
];

2. Retrieve the hook with parameters:

Pass an associative array as the second argument to the hook() helper to replace the placeholders.

// Returns "Welcome back, John!"
echo hook('Greetings.welcome', ['name' => 'John']);

Module Structure

A typical module structure looks like this:

modules/
  Blog/
    Config/
      Routes.php
    Controllers/
    Models/
    Views/
    Hooks/
    init.php

Development Module Structure

A typical development module structure looks like this:

modules/
  .star-dev/
    DebugToolbar/
      Config/
        Routes.php
      Controllers/
      Models/
      Views/
      Hooks/
      init.php

Module Autoloading

StarCore automatically discovers and loads modules located in the modules/ directory (and modules/.star-dev/ for development modules). This process happens immediately when the library is loaded (before the pre_system event) to ensure namespaces and event listeners are available during the application bootstrapping.

  1. Namespace Registration: The autoloader automatically registers the module's namespace based on its directory name. For example, a module in modules/Blog will have the Blog namespace registered.
  2. Initialization: If an init.php file exists in the module's root, it is executed. This is the ideal place to register hooks or perform other setup tasks.

Composer Compatibility

StarCore is designed to work seamlessly with Composer.

  • Library Support: You can use any StarCore-supported module as a Composer package. Simply require it in your project's composer.json and use it as usual. StarCore prioritizes Composer packages over its own modules.
  • Local Modules: Unlike Composer’s autoloader, StarCore’s autoloader automatically registers namespaces for modules in the modules/ directory. This means you don’t need to run composer dump-autoload every time you create a new module or class, helping speed up your development workflow.

Choose the option that best fits your needs.

License

This project is licensed under the MIT License.

damarbob/starcore 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-26