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
|
|
|
throw new RuntimeException(sprintf('Pages path "%s" not found.', $this->config->getPagesPath())); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
$this->page = $options['page']; |
48
|
1 |
|
$this->canProcess = true; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
1 |
|
public function process(): void |
55
|
|
|
{ |
56
|
1 |
|
$namePattern = '/\.(' . implode('|', (array) $this->config->get('pages.ext')) . ')$/'; |
57
|
1 |
|
$pages = Finder::create() |
58
|
1 |
|
->files() |
59
|
1 |
|
->in($this->config->getPagesPath()) |
60
|
1 |
|
->sort(function (SplFileInfo $a, SplFileInfo $b): int { |
61
|
|
|
// root pages first |
62
|
1 |
|
if (empty($a->getRelativePath()) && !empty($b->getRelativePath())) { |
63
|
1 |
|
return -1; |
64
|
|
|
} |
65
|
1 |
|
if (empty($b->getRelativePath()) && !empty($a->getRelativePath())) { |
66
|
1 |
|
return 1; |
67
|
|
|
} |
68
|
|
|
// section's index first |
69
|
1 |
|
if ($a->getRelativePath() == $b->getRelativePath() && $a->getBasename('.' . $a->getExtension()) == 'index') { |
70
|
1 |
|
return -1; |
71
|
|
|
} |
72
|
1 |
|
if ($b->getRelativePath() == $a->getRelativePath() && $b->getBasename('.' . $b->getExtension()) == 'index') { |
73
|
|
|
return 1; |
74
|
|
|
} |
75
|
|
|
// sort by name |
76
|
|
|
return strnatcasecmp($a->getRelativePath(), $b->getRelativePath()); |
77
|
|
|
}); |
78
|
|
|
// load only one page? |
79
|
|
|
if ($this->page) { |
80
|
|
|
// is the page path starts with the `pages.dir` configuration option? |
81
|
|
|
// (i.e.: `pages/...`, `/pages/...`, `./pages/...`) |
82
|
|
|
$pagePathAsArray = explode(DIRECTORY_SEPARATOR, Util::joinFile($this->page)); |
83
|
|
|
if ($pagePathAsArray[0] == (string) $this->config->get('pages.dir')) { |
84
|
|
|
unset($pagePathAsArray[0]); |
85
|
|
|
$this->page = implode(DIRECTORY_SEPARATOR, $pagePathAsArray); |
86
|
|
|
} |
87
|
|
|
if ($pagePathAsArray[0] == '.' && $pagePathAsArray[1] == (string) $this->config->get('pages.dir')) { |
88
|
|
|
unset($pagePathAsArray[0]); |
89
|
|
|
unset($pagePathAsArray[1]); |
90
|
|
|
$this->page = implode(DIRECTORY_SEPARATOR, $pagePathAsArray); |
91
|
|
|
} |
92
|
1 |
|
if (!util\File::getFS()->exists(Util::joinFile($this->config->getPagesPath(), $this->page))) { |
93
|
1 |
|
$this->builder->getLogger()->error(sprintf('File "%s" doesn\'t exist.', $this->page)); |
94
|
1 |
|
} |
95
|
1 |
|
$pages->path('.')->path(\dirname($this->page)); |
96
|
1 |
|
$pages->name('/index\.(' . implode('|', (array) $this->config->get('pages.ext')) . ')$/'); |
97
|
|
|
$namePattern = basename($this->page); |
98
|
1 |
|
} |
99
|
|
|
$pages->name($namePattern); |
100
|
|
|
if (\is_array($exclude = $this->config->get('pages.exclude'))) { |
101
|
1 |
|
$pages->exclude($exclude); |
102
|
|
|
$pages->notPath($exclude); |
103
|
1 |
|
$pages->notName($exclude); |
104
|
1 |
|
} |
105
|
1 |
|
if (file_exists(Util::joinFile($this->config->getPagesPath(), '.gitignore'))) { |
106
|
|
|
$pages->ignoreVCSIgnored(true); |
107
|
|
|
} |
108
|
|
|
$this->builder->setPagesFiles($pages); |
109
|
|
|
|
110
|
1 |
|
$total = $pages->count(); |
111
|
1 |
|
$count = 0; |
112
|
1 |
|
if ($total === 0) { |
113
|
|
|
$this->builder->getLogger()->info('Nothing to load'); |
114
|
|
|
|
115
|
|
|
return; |
116
|
|
|
} |
117
|
|
|
foreach ($pages as $file) { |
118
|
|
|
$count++; |
119
|
|
|
$this->builder->getLogger()->info(sprintf('File "%s" loaded', $file->getRelativePathname()), ['progress' => [$count, $total]]); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|