Passed
Push — feat/exclude-content ( 8a37c6 )
by Arnaud
04:51
created

Load::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
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\Content;
15
16
use Cecil\Step\AbstractStep;
17
use Cecil\Util;
18
use Symfony\Component\Finder\Finder;
19
20
/**
21
 * Loads content.
22
 */
23
class Load extends AbstractStep
24
{
25
    /** @var string */
26
    protected $page;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function getName(): string
32
    {
33
        return 'Loading content';
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function init(array $options): void
40
    {
41
        /** @var \Cecil\Builder $builder */
42
        if (is_dir($this->builder->getConfig()->getContentPath())) {
43
            $this->canProcess = true;
44
        }
45
        $this->page = $options['page'];
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function process(): void
52
    {
53
        $namePattern = '/\.('.implode('|', (array) $this->builder->getConfig()->get('content.ext')).')$/';
54
        $content = Finder::create()
55
            ->files()
56
            ->in($this->builder->getConfig()->getContentPath())
57
            ->sortByName(true);
58
        if ($this->page) {
59
            if (!util\File::getFS()->exists(Util::joinFile($this->builder->getConfig()->getContentPath(), $this->page))) {
60
                $this->builder->getLogger()->error(sprintf('File "%s" doesn\'t exist.', $this->page));
61
            }
62
            $content->path('.')->path(dirname($this->page));
63
            $content->name('/index\.('.implode('|', (array) $this->builder->getConfig()->get('content.ext')).')$/');
64
            $namePattern = basename($this->page);
65
        }
66
        $content->name($namePattern);
67
        if (is_array($this->builder->getConfig()->get('content.exclude'))) {
68
            $content->exclude($this->builder->getConfig()->get('content.exclude'));
69
            $content->notPath($this->builder->getConfig()->get('content.exclude'));
70
            $content->notName($this->builder->getConfig()->get('content.exclude'));
71
        }
72
        if (file_exists(Util::joinFile($this->builder->getConfig()->getContentPath(), '.gitignore'))) {
73
            $content->ignoreVCSIgnored(true);
74
        }
75
        $this->builder->setContent($content);
76
77
        $count = $content->count();
78
        if ($count === 0) {
79
            $this->builder->getLogger()->info('Nothing to load');
80
81
            return;
82
        }
83
        $this->builder->getLogger()->info('Files loaded', ['progress' => [$count, $count]]);
84
    }
85
}
86