Generate::init()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 4
rs 10
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\Pages;
15
16
use Cecil\Generator\GeneratorManager;
17
use Cecil\Step\AbstractStep;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19
20
/**
21
 * Generate pages step.
22
 *
23
 * This step is responsible for generating pages based on the configured generators.
24
 * It initializes the generator manager with the generators defined in the configuration
25
 * and processes them to create the pages. The generated pages are then set in the builder.
26
 */
27
class Generate extends AbstractStep
28
{
29
    /** @var GeneratorManager */
30
    protected $generatorManager;
31
32
    /** @var ContainerInterface */
33
    protected $container;
34
35
    public function __construct(GeneratorManager $generatorManager, ContainerInterface $container)
36
    {
37
        $this->generatorManager = $generatorManager;
38
        $this->container = $container;
39
    }
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getName(): string
44
    {
45
        return 'Generating pages';
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function init(array $options): void
52
    {
53
        if (\count((array) $this->config->get('pages.generators')) > 0) {
54
            $this->canProcess = true;
55
        }
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function process(): void
62
    {
63
        $generators = (array) $this->config->get('pages.generators');
64
        array_walk($generators, function ($generatorClass, $priority) {
65
            if (!class_exists($generatorClass)) {
66
                $message = \sprintf('Unable to load generator "%s" (priority: %s).', $generatorClass, $priority);
67
                $this->builder->getLogger()->error($message);
68
69
                return;
70
            }
71
            // Try to get generator from container, otherwise instantiate manually
72
            try {
73
                $generator = $this->container->get($generatorClass);
74
                // Set builder for generators (needed for context)
75
                if (method_exists($generator, 'setBuilder')) {
76
                    $generator->setBuilder($this->builder);
77
                }
78
            } catch (\Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException $e) {
79
                // Generator not in container or inlined, instantiate manually
80
                // Special case for ExternalBody which needs Converter
81
                if ($generatorClass === 'Cecil\\Generator\\ExternalBody') {
82
                    try {
83
                        $converter = $this->container->get('Cecil\\Converter\\Converter');
84
                    } catch (\Exception $converterException) {
85
                        // Fallback: create converter manually
86
                        $parsedown = new \Cecil\Converter\Parsedown($this->builder);
87
                        $converter = new \Cecil\Converter\Converter($this->builder, $parsedown);
88
                    }
89
                    $generator = new $generatorClass($this->builder, $converter);
90
                } else {
91
                    $generator = new $generatorClass($this->builder);
92
                    // Verify builder is set
93
                    if (property_exists($generator, 'builder')) {
94
                        $reflection = new \ReflectionProperty($generator, 'builder');
95
                        $reflection->setAccessible(true);
96
                        $builderValue = $reflection->getValue($generator);
97
                        if ($builderValue === null) {
98
                            throw new \RuntimeException("Builder not set for generator $generatorClass");
99
                        }
100
                    }
101
                }
102
            }
103
            $this->generatorManager->addGenerator($generator, $priority);
0 ignored issues
show
Bug introduced by
It seems like $generator can also be of type null; however, parameter $generator of Cecil\Generator\GeneratorManager::addGenerator() does only seem to accept Cecil\Generator\GeneratorInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
            $this->generatorManager->addGenerator(/** @scrutinizer ignore-type */ $generator, $priority);
Loading history...
104
        });
105
        $pages = $this->generatorManager->process();
106
        $this->builder->setPages($pages);
107
    }
108
}
109