Passed
Push — analysis-A7yJ5k ( c97a2e )
by Arnaud
10:56 queued 05:11
created

Generate::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
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\Builder;
17
use Cecil\Config;
18
use Cecil\Generator\GeneratorManager;
19
use Cecil\Step\AbstractStep;
20
use Psr\Log\LoggerInterface;
21
22
/**
23
 * Generate pages step.
24
 *
25
 * This step is responsible for generating pages based on the configured generators.
26
 * It initializes the generator manager with the generators defined in the configuration
27
 * and processes them to create the pages. The generated pages are then set in the builder.
28
 */
29
class Generate extends AbstractStep
30
{
31
    private GeneratorManager $generatorManager;
32
33
    public function __construct(
34
        Builder $builder,
35
        Config $config,
36
        LoggerInterface $logger,
37
        GeneratorManager $generatorManager
38
    ) {
39
        parent::__construct($builder, $config, $logger);
40
        $this->generatorManager = $generatorManager;
41
    }
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getName(): string
46
    {
47
        return 'Generating pages';
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function init(array $options): void
54
    {
55
        if (\count((array) $this->config->get('pages.generators')) > 0) {
56
            $this->canProcess = true;
57
        }
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function process(): void
64
    {
65
        $generators = (array) $this->config->get('pages.generators');
66
        array_walk($generators, function ($generator, $priority) {
67
            if (!class_exists($generator)) {
68
                $message = \sprintf('Unable to load generator "%s" (priority: %s).', $generator, $priority);
69
                $this->logger->error($message);
70
71
                return;
72
            }
73
            // Use DI container to create the generator if possible
74
            try {
75
                $generatorInstance = $this->builder->get($generator);
76
            } catch (\Exception $e) {
77
                // Fallback: create manually if not in container
78
                $this->logger->debug(\sprintf('Using fallback for generator "%s": %s', $generator, $e->getMessage()));
79
                $generatorInstance = new $generator($this->builder);
80
            }
81
            $this->generatorManager->addGenerator($generatorInstance, $priority);
82
        });
83
        $pages = $this->generatorManager->process();
84
        $this->builder->setPages($pages);
85
    }
86
}
87