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