Passed
Push — feat/content_to_pages ( 41bab6 )
by Arnaud
03:44
created

Load::process()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 33
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 24
c 0
b 0
f 0
nc 24
nop 0
dl 0
loc 33
rs 8.9137
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Cecil\Step\Pages;
15
16
use Cecil\Exception\RuntimeException;
17
use Cecil\Step\AbstractStep;
18
use Cecil\Util;
19
use Symfony\Component\Finder\Finder;
20
21
/**
22
 * Loads pages.
23
 */
24
class Load extends AbstractStep
25
{
26
    /** @var string */
27
    protected $page;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function getName(): string
33
    {
34
        return 'Loading pages';
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function init(array $options): void
41
    {
42
        // legacy support
43
        if (is_dir(Util::joinFile($this->builder->getConfig()->getSourceDir(), 'content'))) {
44
            $this->builder->getLogger()->alert('"content" directory is deprecated, please rename it to "pages"');
45
        }
46
47
        if (!is_dir($this->builder->getConfig()->getPagesPath())) {
48
            throw new RuntimeException(\sprintf('Pages path "%s" not found.', $this->builder->getConfig()->getPagesPath()));
49
        }
50
51
        $this->page = $options['page'];
52
        $this->canProcess = true;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function process(): void
59
    {
60
        $namePattern = '/\.('.implode('|', (array) $this->builder->getConfig()->get('pages.ext')).')$/';
61
        $content = Finder::create()
62
            ->files()
63
            ->in($this->builder->getConfig()->getPagesPath())
64
            ->sortByName(true);
65
        if ($this->page) {
66
            if (!util\File::getFS()->exists(Util::joinFile($this->builder->getConfig()->getPagesPath(), $this->page))) {
67
                $this->builder->getLogger()->error(sprintf('File "%s" doesn\'t exist.', $this->page));
68
            }
69
            $content->path('.')->path(dirname($this->page));
70
            $content->name('/index\.('.implode('|', (array) $this->builder->getConfig()->get('pages.ext')).')$/');
71
            $namePattern = basename($this->page);
72
        }
73
        $content->name($namePattern);
74
        if (is_array($this->builder->getConfig()->get('pages.exclude'))) {
75
            $content->exclude($this->builder->getConfig()->get('pages.exclude'));
76
            $content->notPath($this->builder->getConfig()->get('pages.exclude'));
77
            $content->notName($this->builder->getConfig()->get('pages.exclude'));
78
        }
79
        if (file_exists(Util::joinFile($this->builder->getConfig()->getPagesPath(), '.gitignore'))) {
80
            $content->ignoreVCSIgnored(true);
81
        }
82
        $this->builder->setPagesFiles($content);
83
84
        $count = $content->count();
85
        if ($count === 0) {
86
            $this->builder->getLogger()->info('Nothing to load');
87
88
            return;
89
        }
90
        $this->builder->getLogger()->info('Files loaded', ['progress' => [$count, $count]]);
91
    }
92
}
93