eydamos/console-bundle

Adds autowire functionality to symfony/console.

This package's canonical repository appears to be gone and the package has been frozen as a result.

3.4.3.1 2018-01-15 09:22 UTC

This package is auto-updated.

Last update: 2024-03-21 19:30:10 UTC


README

Adds autowire functionality to symfony/console.

Reason

A standalone symfony/console application without symfony/http-kernel has no autowire functionality.
Each service and each command must be registered manually in contrast to a symfony environment with http-kernel.

Installation

composer require eydamos/console-bundle

Setup

Step 1

Create a bin/console file as an entry point with the following code:

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

use Eydamos\ConsoleBundle\ConsoleKernel;
use Eydamos\ConsoleBundle\ConsoleApplication;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Debug\Debug;
  
// if you don't want to setup permissions the proper way, just uncomment the following PHP line
// read https://symfony.com/doc/current/setup.html#checking-symfony-application-configuration-and-setup
// for more information
//umask(0000);
  
set_time_limit(0);
  
// Composer autoloader requirement
require __DIR__.'/../vendor/autoload.php';
  
$input = new ArgvInput();
$env = $input->getParameterOption(['--env', '-e'], getenv('SYMFONY_ENV') ?: 'dev');
$debug = getenv('SYMFONY_DEBUG') !== '0' && !$input->hasParameterOption(['--no-debug', '']) && $env !== 'prod';
  
if ($debug) {
    Debug::enable();
}
  
$kernel = new ConsoleKernel($env, $debug);
$application = new ConsoleApplication($kernel);
$application->run($input);

Step 2

Create the basic needed configuration. This is the same config you would have in a symfony environment with http-kernel.

app/config/console.yml

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
        public: false       # Allows optimizing the container by removing unused services; this also means
                            # fetching services directly from the container via $container->get() won't work.
                            # The best practice is to be explicit about your dependencies anyway.
    
    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    AppBundle\:
        resource: '../../src/AppBundle/*'
        exclude: '../../src/AppBundle/{Entity,Migrations,Tests}'
    
    # controllers are imported separately to make sure services can be injected
    # as action arguments even if you don't extend any base controller class
    AppBundle\Controller\:
        resource: '../../src/AppBundle/Controller'
        tags: ['controller.service_arguments']
    
    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones

Now all commands in src/AppBundle will be automatically registered and all dependencies are autowired.

Notice

The code of this bundle is basically just a copy of the relevant code from the symfony/http-kernel bundle in a simplified form in order to provide an already present feature without the need to require the overhead of the http-kernel bundel and all it's dependencies.