定制 morningtrain/php-loader 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

morningtrain/php-loader

Composer 安装命令:

composer require morningtrain/php-loader

包简介

A class for loading PHP files and classes in a PSR-4 directory

README 文档

README

A simple PHP File or class loader for PHP. Built with PHP.

Table of Contents

Introduction

This package is a tool to help you initialize parts of your projects by loading in all files that match certain filename rules in a defined directory.

You may even initialize the classes contained in these files as long as they are PSR-4 compliant.

For instance, you may use this tool to load all files in a "/routes" directory or all files ending with "Block.php" and initialize all found classes that extend Block and then call init on them.

More specifically, this tool is made for, but not dependent on, the WP-Framework. Here it is useful for loading in all routes, registering all blocks and initializing all Hooks.

Getting Started

To get started install the package as described below in Installation.

To use the tool have a look at Usage

Installation

composer require morningtrain/php-loader

Dependencies

symfony/finder

Finder is used to find files in the directory

Usage

First create a Loader using Loader::create. This takes an absolute path to the directory you wish to load from as an argument and returns a Loader. The Loader is further configured by chaining.

In its simples form the Loader only needs a path. This will tell it to load all .php files in that directory using require

    // Loading all PHP files in ./MyDir
    use Morningtrain\PHPLoader\Loader;
    
    Loader::create(__DIR__ . '/MyDir');

Multiple Paths

You may supply an array of full paths to Loader::create if you need to handle multiple directories;

    // Loading all PHP files in ./MyDir and ./MyOtherDir
    use Morningtrain\PHPLoader\Loader;
    
    Loader::create([__DIR__ . '/MyDir',__DIR__ . '/MyOtherDir']);

Filename Constraints

To limit the loader to only load files with a given name use fileName(string|array $filename) See Symfoni Finder: File Name for options.

By default $fileName is *.php

    // Loading all PHP files that end with "Foo" in ./MyDir
    use Morningtrain\PHPLoader\Loader;
    
    Loader::create(__DIR__ . '/MyDir')
        ->fileName('*Foo.php');

Using Multiple file names

If you need to allow multiple filename formats then supply an array for Loader::fileName

Loading Classes

As long as no class related options are set on the Loader it will simply load the files.

This is useful for route files and similar.

If you have classes that you wish to load and initialize then read on!

Note: All files will be loaded even if the class requirements are not fulfilled. The Loader has no knowledge of its classes before they are loaded.

Has Method

Aborts handling a found class if it does not have a specific method.

Note: it is not necessary to specify hasMethod if call or callStatic is used.

    // Loading all PHP files in ./MyDir and invoke them if they have the method myMethod
    use Morningtrain\PHPLoader\Loader;
    
    Loader::create(__DIR__ . '/MyDir')
        ->hasMethod('myMethod')
        ->invoke();

Call Static

To call a static method on all loaded classes specify the method using Loader::callStatic($methodName)

This will call said method on every loaded class that has it. You do not need to check using Loader::hasMethod beforehand

    // Loading all PHP files in ./MyDir and call a static method on it if it is of the class Foo
    use Morningtrain\PHPLoader\Loader;
    
    Loader::create(__DIR__ . '/MyDir')
        ->isA(\Foo::class)
        ->callStatic('myMethod');

Class or inheritance requirement

To only call methods on classes that are of a given class or extended from it use Loader::isA($className). This works the same way is ia_a($obj,$class) where $obj is the found class.

If the found class does not match the required class then the Loader will stop handling the current class and the class will never be constructed or called.

    // Loading all PHP files in ./MyDir and call a static method on it if it is of the class Foo
    use Morningtrain\PHPLoader\Loader;
    
    Loader::create(__DIR__ . '/MyDir')
        ->isA(\Foo::class)
        ->callStatic('myMethod');

Constructing, invoking or calling

You can also construct an instance from the loaded classes, call a method on an instance or invoke an instance using the Loader.

If you use Loader::invoke or Loader::call then it is not necessary to use Loader::construct as well

    // Loading all PHP files in ./MyDir, construct them and then call 'myMethod'
    use Morningtrain\PHPLoader\Loader;
    
    Loader::create(__DIR__ . '/MyDir')
        ->call('myMethod');

Credits

Testing

composer test

License

The MIT License (MIT). Please see License File for more information.

morningtrain/php-loader 适用场景与选型建议

morningtrain/php-loader 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.91k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 06 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「loader」 「morningtrain」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 morningtrain/php-loader 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 3.91k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 16
  • 依赖项目数: 8
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-06-30