00f100/fcphp-autoload

该包的最新版本(0.3.1)没有提供许可证信息。

将自动加载提供者和路由导入到composer供应商包中

0.3.1 2018-08-04 19:01 UTC

This package is auto-updated.

Last update: 2024-09-18 06:18:36 UTC


README

自动加载提供者和路由到其他composer包的包

Build Status codecov Total Downloads

如何安装

Composer

$ composer require 00f100/fcphp-autoload

或在composer.json中添加

{
    "require": {
        "00f100/fcphp-autoload": "*"
    }
}

如何使用

创建我的 providers.php

<?php

return [
    \path\to\SomeClass::class,
    \path\to\package\Cool::class
];

创建我的 routes.php

<?php

return [
    'path/to/route' => [
        'post' => 'SiteController@method'
    ]
];

在目录中查找内容

<?php

use FcPhp\Autoload\Autoload;

/**
 * Method to load path and find match's
 *
 * @param string $pathExpression Directory to find file(s)
 * @param array $fileNameMatch List of filename
 * @param array $extensionMatch List of enable extensions
 * @return void
 */
$autoload = Autoload::getInstance();
$autoload->path(string $pathExpression, array $fileNameMatch, array $extensionMatch);

/*
    Example to find inside composer directory

    ============================================
    Example directory:
    ============================================

    vendor/
        00f100/
            fcphp-di/
                autoload/
                    providers.php
                    prividers.txt
            fcphp-i18n/
            fcphp-provider/
                autoload/
                    routes.php
        doctrine/
            doctrine/
            instructor/
        cake/
            bin/
            cake/
                autoload/
                    providers.php

*/
$autoload->path('vendor/*/*/autoload', ['providers', 'routes'], ['php']);
/*
    ============================================
    Below example match this files:
    ============================================

    vendor/00f100/fcphp-di/autoload/providers.php
    vendor/00f100/fcphp-provider/autoload/routes.php
    vendor/cake/cake/autoload/providers.php
*/

获取文件内容

/*
    ============================================
    Get the content using 'get' method
    ============================================
    [
        'path/to/route' => [
            'post' => 'SiteController@method'
        ]
    ]
    $arrayProviders = $autoload->get('providers');

    [
        \path\to\SomeClass,
        \path\to\package\Cool
    ]
    $arrayRoutes = $autoload->get('routes');
*/
/**
 * Method to return autoloaded files
 *
 * @param string $key Filename
 * @return array
 */
$autoload->get(string $fileName);

触发器

匹配前

此闭包在匹配运行之前执行

$instance->beforeMatch(function(string $pathExpression, array $fileNameMatch, array $extensionMatch) {
    // your code here
});

再次匹配前

此闭包在再次匹配某些目录之前执行

$instance->beforeMatchAgain(function(array $paths, array $files, array $extensions, string $path, string $now) {
    // your code here
});

存储前

此闭包在存储文件内容之前执行

$instance->beforeStorage(function(string $file, string $filePath) {
    // your code here
});