|
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\Step\AbstractStep; |
|
17
|
|
|
use Cecil\Util; |
|
18
|
|
|
use Symfony\Component\Finder\Finder; |
|
19
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Load pages step. |
|
23
|
|
|
* |
|
24
|
|
|
* This step is responsible for loading pages from the configured pages directory. |
|
25
|
|
|
* It initializes the pages finder, applies sorting, and filters based on the |
|
26
|
|
|
* specified page or the default configuration. It also handles exclusions and |
|
27
|
|
|
* respects the `.gitignore` file if present. The loaded pages are then set in |
|
28
|
|
|
* the builder for further processing. |
|
29
|
|
|
*/ |
|
30
|
|
|
class Load extends AbstractStep |
|
31
|
|
|
{ |
|
32
|
|
|
/** @var string */ |
|
33
|
|
|
protected $page; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
1 |
|
public function getName(): string |
|
39
|
|
|
{ |
|
40
|
1 |
|
return 'Loading pages'; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
1 |
|
public function init(array $options): void |
|
47
|
|
|
{ |
|
48
|
1 |
|
if (!is_dir($this->config->getPagesPath())) { |
|
49
|
|
|
$this->builder->getLogger()->debug(\sprintf('"%s" is not a valid pages directory', $this->config->getPagesPath())); |
|
50
|
|
|
$this->canProcess = false; |
|
51
|
|
|
|
|
52
|
|
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
$this->page = $options['page']; |
|
56
|
1 |
|
$this->canProcess = true; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function process(): void |
|
63
|
|
|
{ |
|
64
|
1 |
|
$namePattern = '/\.(' . implode('|', (array) $this->config->get('pages.ext')) . ')$/'; |
|
65
|
1 |
|
$pages = Finder::create() |
|
66
|
1 |
|
->files() |
|
67
|
1 |
|
->in($this->config->getPagesPath()) |
|
68
|
1 |
|
->sort(function (SplFileInfo $a, SplFileInfo $b): int { |
|
69
|
|
|
// section's index first |
|
70
|
1 |
|
if ($a->getRelativePath() == $b->getRelativePath() && $a->getBasename('.' . $a->getExtension()) == 'index') { |
|
71
|
1 |
|
return -1; |
|
72
|
|
|
} |
|
73
|
1 |
|
if ($b->getRelativePath() == $a->getRelativePath() && $b->getBasename('.' . $b->getExtension()) == 'index') { |
|
74
|
1 |
|
return 1; |
|
75
|
|
|
} |
|
76
|
|
|
// sort by name |
|
77
|
1 |
|
return strnatcasecmp($a->getRealPath(), $b->getRealPath()); |
|
78
|
1 |
|
}); |
|
79
|
|
|
// load only one page? |
|
80
|
1 |
|
if ($this->page) { |
|
81
|
|
|
// is the page path starts with the `pages.dir` configuration option? |
|
82
|
|
|
// (i.e.: `pages/...`, `/pages/...`, `./pages/...`) |
|
83
|
|
|
$pagePathAsArray = explode(DIRECTORY_SEPARATOR, Util::joinFile($this->page)); |
|
84
|
|
|
if ($pagePathAsArray[0] == (string) $this->config->get('pages.dir')) { |
|
85
|
|
|
unset($pagePathAsArray[0]); |
|
86
|
|
|
$this->page = implode(DIRECTORY_SEPARATOR, $pagePathAsArray); |
|
87
|
|
|
} |
|
88
|
|
|
if ($pagePathAsArray[0] == '.' && $pagePathAsArray[1] == (string) $this->config->get('pages.dir')) { |
|
89
|
|
|
unset($pagePathAsArray[0]); |
|
90
|
|
|
unset($pagePathAsArray[1]); |
|
91
|
|
|
$this->page = implode(DIRECTORY_SEPARATOR, $pagePathAsArray); |
|
92
|
|
|
} |
|
93
|
|
|
if (!util\File::getFS()->exists(Util::joinFile($this->config->getPagesPath(), $this->page))) { |
|
94
|
|
|
$this->builder->getLogger()->error(\sprintf('File "%s" doesn\'t exist.', $this->page)); |
|
95
|
|
|
} |
|
96
|
|
|
$pages->path('.')->path(\dirname($this->page)); |
|
97
|
|
|
$pages->name('/index\.(' . implode('|', (array) $this->config->get('pages.ext')) . ')$/'); |
|
98
|
|
|
$namePattern = basename($this->page); |
|
99
|
|
|
} |
|
100
|
1 |
|
$pages->name($namePattern); |
|
101
|
1 |
|
if (\is_array($exclude = $this->config->get('pages.exclude'))) { |
|
102
|
1 |
|
$pages->exclude($exclude); |
|
103
|
1 |
|
$pages->notPath($exclude); |
|
104
|
1 |
|
$pages->notName($exclude); |
|
105
|
|
|
} |
|
106
|
1 |
|
if (file_exists(Util::joinFile($this->config->getPagesPath(), '.gitignore'))) { |
|
107
|
|
|
$pages->ignoreVCSIgnored(true); |
|
108
|
|
|
} |
|
109
|
1 |
|
$this->builder->setPagesFiles($pages); |
|
110
|
|
|
|
|
111
|
1 |
|
$total = $pages->count(); |
|
112
|
1 |
|
$count = 0; |
|
113
|
1 |
|
if ($total === 0) { |
|
114
|
|
|
$this->builder->getLogger()->info('Nothing to load'); |
|
115
|
|
|
|
|
116
|
|
|
return; |
|
117
|
|
|
} |
|
118
|
1 |
|
foreach ($pages as $file) { |
|
119
|
1 |
|
$count++; |
|
120
|
1 |
|
$this->builder->getLogger()->info(\sprintf('File "%s" loaded', $file->getRelativePathname()), ['progress' => [$count, $total]]); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|