• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Cecilapp / Cecil / 14620241211

23 Apr 2025 02:06PM UTC coverage: 83.787%. First build
14620241211

Pull #2148

github

web-flow
Merge 12fc09dec into 6d7ba8f0a
Pull Request #2148: refactor: configuration and cache

361 of 423 new or added lines in 26 files covered. (85.34%)

3049 of 3639 relevant lines covered (83.79%)

0.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.22
/src/Collection/Page/Page.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <arnaud@ligny.fr>
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\Collection\Page;
15

16
use Cecil\Collection\Item;
17
use Cecil\Exception\RuntimeException;
18
use Cecil\Util;
19
use Cocur\Slugify\Slugify;
20
use Symfony\Component\Finder\SplFileInfo;
21

22
/**
23
 * Class Page.
24
 */
25
class Page extends Item
26
{
27
    public const SLUGIFY_PATTERN = '/(^\/|[^._a-z0-9\/]|-)+/'; // should be '/^\/|[^_a-z0-9\/]+/'
28

29
    /** @var bool True if page is not created from a file. */
30
    protected $virtual;
31

32
    /** @var SplFileInfo */
33
    protected $file;
34

35
    /** @var Type Type */
36
    protected $type;
37

38
    /** @var string */
39
    protected $folder;
40

41
    /** @var string */
42
    protected $slug;
43

44
    /** @var string path = folder + slug. */
45
    protected $path;
46

47
    /** @var string */
48
    protected $section;
49

50
    /** @var string */
51
    protected $frontmatter;
52

53
    /** @var array Front matter before conversion. */
54
    protected $fmVariables = [];
55

56
    /** @var string Body before conversion. */
57
    protected $body;
58

59
    /** @var string Body after conversion. */
60
    protected $html;
61

62
    /** @var array Output, by format */
63
    protected $rendered = [];
64

65
    /** @var Collection Subpages of a list page. */
66
    protected $subPages;
67

68
    /** @var array */
69
    protected $paginator = [];
70

71
    /** @var \Cecil\Collection\Taxonomy\Vocabulary Terms of a vocabulary. */
72
    protected $terms;
73

74
    /** @var Slugify */
75
    private static $slugifier;
76

77
    public function __construct(string $id)
78
    {
79
        parent::__construct($id);
1✔
80
        $this->setVirtual(true);
1✔
81
        $this->setType(Type::PAGE->value);
1✔
82
        // default variables
83
        $this->setVariables([
1✔
84
            'title'            => 'Page Title',
1✔
85
            'date'             => new \DateTime(),
1✔
86
            'updated'          => new \DateTime(),
1✔
87
            'weight'           => null,
1✔
88
            'filepath'         => null,
1✔
89
            'published'        => true,
1✔
90
            'content_template' => 'page.content.twig',
1✔
91
        ]);
1✔
92
    }
93

94
    /**
95
     * toString magic method to prevent Twig get_attribute fatal error.
96
     *
97
     * @return string
98
     */
99
    public function __toString()
100
    {
101
        return $this->getId();
1✔
102
    }
103

104
    /**
105
     * Turns a path (string) into a slug (URI).
106
     */
107
    public static function slugify(string $path): string
108
    {
109
        if (!self::$slugifier instanceof Slugify) {
1✔
110
            self::$slugifier = Slugify::create(['regexp' => self::SLUGIFY_PATTERN]);
1✔
111
        }
112

113
        return self::$slugifier->slugify($path);
1✔
114
    }
115

116
    /**
117
     * Creates the ID from the file (path).
118
     */
119
    public static function createIdFromFile(SplFileInfo $file): string
120
    {
121
        $relativePath = self::slugify(str_replace(DIRECTORY_SEPARATOR, '/', $file->getRelativePath()));
1✔
122
        $basename = self::slugify(PrefixSuffix::subPrefix($file->getBasename('.' . $file->getExtension())));
1✔
123
        // if file is "README.md", ID is "index"
124
        $basename = (string) str_ireplace('readme', 'index', $basename);
1✔
125
        // if file is section's index: "section/index.md", ID is "section"
126
        if (!empty($relativePath) && PrefixSuffix::sub($basename) == 'index') {
1✔
127
            // case of a localized section's index: "section/index.fr.md", ID is "fr/section"
128
            if (PrefixSuffix::hasSuffix($basename)) {
1✔
129
                return PrefixSuffix::getSuffix($basename) . '/' . $relativePath;
1✔
130
            }
131

132
            return $relativePath;
1✔
133
        }
134
        // localized page
135
        if (PrefixSuffix::hasSuffix($basename)) {
1✔
136
            return trim(Util::joinPath(PrefixSuffix::getSuffix($basename), $relativePath, PrefixSuffix::sub($basename)), '/');
1✔
137
        }
138

139
        return trim(Util::joinPath($relativePath, $basename), '/');
1✔
140
    }
141

142
    /**
143
     * Returns the ID of a page without language.
144
     */
145
    public function getIdWithoutLang(): string
146
    {
147
        $langPrefix = $this->getVariable('language') . '/';
1✔
148
        if ($this->hasVariable('language') && Util\Str::startsWith($this->getId(), $langPrefix)) {
1✔
149
            return substr($this->getId(), \strlen($langPrefix));
1✔
150
        }
151

152
        return $this->getId();
1✔
153
    }
154

155
    /**
156
     * Set file.
157
     */
158
    public function setFile(SplFileInfo $file): self
159
    {
160
        $this->file = $file;
1✔
161
        $this->setVirtual(false);
1✔
162

163
        /*
164
         * File path components
165
         */
166
        $fileRelativePath = str_replace(DIRECTORY_SEPARATOR, '/', $this->file->getRelativePath());
1✔
167
        $fileExtension = $this->file->getExtension();
1✔
168
        $fileName = $this->file->getBasename('.' . $fileExtension);
1✔
169
        // renames "README" to "index"
170
        $fileName = (string) str_ireplace('readme', 'index', $fileName);
1✔
171
        // case of "index" = home page
172
        if (empty($this->file->getRelativePath()) && PrefixSuffix::sub($fileName) == 'index') {
1✔
173
            $this->setType(Type::HOMEPAGE->value);
1✔
174
        }
175
        /*
176
         * Set page properties and variables
177
         */
178
        $this->setFolder($fileRelativePath);
1✔
179
        $this->setSlug($fileName);
1✔
180
        $this->setPath($this->getFolder() . '/' . $this->getSlug());
1✔
181
        $this->setVariables([
1✔
182
            'title'    => PrefixSuffix::sub($fileName),
1✔
183
            'date'     => (new \DateTime())->setTimestamp($this->file->getMTime()),
1✔
184
            'updated'  => (new \DateTime())->setTimestamp($this->file->getMTime()),
1✔
185
            'filepath' => $this->file->getRelativePathname(),
1✔
186
        ]);
1✔
187
        /*
188
         * Set specific variables
189
         */
190
        // is file has a prefix?
191
        if (PrefixSuffix::hasPrefix($fileName)) {
1✔
192
            $prefix = PrefixSuffix::getPrefix($fileName);
1✔
193
            if ($prefix !== null) {
1✔
194
                // prefix is a valid date?
195
                if (Util\Date::isValid($prefix)) {
1✔
196
                    $this->setVariable('date', (string) $prefix);
1✔
197
                } else {
198
                    // prefix is an integer: used for sorting
199
                    $this->setVariable('weight', (int) $prefix);
1✔
200
                }
201
            }
202
        }
203
        // is file has a language suffix?
204
        if (PrefixSuffix::hasSuffix($fileName)) {
1✔
205
            $this->setVariable('language', PrefixSuffix::getSuffix($fileName));
1✔
206
        }
207
        // set reference between page's translations, even if it exist in only one language
208
        $this->setVariable('langref', $this->getPath());
1✔
209

210
        return $this;
1✔
211
    }
212

213
    /**
214
     * Returns file name, with extension.
215
     */
216
    public function getFileName(): string
217
    {
NEW
218
        return $this->file->getBasename();
×
219
    }
220

221
    /**
222
     * Returns file real path.
223
     */
224
    public function getFilePath(): ?string
225
    {
226
        if ($this->file === null) {
1✔
227
            return null;
×
228
        }
229

230
        return $this->file->getRealPath() === false ? null : $this->file->getRealPath();
1✔
231
    }
232

233
    /**
234
     * Parse file content.
235
     */
236
    public function parse(): self
237
    {
238
        $parser = new Parser($this->file);
1✔
239
        $parsed = $parser->parse();
1✔
240
        $this->frontmatter = $parsed->getFrontmatter();
1✔
241
        $this->body = $parsed->getBody();
1✔
242

243
        return $this;
1✔
244
    }
245

246
    /**
247
     * Get front matter.
248
     */
249
    public function getFrontmatter(): ?string
250
    {
251
        return $this->frontmatter;
1✔
252
    }
253

254
    /**
255
     * Get body as raw.
256
     */
257
    public function getBody(): ?string
258
    {
259
        return $this->body;
1✔
260
    }
261

262
    /**
263
     * Set virtual status.
264
     */
265
    public function setVirtual(bool $virtual): self
266
    {
267
        $this->virtual = $virtual;
1✔
268

269
        return $this;
1✔
270
    }
271

272
    /**
273
     * Is current page is virtual?
274
     */
275
    public function isVirtual(): bool
276
    {
277
        return $this->virtual;
1✔
278
    }
279

280
    /**
281
     * Set page type.
282
     */
283
    public function setType(string $type): self
284
    {
285
        $this->type = Type::from($type);
1✔
286

287
        return $this;
1✔
288
    }
289

290
    /**
291
     * Get page type.
292
     */
293
    public function getType(): string
294
    {
295
        return $this->type->value;
1✔
296
    }
297

298
    /**
299
     * Set path without slug.
300
     */
301
    public function setFolder(string $folder): self
302
    {
303
        $this->folder = self::slugify($folder);
1✔
304

305
        return $this;
1✔
306
    }
307

308
    /**
309
     * Get path without slug.
310
     */
311
    public function getFolder(): ?string
312
    {
313
        return $this->folder;
1✔
314
    }
315

316
    /**
317
     * Set slug.
318
     */
319
    public function setSlug(string $slug): self
320
    {
321
        if (!$this->slug) {
1✔
322
            $slug = self::slugify(PrefixSuffix::sub($slug));
1✔
323
        }
324
        // force slug and update path
325
        if ($this->slug && $this->slug != $slug) {
1✔
326
            $this->setPath($this->getFolder() . '/' . $slug);
1✔
327
        }
328
        $this->slug = $slug;
1✔
329

330
        return $this;
1✔
331
    }
332

333
    /**
334
     * Get slug.
335
     */
336
    public function getSlug(): string
337
    {
338
        return $this->slug;
1✔
339
    }
340

341
    /**
342
     * Set path.
343
     */
344
    public function setPath(string $path): self
345
    {
346
        $path = trim($path, '/');
1✔
347

348
        // case of homepage
349
        if ($path == 'index') {
1✔
350
            $this->path = '';
1✔
351

352
            return $this;
1✔
353
        }
354

355
        // case of custom sections' index (ie: section/index.md -> section)
356
        if (substr($path, -6) == '/index') {
1✔
357
            $path = substr($path, 0, \strlen($path) - 6);
1✔
358
        }
359
        $this->path = $path;
1✔
360

361
        $lastslash = strrpos($this->path, '/');
1✔
362

363
        // case of root/top-level pages
364
        if ($lastslash === false) {
1✔
365
            $this->slug = $this->path;
1✔
366

367
            return $this;
1✔
368
        }
369

370
        // case of sections' pages: set section
371
        if (!$this->virtual && $this->getSection() === null) {
1✔
372
            $this->section = explode('/', $this->path)[0];
1✔
373
        }
374
        // set/update folder and slug
375
        $this->folder = substr($this->path, 0, $lastslash);
1✔
376
        $this->slug = substr($this->path, -(\strlen($this->path) - $lastslash - 1));
1✔
377

378
        return $this;
1✔
379
    }
380

381
    /**
382
     * Get path.
383
     */
384
    public function getPath(): ?string
385
    {
386
        return $this->path;
1✔
387
    }
388

389
    /**
390
     * @see getPath()
391
     */
392
    public function getPathname(): ?string
393
    {
394
        return $this->getPath();
×
395
    }
396

397
    /**
398
     * Set section.
399
     */
400
    public function setSection(string $section): self
401
    {
402
        $this->section = $section;
1✔
403

404
        return $this;
1✔
405
    }
406

407
    /**
408
     * Get section.
409
     */
410
    public function getSection(): ?string
411
    {
412
        return !empty($this->section) ? $this->section : null;
1✔
413
    }
414

415
    /**
416
     * Unset section.
417
     */
418
    public function unSection(): self
419
    {
420
        $this->section = null;
×
421

422
        return $this;
×
423
    }
424

425
    /**
426
     * Set body as HTML.
427
     */
428
    public function setBodyHtml(string $html): self
429
    {
430
        $this->html = $html;
1✔
431

432
        return $this;
1✔
433
    }
434

435
    /**
436
     * Get body as HTML.
437
     */
438
    public function getBodyHtml(): ?string
439
    {
440
        return $this->html;
1✔
441
    }
442

443
    /**
444
     * @see getBodyHtml()
445
     */
446
    public function getContent(): ?string
447
    {
448
        return $this->getBodyHtml();
1✔
449
    }
450

451
    /**
452
     * Add rendered.
453
     */
454
    public function addRendered(array $rendered): self
455
    {
456
        $this->rendered += $rendered;
1✔
457

458
        return $this;
1✔
459
    }
460

461
    /**
462
     * Get rendered.
463
     */
464
    public function getRendered(): array
465
    {
466
        return $this->rendered;
1✔
467
    }
468

469
    /**
470
     * Set Subpages.
471
     */
472
    public function setPages(\Cecil\Collection\Page\Collection $subPages): self
473
    {
474
        $this->subPages = $subPages;
1✔
475

476
        return $this;
1✔
477
    }
478

479
    /**
480
     * Get Subpages.
481
     */
482
    public function getPages(): ?\Cecil\Collection\Page\Collection
483
    {
484
        return $this->subPages;
1✔
485
    }
486

487
    /**
488
     * Set paginator.
489
     */
490
    public function setPaginator(array $paginator): self
491
    {
492
        $this->paginator = $paginator;
1✔
493

494
        return $this;
1✔
495
    }
496

497
    /**
498
     * Get paginator.
499
     */
500
    public function getPaginator(): array
501
    {
502
        return $this->paginator;
1✔
503
    }
504

505
    /**
506
     * Paginator backward compatibility.
507
     */
508
    public function getPagination(): array
509
    {
510
        return $this->getPaginator();
×
511
    }
512

513
    /**
514
     * Set vocabulary terms.
515
     */
516
    public function setTerms(\Cecil\Collection\Taxonomy\Vocabulary $terms): self
517
    {
518
        $this->terms = $terms;
1✔
519

520
        return $this;
1✔
521
    }
522

523
    /**
524
     * Get vocabulary terms.
525
     */
526
    public function getTerms(): \Cecil\Collection\Taxonomy\Vocabulary
527
    {
528
        return $this->terms;
1✔
529
    }
530

531
    /*
532
     * Helpers to set and get variables.
533
     */
534

535
    /**
536
     * Set an array as variables.
537
     *
538
     * @throws RuntimeException
539
     */
540
    public function setVariables(array $variables): self
541
    {
542
        foreach ($variables as $key => $value) {
1✔
543
            $this->setVariable($key, $value);
1✔
544
        }
545

546
        return $this;
1✔
547
    }
548

549
    /**
550
     * Get all variables.
551
     */
552
    public function getVariables(): array
553
    {
554
        return $this->properties;
1✔
555
    }
556

557
    /**
558
     * Set a variable.
559
     *
560
     * @param string $name  Name of the variable
561
     * @param mixed  $value Value of the variable
562
     *
563
     * @throws RuntimeException
564
     */
565
    public function setVariable(string $name, $value): self
566
    {
567
        $this->filterBool($value);
1✔
568
        switch ($name) {
569
            case 'date':
1✔
570
            case 'updated':
1✔
571
            case 'lastmod':
1✔
572
                try {
573
                    $date = Util\Date::toDatetime($value);
1✔
574
                } catch (\Exception) {
×
575
                    throw new \Exception(\sprintf('The value of "%s" is not a valid date: "%s".', $name, var_export($value, true)));
×
576
                }
577
                $this->offsetSet($name == 'lastmod' ? 'updated' : $name, $date);
1✔
578
                break;
1✔
579

580
            case 'schedule':
1✔
581
                /*
582
                 * publish: 2012-10-08
583
                 * expiry: 2012-10-09
584
                 */
585
                $this->offsetSet('published', false);
1✔
586
                if (\is_array($value)) {
1✔
587
                    if (\array_key_exists('publish', $value) && Util\Date::toDatetime($value['publish']) <= Util\Date::toDatetime('now')) {
1✔
588
                        $this->offsetSet('published', true);
1✔
589
                    }
590
                    if (\array_key_exists('expiry', $value) && Util\Date::toDatetime($value['expiry']) >= Util\Date::toDatetime('now')) {
1✔
591
                        $this->offsetSet('published', true);
×
592
                    }
593
                }
594
                break;
1✔
595
            case 'draft':
1✔
596
                // draft: true = published: false
597
                if ($value === true) {
1✔
598
                    $this->offsetSet('published', false);
1✔
599
                }
600
                break;
1✔
601
            case 'path':
1✔
602
            case 'slug':
1✔
603
                $slugify = self::slugify((string) $value);
1✔
604
                if ($value != $slugify) {
1✔
605
                    throw new RuntimeException(\sprintf('"%s" variable should be "%s" (not "%s") in "%s".', $name, $slugify, (string) $value, $this->getId()));
×
606
                }
607
                $method = 'set' . ucfirst($name);
1✔
608
                $this->$method($value);
1✔
609
                break;
1✔
610
            default:
611
                $this->offsetSet($name, $value);
1✔
612
        }
613

614
        return $this;
1✔
615
    }
616

617
    /**
618
     * Is variable exists?
619
     *
620
     * @param string $name Name of the variable
621
     */
622
    public function hasVariable(string $name): bool
623
    {
624
        return $this->offsetExists($name);
1✔
625
    }
626

627
    /**
628
     * Get a variable.
629
     *
630
     * @param string     $name    Name of the variable
631
     * @param mixed|null $default Default value
632
     *
633
     * @return mixed|null
634
     */
635
    public function getVariable(string $name, $default = null)
636
    {
637
        if ($this->offsetExists($name)) {
1✔
638
            return $this->offsetGet($name);
1✔
639
        }
640

641
        return $default;
1✔
642
    }
643

644
    /**
645
     * Unset a variable.
646
     *
647
     * @param string $name Name of the variable
648
     */
649
    public function unVariable(string $name): self
650
    {
651
        if ($this->offsetExists($name)) {
1✔
652
            $this->offsetUnset($name);
1✔
653
        }
654

655
        return $this;
1✔
656
    }
657

658
    /**
659
     * Set front matter (only) variables.
660
     */
661
    public function setFmVariables(array $variables): self
662
    {
663
        $this->fmVariables = $variables;
1✔
664

665
        return $this;
1✔
666
    }
667

668
    /**
669
     * Get front matter variables.
670
     */
671
    public function getFmVariables(): array
672
    {
673
        return $this->fmVariables;
1✔
674
    }
675

676
    /**
677
     * Cast "boolean" string (or array of strings) to boolean.
678
     *
679
     * @param mixed $value Value to filter
680
     *
681
     * @return bool|mixed
682
     *
683
     * @see strToBool()
684
     */
685
    private function filterBool(&$value)
686
    {
687
        \Cecil\Util\Str::strToBool($value);
1✔
688
        if (\is_array($value)) {
1✔
689
            array_walk_recursive($value, '\Cecil\Util\Str::strToBool');
1✔
690
        }
691
    }
692

693
    /**
694
     * {@inheritdoc}
695
     */
696
    public function setId(string $id): self
697
    {
698
        return parent::setId($id);
1✔
699
    }
700
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc