dwendrich/console-config-resolver

通过 zend-servicemanager 简单创建一个 symfony 控制台应用程序实例。

0.2.1 2017-05-18 09:27 UTC

This package is auto-updated.

Last update: 2024-09-29 04:48:46 UTC


README

通过 zend-servicemanager 简单创建一个 symfony 控制台应用程序实例。

Build Status Coverage Status Latest Stable Version

需求

安装

ConsoleConfigResolver 可以通过 composer 安装。有关如何获取 composer 或如何使用它的信息,请参阅 getcomposer.org

通过命令行安装

$ php composer.phar require dwendrich/console-config-resolver

通过 composer.json 文件安装

{
    "require": {
        "dwendrich/console-config-resolver": "*"
    }
}

用法

作为服务管理器配置的一部分,您可以为控制台应用程序提供一个部分,例如:

return [
    'Example\Console' => [
        'name'     => 'My console application',
        'version'  => '1.0.0',
        'commands' => [
            // provide a class name or a service name configured in service manager
            MyConsoleCommand::class,
            
            // instances have to extend Symfony\Component\Console\Command\Command
            new OtherConsoleCommand(),
        ],
    ],
    
    // in zend framework applications this section is called 'service_manager'
    'dependencies' => [
        'factories' => [
            'Example\Console' => ConsoleConfigResolver\Factory\ConfigResolverFactory::class,
        ],
    ],
];

commands 键下,您可以提供要添加到应用程序中的命令。这些可以是类名或扩展 Symfony\Component\Console\Command\Command 的对象实例。

现在在您的代码中,您可以使用服务管理器创建控制台应用程序实例,例如在名为 console.php 的文件中,您可以这样做

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

chdir(dirname(__FILE__));

call_user_func(function () {
    // get your service manager instance
    $container = require 'config/container.php';
    
    // create the console application as configured in the example above
    $console = $container->get('Example\Console');
    
    // run the application
    $console->run();
});

在终端中运行应用程序,例如:

$ php console.php list