ConsolePlugin::provideDependencies()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 11
rs 10
ccs 9
cts 9
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 *  This file is part of the Micro framework package.
5
 *
6
 *  (c) Stanislau Komar <[email protected]>
7
 *
8
 *  For the full copyright and license information, please view the LICENSE
9
 *  file that was distributed with this source code.
10
 */
11
12
namespace Micro\Plugin\Console;
13
14
use Micro\Component\DependencyInjection\Autowire\AutowireHelperFactory;
15
use Micro\Component\DependencyInjection\Autowire\AutowireHelperFactoryInterface;
16
use Micro\Component\DependencyInjection\Container;
17
use Micro\Framework\Kernel\Plugin\DependencyProviderInterface;
18
use Micro\Plugin\Console\Business\Factory\ConsoleApplicationFactory;
19
use Micro\Plugin\Console\Business\Factory\ConsoleApplicationFactoryInterface;
20
use Micro\Plugin\Console\Facade\ConsoleApplicationFacade;
21
use Micro\Plugin\Console\Facade\ConsoleApplicationFacadeInterface;
22
use Micro\Plugin\Locator\Facade\LocatorFacadeInterface;
23
24
/**
25
 * A plugin that allows you to provide a console interface for executing commands.
26
 *
27
 * All application classes that inherit "Symfony\Component\Console\Command\Command" will be automatically found and, if necessary, all dependencies will be automatically provided to the constructor.
28
 *
29
 * The plugin is an adapter for the <a href="https://symfony.com/doc/current/components/console.html#learn-more" target="_blank">symfony/console</a> component.
30
 *
31
 * **Installation**
32
 * Install plugin
33
 * ```bash
34
 * $ composer require micro/plugin-console
35
 * ```
36
 *
37
 * Enable plugin
38
 * ```php
39
 * // /etc/plugins.php
40
 *
41
 * return [
42
 *      // ...other bundles
43
 *      Micro\Plugin\Console\ConsolePlugin::class
44
 * ];
45
 * ```
46
 * Register command
47
 * ```php
48
 *
49
 * use Symfony\Component\Console\Command\Command;
50
 * use Symfony\Component\Console\Input\InputInterface;
51
 * use Symfony\Component\Console\Output\OutputInterface;
52
 *
53
 * class MyTestCommand extends Command
54
 * {
55
 *      public function __construct(
56
 *          private readonly SomeDependentFacadeInterface $dependentService
57
 *      )
58
 *      {
59
 *          parent::__construct('test:command');
60
 *      }
61
 *
62
 *      public function configure()
63
 *      {
64
 *          // Configure some options and arguments
65
 *      }
66
 *
67
 *      public function execute(InputInterface $input, OutputInterface $output)
68
 *      {
69
 *          $result = $this->dependentService->doSomething($input->getArgument('some-configured-argument'));
70
 *
71
 *          $output->writeln('Result: ' . $result);
72
 *
73
 *          return self::SUCCESS;
74
 *      }
75
 * }
76
 * ```
77
 *
78
 *  Execute
79
 * ```bash
80
 * $ php bin/console test:command
81
 * ```
82
 *
83
 * @author Stanislau Komar <[email protected]>
84
 *
85
 * @api
86
 */
87
class ConsolePlugin implements DependencyProviderInterface
88
{
89
    private LocatorFacadeInterface $locatorFacade;
90
91
    private AutowireHelperFactoryInterface $autowireHelperFactory;
92
93 1
    public function provideDependencies(Container $container): void
94
    {
95 1
        $container->register(
96 1
            ConsoleApplicationFacadeInterface::class, function (
97 1
                LocatorFacadeInterface $locatorFacade,
98 1
                Container $container
99 1
            ): ConsoleApplicationFacadeInterface {
100 1
                $this->locatorFacade = $locatorFacade;
101 1
                $this->autowireHelperFactory = new AutowireHelperFactory($container);
102
103 1
                return new ConsoleApplicationFacade($this->createConsoleApplicationFactory());
104 1
            }
105 1
        );
106
    }
107
108 1
    protected function createConsoleApplicationFactory(): ConsoleApplicationFactoryInterface
109
    {
110 1
        return new ConsoleApplicationFactory(
111 1
            $this->locatorFacade,
112 1
            $this->autowireHelperFactory->create()
113 1
        );
114
    }
115
}
116