ricardoper/micra

基于 Symfony 组件的 CLI 框架,具有最小开销

v2.0.0 2020-05-25 11:03 UTC

This package is auto-updated.

Last update: 2024-08-25 20:28:39 UTC


README

Micra 是一个基于 Symfony 组件 的 CLI 框架,具有最小开销。

  • PHP >= 7.2
  • 易于配置的定制化
    • 模型
    • 命令
    • 处理器
    • 配置
    • 服务提供者
    • 错误处理器
    • 关闭处理器
  • PSR-3 日志记录器
  • 全局助手
  • 更好的、更详细的错误处理器
  • 使用 PHP dotenv 的环境变量
  • Pimple 依赖注入容器
  • Medoo 数据库框架(MySQL、PostgreSQL、SQLite、MS SQL Server 等)

目录

如何安装

在您想要安装新 Micra 框架 的目录中运行此命令。

composer create-project ricardoper/micra [my-app-name]

注意:

  • [my-app-name] 替换为您新应用程序的所需目录名。
  • 确保 storage/ 可用于网络写入。

最重要的文件夹

  • /app : 应用程序 代码(《App 命名空间 - PSR-4》)
    • ./Commands : 您任务的起点。在这里添加您的 控制器
    • ./Handlers : 处理应用程序指定的行为。在这里添加您的 处理器
    • ./Models : 管理应用程序的数据、逻辑和规则。在这里添加您的 模型
    • ./Services : 定义绑定并注入依赖。在这里添加您的 服务提供者
  • /configs : 在这里添加/修改您的 配置

全局助手

  • env(string $variable, string $default) - 返回 环境 变量(使用 DotEnv)
  • app() - 返回 App 实例
  • container(string $name) - 返回 容器 注册的数据
  • configs(string $variable, string $default) - 返回 配置 数据
  • base_path(string $path) - 返回 基础路径 位置
  • app_path(string $path) - 返回 app 路径 位置
  • configs_path(string $path) - 返回 配置路径 位置
  • storage_path(string $path) - 返回 存储路径 位置

仅开发的全局助手

  • dump($var1, $var2, ...) - 输出变量
  • dd($var1, $var2, ...) - 输出 & 停止变量

配置

您可以添加任意数量的配置文件(/configs)。这些文件将根据所选环境自动预加载并合并到容器中。

如果您有一个名为 "sandbox" 的环境,并且只想为此环境覆盖一些配置,您需要在 /configs 中创建一个名为 "sandbox" 的子文件夹。例如 /configs/sandbox。然后创建一个包含您需要替换的配置以及相应键和值的文件。

/configs/logger.php

return [

    'name' => 'app',

    'maxFiles' => 7,

];

/configs/local/logger.php

return [

    'name' => 'app-local',

];

环境的 name 结果

  • prod : 'app'
  • sandbox : 'app'
  • local : 'app-local'
  • testing : 'app'

注意:您可以在本框架中查看 local 环境的示例。

配置点表示法

您可以使用 点表示法 从配置中获取值。

/configs/example.php

return [

    'types' => [
        'mysql' => [
            'host' => 'localhost',
            'port' => '3306',
        ],
        'postgre' => [
            'host' => 'localhost',
            'port' => '3306',
        ],
    ],

];

如果您想获取 MySQL 类型的 host

$this->getConfigs('example.types.mysql.host')  => 'localhost'

configs('example.types.mysql.host') => 'localhost'

container('configs')->get('example.types.mysql.host') => 'localhost'

命令

您任务开始的起点。

您可以以整洁的方式添加任意多的 命令/app/Commands)。

添加您的 命令 后,您可以在配置文件 config/[env/]commands.php 中启用或禁用它。

注意:要使用助手,您必须使用位于 \App\Kernel\AbstractsControllerAbstract 扩展 命令

use App\Kernel\Abstracts\CommandAbstract;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class HelloCommand extends CommandAbstract
{

    /**
     * Command name
     *
     * @var string
     */
    protected $cmdName = 'Hello';


    /**
     * Configure/explain command
     */
    protected function configure()
    {
        $this
            ->setName($this->cmdName)
            ->setDescription('Hello World!');

    }

    /**
     * Execute command process
     *
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return int
     */
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $output->writeln('Hello World!');

        return 0;
    }
}

config/[env/]commands.php 中启用它

use App\Commands\Demo\HelloCommand;

return [

    'hello' => HelloCommand::class,

];

命令助手

  • getApp() - 返回 App 对象
  • getContainer(string $name) - 返回 App 容器
  • getConfigs(string $name) - 返回 App 配置
  • getService(string $service) - 通过名称从容器中返回 服务提供者

内置命令

  • Deps:是一个内置命令,用于检查您的系统是否满足运行此框架的所有依赖项。
  • Hello:仅用于 本地 环境的命令(可以删除),仅用于演示目的。
  • Example:仅用于 本地 环境的命令(可以删除),仅用于演示目的。
  • Addresses:仅用于 本地 环境的命令(可以删除),仅用于演示目的。

运行命令

使用 micra PHP 可执行文件运行命令。
./micra [command-name]

如果您需要帮助
./micra -h

如果您需要命令帮助
./micra [command-name] -h

模型

管理应用程序的数据、逻辑和规则。

您可以以整洁的方式添加任意多的 模型/app/Models)。

添加您的 模型 后,您可以在控制器中用它,例如。

注意:要使用助手,您必须使用位于 \App\Kernel\AbstractsModelAbstract 扩展 模型

use App\Kernel\Abstracts\ModelAbstract;
use PDO;

class AddressesModel extends ModelAbstract
{

    /**
     * Get Last Addresses with Pdo
     *
     * @param int $limit
     * @return array
     */
    public function getLastWithPdo(int $limit = 25): array
    {
        /** @var $db PDO */
        $db = $this->getDb()->pdo;

        $sql = 'SELECT `address`.`address_id`,`address`.`address`,`address`.`address2`,`address`.`district`,`city`.`city`,`address`.`postal_code`,`address`.`phone` FROM `address` ';
        $sql .= 'LEFT JOIN `city` ON `address`.`city_id` = `city`.`city_id` ';
        $sql .= 'ORDER BY `address_id` DESC LIMIT 10';

        return $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);
    }

模型助手

  • getApp() - 返回 App 对象
  • getContainer(string $name) - 返回 App 容器
  • getConfigs(string $name) - 返回 App 配置
  • getService(string $service) - 通过名称从容器中返回 服务提供者
  • getDb() - 返回 数据库 对象

服务提供者

定义绑定和注入依赖项。

您可以以整洁的方式添加任意多的 服务提供者/app/Services)。

添加您的 服务提供者 后,您可以在配置文件 configs/services.php 中启用或禁用它。

注意服务提供者 必须遵守位于 \App\Kernel\InterfacesServiceProviderInterface

use App\Kernel\Interfaces\ServiceProviderInterface;
use Pimple\Container;

class ExampleServiceProvider implements ServiceProviderInterface
{

    /**
     * Service register name
     */
    public function name(): string
    {
        return 'example';
    }

    /**
     * Register new service on dependency container
     *
     * @param Container $container
     * @return mixed
     */
    public function register(Container $container)
    {
        return function (Container $c) {
            unset($c);

            return new Example();
        };
    }
}

configs/services.php 中启用它

use App\Services\Example\ExampleServiceProvider;

return [

    'example' => ExampleServiceProvider::class,

];

处理器

处理应用程序的特定行为。

您可以以整洁的方式覆盖以下处理器(/app/Handlers

  • ErrorHandler(默认位于 /app/Handlers/ErrorHandler
  • ShutdownHandler(默认位于 /app/Handlers/ShutdownHandler

添加您的 处理器 后,您可以在配置文件 configs/app.php 中启用或禁用它。

use App\Handlers\ErrorHandler;
use App\Handlers\ShutdownHandler;

return [

    // Handlers //
    'errorHandler' => ErrorHandler::class,

    'shutdownHandler' => ShutdownHandler::class,

数据库支持

Medoo 作为 服务提供者 默认实现。使用 是可选的,默认不启用。

要使用 Medoo 启用数据库支持,您需要使用 Composer 添加此库/供应商

composer require catfan/medoo

安装后,您需要在 configs/services.php 中启用 服务提供者

use App\Services\Database\DatabaseServiceProvider;

return [

    'database' => DatabaseServiceProvider::class,

];

现在您已经准备好使用它了...

如果您需要更多信息,请访问 Medoo 网页:[https://medoo.in/](https://medoo.in/)

注意:

  • 不要忘记为您的数据库加载 PDO 扩展。例如,如果您需要 MySQL,则需要安装 pdo_mysql PHP 扩展。
  • 您可以使用其他库作为 服务提供者(MySQLi、PostgreSQL、MongoDB、Redis 的本地驱动程序等)。

异常

您有一些开箱即用的 异常,位于 \App\Kernel\Exceptions,您可以使用它

CommandException  - For Commands Exceptions
ConfigsException  - For Configurations Exceptions
ModelException    - For Models Exceptions
ServiceException  - For Service Providers Exceptions

日志记录

日志始终启用,您可以在 /storage/logs/app-[date].log 中查看所有输出。

注意:日志将自动由日志记录器删除,超过 7 天的日志将被删除。

基准测试

用于Hello命令的一些数字...

机器
英特尔® 酷睿™ i5-8400 CPU @ 2.80GHz × 6
16Gb 内存
固态硬盘

版本
Ubuntu 20.04 LTS
Docker v19.03.8
PHP v7.4.3
PHP v7.2.24
启用Zend OPcache


享受简单性 :oP