Passed
Push — di ( 61d74f...f00c6d )
by Arnaud
15:03 queued 11:43
created

StepRegistry::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * This file is part of Cecil.
5
 *
6
 * (c) Arnaud Ligny <[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
declare(strict_types=1);
13
14
namespace Cecil\Step;
15
16
use Cecil\Builder;
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18
19
/**
20
 * Step Registry for managing build steps with dependency injection.
21
 *
22
 * This registry provides a centralized way to manage build steps
23
 * by retrieving them from the DI container when available.
24
 */
25
class StepRegistry
26
{
27
    /**
28
     * @var ContainerInterface
29
     */
30
    private ContainerInterface $container;
31
32
    /**
33
     * @var Builder
34
     */
35
    private Builder $builder;
36
37
    /**
38
     * Constructor.
39
     *
40
     * @param Builder $builder The builder instance
41
     * @param ContainerInterface $container DI container
42
     */
43
    public function __construct(Builder $builder, ContainerInterface $container)
44
    {
45
        $this->builder = $builder;
46
        $this->container = $container;
47
    }
48
49
    /**
50
     * Creates a step instance.
51
     *
52
     * Tries to retrieve the step from the container first,
53
     * otherwise instantiates it directly.
54
     *
55
     * @param string $stepClass The step class name
56
     * @return StepInterface The step instance
57
     */
58
    public function createStep(string $stepClass): StepInterface
59
    {
60
        // Try to get from container if registered
61
        if ($this->container->has($stepClass)) {
62
            $step = $this->container->get($stepClass);
63
            // Set builder for DI-injected steps (they may have other dependencies injected)
64
            if (method_exists($step, 'setBuilder')) {
65
                $step->setBuilder($this->builder);
66
            }
67
            return $step;
68
        }
69
70
        // Fallback to direct instantiation
71
        return new $stepClass($this->builder);
72
    }
73
74
    /**
75
     * Gets all steps for the build process.
76
     *
77
     * @param array $options Build options
78
     * @return StepInterface[] Array of initialized and processable steps
79
     */
80
    public function getSteps(array $options): array
81
    {
82
        $steps = [];
83
84
        foreach (Builder::STEPS as $stepClass) {
85
            $step = $this->createStep($stepClass);
86
            $step->init($options);
87
88
            if ($step->canProcess()) {
89
                $steps[] = $step;
90
            }
91
        }
92
93
        return $steps;
94
    }
95
}
96