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
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Loads pages. |
23
|
|
|
*/ |
24
|
|
|
class Load extends AbstractStep |
25
|
|
|
{ |
26
|
|
|
/** @var string */ |
27
|
|
|
protected $page; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
|
|
public function getName(): string |
33
|
|
|
{ |
34
|
|
|
return 'Loading pages'; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function init(array $options): void |
41
|
|
|
{ |
42
|
|
|
// legacy support |
43
|
|
|
if (is_dir(Util::joinFile($this->config->getSourceDir(), 'content'))) { |
44
|
|
|
$this->builder->getLogger()->alert('"content" directory is deprecated, please rename it to "pages"'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (!is_dir($this->config->getPagesPath())) { |
48
|
|
|
throw new RuntimeException(sprintf('Pages path "%s" not found.', $this->config->getPagesPath())); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->page = $options['page']; |
52
|
|
|
$this->canProcess = true; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function process(): void |
59
|
|
|
{ |
60
|
|
|
$namePattern = '/\.(' . implode('|', (array) $this->config->get('pages.ext')) . ')$/'; |
61
|
|
|
$content = Finder::create() |
62
|
|
|
->files() |
63
|
|
|
->in($this->config->getPagesPath()) |
64
|
|
|
//->sortByName(true) |
65
|
|
|
->sort(function (\SplFileInfo $a, \SplFileInfo $b) { |
66
|
|
|
// section's index first |
67
|
|
|
if ($a->getBasename('.' . $a->getExtension()) == 'index' && $b->getBasename('.' . $b->getExtension()) != 'index') { |
68
|
|
|
return -1; |
69
|
|
|
} |
70
|
|
|
if ($b->getBasename('.' . $b->getExtension()) == 'index' && $a->getBasename('.' . $a->getExtension()) != 'index') { |
71
|
|
|
return 1; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return strnatcmp($a->getRealPath(), $b->getRealPath()); |
75
|
|
|
}); |
76
|
|
|
// load only one page? |
77
|
|
|
if ($this->page) { |
78
|
|
|
// is the page path starts with the `pages.dir` configuration option? |
79
|
|
|
// (i.e.: `pages/...`, `/pages/...`, `./pages/...`) |
80
|
|
|
$pagePathAsArray = explode(DIRECTORY_SEPARATOR, $this->page); |
81
|
|
|
if ($pagePathAsArray[0] == (string) $this->config->get('pages.dir')) { |
82
|
|
|
unset($pagePathAsArray[0]); |
83
|
|
|
$this->page = implode(DIRECTORY_SEPARATOR, $pagePathAsArray); |
84
|
|
|
} |
85
|
|
|
if ($pagePathAsArray[0] == '.' && $pagePathAsArray[1] == (string) $this->config->get('pages.dir')) { |
86
|
|
|
unset($pagePathAsArray[0]); |
87
|
|
|
unset($pagePathAsArray[1]); |
88
|
|
|
$this->page = implode(DIRECTORY_SEPARATOR, $pagePathAsArray); |
89
|
|
|
} |
90
|
|
|
if (!util\File::getFS()->exists(Util::joinFile($this->config->getPagesPath(), $this->page))) { |
91
|
|
|
$this->builder->getLogger()->error(sprintf('File "%s" doesn\'t exist.', $this->page)); |
92
|
|
|
} |
93
|
|
|
$content->path('.')->path(\dirname($this->page)); |
94
|
|
|
$content->name('/index\.(' . implode('|', (array) $this->config->get('pages.ext')) . ')$/'); |
95
|
|
|
$namePattern = basename($this->page); |
96
|
|
|
} |
97
|
|
|
$content->name($namePattern); |
98
|
|
|
if (\is_array($this->config->get('pages.exclude'))) { |
99
|
|
|
$content->exclude($this->config->get('pages.exclude')); |
100
|
|
|
$content->notPath($this->config->get('pages.exclude')); |
101
|
|
|
$content->notName($this->config->get('pages.exclude')); |
102
|
|
|
} |
103
|
|
|
if (file_exists(Util::joinFile($this->config->getPagesPath(), '.gitignore'))) { |
104
|
|
|
$content->ignoreVCSIgnored(true); |
105
|
|
|
} |
106
|
|
|
$this->builder->setPagesFiles($content); |
107
|
|
|
|
108
|
|
|
$count = $content->count(); |
109
|
|
|
if ($count === 0) { |
110
|
|
|
$this->builder->getLogger()->info('Nothing to load'); |
111
|
|
|
|
112
|
|
|
return; |
113
|
|
|
} |
114
|
|
|
$this->builder->getLogger()->info('Files loaded', ['progress' => [$count, $count]]); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|