Passed
Pull Request — master (#2117)
by Arnaud
19:02 queued 12:57
created

Load::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.3149

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 11
ccs 4
cts 7
cp 0.5714
crap 2.3149
rs 10
c 0
b 0
f 0
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
use Symfony\Component\Finder\SplFileInfo;
21
22
/**
23
 * Loads pages.
24
 */
25
class Load extends AbstractStep
26
{
27
    /** @var string */
28
    protected $page;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 1
    public function getName(): string
34
    {
35 1
        return 'Loading pages';
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 1
    public function init(array $options): void
42
    {
43 1
        if (!is_dir($this->config->getPagesPath())) {
44
            $this->builder->getLogger()->debug(\sprintf('"%s" is not a valid pages directory', $this->config->getPagesPath()));
45
            $this->canProcess = false;
46
47
            return;
48
        }
49
50 1
        $this->page = $options['page'];
51 1
        $this->canProcess = true;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 1
    public function process(): void
58
    {
59 1
        $namePattern = '/\.(' . implode('|', (array) $this->config->get('pages.ext')) . ')$/';
60 1
        $pages = Finder::create()
61 1
            ->files()
62 1
            ->in($this->config->getPagesPath())
63 1
            ->sort(function (SplFileInfo $a, SplFileInfo $b): int {
64
                // section's index first
65 1
                if ($a->getRelativePath() == $b->getRelativePath() && $a->getBasename('.' . $a->getExtension()) == 'index') {
66 1
                    return -1;
67
                }
68 1
                if ($b->getRelativePath() == $a->getRelativePath() && $b->getBasename('.' . $b->getExtension()) == 'index') {
69 1
                    return 1;
70
                }
71
                // sort by name
72 1
                return strnatcasecmp($a->getRealPath(), $b->getRealPath());
73 1
            });
74
        // load only one page?
75 1
        if ($this->page) {
76
            // is the page path starts with the `pages.dir` configuration option?
77
            // (i.e.: `pages/...`, `/pages/...`, `./pages/...`)
78
            $pagePathAsArray = explode(DIRECTORY_SEPARATOR, Util::joinFile($this->page));
79
            if ($pagePathAsArray[0] == (string) $this->config->get('pages.dir')) {
80
                unset($pagePathAsArray[0]);
81
                $this->page = implode(DIRECTORY_SEPARATOR, $pagePathAsArray);
82
            }
83
            if ($pagePathAsArray[0] == '.' && $pagePathAsArray[1] == (string) $this->config->get('pages.dir')) {
84
                unset($pagePathAsArray[0]);
85
                unset($pagePathAsArray[1]);
86
                $this->page = implode(DIRECTORY_SEPARATOR, $pagePathAsArray);
87
            }
88
            if (!util\File::getFS()->exists(Util::joinFile($this->config->getPagesPath(), $this->page))) {
89
                $this->builder->getLogger()->error(\sprintf('File "%s" doesn\'t exist.', $this->page));
90
            }
91
            $pages->path('.')->path(\dirname($this->page));
92
            $pages->name('/index\.(' . implode('|', (array) $this->config->get('pages.ext')) . ')$/');
93
            $namePattern = basename($this->page);
94
        }
95 1
        $pages->name($namePattern);
96 1
        if (\is_array($exclude = $this->config->get('pages.exclude'))) {
97 1
            $pages->exclude($exclude);
98 1
            $pages->notPath($exclude);
99 1
            $pages->notName($exclude);
100
        }
101 1
        if (file_exists(Util::joinFile($this->config->getPagesPath(), '.gitignore'))) {
102
            $pages->ignoreVCSIgnored(true);
103
        }
104 1
        $this->builder->setPagesFiles($pages);
105
106 1
        $total = $pages->count();
107 1
        $count = 0;
108 1
        if ($total === 0) {
109
            $this->builder->getLogger()->info('Nothing to load');
110
111
            return;
112
        }
113 1
        foreach ($pages as $file) {
114 1
            $count++;
115 1
            $this->builder->getLogger()->info(\sprintf('File "%s" loaded', $file->getRelativePathname()), ['progress' => [$count, $total]]);
116
        }
117
    }
118
}
119