cilex/console-service-provider

控制台服务提供者

1.1.0 2014-07-28 20:37 UTC

This package is auto-updated.

Last update: 2024-09-22 05:30:22 UTC


README

Console提供作为服务给Pimple应用程序。

需求

  • PHP 5.3+
  • Pimple ~2.1
  • Symfony Console ~2.4

安装

通过Composercilex/console-service-provider方式。

使用方法

Pimple

<?php
use Cilex\Provider\Console\ConsoleServiceProvider;

$app = new Pimple\Container;

$app['console.name'] = 'MyApp';
$app['console.version'] = '1.0.5';

$consoleServiceProvider = new ConsoleServiceProvider;
$consoleServiceProvider->register($app);

$app['console']->run();

Silex

<?php
use Acme\Console\Command;
use Cilex\Provider\Console\ConsoleServiceProvider;
use Silex\Application;

$app = new Application;

$app->register(new ConsoleServiceProvider(), array(
    'console.name' => 'MyApp',
    'console.version' => '1.0.5',
));

$app['console']->add(new Command\XyzInfoCommand());
$app['console']->add(new Command\XyzSnapshotCommand());

$app['console']->run();

Cilex

控制台服务提供者已经集成在Cilex应用程序中,因此无需手动注册。

<?php
use Cilex\Application;

$app = new Application('MyApp', '1.0.5');

$app->command(new Command\XyzInfoCommand());

$app->run();

配置

参数

  • console.name: 控制台应用程序的名称。
  • console.version: 控制台应用程序的版本。
  • console.class: 要创建的控制台应用程序的类。

服务

  • console: 控制台应用程序,实例 Symfony\Component\Console\Application

控制台服务提供者应用程序类

默认情况下,控制台服务提供者将实例化一个\Cilex\Provider\Console\ContainerAwareApplication的实例。

方法

getContainer() : \Pimple\Container

返回Pimple容器。

getService($name) : mixed|null

返回应用程序容器中的服务,如果未找到则返回null。这是一个方便的方法,以避免重复调用getContainer()或需要分配容器。

从命令中访问容器和服务

以下是一些从命令中访问容器和服务的示例

<?php

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class SomeCommand extends Command
{
    protected function configure()
    {
        // configure the command
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // Direct access to the Container.
        $container = $this->getApplication()->getContainer();

        // Direct access to a service.
        $service = $this->getApplication->getService('some.service');
    }
}

许可

MIT,见LICENSE。