实际上/预加载

该软件包最新版本(v1.0.1)没有可用的许可证信息。

PHP >=7.4 的预加载

v1.0.1 2021-05-10 06:48 UTC

README

PHP >=7.4 的预加载。预加载是 PHP 的一个功能,它将预编译 PHP 函数和类到 opcache。因此,这些函数和类可以在您的程序中直接使用,无需引入文件,这提高了速度。要了解更多关于 PHP 预加载的信息,您可以查看 opcache.preloading 文档

安装

首选方法是使用 composer。

composer require practically/preloader

用法

安装后,您需要创建一个预加载可执行文件。

#!/usr/bin/env php
<?php

use Composer\Autoload\ClassLoader;
use Practically\Preloading\Preloader;

if (!function_exists('opcache_compile_file') || !ini_get('opcache.enable')) {
    echo 'Opcache is not available.';
    die(1);
}

if ('cli' === PHP_SAPI && !ini_get('opcache.enable_cli')) {
    echo 'Opcache is not enabled for CLI applications.';
    die(1);
}

/** @var ClassLoader */
$autoloader = require __DIR__ . '/vendor/autoload.php';
$preloader = new Preloader($autoloader);

/**
 * Set regex patterns of classes you want to exclude from preloading. In this
 * example any of the yii2 dev classes and the `Object` class that has been removed
 * and is no longer used
 */
$preloader->exclude('/^yii\\\composer/');
$preloader->exclude('/^yii\\\test/');
$preloader->exclude('/yii\\\base\\\Object/');

/**
 * A regex pattern of classes to preload this will get of the preloader and
 * preload all of the yii2 classes excluding the above and all of the app
 * classes
 */
$preloader->preload('/^yii/');
$preloader->preload('/^app/');

/**
 * Preload a directory of php files recucivly
 */
$preloader->preloadDirectory(__DIR__ . '/views');

预加载器在底层使用 composer 类映射,因此您需要使用 composer 优化的自动加载器(运行 composer 时使用 -o 标志)生成类映射。

composer install -o

完成所有这些后,您需要配置 php.ini 以使用预加载脚本来将类和文件预加载到 opcache。

;
; Ensure opcache is enable
;
opcache.enable=1
;
; You may need to ensure opcache is enable for cli scripts if needed
;
opcache.enable_cli=1
;
; Set the path to the preload script and setup the user if needed
;
opcache.preload=/path/to/preload.php
opcache.preload_user=user

贡献

设置

克隆存储库并运行 composer install。然后开始修改!

测试

所有新功能或错误修复都必须经过测试。测试使用 phpunit 进行,可以通过以下命令运行:

composer run-script test

编码标准

此库使用 Practically 编码标准和 squizlabs/php_codesniffer 进行代码检查。为此提供了一个 composer 脚本

composer run-script cs:check

拉取请求

在您创建包含更改的拉取请求之前,预提交脚本必须通过。可以按照以下方式运行:

composer run-script pre-commit

致谢

此软件包由 Practically.io 创建和维护